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_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_ 12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_ 13 14 #include <list> 15 #include <vector> 16 17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" 18 #include "webrtc/modules/rtp_rtcp/source/rtcp_receiver.h" 19 #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h" 20 #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h" 21 #include "webrtc/system_wrappers/interface/scoped_ptr.h" 22 #include "webrtc/test/testsupport/gtest_prod_util.h" 23 24 namespace webrtc { 25 26 class ModuleRtpRtcpImpl : public RtpRtcp { 27 public: 28 explicit ModuleRtpRtcpImpl(const RtpRtcp::Configuration& configuration); 29 30 virtual ~ModuleRtpRtcpImpl(); 31 32 // Returns the number of milliseconds until the module want a worker thread to 33 // call Process. 34 virtual int32_t TimeUntilNextProcess() OVERRIDE; 35 36 // Process any pending tasks such as timeouts. 37 virtual int32_t Process() OVERRIDE; 38 39 // Receiver part. 40 41 // Called when we receive an RTCP packet. 42 virtual int32_t IncomingRtcpPacket(const uint8_t* incoming_packet, 43 uint16_t incoming_packet_length) OVERRIDE; 44 45 virtual void SetRemoteSSRC(const uint32_t ssrc) OVERRIDE; 46 47 // Sender part. 48 49 virtual int32_t RegisterSendPayload(const CodecInst& voice_codec) OVERRIDE; 50 51 virtual int32_t RegisterSendPayload(const VideoCodec& video_codec) OVERRIDE; 52 53 virtual int32_t DeRegisterSendPayload(const int8_t payload_type) OVERRIDE; 54 55 int8_t SendPayloadType() const; 56 57 // Register RTP header extension. 58 virtual int32_t RegisterSendRtpHeaderExtension( 59 const RTPExtensionType type, 60 const uint8_t id) OVERRIDE; 61 62 virtual int32_t DeregisterSendRtpHeaderExtension( 63 const RTPExtensionType type) OVERRIDE; 64 65 // Get start timestamp. 66 virtual uint32_t StartTimestamp() const OVERRIDE; 67 68 // Configure start timestamp, default is a random number. 69 virtual int32_t SetStartTimestamp(const uint32_t timestamp) OVERRIDE; 70 71 virtual uint16_t SequenceNumber() const OVERRIDE; 72 73 // Set SequenceNumber, default is a random number. 74 virtual int32_t SetSequenceNumber(const uint16_t seq) OVERRIDE; 75 76 virtual void SetRtpStateForSsrc(uint32_t ssrc, 77 const RtpState& rtp_state) OVERRIDE; 78 virtual bool GetRtpStateForSsrc(uint32_t ssrc, RtpState* rtp_state) OVERRIDE; 79 80 virtual uint32_t SSRC() const OVERRIDE; 81 82 // Configure SSRC, default is a random number. 83 virtual void SetSSRC(const uint32_t ssrc) OVERRIDE; 84 85 virtual int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const OVERRIDE; 86 87 virtual int32_t SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize], 88 const uint8_t arr_length) OVERRIDE; 89 90 virtual int32_t SetCSRCStatus(const bool include) OVERRIDE; 91 92 RTCPSender::FeedbackState GetFeedbackState(); 93 94 int CurrentSendFrequencyHz() const; 95 96 virtual void SetRTXSendStatus(const int mode) OVERRIDE; 97 98 virtual void RTXSendStatus(int* mode, uint32_t* ssrc, 99 int* payloadType) const OVERRIDE; 100 101 virtual void SetRtxSsrc(uint32_t ssrc) OVERRIDE; 102 103 virtual void SetRtxSendPayloadType(int payload_type) OVERRIDE; 104 105 // Sends kRtcpByeCode when going from true to false. 106 virtual int32_t SetSendingStatus(const bool sending) OVERRIDE; 107 108 virtual bool Sending() const OVERRIDE; 109 110 // Drops or relays media packets. 111 virtual int32_t SetSendingMediaStatus(const bool sending) OVERRIDE; 112 113 virtual bool SendingMedia() const OVERRIDE; 114 115 // Used by the codec module to deliver a video or audio frame for 116 // packetization. 117 virtual int32_t SendOutgoingData( 118 const FrameType frame_type, 119 const int8_t payload_type, 120 const uint32_t time_stamp, 121 int64_t capture_time_ms, 122 const uint8_t* payload_data, 123 const uint32_t payload_size, 124 const RTPFragmentationHeader* fragmentation = NULL, 125 const RTPVideoHeader* rtp_video_hdr = NULL) OVERRIDE; 126 127 virtual bool TimeToSendPacket(uint32_t ssrc, 128 uint16_t sequence_number, 129 int64_t capture_time_ms, 130 bool retransmission) OVERRIDE; 131 // Returns the number of padding bytes actually sent, which can be more or 132 // less than |bytes|. 133 virtual int TimeToSendPadding(int bytes) OVERRIDE; 134 135 virtual bool GetSendSideDelay(int* avg_send_delay_ms, 136 int* max_send_delay_ms) const OVERRIDE; 137 138 // RTCP part. 139 140 // Get RTCP status. 141 virtual RTCPMethod RTCP() const OVERRIDE; 142 143 // Configure RTCP status i.e on/off. 144 virtual int32_t SetRTCPStatus(const RTCPMethod method) OVERRIDE; 145 146 // Set RTCP CName. 147 virtual int32_t SetCNAME(const char c_name[RTCP_CNAME_SIZE]) OVERRIDE; 148 149 // Get remote CName. 150 virtual int32_t RemoteCNAME(const uint32_t remote_ssrc, 151 char c_name[RTCP_CNAME_SIZE]) const OVERRIDE; 152 153 // Get remote NTP. 154 virtual int32_t RemoteNTP(uint32_t* received_ntp_secs, 155 uint32_t* received_ntp_frac, 156 uint32_t* rtcp_arrival_time_secs, 157 uint32_t* rtcp_arrival_time_frac, 158 uint32_t* rtcp_timestamp) const OVERRIDE; 159 160 virtual int32_t AddMixedCNAME(const uint32_t ssrc, 161 const char c_name[RTCP_CNAME_SIZE]) OVERRIDE; 162 163 virtual int32_t RemoveMixedCNAME(const uint32_t ssrc) OVERRIDE; 164 165 // Get RoundTripTime. 166 virtual int32_t RTT(const uint32_t remote_ssrc, 167 uint16_t* rtt, 168 uint16_t* avg_rtt, 169 uint16_t* min_rtt, 170 uint16_t* max_rtt) const OVERRIDE; 171 172 // Reset RoundTripTime statistics. 173 virtual int32_t ResetRTT(const uint32_t remote_ssrc) OVERRIDE; 174 175 // Force a send of an RTCP packet. 176 // Normal SR and RR are triggered via the process function. 177 virtual int32_t SendRTCP(uint32_t rtcp_packet_type = kRtcpReport) OVERRIDE; 178 179 virtual int32_t ResetSendDataCountersRTP() OVERRIDE; 180 181 // Statistics of the amount of data sent and received. 182 virtual int32_t DataCountersRTP(uint32_t* bytes_sent, 183 uint32_t* packets_sent) const OVERRIDE; 184 185 // Get received RTCP report, sender info. 186 virtual int32_t RemoteRTCPStat(RTCPSenderInfo* sender_info) OVERRIDE; 187 188 // Get received RTCP report, report block. 189 virtual int32_t RemoteRTCPStat( 190 std::vector<RTCPReportBlock>* receive_blocks) const OVERRIDE; 191 192 // Set received RTCP report block. 193 virtual int32_t AddRTCPReportBlock( 194 const uint32_t ssrc, const RTCPReportBlock* receive_block) OVERRIDE; 195 196 virtual int32_t RemoveRTCPReportBlock(const uint32_t ssrc) OVERRIDE; 197 198 virtual void GetRtcpPacketTypeCounters( 199 RtcpPacketTypeCounter* packets_sent, 200 RtcpPacketTypeCounter* packets_received) const OVERRIDE; 201 202 // (REMB) Receiver Estimated Max Bitrate. 203 virtual bool REMB() const OVERRIDE; 204 205 virtual int32_t SetREMBStatus(const bool enable) OVERRIDE; 206 207 virtual int32_t SetREMBData(const uint32_t bitrate, 208 const uint8_t number_of_ssrc, 209 const uint32_t* ssrc) OVERRIDE; 210 211 // (IJ) Extended jitter report. 212 virtual bool IJ() const OVERRIDE; 213 214 virtual int32_t SetIJStatus(const bool enable) OVERRIDE; 215 216 // (TMMBR) Temporary Max Media Bit Rate. 217 virtual bool TMMBR() const OVERRIDE; 218 219 virtual int32_t SetTMMBRStatus(const bool enable) OVERRIDE; 220 221 int32_t SetTMMBN(const TMMBRSet* bounding_set); 222 223 virtual uint16_t MaxPayloadLength() const OVERRIDE; 224 225 virtual uint16_t MaxDataPayloadLength() const OVERRIDE; 226 227 virtual int32_t SetMaxTransferUnit(const uint16_t size) OVERRIDE; 228 229 virtual int32_t SetTransportOverhead( 230 const bool tcp, 231 const bool ipv6, 232 const uint8_t authentication_overhead = 0) OVERRIDE; 233 234 // (NACK) Negative acknowledgment part. 235 236 virtual int SelectiveRetransmissions() const OVERRIDE; 237 238 virtual int SetSelectiveRetransmissions(uint8_t settings) OVERRIDE; 239 240 // Send a Negative acknowledgment packet. 241 virtual int32_t SendNACK(const uint16_t* nack_list, 242 const uint16_t size) OVERRIDE; 243 244 // Store the sent packets, needed to answer to a negative acknowledgment 245 // requests. 246 virtual int32_t SetStorePacketsStatus( 247 const bool enable, const uint16_t number_to_store) OVERRIDE; 248 249 virtual bool StorePackets() const OVERRIDE; 250 251 // Called on receipt of RTCP report block from remote side. 252 virtual void RegisterSendChannelRtcpStatisticsCallback( 253 RtcpStatisticsCallback* callback) OVERRIDE; 254 virtual RtcpStatisticsCallback* 255 GetSendChannelRtcpStatisticsCallback() OVERRIDE; 256 257 // (APP) Application specific data. 258 virtual int32_t SetRTCPApplicationSpecificData( 259 const uint8_t sub_type, 260 const uint32_t name, 261 const uint8_t* data, 262 const uint16_t length) OVERRIDE; 263 264 // (XR) VOIP metric. 265 virtual int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) OVERRIDE; 266 267 // (XR) Receiver reference time report. 268 virtual void SetRtcpXrRrtrStatus(bool enable) OVERRIDE; 269 270 virtual bool RtcpXrRrtrStatus() const OVERRIDE; 271 272 // Audio part. 273 274 // Set audio packet size, used to determine when it's time to send a DTMF 275 // packet in silence (CNG). 276 virtual int32_t SetAudioPacketSize( 277 const uint16_t packet_size_samples) OVERRIDE; 278 279 virtual bool SendTelephoneEventActive(int8_t& telephone_event) const OVERRIDE; 280 281 // Send a TelephoneEvent tone using RFC 2833 (4733). 282 virtual int32_t SendTelephoneEventOutband(const uint8_t key, 283 const uint16_t time_ms, 284 const uint8_t level) OVERRIDE; 285 286 // Set payload type for Redundant Audio Data RFC 2198. 287 virtual int32_t SetSendREDPayloadType(const int8_t payload_type) OVERRIDE; 288 289 // Get payload type for Redundant Audio Data RFC 2198. 290 virtual int32_t SendREDPayloadType(int8_t& payload_type) const OVERRIDE; 291 292 // Store the audio level in d_bov for header-extension-for-audio-level- 293 // indication. 294 virtual int32_t SetAudioLevel(const uint8_t level_d_bov) OVERRIDE; 295 296 // Video part. 297 298 virtual int32_t SendRTCPSliceLossIndication( 299 const uint8_t picture_id) OVERRIDE; 300 301 // Set method for requestion a new key frame. 302 virtual int32_t SetKeyFrameRequestMethod( 303 const KeyFrameRequestMethod method) OVERRIDE; 304 305 // Send a request for a keyframe. 306 virtual int32_t RequestKeyFrame() OVERRIDE; 307 308 virtual int32_t SetCameraDelay(const int32_t delay_ms) OVERRIDE; 309 310 virtual void SetTargetSendBitrate( 311 const std::vector<uint32_t>& stream_bitrates) OVERRIDE; 312 313 virtual int32_t SetGenericFECStatus( 314 const bool enable, 315 const uint8_t payload_type_red, 316 const uint8_t payload_type_fec) OVERRIDE; 317 318 virtual int32_t GenericFECStatus( 319 bool& enable, 320 uint8_t& payload_type_red, 321 uint8_t& payload_type_fec) OVERRIDE; 322 323 virtual int32_t SetFecParameters( 324 const FecProtectionParams* delta_params, 325 const FecProtectionParams* key_params) OVERRIDE; 326 327 bool LastReceivedNTP(uint32_t* NTPsecs, 328 uint32_t* NTPfrac, 329 uint32_t* remote_sr) const; 330 331 bool LastReceivedXrReferenceTimeInfo(RtcpReceiveTimeInfo* info) const; 332 333 virtual int32_t BoundingSet(bool& tmmbr_owner, TMMBRSet*& bounding_set_rec); 334 335 virtual void BitrateSent(uint32_t* total_rate, 336 uint32_t* video_rate, 337 uint32_t* fec_rate, 338 uint32_t* nackRate) const OVERRIDE; 339 340 uint32_t SendTimeOfSendReport(const uint32_t send_report); 341 342 bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const; 343 344 // Good state of RTP receiver inform sender. 345 virtual int32_t SendRTCPReferencePictureSelection( 346 const uint64_t picture_id) OVERRIDE; 347 348 virtual void RegisterSendChannelRtpStatisticsCallback( 349 StreamDataCountersCallback* callback) OVERRIDE; 350 virtual StreamDataCountersCallback* 351 GetSendChannelRtpStatisticsCallback() const OVERRIDE; 352 353 void OnReceivedTMMBR(); 354 355 // Bad state of RTP receiver request a keyframe. 356 void OnRequestIntraFrame(); 357 358 // Received a request for a new SLI. 359 void OnReceivedSliceLossIndication(const uint8_t picture_id); 360 361 // Received a new reference frame. 362 void OnReceivedReferencePictureSelectionIndication( 363 const uint64_t picture_id); 364 365 void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers); 366 367 void OnRequestSendReport(); 368 369 protected: 370 void RegisterChildModule(RtpRtcp* module); 371 372 void DeRegisterChildModule(RtpRtcp* module); 373 374 bool UpdateRTCPReceiveInformationTimers(); 375 376 uint32_t BitrateReceivedNow() const; 377 378 // Get remote SequenceNumber. 379 uint16_t RemoteSequenceNumber() const; 380 381 // Only for internal testing. 382 uint32_t LastSendReport(uint32_t& last_rtcptime); 383 384 RTPSender rtp_sender_; 385 386 RTCPSender rtcp_sender_; 387 RTCPReceiver rtcp_receiver_; 388 389 Clock* clock_; 390 391 private: 392 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImplTest, Rtt); 393 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImplTest, RttForReceiverOnly); 394 int64_t RtcpReportInterval(); 395 void SetRtcpReceiverSsrcs(uint32_t main_ssrc); 396 397 void set_rtt_ms(uint32_t rtt_ms); 398 uint32_t rtt_ms() const; 399 400 bool IsDefaultModule() const; 401 402 int32_t id_; 403 const bool audio_; 404 bool collision_detected_; 405 int64_t last_process_time_; 406 int64_t last_bitrate_process_time_; 407 int64_t last_rtt_process_time_; 408 uint16_t packet_overhead_; 409 410 scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_; 411 scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_feedback_; 412 ModuleRtpRtcpImpl* default_module_; 413 std::vector<ModuleRtpRtcpImpl*> child_modules_; 414 size_t padding_index_; 415 416 // Send side 417 NACKMethod nack_method_; 418 uint32_t nack_last_time_sent_full_; 419 uint16_t nack_last_seq_number_sent_; 420 421 bool simulcast_; 422 VideoCodec send_video_codec_; 423 KeyFrameRequestMethod key_frame_req_method_; 424 425 RemoteBitrateEstimator* remote_bitrate_; 426 427 RtcpRttStats* rtt_stats_; 428 429 // The processed RTT from RtcpRttStats. 430 scoped_ptr<CriticalSectionWrapper> critical_section_rtt_; 431 uint32_t rtt_ms_; 432 }; 433 434 } // namespace webrtc 435 436 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_ 437