Add support for reporting "display composited video frames" through the VideoPlayback...
[WebKit-https.git] / Source / WebCore / platform / mock / mediasource / MockMediaSourcePrivate.cpp
1 /*
2  * Copyright (C) 2013-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "MockMediaSourcePrivate.h"
28
29 #if ENABLE(MEDIA_SOURCE)
30
31 #include "ContentType.h"
32 #include "MediaSourcePrivateClient.h"
33 #include "MockMediaPlayerMediaSource.h"
34 #include "MockSourceBufferPrivate.h"
35
36 namespace WebCore {
37
38 Ref<MockMediaSourcePrivate> MockMediaSourcePrivate::create(MockMediaPlayerMediaSource& parent, MediaSourcePrivateClient& client)
39 {
40     auto source = adoptRef(*new MockMediaSourcePrivate(parent, client));
41     client.setPrivateAndOpen(source.copyRef());
42     return source;
43 }
44
45 MockMediaSourcePrivate::MockMediaSourcePrivate(MockMediaPlayerMediaSource& parent, MediaSourcePrivateClient& client)
46     : m_player(parent)
47     , m_client(client)
48 {
49 }
50
51 MockMediaSourcePrivate::~MockMediaSourcePrivate()
52 {
53     for (auto& buffer : m_sourceBuffers)
54         buffer->clearMediaSource();
55 }
56
57 MediaSourcePrivate::AddStatus MockMediaSourcePrivate::addSourceBuffer(const ContentType& contentType, RefPtr<SourceBufferPrivate>& outPrivate)
58 {
59     MediaEngineSupportParameters parameters;
60     parameters.isMediaSource = true;
61     parameters.type = contentType;
62     if (MockMediaPlayerMediaSource::supportsType(parameters) == MediaPlayer::IsNotSupported)
63         return NotSupported;
64
65     m_sourceBuffers.append(MockSourceBufferPrivate::create(this));
66     outPrivate = m_sourceBuffers.last();
67
68     return Ok;
69 }
70
71 void MockMediaSourcePrivate::removeSourceBuffer(SourceBufferPrivate* buffer)
72 {
73     ASSERT(m_sourceBuffers.contains(buffer));
74     m_activeSourceBuffers.removeFirst(buffer);
75     m_sourceBuffers.removeFirst(buffer);
76 }
77
78 MediaTime MockMediaSourcePrivate::duration()
79 {
80     return m_client->duration();
81 }
82
83 std::unique_ptr<PlatformTimeRanges> MockMediaSourcePrivate::buffered()
84 {
85     return m_client->buffered();
86 }
87
88 void MockMediaSourcePrivate::durationChanged()
89 {
90     m_player.updateDuration(duration());
91 }
92
93 void MockMediaSourcePrivate::markEndOfStream(EndOfStreamStatus status)
94 {
95     if (status == EosNoError)
96         m_player.setNetworkState(MediaPlayer::Loaded);
97     m_isEnded = true;
98 }
99
100 void MockMediaSourcePrivate::unmarkEndOfStream()
101 {
102     m_isEnded = false;
103 }
104
105 MediaPlayer::ReadyState MockMediaSourcePrivate::readyState() const
106 {
107     return m_player.readyState();
108 }
109
110 void MockMediaSourcePrivate::setReadyState(MediaPlayer::ReadyState readyState)
111 {
112     m_player.setReadyState(readyState);
113 }
114
115 void MockMediaSourcePrivate::waitForSeekCompleted()
116 {
117     m_player.waitForSeekCompleted();
118 }
119
120 void MockMediaSourcePrivate::seekCompleted()
121 {
122     m_player.seekCompleted();
123 }
124
125 void MockMediaSourcePrivate::sourceBufferPrivateDidChangeActiveState(MockSourceBufferPrivate* buffer, bool active)
126 {
127     if (active && !m_activeSourceBuffers.contains(buffer))
128         m_activeSourceBuffers.append(buffer);
129
130     if (!active)
131         m_activeSourceBuffers.removeFirst(buffer);
132 }
133
134 static bool MockSourceBufferPrivateHasAudio(MockSourceBufferPrivate* sourceBuffer)
135 {
136     return sourceBuffer->hasAudio();
137 }
138
139 bool MockMediaSourcePrivate::hasAudio() const
140 {
141     return std::any_of(m_activeSourceBuffers.begin(), m_activeSourceBuffers.end(), MockSourceBufferPrivateHasAudio);
142 }
143
144 static bool MockSourceBufferPrivateHasVideo(MockSourceBufferPrivate* sourceBuffer)
145 {
146     return sourceBuffer->hasVideo();
147 }
148
149 bool MockMediaSourcePrivate::hasVideo() const
150 {
151     return std::any_of(m_activeSourceBuffers.begin(), m_activeSourceBuffers.end(), MockSourceBufferPrivateHasVideo);
152 }
153
154 void MockMediaSourcePrivate::seekToTime(const MediaTime& time)
155 {
156     m_client->seekToTime(time);
157 }
158
159 MediaTime MockMediaSourcePrivate::seekToTime(const MediaTime& targetTime, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold)
160 {
161     MediaTime seekTime = targetTime;
162     for (auto& buffer : m_activeSourceBuffers) {
163         MediaTime sourceSeekTime = buffer->fastSeekTimeForMediaTime(targetTime, negativeThreshold, positiveThreshold);
164         if (abs(targetTime - sourceSeekTime) > abs(targetTime - seekTime))
165             seekTime = sourceSeekTime;
166     }
167
168     for (auto& buffer : m_activeSourceBuffers)
169         buffer->seekToTime(seekTime);
170
171     return seekTime;
172 }
173
174 std::optional<VideoPlaybackQualityMetrics> MockMediaSourcePrivate::videoPlaybackQualityMetrics()
175 {
176     return VideoPlaybackQualityMetrics {
177         m_totalVideoFrames,
178         m_droppedVideoFrames,
179         m_corruptedVideoFrames,
180         m_totalFrameDelay.toDouble(),
181         0,
182     };
183 }
184
185 }
186
187 #endif