| 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 |
| 11 | #include "video/decode_synchronizer.h" |
| 12 |
| 13 | #include <iterator> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "api/sequence_checker.h" |
| 19 | #include "api/units/time_delta.h" |
| 20 | #include "api/units/timestamp.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
| 23 | #include "rtc_base/trace_event.h" |
| 24 | #include "video/frame_decode_scheduler.h" |
| 25 | #include "video/frame_decode_timing.h" |
| 26 |
| 27 | namespace webrtc { |
| 28 |
| 29 | DecodeSynchronizer::ScheduledFrame::ScheduledFrame( |
| 30 | uint32_t rtp_timestamp, |
| 31 | FrameDecodeTiming::FrameSchedule schedule, |
| 32 | FrameDecodeScheduler::FrameReleaseCallback callback) |
| 33 | : rtp_timestamp_(rtp_timestamp), |
| 34 | schedule_(std::move(schedule)), |
| 35 | callback_(std::move(callback)) {} |
| 36 |
| 37 | void DecodeSynchronizer::ScheduledFrame::RunFrameReleaseCallback() && { |
| 38 | // Inspiration from Chromium base::OnceCallback. Move `*this` to a local |
| 39 | // before execution to ensure internal state is cleared after callback |
| 40 | // execution. |
| 41 | auto sf = std::move(*this); |
| 42 | std::move(sf.callback_)(sf.rtp_timestamp_, sf.schedule_.render_time); |
| 43 | } |
| 44 |
| 45 | Timestamp DecodeSynchronizer::ScheduledFrame::LatestDecodeTime() const { |
| 46 | return schedule_.latest_decode_time; |
| 47 | } |
| 48 |
| 49 | DecodeSynchronizer::SynchronizedFrameDecodeScheduler:: |
| 50 | SynchronizedFrameDecodeScheduler(DecodeSynchronizer* sync) |
| 51 | : sync_(sync) { |
| 52 | RTC_DCHECK(sync_); |
| 53 | } |
| 54 |
| 55 | DecodeSynchronizer::SynchronizedFrameDecodeScheduler:: |
| 56 | ~SynchronizedFrameDecodeScheduler() { |
| 57 | RTC_DCHECK(!next_frame_); |
| 58 | RTC_DCHECK(stopped_); |
| 59 | } |
| 60 |
| 61 | absl::optional<uint32_t> |
| 62 | DecodeSynchronizer::SynchronizedFrameDecodeScheduler::ScheduledRtpTimestamp() { |
| 63 | return next_frame_.has_value() |
| 64 | ? absl::make_optional(next_frame_->rtp_timestamp()) |
| 65 | : absl::nullopt; |
| 66 | } |
| 67 |
| 68 | DecodeSynchronizer::ScheduledFrame |
| 69 | DecodeSynchronizer::SynchronizedFrameDecodeScheduler::ReleaseNextFrame() { |
| 70 | RTC_DCHECK(!stopped_); |
| 71 | RTC_DCHECK(next_frame_); |
| 72 | auto res = std::move(*next_frame_); |
| 73 | next_frame_.reset(); |
| 74 | return res; |
| 75 | } |
| 76 |
| 77 | Timestamp |
| 78 | DecodeSynchronizer::SynchronizedFrameDecodeScheduler::LatestDecodeTime() { |
| 79 | RTC_DCHECK(next_frame_); |
| 80 | return next_frame_->LatestDecodeTime(); |
| 81 | } |
| 82 |
| 83 | void DecodeSynchronizer::SynchronizedFrameDecodeScheduler::ScheduleFrame( |
| 84 | uint32_t rtp, |
| 85 | FrameDecodeTiming::FrameSchedule schedule, |
| 86 | FrameReleaseCallback cb) { |
| 87 | RTC_DCHECK(!stopped_); |
| 88 | RTC_DCHECK(!next_frame_) << "Can not schedule two frames at once."; |
| 89 | next_frame_ = ScheduledFrame(rtp, std::move(schedule), std::move(cb)); |
| 90 | sync_->OnFrameScheduled(this); |
| 91 | } |
| 92 |
| 93 | void DecodeSynchronizer::SynchronizedFrameDecodeScheduler::CancelOutstanding() { |
| 94 | next_frame_.reset(); |
| 95 | } |
| 96 |
| 97 | void DecodeSynchronizer::SynchronizedFrameDecodeScheduler::Stop() { |
| 98 | if (stopped_) { |
| 99 | return; |
| 100 | } |
| 101 | CancelOutstanding(); |
| 102 | stopped_ = true; |
| 103 | sync_->RemoveFrameScheduler(this); |
| 104 | } |
| 105 |
| 106 | DecodeSynchronizer::DecodeSynchronizer(Clock* clock, |
| 107 | Metronome* metronome, |
| 108 | TaskQueueBase* worker_queue) |
| 109 | : clock_(clock), worker_queue_(worker_queue), metronome_(metronome) { |
| 110 | RTC_DCHECK(metronome_); |
| 111 | RTC_DCHECK(worker_queue_); |
| 112 | } |
| 113 |
| 114 | DecodeSynchronizer::~DecodeSynchronizer() { |
| 115 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 116 | RTC_CHECK(schedulers_.empty()); |
| 117 | } |
| 118 |
| 119 | std::unique_ptr<FrameDecodeScheduler> |
| 120 | DecodeSynchronizer::CreateSynchronizedFrameScheduler() { |
| 121 | TRACE_EVENT0("webrtc", __func__); |
| 122 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 123 | auto scheduler = std::make_unique<SynchronizedFrameDecodeScheduler>(this); |
| 124 | auto [it, inserted] = schedulers_.emplace(scheduler.get()); |
| 125 | // If this is the first `scheduler` added, start listening to the metronome. |
| 126 | if (inserted && schedulers_.size() == 1) { |
| 127 | RTC_DLOG(LS_VERBOSE) << "Listening to metronome"; |
| 128 | ScheduleNextTick(); |
| 129 | } |
| 130 |
| 131 | return std::move(scheduler); |
| 132 | } |
| 133 |
| 134 | void DecodeSynchronizer::OnFrameScheduled( |
| 135 | SynchronizedFrameDecodeScheduler* scheduler) { |
| 136 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 137 | RTC_DCHECK(scheduler->ScheduledRtpTimestamp()); |
| 138 |
| 139 | Timestamp now = clock_->CurrentTime(); |
| 140 | Timestamp next_tick = expected_next_tick_; |
| 141 | // If no tick has registered yet assume it will occur in the tick period. |
| 142 | if (next_tick.IsInfinite()) { |
| 143 | next_tick = now + metronome_->TickPeriod(); |
| 144 | } |
| 145 |
| 146 | // Release the frame right away if the decode time is too soon. Otherwise |
| 147 | // the stream may fall behind too much. |
| 148 | bool decode_before_next_tick = |
| 149 | scheduler->LatestDecodeTime() < |
| 150 | (next_tick - FrameDecodeTiming::kMaxAllowedFrameDelay); |
| 151 | // Decode immediately if the decode time is in the past. |
| 152 | bool decode_time_in_past = scheduler->LatestDecodeTime() < now; |
| 153 |
| 154 | if (decode_before_next_tick || decode_time_in_past) { |
| 155 | ScheduledFrame scheduled_frame = scheduler->ReleaseNextFrame(); |
| 156 | std::move(scheduled_frame).RunFrameReleaseCallback(); |
| 157 | } |
| 158 | } |
| 159 |
| 160 | void DecodeSynchronizer::RemoveFrameScheduler( |
| 161 | SynchronizedFrameDecodeScheduler* scheduler) { |
| 162 | TRACE_EVENT0("webrtc", __func__); |
| 163 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 164 | RTC_DCHECK(scheduler); |
| 165 | auto it = schedulers_.find(scheduler); |
| 166 | if (it == schedulers_.end()) { |
| 167 | return; |
| 168 | } |
| 169 | schedulers_.erase(it); |
| 170 | // If there are no more schedulers active, stop listening for metronome ticks. |
| 171 | if (schedulers_.empty()) { |
| 172 | expected_next_tick_ = Timestamp::PlusInfinity(); |
| 173 | } |
| 174 | } |
| 175 |
| 176 | void DecodeSynchronizer::ScheduleNextTick() { |
| 177 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 178 | if (tick_scheduled_) { |
| 179 | return; |
| 180 | } |
| 181 | tick_scheduled_ = true; |
| 182 | metronome_->RequestCallOnNextTick( |
| 183 | SafeTask(safety_.flag(), [this] { OnTick(); })); |
| 184 | } |
| 185 |
| 186 | void DecodeSynchronizer::OnTick() { |
| 187 | TRACE_EVENT0("webrtc", __func__); |
| 188 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 189 | tick_scheduled_ = false; |
| 190 | expected_next_tick_ = clock_->CurrentTime() + metronome_->TickPeriod(); |
| 191 |
| 192 | for (auto* scheduler : schedulers_) { |
| 193 | if (scheduler->ScheduledRtpTimestamp() && |
| 194 | scheduler->LatestDecodeTime() < expected_next_tick_) { |
| 195 | auto scheduled_frame = scheduler->ReleaseNextFrame(); |
| 196 | std::move(scheduled_frame).RunFrameReleaseCallback(); |
| 197 | } |
| 198 | } |
| 199 |
| 200 | if (!schedulers_.empty()) |
| 201 | ScheduleNextTick(); |
| 202 | } |
| 203 |
| 204 | } // namespace webrtc |
| 205 |