AudioTrack.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/IDAllocator.h>
  7. #include <AK/Time.h>
  8. #include <LibAudio/Loader.h>
  9. #include <LibAudio/Resampler.h>
  10. #include <LibAudio/Sample.h>
  11. #include <LibJS/Runtime/Realm.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibWeb/Bindings/AudioTrackPrototype.h>
  14. #include <LibWeb/Bindings/Intrinsics.h>
  15. #include <LibWeb/DOM/Event.h>
  16. #include <LibWeb/HTML/AudioTrack.h>
  17. #include <LibWeb/HTML/AudioTrackList.h>
  18. #include <LibWeb/HTML/EventNames.h>
  19. #include <LibWeb/HTML/HTMLMediaElement.h>
  20. #include <LibWeb/Layout/Node.h>
  21. #include <LibWeb/Platform/AudioCodecPlugin.h>
  22. #include <LibWeb/Platform/Timer.h>
  23. namespace Web::HTML {
  24. static IDAllocator s_audio_track_id_allocator;
  25. // Number of milliseconds of audio data contained in each audio buffer
  26. static constexpr u32 BUFFER_SIZE_MS = 50;
  27. AudioTrack::AudioTrack(JS::Realm& realm, JS::NonnullGCPtr<HTMLMediaElement> media_element, NonnullRefPtr<Audio::Loader> loader)
  28. : PlatformObject(realm)
  29. , m_media_element(media_element)
  30. , m_audio_plugin(Platform::AudioCodecPlugin::create().release_value_but_fixme_should_propagate_errors())
  31. , m_loader(move(loader))
  32. , m_sample_timer(Platform::Timer::create_repeating(BUFFER_SIZE_MS, [this]() {
  33. play_next_samples();
  34. }))
  35. {
  36. m_audio_plugin->device_sample_rate();
  37. }
  38. AudioTrack::~AudioTrack()
  39. {
  40. auto id = m_id.to_number<int>();
  41. VERIFY(id.has_value());
  42. s_audio_track_id_allocator.deallocate(id.value());
  43. }
  44. JS::ThrowCompletionOr<void> AudioTrack::initialize(JS::Realm& realm)
  45. {
  46. MUST_OR_THROW_OOM(Base::initialize(realm));
  47. set_prototype(&Bindings::ensure_web_prototype<Bindings::AudioTrackPrototype>(realm, "AudioTrack"));
  48. auto id = s_audio_track_id_allocator.allocate();
  49. m_id = TRY_OR_THROW_OOM(realm.vm(), String::number(id));
  50. return {};
  51. }
  52. void AudioTrack::play(Badge<HTMLAudioElement>)
  53. {
  54. m_audio_plugin->resume_playback();
  55. m_sample_timer->start();
  56. }
  57. void AudioTrack::pause(Badge<HTMLAudioElement>)
  58. {
  59. m_audio_plugin->pause_playback();
  60. m_sample_timer->stop();
  61. }
  62. Duration AudioTrack::position() const
  63. {
  64. auto samples_played = static_cast<double>(m_loader->loaded_samples());
  65. auto sample_rate = static_cast<double>(m_loader->sample_rate());
  66. auto source_to_device_ratio = sample_rate / static_cast<double>(m_audio_plugin->device_sample_rate());
  67. samples_played *= source_to_device_ratio;
  68. return Duration::from_milliseconds(static_cast<i64>(samples_played / sample_rate * 1000.0));
  69. }
  70. Duration AudioTrack::duration() const
  71. {
  72. auto duration = static_cast<double>(m_loader->total_samples()) / static_cast<double>(m_loader->sample_rate());
  73. return Duration::from_milliseconds(static_cast<i64>(duration * 1000.0));
  74. }
  75. void AudioTrack::seek(double position, MediaSeekMode seek_mode)
  76. {
  77. // FIXME: Implement seeking mode.
  78. (void)seek_mode;
  79. auto duration = static_cast<double>(this->duration().to_milliseconds()) / 1000.0;
  80. position = position / duration * static_cast<double>(m_loader->total_samples());
  81. m_loader->seek(position).release_value_but_fixme_should_propagate_errors();
  82. m_media_element->set_current_playback_position(this->position().to_milliseconds() / 1000.0);
  83. }
  84. void AudioTrack::update_volume()
  85. {
  86. m_audio_plugin->set_volume(m_media_element->effective_media_volume());
  87. }
  88. void AudioTrack::visit_edges(Cell::Visitor& visitor)
  89. {
  90. Base::visit_edges(visitor);
  91. visitor.visit(m_media_element);
  92. visitor.visit(m_audio_track_list);
  93. }
  94. // https://html.spec.whatwg.org/multipage/media.html#dom-audiotrack-enabled
  95. void AudioTrack::set_enabled(bool enabled)
  96. {
  97. // On setting, it must enable the track if the new value is true, and disable it otherwise. (If the track is no
  98. // longer in an AudioTrackList object, then the track being enabled or disabled has no effect beyond changing the
  99. // value of the attribute on the AudioTrack object.)
  100. if (m_enabled == enabled)
  101. return;
  102. if (m_audio_track_list) {
  103. // Whenever an audio track in an AudioTrackList that was disabled is enabled, and whenever one that was enabled
  104. // is disabled, the user agent must queue a media element task given the media element to fire an event named
  105. // change at the AudioTrackList object.
  106. m_media_element->queue_a_media_element_task([this]() {
  107. m_audio_track_list->dispatch_event(DOM::Event::create(realm(), HTML::EventNames::change).release_value_but_fixme_should_propagate_errors());
  108. });
  109. }
  110. m_enabled = enabled;
  111. }
  112. Optional<FixedArray<Audio::Sample>> AudioTrack::get_next_samples()
  113. {
  114. bool all_samples_loaded = m_loader->loaded_samples() >= m_loader->total_samples();
  115. bool audio_server_done = m_audio_plugin->remaining_samples() == 0;
  116. if (all_samples_loaded && audio_server_done)
  117. return {};
  118. auto samples_to_load_per_buffer = static_cast<size_t>(BUFFER_SIZE_MS / 1000.0f * static_cast<float>(m_loader->sample_rate()));
  119. auto buffer_or_error = m_loader->get_more_samples(samples_to_load_per_buffer);
  120. if (buffer_or_error.is_error()) {
  121. dbgln("Error while loading samples: {}", buffer_or_error.error().description);
  122. return {};
  123. }
  124. return buffer_or_error.release_value();
  125. }
  126. void AudioTrack::play_next_samples()
  127. {
  128. if (auto* layout_node = m_media_element->layout_node())
  129. layout_node->set_needs_display();
  130. auto samples = get_next_samples();
  131. if (!samples.has_value()) {
  132. m_audio_plugin->playback_ended();
  133. (void)m_loader->reset();
  134. auto playback_position = static_cast<double>(duration().to_milliseconds()) / 1000.0;
  135. m_media_element->set_current_playback_position(playback_position);
  136. return;
  137. }
  138. Audio::ResampleHelper<Audio::Sample> resampler(m_loader->sample_rate(), m_audio_plugin->device_sample_rate());
  139. auto resampled = FixedArray<Audio::Sample>::create(resampler.resample(samples.release_value()).span()).release_value_but_fixme_should_propagate_errors();
  140. m_audio_plugin->enqueue_samples(move(resampled));
  141. auto playback_position = static_cast<double>(position().to_milliseconds()) / 1000.0;
  142. m_media_element->set_current_playback_position(playback_position);
  143. }
  144. }