| 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 | #ifndef VIDEO_VIDEO_RECEIVE_STREAM2_H_ |
| 12 | #define VIDEO_VIDEO_RECEIVE_STREAM2_H_ |
| 13 |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/environment/environment.h" |
| 21 | #include "api/sequence_checker.h" |
| 22 | #include "api/task_queue/pending_task_safety_flag.h" |
| 23 | #include "api/task_queue/task_queue_base.h" |
| 24 | #include "api/units/time_delta.h" |
| 25 | #include "api/units/timestamp.h" |
| 26 | #include "api/video/recordable_encoded_frame.h" |
| 27 | #include "call/call.h" |
| 28 | #include "call/rtp_packet_sink_interface.h" |
| 29 | #include "call/syncable.h" |
| 30 | #include "call/video_receive_stream.h" |
| 31 | #include "modules/rtp_rtcp/source/source_tracker.h" |
| 32 | #include "modules/video_coding/nack_requester.h" |
| 33 | #include "modules/video_coding/video_receiver2.h" |
| 34 | #include "rtc_base/system/no_unique_address.h" |
| 35 | #include "rtc_base/thread_annotations.h" |
| 36 | #include "video/receive_statistics_proxy.h" |
| 37 | #include "video/rtp_streams_synchronizer2.h" |
| 38 | #include "video/rtp_video_stream_receiver2.h" |
| 39 | #include "video/transport_adapter.h" |
| 40 | #include "video/video_stream_buffer_controller.h" |
| 41 | #include "video/video_stream_decoder2.h" |
| 42 |
| 43 | namespace webrtc { |
| 44 |
| 45 | class RtpStreamReceiverInterface; |
| 46 | class RtpStreamReceiverControllerInterface; |
| 47 | class RtxReceiveStream; |
| 48 | class VCMTiming; |
| 49 |
| 50 | constexpr TimeDelta kMaxWaitForKeyFrame = TimeDelta::Millis(200); |
| 51 | constexpr TimeDelta kMaxWaitForFrame = TimeDelta::Seconds(3); |
| 52 |
| 53 | namespace internal { |
| 54 |
| 55 | class CallStats; |
| 56 |
| 57 | // Utility struct for grabbing metadata from a VideoFrame and processing it |
| 58 | // asynchronously without needing the actual frame data. |
| 59 | // Additionally the caller can bundle information from the current clock |
| 60 | // when the metadata is captured, for accurate reporting and not needing |
| 61 | // multiple calls to clock->Now(). |
| 62 | struct VideoFrameMetaData { |
| 63 | VideoFrameMetaData(const webrtc::VideoFrame& frame, Timestamp now) |
| 64 | : rtp_timestamp(frame.timestamp()), |
| 65 | timestamp_us(frame.timestamp_us()), |
| 66 | ntp_time_ms(frame.ntp_time_ms()), |
| 67 | width(frame.width()), |
| 68 | height(frame.height()), |
| 69 | decode_timestamp(now) {} |
| 70 |
| 71 | int64_t render_time_ms() const { |
| 72 | return timestamp_us / rtc::kNumMicrosecsPerMillisec; |
| 73 | } |
| 74 |
| 75 | const uint32_t rtp_timestamp; |
| 76 | const int64_t timestamp_us; |
| 77 | const int64_t ntp_time_ms; |
| 78 | const int width; |
| 79 | const int height; |
| 80 |
| 81 | const Timestamp decode_timestamp; |
| 82 | }; |
| 83 |
| 84 | class VideoReceiveStream2 |
| 85 | : public webrtc::VideoReceiveStreamInterface, |
| 86 | public rtc::VideoSinkInterface<VideoFrame>, |
| 87 | public RtpVideoStreamReceiver2::OnCompleteFrameCallback, |
| 88 | public Syncable, |
| 89 | public CallStatsObserver, |
| 90 | public FrameSchedulingReceiver { |
| 91 | public: |
| 92 | // The maximum number of buffered encoded frames when encoded output is |
| 93 | // configured. |
| 94 | static constexpr size_t kBufferedEncodedFramesMaxSize = 60; |
| 95 |
| 96 | VideoReceiveStream2(const Environment& env, |
| 97 | Call* call, |
| 98 | int num_cpu_cores, |
| 99 | PacketRouter* packet_router, |
| 100 | VideoReceiveStreamInterface::Config config, |
| 101 | CallStats* call_stats, |
| 102 | std::unique_ptr<VCMTiming> timing, |
| 103 | NackPeriodicProcessor* nack_periodic_processor, |
| 104 | DecodeSynchronizer* decode_sync); |
| 105 | // Destruction happens on the worker thread. Prior to destruction the caller |
| 106 | // must ensure that a registration with the transport has been cleared. See |
| 107 | // `RegisterWithTransport` for details. |
| 108 | // TODO(tommi): As a further improvement to this, performing the full |
| 109 | // destruction on the network thread could be made the default. |
| 110 | ~VideoReceiveStream2() override; |
| 111 |
| 112 | // Called on `packet_sequence_checker_` to register/unregister with the |
| 113 | // network transport. |
| 114 | void RegisterWithTransport( |
| 115 | RtpStreamReceiverControllerInterface* receiver_controller); |
| 116 | // If registration has previously been done (via `RegisterWithTransport`) then |
| 117 | // `UnregisterFromTransport` must be called prior to destruction, on the |
| 118 | // network thread. |
| 119 | void UnregisterFromTransport(); |
| 120 |
| 121 | // Accessor for the a/v sync group. This value may change and the caller |
| 122 | // must be on the packet delivery thread. |
| 123 | const std::string& sync_group() const; |
| 124 |
| 125 | // Getters for const remote SSRC values that won't change throughout the |
| 126 | // object's lifetime. |
| 127 | uint32_t remote_ssrc() const { return config_.rtp.remote_ssrc; } |
| 128 | // RTX ssrc can be updated. |
| 129 | uint32_t rtx_ssrc() const { |
| 130 | RTC_DCHECK_RUN_ON(&packet_sequence_checker_); |
| 131 | return updated_rtx_ssrc_.value_or(config_.rtp.rtx_ssrc); |
| 132 | } |
| 133 |
| 134 | void SignalNetworkState(NetworkState state); |
| 135 | bool DeliverRtcp(const uint8_t* packet, size_t length); |
| 136 |
| 137 | void SetSync(Syncable* audio_syncable); |
| 138 |
| 139 | // Updates the `rtp_video_stream_receiver_`'s `local_ssrc` when the default |
| 140 | // sender has been created, changed or removed. |
| 141 | void SetLocalSsrc(uint32_t local_ssrc); |
| 142 |
| 143 | // Implements webrtc::VideoReceiveStreamInterface. |
| 144 | void Start() override; |
| 145 | void Stop() override; |
| 146 |
| 147 | void SetRtcpMode(RtcpMode mode) override; |
| 148 | void SetFlexFecProtection(RtpPacketSinkInterface* flexfec_sink) override; |
| 149 | void SetLossNotificationEnabled(bool enabled) override; |
| 150 | void SetNackHistory(TimeDelta history) override; |
| 151 | void SetProtectionPayloadTypes(int red_payload_type, |
| 152 | int ulpfec_payload_type) override; |
| 153 | void SetRtcpXr(Config::Rtp::RtcpXr rtcp_xr) override; |
| 154 | void SetAssociatedPayloadTypes( |
| 155 | std::map<int, int> associated_payload_types) override; |
| 156 |
| 157 | webrtc::VideoReceiveStreamInterface::Stats GetStats() const override; |
| 158 |
| 159 | // SetBaseMinimumPlayoutDelayMs and GetBaseMinimumPlayoutDelayMs are called |
| 160 | // from webrtc/api level and requested by user code. For e.g. blink/js layer |
| 161 | // in Chromium. |
| 162 | bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override; |
| 163 | int GetBaseMinimumPlayoutDelayMs() const override; |
| 164 |
| 165 | void SetFrameDecryptor( |
| 166 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override; |
| 167 | void SetDepacketizerToDecoderFrameTransformer( |
| 168 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override; |
| 169 |
| 170 | // Implements rtc::VideoSinkInterface<VideoFrame>. |
| 171 | void OnFrame(const VideoFrame& video_frame) override; |
| 172 |
| 173 | // Implements RtpVideoStreamReceiver2::OnCompleteFrameCallback. |
| 174 | void OnCompleteFrame(std::unique_ptr<EncodedFrame> frame) override; |
| 175 |
| 176 | // Implements CallStatsObserver::OnRttUpdate |
| 177 | void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override; |
| 178 |
| 179 | // Implements Syncable. |
| 180 | uint32_t id() const override; |
| 181 | absl::optional<Syncable::Info> GetInfo() const override; |
| 182 | bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp, |
| 183 | int64_t* time_ms) const override; |
| 184 | void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms, |
| 185 | int64_t time_ms) override; |
| 186 |
| 187 | // SetMinimumPlayoutDelay is only called by A/V sync. |
| 188 | bool SetMinimumPlayoutDelay(int delay_ms) override; |
| 189 |
| 190 | std::vector<webrtc::RtpSource> GetSources() const override; |
| 191 |
| 192 | RecordingState SetAndGetRecordingState(RecordingState state, |
| 193 | bool generate_key_frame) override; |
| 194 | void GenerateKeyFrame() override; |
| 195 |
| 196 | void UpdateRtxSsrc(uint32_t ssrc) override; |
| 197 |
| 198 | private: |
| 199 | // FrameSchedulingReceiver implementation. |
| 200 | // Called on packet sequence. |
| 201 | void OnEncodedFrame(std::unique_ptr<EncodedFrame> frame) override; |
| 202 | // Called on packet sequence. |
| 203 | void OnDecodableFrameTimeout(TimeDelta wait) override; |
| 204 |
| 205 | void CreateAndRegisterExternalDecoder(const Decoder& decoder); |
| 206 |
| 207 | struct DecodeFrameResult { |
| 208 | // True if the decoder returned code WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME, |
| 209 | // or if the decoder failed and a keyframe is required. When true, a |
| 210 | // keyframe request should be sent even if a keyframe request was sent |
| 211 | // recently. |
| 212 | bool force_request_key_frame; |
| 213 |
| 214 | // The picture id of the frame that was decoded, or nullopt if the frame was |
| 215 | // not decoded. |
| 216 | absl::optional<int64_t> decoded_frame_picture_id; |
| 217 |
| 218 | // True if the next frame decoded must be a keyframe. This value will set |
| 219 | // the value of `keyframe_required_`, which will force the frame buffer to |
| 220 | // drop all frames that are not keyframes. |
| 221 | bool keyframe_required; |
| 222 | }; |
| 223 |
| 224 | DecodeFrameResult HandleEncodedFrameOnDecodeQueue( |
| 225 | std::unique_ptr<EncodedFrame> frame, |
| 226 | bool keyframe_request_is_due, |
| 227 | bool keyframe_required) RTC_RUN_ON(decode_sequence_checker_); |
| 228 | void UpdatePlayoutDelays() const |
| 229 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_sequence_checker_); |
| 230 | void RequestKeyFrame(Timestamp now) RTC_RUN_ON(packet_sequence_checker_); |
| 231 | void HandleKeyFrameGeneration(bool received_frame_is_keyframe, |
| 232 | Timestamp now, |
| 233 | bool always_request_key_frame, |
| 234 | bool keyframe_request_is_due) |
| 235 | RTC_RUN_ON(packet_sequence_checker_); |
| 236 | bool IsReceivingKeyFrame(Timestamp timestamp) const |
| 237 | RTC_RUN_ON(packet_sequence_checker_); |
| 238 | int DecodeAndMaybeDispatchEncodedFrame(std::unique_ptr<EncodedFrame> frame) |
| 239 | RTC_RUN_ON(decode_sequence_checker_); |
| 240 |
| 241 | void UpdateHistograms(); |
| 242 |
| 243 | const Environment env_; |
| 244 |
| 245 | RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_sequence_checker_; |
| 246 | // TODO(bugs.webrtc.org/11993): This checker conceptually represents |
| 247 | // operations that belong to the network thread. The Call class is currently |
| 248 | // moving towards handling network packets on the network thread and while |
| 249 | // that work is ongoing, this checker may in practice represent the worker |
| 250 | // thread, but still serves as a mechanism of grouping together concepts |
| 251 | // that belong to the network thread. Once the packets are fully delivered |
| 252 | // on the network thread, this comment will be deleted. |
| 253 | RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_; |
| 254 |
| 255 | RTC_NO_UNIQUE_ADDRESS SequenceChecker decode_sequence_checker_; |
| 256 |
| 257 | TransportAdapter transport_adapter_; |
| 258 | const VideoReceiveStreamInterface::Config config_; |
| 259 | const int num_cpu_cores_; |
| 260 | Call* const call_; |
| 261 |
| 262 | CallStats* const call_stats_; |
| 263 |
| 264 | bool decoder_running_ RTC_GUARDED_BY(worker_sequence_checker_) = false; |
| 265 | bool decoder_stopped_ RTC_GUARDED_BY(decode_sequence_checker_) = true; |
| 266 |
| 267 | SourceTracker source_tracker_; |
| 268 | ReceiveStatisticsProxy stats_proxy_; |
| 269 | // Shared by media and rtx stream receivers, since the latter has no RtpRtcp |
| 270 | // module of its own. |
| 271 | const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_; |
| 272 |
| 273 | std::unique_ptr<VCMTiming> timing_; // Jitter buffer experiment. |
| 274 | VideoReceiver2 video_receiver_; |
| 275 | std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> incoming_video_stream_; |
| 276 | RtpVideoStreamReceiver2 rtp_video_stream_receiver_; |
| 277 | std::unique_ptr<VideoStreamDecoder> video_stream_decoder_; |
| 278 | RtpStreamsSynchronizer rtp_stream_sync_; |
| 279 |
| 280 | std::unique_ptr<VideoStreamBufferController> buffer_; |
| 281 |
| 282 | // `receiver_controller_` is valid from when RegisterWithTransport is invoked |
| 283 | // until UnregisterFromTransport. |
| 284 | RtpStreamReceiverControllerInterface* receiver_controller_ |
| 285 | RTC_GUARDED_BY(packet_sequence_checker_) = nullptr; |
| 286 |
| 287 | std::unique_ptr<RtpStreamReceiverInterface> media_receiver_ |
| 288 | RTC_GUARDED_BY(packet_sequence_checker_); |
| 289 | std::unique_ptr<RtxReceiveStream> rtx_receive_stream_ |
| 290 | RTC_GUARDED_BY(packet_sequence_checker_); |
| 291 | absl::optional<uint32_t> updated_rtx_ssrc_ |
| 292 | RTC_GUARDED_BY(packet_sequence_checker_); |
| 293 | std::unique_ptr<RtpStreamReceiverInterface> rtx_receiver_ |
| 294 | RTC_GUARDED_BY(packet_sequence_checker_); |
| 295 |
| 296 | // Whenever we are in an undecodable state (stream has just started or due to |
| 297 | // a decoding error) we require a keyframe to restart the stream. |
| 298 | bool keyframe_required_ RTC_GUARDED_BY(packet_sequence_checker_) = true; |
| 299 |
| 300 | // If we have successfully decoded any frame. |
| 301 | bool frame_decoded_ RTC_GUARDED_BY(decode_sequence_checker_) = false; |
| 302 |
| 303 | absl::optional<Timestamp> last_keyframe_request_ |
| 304 | RTC_GUARDED_BY(packet_sequence_checker_); |
| 305 |
| 306 | // Keyframe request intervals are configurable through field trials. |
| 307 | TimeDelta max_wait_for_keyframe_ RTC_GUARDED_BY(packet_sequence_checker_); |
| 308 | TimeDelta max_wait_for_frame_ RTC_GUARDED_BY(packet_sequence_checker_); |
| 309 |
| 310 | // All of them tries to change current min_playout_delay on `timing_` but |
| 311 | // source of the change request is different in each case. Among them the |
| 312 | // biggest delay is used. -1 means use default value from the `timing_`. |
| 313 | // |
| 314 | // Minimum delay as decided by the RTP playout delay extension. |
| 315 | absl::optional<TimeDelta> frame_minimum_playout_delay_ |
| 316 | RTC_GUARDED_BY(worker_sequence_checker_); |
| 317 | // Minimum delay as decided by the setLatency function in "webrtc/api". |
| 318 | absl::optional<TimeDelta> base_minimum_playout_delay_ |
| 319 | RTC_GUARDED_BY(worker_sequence_checker_); |
| 320 | // Minimum delay as decided by the A/V synchronization feature. |
| 321 | absl::optional<TimeDelta> syncable_minimum_playout_delay_ |
| 322 | RTC_GUARDED_BY(worker_sequence_checker_); |
| 323 |
| 324 | // Maximum delay as decided by the RTP playout delay extension. |
| 325 | absl::optional<TimeDelta> frame_maximum_playout_delay_ |
| 326 | RTC_GUARDED_BY(worker_sequence_checker_); |
| 327 |
| 328 | // Function that is triggered with encoded frames, if not empty. |
| 329 | std::function<void(const RecordableEncodedFrame&)> |
| 330 | encoded_frame_buffer_function_ RTC_GUARDED_BY(decode_sequence_checker_); |
| 331 | // Set to true while we're requesting keyframes but not yet received one. |
| 332 | bool keyframe_generation_requested_ RTC_GUARDED_BY(packet_sequence_checker_) = |
| 333 | false; |
| 334 | // Lock to avoid unnecessary per-frame idle wakeups in the code. |
| 335 | webrtc::Mutex pending_resolution_mutex_; |
| 336 | // Signal from decode queue to OnFrame callback to fill pending_resolution_. |
| 337 | // absl::nullopt - no resolution needed. 0x0 - next OnFrame to fill with |
| 338 | // received resolution. Not 0x0 - OnFrame has filled a resolution. |
| 339 | absl::optional<RecordableEncodedFrame::EncodedResolution> pending_resolution_ |
| 340 | RTC_GUARDED_BY(pending_resolution_mutex_); |
| 341 | // Buffered encoded frames held while waiting for decoded resolution. |
| 342 | std::vector<std::unique_ptr<EncodedFrame>> buffered_encoded_frames_ |
| 343 | RTC_GUARDED_BY(decode_sequence_checker_); |
| 344 |
| 345 | // Used to signal destruction to potentially pending tasks. |
| 346 | ScopedTaskSafety task_safety_; |
| 347 |
| 348 | // Defined last so they are destroyed before all other members, in particular |
| 349 | // `decode_queue_` should be stopped before `decode_sequence_checker_` is |
| 350 | // destructed to avoid races when running tasks on the `decode_queue_` during |
| 351 | // VideoReceiveStream2 destruction. |
| 352 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> decode_queue_; |
| 353 | }; |
| 354 |
| 355 | } // namespace internal |
| 356 | } // namespace webrtc |
| 357 |
| 358 | #endif // VIDEO_VIDEO_RECEIVE_STREAM2_H_ |
| 359 |