niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 0e703f4 | 2012-03-06 12:02:20 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "common_video/include/incoming_video_stream.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "common_video/video_render_frames.h" |
| 16 | #include "rtc_base/timeutils.h" |
| 17 | #include "rtc_base/trace_event.h" |
| 18 | #include "system_wrappers/include/event_wrapper.h" |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 20 | namespace webrtc { |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 21 | namespace { |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 22 | const char kIncomingQueueName[] = "IncomingVideoStream"; |
| 23 | } |
| 24 | |
| 25 | // Capture by moving (std::move) into a lambda isn't possible in C++11 |
| 26 | // (supported in C++14). This class provides the functionality of what would be |
| 27 | // something like (inside OnFrame): |
| 28 | // VideoFrame frame(video_frame); |
| 29 | // incoming_render_queue_.PostTask([this, frame = std::move(frame)](){ |
| 30 | // if (render_buffers_.AddFrame(std::move(frame)) == 1) |
| 31 | // Dequeue(); |
| 32 | // }); |
| 33 | class IncomingVideoStream::NewFrameTask : public rtc::QueuedTask { |
| 34 | public: |
| 35 | NewFrameTask(IncomingVideoStream* stream, VideoFrame frame) |
| 36 | : stream_(stream), frame_(std::move(frame)) {} |
| 37 | |
| 38 | private: |
| 39 | bool Run() override { |
nisse | 2c7b7a6 | 2017-09-04 12:18:21 | [diff] [blame] | 40 | RTC_DCHECK(stream_->incoming_render_queue_.IsCurrent()); |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 41 | if (stream_->render_buffers_.AddFrame(std::move(frame_)) == 1) |
| 42 | stream_->Dequeue(); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | IncomingVideoStream* stream_; |
| 47 | VideoFrame frame_; |
| 48 | }; |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 49 | |
tommi | 2e82f38 | 2016-06-21 07:26:43 | [diff] [blame] | 50 | IncomingVideoStream::IncomingVideoStream( |
| 51 | int32_t delay_ms, |
| 52 | rtc::VideoSinkInterface<VideoFrame>* callback) |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 53 | : render_buffers_(delay_ms), |
| 54 | callback_(callback), |
| 55 | incoming_render_queue_(kIncomingQueueName, |
| 56 | rtc::TaskQueue::Priority::HIGH) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 57 | |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 58 | IncomingVideoStream::~IncomingVideoStream() { |
tommi | 2e82f38 | 2016-06-21 07:26:43 | [diff] [blame] | 59 | RTC_DCHECK(main_thread_checker_.CalledOnValidThread()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 60 | } |
| 61 | |
nisse | 30f118e | 2016-05-03 08:09:11 | [diff] [blame] | 62 | void IncomingVideoStream::OnFrame(const VideoFrame& video_frame) { |
tommi | db23ea6 | 2017-03-03 15:21:18 | [diff] [blame] | 63 | TRACE_EVENT0("webrtc", "IncomingVideoStream::OnFrame"); |
Peter Boström | 02bafc6 | 2016-07-01 10:45:15 | [diff] [blame] | 64 | RTC_CHECK_RUNS_SERIALIZED(&decoder_race_checker_); |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 65 | RTC_DCHECK(!incoming_render_queue_.IsCurrent()); |
| 66 | incoming_render_queue_.PostTask( |
| 67 | std::unique_ptr<rtc::QueuedTask>(new NewFrameTask(this, video_frame))); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 68 | } |
| 69 | |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 70 | void IncomingVideoStream::Dequeue() { |
tommi | db23ea6 | 2017-03-03 15:21:18 | [diff] [blame] | 71 | TRACE_EVENT0("webrtc", "IncomingVideoStream::Dequeue"); |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 72 | RTC_DCHECK(incoming_render_queue_.IsCurrent()); |
| 73 | rtc::Optional<VideoFrame> frame_to_render = render_buffers_.FrameToRender(); |
| 74 | if (frame_to_render) |
| 75 | callback_->OnFrame(*frame_to_render); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 76 | |
tommi | ede0759 | 2017-02-27 15:16:10 | [diff] [blame] | 77 | if (render_buffers_.HasPendingFrames()) { |
| 78 | uint32_t wait_time = render_buffers_.TimeToNextFrameRelease(); |
| 79 | incoming_render_queue_.PostDelayedTask([this]() { Dequeue(); }, wait_time); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 80 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 81 | } |
| 82 | |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 | [diff] [blame] | 83 | } // namespace webrtc |