AudioTrack.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <LibAudio/Loader.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibJS/Runtime/VM.h>
  10. #include <LibWeb/Bindings/AudioTrackPrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/HTML/AudioTrack.h>
  14. #include <LibWeb/HTML/AudioTrackList.h>
  15. #include <LibWeb/HTML/EventNames.h>
  16. #include <LibWeb/HTML/HTMLMediaElement.h>
  17. #include <LibWeb/Layout/Node.h>
  18. #include <LibWeb/Platform/AudioCodecPlugin.h>
  19. namespace Web::HTML {
  20. static IDAllocator s_audio_track_id_allocator;
  21. AudioTrack::AudioTrack(JS::Realm& realm, JS::NonnullGCPtr<HTMLMediaElement> media_element, NonnullRefPtr<Audio::Loader> loader)
  22. : PlatformObject(realm)
  23. , m_media_element(media_element)
  24. , m_audio_plugin(Platform::AudioCodecPlugin::create(move(loader)).release_value_but_fixme_should_propagate_errors())
  25. {
  26. m_audio_plugin->on_playback_position_updated = [this](auto position) {
  27. if (auto* layout_node = m_media_element->layout_node())
  28. layout_node->set_needs_display();
  29. auto playback_position = static_cast<double>(position.to_milliseconds()) / 1000.0;
  30. m_media_element->set_current_playback_position(playback_position);
  31. };
  32. }
  33. AudioTrack::~AudioTrack()
  34. {
  35. auto id = m_id.to_number<int>();
  36. VERIFY(id.has_value());
  37. s_audio_track_id_allocator.deallocate(id.value());
  38. }
  39. void AudioTrack::initialize(JS::Realm& realm)
  40. {
  41. Base::initialize(realm);
  42. set_prototype(&Bindings::ensure_web_prototype<Bindings::AudioTrackPrototype>(realm, "AudioTrack"));
  43. auto id = s_audio_track_id_allocator.allocate();
  44. m_id = MUST(String::number(id));
  45. }
  46. void AudioTrack::play(Badge<HTMLAudioElement>)
  47. {
  48. m_audio_plugin->resume_playback();
  49. }
  50. void AudioTrack::pause(Badge<HTMLAudioElement>)
  51. {
  52. m_audio_plugin->pause_playback();
  53. }
  54. Duration AudioTrack::duration()
  55. {
  56. return m_audio_plugin->duration();
  57. }
  58. void AudioTrack::seek(double position, MediaSeekMode seek_mode)
  59. {
  60. // FIXME: Implement seeking mode.
  61. (void)seek_mode;
  62. m_audio_plugin->seek(position);
  63. }
  64. void AudioTrack::update_volume()
  65. {
  66. m_audio_plugin->set_volume(m_media_element->effective_media_volume());
  67. }
  68. void AudioTrack::visit_edges(Cell::Visitor& visitor)
  69. {
  70. Base::visit_edges(visitor);
  71. visitor.visit(m_media_element);
  72. visitor.visit(m_audio_track_list);
  73. }
  74. // https://html.spec.whatwg.org/multipage/media.html#dom-audiotrack-enabled
  75. void AudioTrack::set_enabled(bool enabled)
  76. {
  77. // On setting, it must enable the track if the new value is true, and disable it otherwise. (If the track is no
  78. // longer in an AudioTrackList object, then the track being enabled or disabled has no effect beyond changing the
  79. // value of the attribute on the AudioTrack object.)
  80. if (m_enabled == enabled)
  81. return;
  82. if (m_audio_track_list) {
  83. // Whenever an audio track in an AudioTrackList that was disabled is enabled, and whenever one that was enabled
  84. // is disabled, the user agent must queue a media element task given the media element to fire an event named
  85. // change at the AudioTrackList object.
  86. m_media_element->queue_a_media_element_task([this]() {
  87. m_audio_track_list->dispatch_event(DOM::Event::create(realm(), HTML::EventNames::change));
  88. });
  89. }
  90. m_enabled = enabled;
  91. }
  92. }