summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/VideoTrack.cpp
blob: d8198d7b9fde0fae3e9533b40a73803dda19ffda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/IDAllocator.h>
#include <AK/Time.h>
#include <LibGfx/Bitmap.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibVideo/PlaybackManager.h>
#include <LibVideo/Track.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/HTMLVideoElement.h>
#include <LibWeb/HTML/VideoTrack.h>
#include <LibWeb/HTML/VideoTrackList.h>

namespace Web::HTML {

static IDAllocator s_video_track_id_allocator;

VideoTrack::VideoTrack(JS::Realm& realm, JS::NonnullGCPtr<HTMLMediaElement> media_element, NonnullOwnPtr<Video::PlaybackManager> playback_manager)
    : PlatformObject(realm)
    , m_media_element(media_element)
    , m_playback_manager(move(playback_manager))
{
    m_playback_manager->on_video_frame = [this](auto frame) {
        auto playback_position = static_cast<double>(position().to_milliseconds()) / 1000.0;

        if (is<HTMLVideoElement>(*m_media_element))
            verify_cast<HTMLVideoElement>(*m_media_element).set_current_frame({}, move(frame), playback_position);

        m_media_element->set_current_playback_position(playback_position);
    };

    m_playback_manager->on_playback_state_change = [this]() {
        switch (m_playback_manager->get_state()) {
        case Video::PlaybackState::Stopped: {
            auto playback_position_ms = static_cast<double>(duration().to_milliseconds());
            m_media_element->set_current_playback_position(playback_position_ms / 1000.0);
            break;
        }

        default:
            break;
        }
    };

    m_playback_manager->on_decoder_error = [this](auto error) {
        auto error_message = String::from_utf8(error.description()).release_value_but_fixme_should_propagate_errors();
        m_media_element->set_decoder_error(move(error_message)).release_value_but_fixme_should_propagate_errors();
    };

    m_playback_manager->on_fatal_playback_error = [this](auto error) {
        auto error_message = String::from_utf8(error.string_literal()).release_value_but_fixme_should_propagate_errors();
        m_media_element->set_decoder_error(move(error_message)).release_value_but_fixme_should_propagate_errors();
    };
}

VideoTrack::~VideoTrack()
{
    auto id = m_id.to_number<int>();
    VERIFY(id.has_value());

    s_video_track_id_allocator.deallocate(id.value());
}

JS::ThrowCompletionOr<void> VideoTrack::initialize(JS::Realm& realm)
{
    MUST_OR_THROW_OOM(Base::initialize(realm));
    set_prototype(&Bindings::ensure_web_prototype<Bindings::VideoTrackPrototype>(realm, "VideoTrack"));

    auto id = s_video_track_id_allocator.allocate();
    m_id = TRY_OR_THROW_OOM(realm.vm(), String::number(id));

    return {};
}

void VideoTrack::visit_edges(Cell::Visitor& visitor)
{
    visitor.visit(m_media_element);
    visitor.visit(m_video_track_list);
}

void VideoTrack::play_video(Badge<HTMLVideoElement>)
{
    m_playback_manager->resume_playback();
}

void VideoTrack::pause_video(Badge<HTMLVideoElement>)
{
    m_playback_manager->pause_playback();
}

Time VideoTrack::position() const
{
    return m_playback_manager->current_playback_time();
}

Time VideoTrack::duration() const
{
    return m_playback_manager->selected_video_track().video_data().duration;
}

void VideoTrack::seek(Time position, MediaSeekMode seek_mode)
{
    switch (seek_mode) {
    case MediaSeekMode::Accurate:
        m_playback_manager->seek_to_timestamp(position, Video::PlaybackManager::SeekMode::Accurate);
        break;
    case MediaSeekMode::ApproximateForSpeed:
        m_playback_manager->seek_to_timestamp(position, Video::PlaybackManager::SeekMode::Fast);
        break;
    }
}

u64 VideoTrack::pixel_width() const
{
    return m_playback_manager->selected_video_track().video_data().pixel_width;
}

u64 VideoTrack::pixel_height() const
{
    return m_playback_manager->selected_video_track().video_data().pixel_height;
}

// 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) {
        for (auto video_track : m_video_track_list->video_tracks({})) {
            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]() {
                m_video_track_list->dispatch_event(DOM::Event::create(realm(), HTML::EventNames::change).release_value_but_fixme_should_propagate_errors());
            });
        }
    }

    m_selected = selected;

    // AD-HOC: Inform the video element node that we have (un)selected a video track for layout.
    if (is<HTMLVideoElement>(*m_media_element)) {
        auto& video_element = verify_cast<HTMLVideoElement>(*m_media_element);
        video_element.set_video_track(m_selected ? this : nullptr);
    }
}

}