2023-04-04 09:26:02 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/IDAllocator.h>
|
2023-04-10 10:07:23 -04:00
|
|
|
#include <AK/Time.h>
|
2023-04-04 09:26:02 -04:00
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
|
#include <LibWeb/Bindings/VideoTrackPrototype.h>
|
|
|
|
|
#include <LibWeb/DOM/Event.h>
|
|
|
|
|
#include <LibWeb/HTML/EventNames.h>
|
|
|
|
|
#include <LibWeb/HTML/HTMLMediaElement.h>
|
|
|
|
|
#include <LibWeb/HTML/VideoTrack.h>
|
|
|
|
|
#include <LibWeb/HTML/VideoTrackList.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(VideoTrack);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2023-04-04 09:26:02 -04:00
|
|
|
static IDAllocator s_video_track_id_allocator;
|
|
|
|
|
|
2025-10-03 00:39:39 -05:00
|
|
|
VideoTrack::VideoTrack(JS::Realm& realm, GC::Ref<HTMLMediaElement> media_element, Media::Track const& track)
|
2023-04-04 09:26:02 -04:00
|
|
|
: PlatformObject(realm)
|
|
|
|
|
, m_media_element(media_element)
|
2025-10-03 00:39:39 -05:00
|
|
|
, m_track_in_playback_manager(track)
|
2023-04-04 09:26:02 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoTrack::~VideoTrack()
|
|
|
|
|
{
|
|
|
|
|
auto id = m_id.to_number<int>();
|
|
|
|
|
VERIFY(id.has_value());
|
|
|
|
|
|
|
|
|
|
s_video_track_id_allocator.deallocate(id.value());
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void VideoTrack::initialize(JS::Realm& realm)
|
2023-04-04 09:26:02 -04:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(VideoTrack);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2023-04-04 09:26:02 -04:00
|
|
|
|
|
|
|
|
auto id = s_video_track_id_allocator.allocate();
|
2024-10-14 10:05:01 +02:00
|
|
|
m_id = String::number(id);
|
2023-04-04 09:26:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoTrack::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
2023-05-17 20:55:04 +02:00
|
|
|
Base::visit_edges(visitor);
|
2023-04-04 09:26:02 -04:00
|
|
|
visitor.visit(m_media_element);
|
|
|
|
|
visitor.visit(m_video_track_list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/media.html#dom-videotrack-selected
|
|
|
|
|
void VideoTrack::set_selected(bool selected)
|
|
|
|
|
{
|
|
|
|
|
// On setting, it must select the track if the new value is true, and unselect it otherwise.
|
|
|
|
|
if (m_selected == selected)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If the track is in a VideoTrackList, then all the other VideoTrack objects in that list must be unselected. (If the track is
|
|
|
|
|
// no longer in a VideoTrackList object, then the track being selected or unselected has no effect beyond changing the value of
|
|
|
|
|
// the attribute on the VideoTrack object.)
|
|
|
|
|
if (m_video_track_list) {
|
2024-04-25 08:39:19 -04:00
|
|
|
for (auto video_track : m_video_track_list->video_tracks()) {
|
2023-04-04 09:26:02 -04:00
|
|
|
if (video_track.ptr() != this)
|
|
|
|
|
video_track->m_selected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Whenever a track in a VideoTrackList that was previously not selected is selected, and whenever the selected track in a
|
|
|
|
|
// VideoTrackList is unselected without a new track being selected in its stead, the user agent must queue a media element
|
|
|
|
|
// task given the media element to fire an event named change at the VideoTrackList object. This task must be queued before
|
|
|
|
|
// the task that fires the resize event, if any.
|
|
|
|
|
auto previously_unselected_track_is_selected = !m_selected && selected;
|
|
|
|
|
auto selected_track_was_unselected_without_another_selection = m_selected && !selected;
|
|
|
|
|
|
|
|
|
|
if (previously_unselected_track_is_selected || selected_track_was_unselected_without_another_selection) {
|
|
|
|
|
m_media_element->queue_a_media_element_task([this]() {
|
2023-08-13 13:05:26 +02:00
|
|
|
m_video_track_list->dispatch_event(DOM::Event::create(realm(), HTML::EventNames::change));
|
2023-04-04 09:26:02 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_selected = selected;
|
2023-04-04 18:32:09 -04:00
|
|
|
|
2025-10-03 00:39:39 -05:00
|
|
|
// AD-HOC: Inform the element node that we have (un)selected a video track for layout.
|
|
|
|
|
m_media_element->set_selected_video_track({}, m_selected ? this : nullptr);
|
2023-04-04 09:26:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|