| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
| 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 | |
| 11 #ifndef WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_ | |
| 12 #define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_ | |
| 13 | |
| 14 #include <list> | |
| 15 #include <map> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "webrtc/base/platform_thread.h" | |
| 19 #include "webrtc/base/scoped_ptr.h" | |
| 20 #include "webrtc/base/scoped_ref_ptr.h" | |
| 21 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimat
or.h" | |
| 22 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | |
| 23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | |
| 24 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | |
| 25 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
| 26 #include "webrtc/system_wrappers/include/tick_util.h" | |
| 27 #include "webrtc/typedefs.h" | |
| 28 #include "webrtc/video_engine/vie_receiver.h" | |
| 29 #include "webrtc/video_engine/vie_sync_module.h" | |
| 30 | |
| 31 namespace webrtc { | |
| 32 | |
| 33 class CallStatsObserver; | |
| 34 class ChannelStatsObserver; | |
| 35 class Config; | |
| 36 class CriticalSectionWrapper; | |
| 37 class EncodedImageCallback; | |
| 38 class I420FrameCallback; | |
| 39 class IncomingVideoStream; | |
| 40 class PacedSender; | |
| 41 class PacketRouter; | |
| 42 class PayloadRouter; | |
| 43 class ProcessThread; | |
| 44 class ReceiveStatisticsProxy; | |
| 45 class ReportBlockStats; | |
| 46 class RtcpRttStats; | |
| 47 class ViEChannelProtectionCallback; | |
| 48 class ViERTPObserver; | |
| 49 class VideoCodingModule; | |
| 50 class VideoDecoder; | |
| 51 class VideoRenderCallback; | |
| 52 class VoEVideoSync; | |
| 53 | |
| 54 enum StreamType { | |
| 55 kViEStreamTypeNormal = 0, // Normal media stream | |
| 56 kViEStreamTypeRtx = 1 // Retransmission media stream | |
| 57 }; | |
| 58 | |
| 59 class ViEChannel : public VCMFrameTypeCallback, | |
| 60 public VCMReceiveCallback, | |
| 61 public VCMReceiveStatisticsCallback, | |
| 62 public VCMDecoderTimingCallback, | |
| 63 public VCMPacketRequestCallback, | |
| 64 public RtpFeedback { | |
| 65 public: | |
| 66 friend class ChannelStatsObserver; | |
| 67 friend class ViEChannelProtectionCallback; | |
| 68 | |
| 69 ViEChannel(uint32_t number_of_cores, | |
| 70 Transport* transport, | |
| 71 ProcessThread* module_process_thread, | |
| 72 RtcpIntraFrameObserver* intra_frame_observer, | |
| 73 RtcpBandwidthObserver* bandwidth_observer, | |
| 74 TransportFeedbackObserver* transport_feedback_observer, | |
| 75 RemoteBitrateEstimator* remote_bitrate_estimator, | |
| 76 RtcpRttStats* rtt_stats, | |
| 77 PacedSender* paced_sender, | |
| 78 PacketRouter* packet_router, | |
| 79 size_t max_rtp_streams, | |
| 80 bool sender); | |
| 81 ~ViEChannel(); | |
| 82 | |
| 83 int32_t Init(); | |
| 84 | |
| 85 // Sets the encoder to use for the channel. |new_stream| indicates the encoder | |
| 86 // type has changed and we should start a new RTP stream. | |
| 87 int32_t SetSendCodec(const VideoCodec& video_codec, bool new_stream = true); | |
| 88 int32_t SetReceiveCodec(const VideoCodec& video_codec); | |
| 89 // Registers an external decoder. |buffered_rendering| means that the decoder | |
| 90 // will render frames after decoding according to the render timestamp | |
| 91 // provided by the video coding module. |render_delay| indicates the time | |
| 92 // needed to decode and render a frame. | |
| 93 int32_t RegisterExternalDecoder(const uint8_t pl_type, | |
| 94 VideoDecoder* decoder, | |
| 95 bool buffered_rendering, | |
| 96 int32_t render_delay); | |
| 97 int32_t ReceiveCodecStatistics(uint32_t* num_key_frames, | |
| 98 uint32_t* num_delta_frames); | |
| 99 uint32_t DiscardedPackets() const; | |
| 100 | |
| 101 // Returns the estimated delay in milliseconds. | |
| 102 int ReceiveDelay() const; | |
| 103 | |
| 104 void SetRTCPMode(const RtcpMode rtcp_mode); | |
| 105 void SetProtectionMode(bool enable_nack, | |
| 106 bool enable_fec, | |
| 107 int payload_type_red, | |
| 108 int payload_type_fec); | |
| 109 bool IsSendingFecEnabled(); | |
| 110 int SetSenderBufferingMode(int target_delay_ms); | |
| 111 int SetSendTimestampOffsetStatus(bool enable, int id); | |
| 112 int SetReceiveTimestampOffsetStatus(bool enable, int id); | |
| 113 int SetSendAbsoluteSendTimeStatus(bool enable, int id); | |
| 114 int SetReceiveAbsoluteSendTimeStatus(bool enable, int id); | |
| 115 int SetSendVideoRotationStatus(bool enable, int id); | |
| 116 int SetReceiveVideoRotationStatus(bool enable, int id); | |
| 117 int SetSendTransportSequenceNumber(bool enable, int id); | |
| 118 int SetReceiveTransportSequenceNumber(bool enable, int id); | |
| 119 void SetRtcpXrRrtrStatus(bool enable); | |
| 120 void EnableTMMBR(bool enable); | |
| 121 | |
| 122 // Sets SSRC for outgoing stream. | |
| 123 int32_t SetSSRC(const uint32_t SSRC, | |
| 124 const StreamType usage, | |
| 125 const unsigned char simulcast_idx); | |
| 126 | |
| 127 // Gets SSRC for outgoing stream number |idx|. | |
| 128 int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc); | |
| 129 | |
| 130 // Gets SSRC for the incoming stream. | |
| 131 uint32_t GetRemoteSSRC(); | |
| 132 | |
| 133 int SetRtxSendPayloadType(int payload_type, int associated_payload_type); | |
| 134 void SetRtxReceivePayloadType(int payload_type, int associated_payload_type); | |
| 135 // If set to true, the RTX payload type mapping supplied in | |
| 136 // |SetRtxReceivePayloadType| will be used when restoring RTX packets. Without | |
| 137 // it, RTX packets will always be restored to the last non-RTX packet payload | |
| 138 // type received. | |
| 139 void SetUseRtxPayloadMappingOnRestore(bool val); | |
| 140 | |
| 141 void SetRtpStateForSsrc(uint32_t ssrc, const RtpState& rtp_state); | |
| 142 RtpState GetRtpStateForSsrc(uint32_t ssrc); | |
| 143 | |
| 144 // Sets the CName for the outgoing stream on the channel. | |
| 145 int32_t SetRTCPCName(const char* rtcp_cname); | |
| 146 | |
| 147 // Gets the CName of the incoming stream. | |
| 148 int32_t GetRemoteRTCPCName(char rtcp_cname[]); | |
| 149 | |
| 150 // Returns statistics reported by the remote client in an RTCP packet. | |
| 151 // TODO(pbos): Remove this along with VideoSendStream::GetRtt(). | |
| 152 int32_t GetSendRtcpStatistics(uint16_t* fraction_lost, | |
| 153 uint32_t* cumulative_lost, | |
| 154 uint32_t* extended_max, | |
| 155 uint32_t* jitter_samples, | |
| 156 int64_t* rtt_ms); | |
| 157 | |
| 158 // Called on receipt of RTCP report block from remote side. | |
| 159 void RegisterSendChannelRtcpStatisticsCallback( | |
| 160 RtcpStatisticsCallback* callback); | |
| 161 | |
| 162 // Called on generation of RTCP stats | |
| 163 void RegisterReceiveChannelRtcpStatisticsCallback( | |
| 164 RtcpStatisticsCallback* callback); | |
| 165 | |
| 166 // Gets send statistics for the rtp and rtx stream. | |
| 167 void GetSendStreamDataCounters(StreamDataCounters* rtp_counters, | |
| 168 StreamDataCounters* rtx_counters) const; | |
| 169 | |
| 170 // Gets received stream data counters. | |
| 171 void GetReceiveStreamDataCounters(StreamDataCounters* rtp_counters, | |
| 172 StreamDataCounters* rtx_counters) const; | |
| 173 | |
| 174 // Called on update of RTP statistics. | |
| 175 void RegisterSendChannelRtpStatisticsCallback( | |
| 176 StreamDataCountersCallback* callback); | |
| 177 | |
| 178 // Called on update of RTP statistics. | |
| 179 void RegisterReceiveChannelRtpStatisticsCallback( | |
| 180 StreamDataCountersCallback* callback); | |
| 181 | |
| 182 void GetSendRtcpPacketTypeCounter( | |
| 183 RtcpPacketTypeCounter* packet_counter) const; | |
| 184 | |
| 185 void GetReceiveRtcpPacketTypeCounter( | |
| 186 RtcpPacketTypeCounter* packet_counter) const; | |
| 187 | |
| 188 void RegisterSendSideDelayObserver(SendSideDelayObserver* observer); | |
| 189 | |
| 190 // Called on any new send bitrate estimate. | |
| 191 void RegisterSendBitrateObserver(BitrateStatisticsObserver* observer); | |
| 192 | |
| 193 // Implements RtpFeedback. | |
| 194 int32_t OnInitializeDecoder(const int8_t payload_type, | |
| 195 const char payload_name[RTP_PAYLOAD_NAME_SIZE], | |
| 196 const int frequency, | |
| 197 const uint8_t channels, | |
| 198 const uint32_t rate) override; | |
| 199 void OnIncomingSSRCChanged(const uint32_t ssrc) override; | |
| 200 void OnIncomingCSRCChanged(const uint32_t CSRC, const bool added) override; | |
| 201 | |
| 202 int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC); | |
| 203 | |
| 204 int32_t StartSend(); | |
| 205 int32_t StopSend(); | |
| 206 bool Sending(); | |
| 207 void StartReceive(); | |
| 208 void StopReceive(); | |
| 209 | |
| 210 int32_t ReceivedRTPPacket(const void* rtp_packet, | |
| 211 const size_t rtp_packet_length, | |
| 212 const PacketTime& packet_time); | |
| 213 int32_t ReceivedRTCPPacket(const void* rtcp_packet, | |
| 214 const size_t rtcp_packet_length); | |
| 215 | |
| 216 // Sets the maximum transfer unit size for the network link, i.e. including | |
| 217 // IP, UDP and RTP headers. | |
| 218 int32_t SetMTU(uint16_t mtu); | |
| 219 | |
| 220 // Gets the modules used by the channel. | |
| 221 RtpRtcp* rtp_rtcp(); | |
| 222 rtc::scoped_refptr<PayloadRouter> send_payload_router(); | |
| 223 VCMProtectionCallback* vcm_protection_callback(); | |
| 224 | |
| 225 | |
| 226 CallStatsObserver* GetStatsObserver(); | |
| 227 | |
| 228 // Implements VCMReceiveCallback. | |
| 229 virtual int32_t FrameToRender(VideoFrame& video_frame); // NOLINT | |
| 230 | |
| 231 // Implements VCMReceiveCallback. | |
| 232 virtual int32_t ReceivedDecodedReferenceFrame( | |
| 233 const uint64_t picture_id); | |
| 234 | |
| 235 // Implements VCMReceiveCallback. | |
| 236 void OnIncomingPayloadType(int payload_type) override; | |
| 237 | |
| 238 // Implements VCMReceiveStatisticsCallback. | |
| 239 void OnReceiveRatesUpdated(uint32_t bit_rate, uint32_t frame_rate) override; | |
| 240 void OnDiscardedPacketsUpdated(int discarded_packets) override; | |
| 241 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override; | |
| 242 | |
| 243 // Implements VCMDecoderTimingCallback. | |
| 244 virtual void OnDecoderTiming(int decode_ms, | |
| 245 int max_decode_ms, | |
| 246 int current_delay_ms, | |
| 247 int target_delay_ms, | |
| 248 int jitter_buffer_ms, | |
| 249 int min_playout_delay_ms, | |
| 250 int render_delay_ms); | |
| 251 | |
| 252 // Implements FrameTypeCallback. | |
| 253 virtual int32_t RequestKeyFrame(); | |
| 254 | |
| 255 // Implements FrameTypeCallback. | |
| 256 virtual int32_t SliceLossIndicationRequest( | |
| 257 const uint64_t picture_id); | |
| 258 | |
| 259 // Implements VideoPacketRequestCallback. | |
| 260 int32_t ResendPackets(const uint16_t* sequence_numbers, | |
| 261 uint16_t length) override; | |
| 262 | |
| 263 int32_t SetVoiceChannel(int32_t ve_channel_id, | |
| 264 VoEVideoSync* ve_sync_interface); | |
| 265 int32_t VoiceChannel(); | |
| 266 | |
| 267 // New-style callbacks, used by VideoReceiveStream. | |
| 268 void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback); | |
| 269 void RegisterPreDecodeImageCallback( | |
| 270 EncodedImageCallback* pre_decode_callback); | |
| 271 | |
| 272 void RegisterSendFrameCountObserver(FrameCountObserver* observer); | |
| 273 void RegisterRtcpPacketTypeCounterObserver( | |
| 274 RtcpPacketTypeCounterObserver* observer); | |
| 275 void RegisterReceiveStatisticsProxy( | |
| 276 ReceiveStatisticsProxy* receive_statistics_proxy); | |
| 277 void SetIncomingVideoStream(IncomingVideoStream* incoming_video_stream); | |
| 278 | |
| 279 protected: | |
| 280 static bool ChannelDecodeThreadFunction(void* obj); | |
| 281 bool ChannelDecodeProcess(); | |
| 282 | |
| 283 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms); | |
| 284 | |
| 285 int ProtectionRequest(const FecProtectionParams* delta_fec_params, | |
| 286 const FecProtectionParams* key_fec_params, | |
| 287 uint32_t* sent_video_rate_bps, | |
| 288 uint32_t* sent_nack_rate_bps, | |
| 289 uint32_t* sent_fec_rate_bps); | |
| 290 | |
| 291 private: | |
| 292 static std::vector<RtpRtcp*> CreateRtpRtcpModules( | |
| 293 bool receiver_only, | |
| 294 ReceiveStatistics* receive_statistics, | |
| 295 Transport* outgoing_transport, | |
| 296 RtcpIntraFrameObserver* intra_frame_callback, | |
| 297 RtcpBandwidthObserver* bandwidth_callback, | |
| 298 TransportFeedbackObserver* transport_feedback_callback, | |
| 299 RtcpRttStats* rtt_stats, | |
| 300 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer, | |
| 301 RemoteBitrateEstimator* remote_bitrate_estimator, | |
| 302 RtpPacketSender* paced_sender, | |
| 303 TransportSequenceNumberAllocator* transport_sequence_number_allocator, | |
| 304 BitrateStatisticsObserver* send_bitrate_observer, | |
| 305 FrameCountObserver* send_frame_count_observer, | |
| 306 SendSideDelayObserver* send_side_delay_observer, | |
| 307 size_t num_modules); | |
| 308 | |
| 309 // Assumed to be protected. | |
| 310 void StartDecodeThread(); | |
| 311 void StopDecodeThread(); | |
| 312 | |
| 313 void ProcessNACKRequest(const bool enable); | |
| 314 // Compute NACK list parameters for the buffering mode. | |
| 315 int GetRequiredNackListSize(int target_delay_ms); | |
| 316 void SetRtxSendStatus(bool enable); | |
| 317 | |
| 318 void UpdateHistograms(); | |
| 319 | |
| 320 // ViEChannel exposes methods that allow to modify observers and callbacks | |
| 321 // to be modified. Such an API-style is cumbersome to implement and maintain | |
| 322 // at all the levels when comparing to only setting them at construction. As | |
| 323 // so this class instantiates its children with a wrapper that can be modified | |
| 324 // at a later time. | |
| 325 template <class T> | |
| 326 class RegisterableCallback : public T { | |
| 327 public: | |
| 328 RegisterableCallback() | |
| 329 : critsect_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 330 callback_(NULL) {} | |
| 331 | |
| 332 void Set(T* callback) { | |
| 333 CriticalSectionScoped cs(critsect_.get()); | |
| 334 callback_ = callback; | |
| 335 } | |
| 336 | |
| 337 protected: | |
| 338 // Note: this should be implemented with a RW-lock to allow simultaneous | |
| 339 // calls into the callback. However that doesn't seem to be needed for the | |
| 340 // current type of callbacks covered by this class. | |
| 341 rtc::scoped_ptr<CriticalSectionWrapper> critsect_; | |
| 342 T* callback_ GUARDED_BY(critsect_); | |
| 343 | |
| 344 private: | |
| 345 RTC_DISALLOW_COPY_AND_ASSIGN(RegisterableCallback); | |
| 346 }; | |
| 347 | |
| 348 class RegisterableBitrateStatisticsObserver: | |
| 349 public RegisterableCallback<BitrateStatisticsObserver> { | |
| 350 virtual void Notify(const BitrateStatistics& total_stats, | |
| 351 const BitrateStatistics& retransmit_stats, | |
| 352 uint32_t ssrc) { | |
| 353 CriticalSectionScoped cs(critsect_.get()); | |
| 354 if (callback_) | |
| 355 callback_->Notify(total_stats, retransmit_stats, ssrc); | |
| 356 } | |
| 357 } send_bitrate_observer_; | |
| 358 | |
| 359 class RegisterableFrameCountObserver | |
| 360 : public RegisterableCallback<FrameCountObserver> { | |
| 361 public: | |
| 362 virtual void FrameCountUpdated(const FrameCounts& frame_counts, | |
| 363 uint32_t ssrc) { | |
| 364 CriticalSectionScoped cs(critsect_.get()); | |
| 365 if (callback_) | |
| 366 callback_->FrameCountUpdated(frame_counts, ssrc); | |
| 367 } | |
| 368 | |
| 369 private: | |
| 370 } send_frame_count_observer_; | |
| 371 | |
| 372 class RegisterableSendSideDelayObserver : | |
| 373 public RegisterableCallback<SendSideDelayObserver> { | |
| 374 void SendSideDelayUpdated(int avg_delay_ms, | |
| 375 int max_delay_ms, | |
| 376 uint32_t ssrc) override { | |
| 377 CriticalSectionScoped cs(critsect_.get()); | |
| 378 if (callback_) | |
| 379 callback_->SendSideDelayUpdated(avg_delay_ms, max_delay_ms, ssrc); | |
| 380 } | |
| 381 } send_side_delay_observer_; | |
| 382 | |
| 383 class RegisterableRtcpPacketTypeCounterObserver | |
| 384 : public RegisterableCallback<RtcpPacketTypeCounterObserver> { | |
| 385 public: | |
| 386 void RtcpPacketTypesCounterUpdated( | |
| 387 uint32_t ssrc, | |
| 388 const RtcpPacketTypeCounter& packet_counter) override { | |
| 389 CriticalSectionScoped cs(critsect_.get()); | |
| 390 if (callback_) | |
| 391 callback_->RtcpPacketTypesCounterUpdated(ssrc, packet_counter); | |
| 392 counter_map_[ssrc] = packet_counter; | |
| 393 } | |
| 394 | |
| 395 virtual std::map<uint32_t, RtcpPacketTypeCounter> GetPacketTypeCounterMap() | |
| 396 const { | |
| 397 CriticalSectionScoped cs(critsect_.get()); | |
| 398 return counter_map_; | |
| 399 } | |
| 400 | |
| 401 private: | |
| 402 std::map<uint32_t, RtcpPacketTypeCounter> counter_map_ | |
| 403 GUARDED_BY(critsect_); | |
| 404 } rtcp_packet_type_counter_observer_; | |
| 405 | |
| 406 const uint32_t number_of_cores_; | |
| 407 const bool sender_; | |
| 408 | |
| 409 ProcessThread* const module_process_thread_; | |
| 410 | |
| 411 // Used for all registered callbacks except rendering. | |
| 412 rtc::scoped_ptr<CriticalSectionWrapper> crit_; | |
| 413 | |
| 414 // Owned modules/classes. | |
| 415 rtc::scoped_refptr<PayloadRouter> send_payload_router_; | |
| 416 rtc::scoped_ptr<ViEChannelProtectionCallback> vcm_protection_callback_; | |
| 417 | |
| 418 VideoCodingModule* const vcm_; | |
| 419 ViEReceiver vie_receiver_; | |
| 420 ViESyncModule vie_sync_; | |
| 421 | |
| 422 // Helper to report call statistics. | |
| 423 rtc::scoped_ptr<ChannelStatsObserver> stats_observer_; | |
| 424 | |
| 425 // Not owned. | |
| 426 ReceiveStatisticsProxy* receive_stats_callback_ GUARDED_BY(crit_); | |
| 427 FrameCounts receive_frame_counts_ GUARDED_BY(crit_); | |
| 428 IncomingVideoStream* incoming_video_stream_ GUARDED_BY(crit_); | |
| 429 RtcpIntraFrameObserver* const intra_frame_observer_; | |
| 430 RtcpRttStats* const rtt_stats_; | |
| 431 PacedSender* const paced_sender_; | |
| 432 PacketRouter* const packet_router_; | |
| 433 | |
| 434 const rtc::scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_; | |
| 435 TransportFeedbackObserver* const transport_feedback_observer_; | |
| 436 | |
| 437 rtc::PlatformThread decode_thread_; | |
| 438 | |
| 439 int nack_history_size_sender_; | |
| 440 int max_nack_reordering_threshold_; | |
| 441 I420FrameCallback* pre_render_callback_ GUARDED_BY(crit_); | |
| 442 | |
| 443 const rtc::scoped_ptr<ReportBlockStats> report_block_stats_sender_; | |
| 444 | |
| 445 int64_t time_of_first_rtt_ms_ GUARDED_BY(crit_); | |
| 446 int64_t rtt_sum_ms_ GUARDED_BY(crit_); | |
| 447 int64_t last_rtt_ms_ GUARDED_BY(crit_); | |
| 448 size_t num_rtts_ GUARDED_BY(crit_); | |
| 449 | |
| 450 // RtpRtcp modules, declared last as they use other members on construction. | |
| 451 const std::vector<RtpRtcp*> rtp_rtcp_modules_; | |
| 452 size_t num_active_rtp_rtcp_modules_ GUARDED_BY(crit_); | |
| 453 }; | |
| 454 | |
| 455 } // namespace webrtc | |
| 456 | |
| 457 #endif // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_ | |
| OLD | NEW |