/src/mozilla-central/dom/media/mediasink/VideoSink.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifdef XP_WIN |
8 | | // Include Windows headers required for enabling high precision timers. |
9 | | #include "windows.h" |
10 | | #include "mmsystem.h" |
11 | | #endif |
12 | | |
13 | | #include "MediaQueue.h" |
14 | | #include "VideoSink.h" |
15 | | #include "VideoUtils.h" |
16 | | |
17 | | #include "mozilla/IntegerPrintfMacros.h" |
18 | | #include "mozilla/StaticPrefs.h" |
19 | | |
20 | | namespace mozilla { |
21 | | |
22 | | extern LazyLogModule gMediaDecoderLog; |
23 | | |
24 | | #undef FMT |
25 | | |
26 | | #define FMT(x, ...) "VideoSink=%p " x, this, ##__VA_ARGS__ |
27 | 0 | #define VSINK_LOG(x, ...) MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, (FMT(x, ##__VA_ARGS__))) |
28 | 0 | #define VSINK_LOG_V(x, ...) MOZ_LOG(gMediaDecoderLog, LogLevel::Verbose, (FMT(x, ##__VA_ARGS__))) |
29 | | |
30 | | using namespace mozilla::layers; |
31 | | |
32 | | namespace media { |
33 | | |
34 | | // Minimum update frequency is 1/120th of a second, i.e. half the |
35 | | // duration of a 60-fps frame. |
36 | | static const int64_t MIN_UPDATE_INTERVAL_US = 1000000 / (60 * 2); |
37 | | |
38 | | VideoSink::VideoSink(AbstractThread* aThread, |
39 | | MediaSink* aAudioSink, |
40 | | MediaQueue<VideoData>& aVideoQueue, |
41 | | VideoFrameContainer* aContainer, |
42 | | FrameStatistics& aFrameStats, |
43 | | uint32_t aVQueueSentToCompositerSize) |
44 | | : mOwnerThread(aThread) |
45 | | , mAudioSink(aAudioSink) |
46 | | , mVideoQueue(aVideoQueue) |
47 | | , mContainer(aContainer) |
48 | | , mProducerID(ImageContainer::AllocateProducerID()) |
49 | | , mFrameStats(aFrameStats) |
50 | | , mOldCompositorDroppedCount(mContainer ? mContainer->GetDroppedImageCount() |
51 | | : 0) |
52 | | , mPendingDroppedCount(0) |
53 | | , mHasVideo(false) |
54 | | , mUpdateScheduler(aThread) |
55 | | , mVideoQueueSendToCompositorSize(aVQueueSentToCompositerSize) |
56 | | , mMinVideoQueueSize(StaticPrefs::MediaRuinAvSyncEnabled() ? 1 : 0) |
57 | | #ifdef XP_WIN |
58 | | , mHiResTimersRequested(false) |
59 | | #endif |
60 | | |
61 | 0 | { |
62 | 0 | MOZ_ASSERT(mAudioSink, "AudioSink should exist."); |
63 | 0 | } |
64 | | |
65 | | VideoSink::~VideoSink() |
66 | 0 | { |
67 | | #ifdef XP_WIN |
68 | | MOZ_ASSERT(!mHiResTimersRequested); |
69 | | #endif |
70 | | } |
71 | | |
72 | | const MediaSink::PlaybackParams& |
73 | | VideoSink::GetPlaybackParams() const |
74 | 0 | { |
75 | 0 | AssertOwnerThread(); |
76 | 0 |
|
77 | 0 | return mAudioSink->GetPlaybackParams(); |
78 | 0 | } |
79 | | |
80 | | void |
81 | | VideoSink::SetPlaybackParams(const PlaybackParams& aParams) |
82 | 0 | { |
83 | 0 | AssertOwnerThread(); |
84 | 0 |
|
85 | 0 | mAudioSink->SetPlaybackParams(aParams); |
86 | 0 | } |
87 | | |
88 | | RefPtr<GenericPromise> |
89 | | VideoSink::OnEnded(TrackType aType) |
90 | 0 | { |
91 | 0 | AssertOwnerThread(); |
92 | 0 | MOZ_ASSERT(mAudioSink->IsStarted(), "Must be called after playback starts."); |
93 | 0 |
|
94 | 0 | if (aType == TrackInfo::kAudioTrack) { |
95 | 0 | return mAudioSink->OnEnded(aType); |
96 | 0 | } else if (aType == TrackInfo::kVideoTrack) { |
97 | 0 | return mEndPromise; |
98 | 0 | } |
99 | 0 | return nullptr; |
100 | 0 | } |
101 | | |
102 | | TimeUnit |
103 | | VideoSink::GetEndTime(TrackType aType) const |
104 | 0 | { |
105 | 0 | AssertOwnerThread(); |
106 | 0 | MOZ_ASSERT(mAudioSink->IsStarted(), "Must be called after playback starts."); |
107 | 0 |
|
108 | 0 | if (aType == TrackInfo::kVideoTrack) { |
109 | 0 | return mVideoFrameEndTime; |
110 | 0 | } else if (aType == TrackInfo::kAudioTrack) { |
111 | 0 | return mAudioSink->GetEndTime(aType); |
112 | 0 | } |
113 | 0 | return TimeUnit::Zero(); |
114 | 0 | } |
115 | | |
116 | | TimeUnit |
117 | | VideoSink::GetPosition(TimeStamp* aTimeStamp) const |
118 | 0 | { |
119 | 0 | AssertOwnerThread(); |
120 | 0 | return mAudioSink->GetPosition(aTimeStamp); |
121 | 0 | } |
122 | | |
123 | | bool |
124 | | VideoSink::HasUnplayedFrames(TrackType aType) const |
125 | 0 | { |
126 | 0 | AssertOwnerThread(); |
127 | 0 | MOZ_ASSERT(aType == TrackInfo::kAudioTrack, "Not implemented for non audio tracks."); |
128 | 0 |
|
129 | 0 | return mAudioSink->HasUnplayedFrames(aType); |
130 | 0 | } |
131 | | |
132 | | void |
133 | | VideoSink::SetPlaybackRate(double aPlaybackRate) |
134 | 0 | { |
135 | 0 | AssertOwnerThread(); |
136 | 0 |
|
137 | 0 | mAudioSink->SetPlaybackRate(aPlaybackRate); |
138 | 0 | } |
139 | | |
140 | | void |
141 | | VideoSink::SetVolume(double aVolume) |
142 | 0 | { |
143 | 0 | AssertOwnerThread(); |
144 | 0 |
|
145 | 0 | mAudioSink->SetVolume(aVolume); |
146 | 0 | } |
147 | | |
148 | | void |
149 | | VideoSink::SetPreservesPitch(bool aPreservesPitch) |
150 | 0 | { |
151 | 0 | AssertOwnerThread(); |
152 | 0 |
|
153 | 0 | mAudioSink->SetPreservesPitch(aPreservesPitch); |
154 | 0 | } |
155 | | |
156 | | void |
157 | | VideoSink::EnsureHighResTimersOnOnlyIfPlaying() |
158 | 0 | { |
159 | | #ifdef XP_WIN |
160 | | const bool needed = IsPlaying(); |
161 | | if (needed == mHiResTimersRequested) { |
162 | | return; |
163 | | } |
164 | | if (needed) { |
165 | | // Ensure high precision timers are enabled on Windows, otherwise the VideoSink |
166 | | // isn't woken up at reliable intervals to set the next frame, and we |
167 | | // drop frames while painting. Note that each call must be matched by a |
168 | | // corresponding timeEndPeriod() call. Enabling high precision timers causes |
169 | | // the CPU to wake up more frequently on Windows 7 and earlier, which causes |
170 | | // more CPU load and battery use. So we only enable high precision timers |
171 | | // when we're actually playing. |
172 | | timeBeginPeriod(1); |
173 | | } else { |
174 | | timeEndPeriod(1); |
175 | | } |
176 | | mHiResTimersRequested = needed; |
177 | | #endif |
178 | | } |
179 | | |
180 | | void |
181 | | VideoSink::SetPlaying(bool aPlaying) |
182 | 0 | { |
183 | 0 | AssertOwnerThread(); |
184 | 0 | VSINK_LOG_V(" playing (%d) -> (%d)", mAudioSink->IsPlaying(), aPlaying); |
185 | 0 |
|
186 | 0 | if (!aPlaying) { |
187 | 0 | // Reset any update timer if paused. |
188 | 0 | mUpdateScheduler.Reset(); |
189 | 0 | // Since playback is paused, tell compositor to render only current frame. |
190 | 0 | RenderVideoFrames(1); |
191 | 0 | if (mContainer) { |
192 | 0 | mContainer->ClearCachedResources(); |
193 | 0 | } |
194 | 0 | } |
195 | 0 |
|
196 | 0 | mAudioSink->SetPlaying(aPlaying); |
197 | 0 |
|
198 | 0 | if (mHasVideo && aPlaying) { |
199 | 0 | // There's no thread in VideoSink for pulling video frames, need to trigger |
200 | 0 | // rendering while becoming playing status. because the VideoQueue may be |
201 | 0 | // full already. |
202 | 0 | TryUpdateRenderedVideoFrames(); |
203 | 0 | } |
204 | 0 |
|
205 | 0 | EnsureHighResTimersOnOnlyIfPlaying(); |
206 | 0 | } |
207 | | |
208 | | void |
209 | | VideoSink::Start(const TimeUnit& aStartTime, const MediaInfo& aInfo) |
210 | 0 | { |
211 | 0 | AssertOwnerThread(); |
212 | 0 | VSINK_LOG("[%s]", __func__); |
213 | 0 |
|
214 | 0 | mAudioSink->Start(aStartTime, aInfo); |
215 | 0 |
|
216 | 0 | mHasVideo = aInfo.HasVideo(); |
217 | 0 |
|
218 | 0 | if (mHasVideo) { |
219 | 0 | mEndPromise = mEndPromiseHolder.Ensure(__func__); |
220 | 0 |
|
221 | 0 | // If the underlying MediaSink has an end promise for the video track (which |
222 | 0 | // happens when mAudioSink refers to a DecodedStream), we must wait for it |
223 | 0 | // to complete before resolving our own end promise. Otherwise, MDSM might |
224 | 0 | // stop playback before DecodedStream plays to the end and cause |
225 | 0 | // test_streams_element_capture.html to time out. |
226 | 0 | RefPtr<GenericPromise> p = mAudioSink->OnEnded(TrackInfo::kVideoTrack); |
227 | 0 | if (p) { |
228 | 0 | RefPtr<VideoSink> self = this; |
229 | 0 | p->Then(mOwnerThread, __func__, |
230 | 0 | [self] () { |
231 | 0 | self->mVideoSinkEndRequest.Complete(); |
232 | 0 | self->TryUpdateRenderedVideoFrames(); |
233 | 0 | // It is possible the video queue size is 0 and we have no frames to |
234 | 0 | // render. However, we need to call MaybeResolveEndPromise() to ensure |
235 | 0 | // mEndPromiseHolder is resolved. |
236 | 0 | self->MaybeResolveEndPromise(); |
237 | 0 | }, [self] () { |
238 | 0 | self->mVideoSinkEndRequest.Complete(); |
239 | 0 | self->TryUpdateRenderedVideoFrames(); |
240 | 0 | self->MaybeResolveEndPromise(); |
241 | 0 | }) |
242 | 0 | ->Track(mVideoSinkEndRequest); |
243 | 0 | } |
244 | 0 |
|
245 | 0 | ConnectListener(); |
246 | 0 | // Run the render loop at least once so we can resolve the end promise |
247 | 0 | // when video duration is 0. |
248 | 0 | UpdateRenderedVideoFrames(); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | | void |
253 | | VideoSink::Stop() |
254 | 0 | { |
255 | 0 | AssertOwnerThread(); |
256 | 0 | MOZ_ASSERT(mAudioSink->IsStarted(), "playback not started."); |
257 | 0 | VSINK_LOG("[%s]", __func__); |
258 | 0 |
|
259 | 0 | mAudioSink->Stop(); |
260 | 0 |
|
261 | 0 | mUpdateScheduler.Reset(); |
262 | 0 | if (mHasVideo) { |
263 | 0 | DisconnectListener(); |
264 | 0 | mVideoSinkEndRequest.DisconnectIfExists(); |
265 | 0 | mEndPromiseHolder.ResolveIfExists(true, __func__); |
266 | 0 | mEndPromise = nullptr; |
267 | 0 | } |
268 | 0 | mVideoFrameEndTime = TimeUnit::Zero(); |
269 | 0 |
|
270 | 0 | EnsureHighResTimersOnOnlyIfPlaying(); |
271 | 0 | } |
272 | | |
273 | | bool |
274 | | VideoSink::IsStarted() const |
275 | 0 | { |
276 | 0 | AssertOwnerThread(); |
277 | 0 |
|
278 | 0 | return mAudioSink->IsStarted(); |
279 | 0 | } |
280 | | |
281 | | bool |
282 | | VideoSink::IsPlaying() const |
283 | 0 | { |
284 | 0 | AssertOwnerThread(); |
285 | 0 |
|
286 | 0 | return mAudioSink->IsPlaying(); |
287 | 0 | } |
288 | | |
289 | | void |
290 | | VideoSink::Shutdown() |
291 | 0 | { |
292 | 0 | AssertOwnerThread(); |
293 | 0 | MOZ_ASSERT(!mAudioSink->IsStarted(), "must be called after playback stops."); |
294 | 0 | VSINK_LOG("[%s]", __func__); |
295 | 0 |
|
296 | 0 | mAudioSink->Shutdown(); |
297 | 0 | } |
298 | | |
299 | | void |
300 | | VideoSink::OnVideoQueuePushed(RefPtr<VideoData>&& aSample) |
301 | 0 | { |
302 | 0 | AssertOwnerThread(); |
303 | 0 | // Listen to push event, VideoSink should try rendering ASAP if first frame |
304 | 0 | // arrives but update scheduler is not triggered yet. |
305 | 0 | if (!aSample->IsSentToCompositor()) { |
306 | 0 | // Since we push rendered frames back to the queue, we will receive |
307 | 0 | // push events for them. We only need to trigger render loop |
308 | 0 | // when this frame is not rendered yet. |
309 | 0 | TryUpdateRenderedVideoFrames(); |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | | void |
314 | | VideoSink::OnVideoQueueFinished() |
315 | 0 | { |
316 | 0 | AssertOwnerThread(); |
317 | 0 | // Run render loop if the end promise is not resolved yet. |
318 | 0 | if (!mUpdateScheduler.IsScheduled() && |
319 | 0 | mAudioSink->IsPlaying() && |
320 | 0 | !mEndPromiseHolder.IsEmpty()) { |
321 | 0 | UpdateRenderedVideoFrames(); |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | | void |
326 | | VideoSink::Redraw(const VideoInfo& aInfo) |
327 | 0 | { |
328 | 0 | AssertOwnerThread(); |
329 | 0 |
|
330 | 0 | // No video track, nothing to draw. |
331 | 0 | if (!aInfo.IsValid() || !mContainer) { |
332 | 0 | return; |
333 | 0 | } |
334 | 0 | |
335 | 0 | RefPtr<VideoData> video = VideoQueue().PeekFront(); |
336 | 0 | if (video) { |
337 | 0 | video->MarkSentToCompositor(); |
338 | 0 | mContainer->SetCurrentFrame(video->mDisplay, video->mImage, TimeStamp::Now()); |
339 | 0 | return; |
340 | 0 | } |
341 | 0 | |
342 | 0 | // When we reach here, it means there are no frames in this video track. |
343 | 0 | // Draw a blank frame to ensure there is something in the image container |
344 | 0 | // to fire 'loadeddata'. |
345 | 0 | RefPtr<Image> blank = |
346 | 0 | mContainer->GetImageContainer()->CreatePlanarYCbCrImage(); |
347 | 0 | mContainer->SetCurrentFrame(aInfo.mDisplay, blank, TimeStamp::Now()); |
348 | 0 | } |
349 | | |
350 | | void |
351 | | VideoSink::TryUpdateRenderedVideoFrames() |
352 | 0 | { |
353 | 0 | AssertOwnerThread(); |
354 | 0 | if (mUpdateScheduler.IsScheduled() || !mAudioSink->IsPlaying()) { |
355 | 0 | return; |
356 | 0 | } |
357 | 0 | RefPtr<VideoData> v = VideoQueue().PeekFront(); |
358 | 0 | if (!v) { |
359 | 0 | // No frames to render. |
360 | 0 | return; |
361 | 0 | } |
362 | 0 | |
363 | 0 | TimeStamp nowTime; |
364 | 0 | const TimeUnit clockTime = mAudioSink->GetPosition(&nowTime); |
365 | 0 | if (clockTime >= v->mTime) { |
366 | 0 | // Time to render this frame. |
367 | 0 | UpdateRenderedVideoFrames(); |
368 | 0 | return; |
369 | 0 | } |
370 | 0 | |
371 | 0 | // If we send this future frame to the compositor now, it will be rendered |
372 | 0 | // immediately and break A/V sync. Instead, we schedule a timer to send it |
373 | 0 | // later. |
374 | 0 | int64_t delta = (v->mTime - clockTime).ToMicroseconds() / |
375 | 0 | mAudioSink->GetPlaybackParams().mPlaybackRate; |
376 | 0 | TimeStamp target = nowTime + TimeDuration::FromMicroseconds(delta); |
377 | 0 | RefPtr<VideoSink> self = this; |
378 | 0 | mUpdateScheduler.Ensure( |
379 | 0 | target, |
380 | 0 | [self]() { self->UpdateRenderedVideoFramesByTimer(); }, |
381 | 0 | [self]() { self->UpdateRenderedVideoFramesByTimer(); }); |
382 | 0 | } |
383 | | |
384 | | void |
385 | | VideoSink::UpdateRenderedVideoFramesByTimer() |
386 | 0 | { |
387 | 0 | AssertOwnerThread(); |
388 | 0 | mUpdateScheduler.CompleteRequest(); |
389 | 0 | UpdateRenderedVideoFrames(); |
390 | 0 | } |
391 | | |
392 | | void |
393 | | VideoSink::ConnectListener() |
394 | 0 | { |
395 | 0 | AssertOwnerThread(); |
396 | 0 | mPushListener = VideoQueue().PushEvent().Connect( |
397 | 0 | mOwnerThread, this, &VideoSink::OnVideoQueuePushed); |
398 | 0 | mFinishListener = VideoQueue().FinishEvent().Connect( |
399 | 0 | mOwnerThread, this, &VideoSink::OnVideoQueueFinished); |
400 | 0 | } |
401 | | |
402 | | void |
403 | | VideoSink::DisconnectListener() |
404 | 0 | { |
405 | 0 | AssertOwnerThread(); |
406 | 0 | mPushListener.Disconnect(); |
407 | 0 | mFinishListener.Disconnect(); |
408 | 0 | } |
409 | | |
410 | | void |
411 | | VideoSink::RenderVideoFrames(int32_t aMaxFrames, |
412 | | int64_t aClockTime, |
413 | | const TimeStamp& aClockTimeStamp) |
414 | 0 | { |
415 | 0 | AssertOwnerThread(); |
416 | 0 |
|
417 | 0 | AutoTArray<RefPtr<VideoData>,16> frames; |
418 | 0 | VideoQueue().GetFirstElements(aMaxFrames, &frames); |
419 | 0 | if (frames.IsEmpty() || !mContainer) { |
420 | 0 | return; |
421 | 0 | } |
422 | 0 | |
423 | 0 | AutoTArray<ImageContainer::NonOwningImage,16> images; |
424 | 0 | TimeStamp lastFrameTime; |
425 | 0 | MediaSink::PlaybackParams params = mAudioSink->GetPlaybackParams(); |
426 | 0 | for (uint32_t i = 0; i < frames.Length(); ++i) { |
427 | 0 | VideoData* frame = frames[i]; |
428 | 0 |
|
429 | 0 | frame->MarkSentToCompositor(); |
430 | 0 |
|
431 | 0 | if (!frame->mImage || !frame->mImage->IsValid() || |
432 | 0 | !frame->mImage->GetSize().width || !frame->mImage->GetSize().height) { |
433 | 0 | continue; |
434 | 0 | } |
435 | 0 | |
436 | 0 | if (frame->mTime.IsNegative()) { |
437 | 0 | // Frame times before the start time are invalid; drop such frames |
438 | 0 | continue; |
439 | 0 | } |
440 | 0 | |
441 | 0 | TimeStamp t; |
442 | 0 | if (aMaxFrames > 1) { |
443 | 0 | MOZ_ASSERT(!aClockTimeStamp.IsNull()); |
444 | 0 | int64_t delta = frame->mTime.ToMicroseconds() - aClockTime; |
445 | 0 | t = aClockTimeStamp + |
446 | 0 | TimeDuration::FromMicroseconds(delta / params.mPlaybackRate); |
447 | 0 | if (!lastFrameTime.IsNull() && t <= lastFrameTime) { |
448 | 0 | // Timestamps out of order; drop the new frame. In theory we should |
449 | 0 | // probably replace the previous frame with the new frame if the |
450 | 0 | // timestamps are equal, but this is a corrupt video file already so |
451 | 0 | // never mind. |
452 | 0 | continue; |
453 | 0 | } |
454 | 0 | lastFrameTime = t; |
455 | 0 | } |
456 | 0 |
|
457 | 0 | ImageContainer::NonOwningImage* img = images.AppendElement(); |
458 | 0 | img->mTimeStamp = t; |
459 | 0 | img->mImage = frame->mImage; |
460 | 0 | img->mFrameID = frame->mFrameID; |
461 | 0 | img->mProducerID = mProducerID; |
462 | 0 |
|
463 | 0 | VSINK_LOG_V("playing video frame %" PRId64 " (id=%x) (vq-queued=%zu)", |
464 | 0 | frame->mTime.ToMicroseconds(), frame->mFrameID, |
465 | 0 | VideoQueue().GetSize()); |
466 | 0 | } |
467 | 0 |
|
468 | 0 | if (images.Length() > 0) { |
469 | 0 | mContainer->SetCurrentFrames(frames[0]->mDisplay, images); |
470 | 0 | } |
471 | 0 | } |
472 | | |
473 | | void |
474 | | VideoSink::UpdateRenderedVideoFrames() |
475 | 0 | { |
476 | 0 | AssertOwnerThread(); |
477 | 0 | MOZ_ASSERT(mAudioSink->IsPlaying(), "should be called while playing."); |
478 | 0 |
|
479 | 0 | // Get the current playback position. |
480 | 0 | TimeStamp nowTime; |
481 | 0 | const auto clockTime = mAudioSink->GetPosition(&nowTime); |
482 | 0 | MOZ_ASSERT(!clockTime.IsNegative(), "Should have positive clock time."); |
483 | 0 |
|
484 | 0 | uint32_t sentToCompositorCount = 0; |
485 | 0 | uint32_t droppedCount = 0; |
486 | 0 |
|
487 | 0 | // Skip frames up to the playback position. |
488 | 0 | TimeUnit lastFrameEndTime; |
489 | 0 | while (VideoQueue().GetSize() > mMinVideoQueueSize && |
490 | 0 | clockTime >= VideoQueue().PeekFront()->GetEndTime()) { |
491 | 0 | RefPtr<VideoData> frame = VideoQueue().PopFront(); |
492 | 0 | lastFrameEndTime = frame->GetEndTime(); |
493 | 0 | if (frame->IsSentToCompositor()) { |
494 | 0 | sentToCompositorCount++; |
495 | 0 | } else { |
496 | 0 | droppedCount++; |
497 | 0 | VSINK_LOG_V("discarding video frame mTime=%" PRId64 " clock_time=%" PRId64, |
498 | 0 | frame->mTime.ToMicroseconds(), clockTime.ToMicroseconds()); |
499 | 0 | } |
500 | 0 | } |
501 | 0 |
|
502 | 0 | if (droppedCount || sentToCompositorCount) { |
503 | 0 | uint32_t totalCompositorDroppedCount = mContainer->GetDroppedImageCount(); |
504 | 0 | uint32_t compositorDroppedCount = |
505 | 0 | totalCompositorDroppedCount - mOldCompositorDroppedCount; |
506 | 0 | if (compositorDroppedCount > 0) { |
507 | 0 | mOldCompositorDroppedCount = totalCompositorDroppedCount; |
508 | 0 | VSINK_LOG_V("%u video frame previously discarded by compositor", |
509 | 0 | compositorDroppedCount); |
510 | 0 | } |
511 | 0 | mPendingDroppedCount += compositorDroppedCount; |
512 | 0 | uint32_t droppedReported = mPendingDroppedCount > sentToCompositorCount |
513 | 0 | ? sentToCompositorCount |
514 | 0 | : mPendingDroppedCount; |
515 | 0 | mPendingDroppedCount -= droppedReported; |
516 | 0 |
|
517 | 0 | mFrameStats.Accumulate({ 0, |
518 | 0 | 0, |
519 | 0 | droppedCount + droppedReported, |
520 | 0 | sentToCompositorCount - droppedReported }); |
521 | 0 | } |
522 | 0 |
|
523 | 0 | // The presentation end time of the last video frame displayed is either |
524 | 0 | // the end time of the current frame, or if we dropped all frames in the |
525 | 0 | // queue, the end time of the last frame we removed from the queue. |
526 | 0 | RefPtr<VideoData> currentFrame = VideoQueue().PeekFront(); |
527 | 0 | mVideoFrameEndTime = std::max(mVideoFrameEndTime, |
528 | 0 | currentFrame ? currentFrame->GetEndTime() : lastFrameEndTime); |
529 | 0 |
|
530 | 0 | RenderVideoFrames( |
531 | 0 | mVideoQueueSendToCompositorSize, |
532 | 0 | clockTime.ToMicroseconds(), nowTime); |
533 | 0 |
|
534 | 0 | MaybeResolveEndPromise(); |
535 | 0 |
|
536 | 0 | // Get the timestamp of the next frame. Schedule the next update at |
537 | 0 | // the start time of the next frame. If we don't have a next frame, |
538 | 0 | // we will run render loops again upon incoming frames. |
539 | 0 | nsTArray<RefPtr<VideoData>> frames; |
540 | 0 | VideoQueue().GetFirstElements(2, &frames); |
541 | 0 | if (frames.Length() < 2) { |
542 | 0 | return; |
543 | 0 | } |
544 | 0 | |
545 | 0 | int64_t nextFrameTime = frames[1]->mTime.ToMicroseconds(); |
546 | 0 | int64_t delta = std::max( |
547 | 0 | nextFrameTime - clockTime.ToMicroseconds(), MIN_UPDATE_INTERVAL_US); |
548 | 0 | TimeStamp target = nowTime + TimeDuration::FromMicroseconds( |
549 | 0 | delta / mAudioSink->GetPlaybackParams().mPlaybackRate); |
550 | 0 |
|
551 | 0 | RefPtr<VideoSink> self = this; |
552 | 0 | mUpdateScheduler.Ensure(target, [self] () { |
553 | 0 | self->UpdateRenderedVideoFramesByTimer(); |
554 | 0 | }, [self] () { |
555 | 0 | self->UpdateRenderedVideoFramesByTimer(); |
556 | 0 | }); |
557 | 0 | } |
558 | | |
559 | | void |
560 | | VideoSink::MaybeResolveEndPromise() |
561 | 0 | { |
562 | 0 | AssertOwnerThread(); |
563 | 0 | // All frames are rendered, Let's resolve the promise. |
564 | 0 | if (VideoQueue().IsFinished() && |
565 | 0 | VideoQueue().GetSize() <= 1 && |
566 | 0 | !mVideoSinkEndRequest.Exists()) { |
567 | 0 | if (VideoQueue().GetSize() == 1) { |
568 | 0 | // Remove the last frame since we have sent it to compositor. |
569 | 0 | RefPtr<VideoData> frame = VideoQueue().PopFront(); |
570 | 0 | if (mPendingDroppedCount > 0) { |
571 | 0 | mFrameStats.Accumulate({ 0, 0, 1, 0 }); |
572 | 0 | mPendingDroppedCount--; |
573 | 0 | } else { |
574 | 0 | mFrameStats.NotifyPresentedFrame(); |
575 | 0 | } |
576 | 0 | } |
577 | 0 | mEndPromiseHolder.ResolveIfExists(true, __func__); |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | | nsCString |
582 | | VideoSink::GetDebugInfo() |
583 | 0 | { |
584 | 0 | AssertOwnerThread(); |
585 | 0 | auto str = nsPrintfCString( |
586 | 0 | "VideoSink: IsStarted=%d IsPlaying=%d VideoQueue(finished=%d " |
587 | 0 | "size=%zu) mVideoFrameEndTime=%" PRId64 " mHasVideo=%d " |
588 | 0 | "mVideoSinkEndRequest.Exists()=%d mEndPromiseHolder.IsEmpty()=%d", |
589 | 0 | IsStarted(), |
590 | 0 | IsPlaying(), |
591 | 0 | VideoQueue().IsFinished(), |
592 | 0 | VideoQueue().GetSize(), |
593 | 0 | mVideoFrameEndTime.ToMicroseconds(), |
594 | 0 | mHasVideo, |
595 | 0 | mVideoSinkEndRequest.Exists(), |
596 | 0 | mEndPromiseHolder.IsEmpty()); |
597 | 0 | AppendStringIfNotEmpty(str, mAudioSink->GetDebugInfo()); |
598 | 0 | return std::move(str); |
599 | 0 | } |
600 | | |
601 | | } // namespace media |
602 | | } // namespace mozilla |