2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
19 #include "webrtc/common_types.h"
20 #include "webrtc/modules/pacing/include/paced_sender.h"
21 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
22 #include "webrtc/modules/rtp_rtcp/source/bitrate.h"
23 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
24 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
25 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
26 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
27 #include "webrtc/modules/rtp_rtcp/source/video_codec_information.h"
28 #include "webrtc/system_wrappers/interface/thread_annotations.h"
30 #define MAX_INIT_RTP_SEQ_NUMBER 32767 // 2^15 -1.
34 class CriticalSectionWrapper;
38 class RTPSenderInterface {
40 RTPSenderInterface() {}
41 virtual ~RTPSenderInterface() {}
43 virtual uint32_t SSRC() const = 0;
44 virtual uint32_t Timestamp() const = 0;
46 virtual int32_t BuildRTPheader(
47 uint8_t *data_buffer, const int8_t payload_type,
48 const bool marker_bit, const uint32_t capture_time_stamp,
49 int64_t capture_time_ms,
50 const bool time_stamp_provided = true,
51 const bool inc_sequence_number = true) = 0;
53 virtual uint16_t RTPHeaderLength() const = 0;
54 virtual uint16_t IncrementSequenceNumber() = 0;
55 virtual uint16_t SequenceNumber() const = 0;
56 virtual uint16_t MaxPayloadLength() const = 0;
57 virtual uint16_t MaxDataPayloadLength() const = 0;
58 virtual uint16_t PacketOverHead() const = 0;
59 virtual uint16_t ActualSendBitrateKbit() const = 0;
61 virtual int32_t SendToNetwork(
62 uint8_t *data_buffer, int payload_length, int rtp_header_length,
63 int64_t capture_time_ms, StorageType storage,
64 PacedSender::Priority priority) = 0;
67 class RTPSender : public RTPSenderInterface, public Bitrate::Observer {
69 RTPSender(const int32_t id, const bool audio, Clock *clock,
70 Transport *transport, RtpAudioFeedback *audio_feedback,
71 PacedSender *paced_sender);
74 void ProcessBitrate();
76 virtual uint16_t ActualSendBitrateKbit() const OVERRIDE;
78 uint32_t VideoBitrateSent() const;
79 uint32_t FecOverheadRate() const;
80 uint32_t NackOverheadRate() const;
82 // Returns true if the statistics have been calculated, and false if no frame
83 // was sent within the statistics window.
84 bool GetSendSideDelay(int* avg_send_delay_ms, int* max_send_delay_ms) const;
86 void SetTargetBitrate(uint32_t bitrate);
87 uint32_t GetTargetBitrate();
89 virtual uint16_t MaxDataPayloadLength() const
90 OVERRIDE; // with RTP and FEC headers.
92 int32_t RegisterPayload(
93 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
94 const int8_t payload_type, const uint32_t frequency,
95 const uint8_t channels, const uint32_t rate);
97 int32_t DeRegisterSendPayload(const int8_t payload_type);
99 int8_t SendPayloadType() const;
101 int SendPayloadFrequency() const;
103 void SetSendingStatus(bool enabled);
105 void SetSendingMediaStatus(const bool enabled);
106 bool SendingMedia() const;
108 // Number of sent RTP packets.
109 uint32_t Packets() const;
111 // Number of sent RTP bytes.
112 uint32_t Bytes() const;
114 void ResetDataCounters();
116 uint32_t StartTimestamp() const;
117 void SetStartTimestamp(uint32_t timestamp, bool force);
119 uint32_t GenerateNewSSRC();
120 void SetSSRC(const uint32_t ssrc);
122 virtual uint16_t SequenceNumber() const OVERRIDE;
123 void SetSequenceNumber(uint16_t seq);
125 int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const;
127 void SetCSRCStatus(const bool include);
129 void SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
130 const uint8_t arr_length);
132 int32_t SetMaxPayloadLength(const uint16_t length,
133 const uint16_t packet_over_head);
135 int32_t SendOutgoingData(
136 const FrameType frame_type, const int8_t payload_type,
137 const uint32_t time_stamp, int64_t capture_time_ms,
138 const uint8_t *payload_data, const uint32_t payload_size,
139 const RTPFragmentationHeader *fragmentation,
140 VideoCodecInformation *codec_info = NULL,
141 const RTPVideoTypeHeader * rtp_type_hdr = NULL);
143 // RTP header extension
144 int32_t SetTransmissionTimeOffset(
145 const int32_t transmission_time_offset);
146 int32_t SetAbsoluteSendTime(
147 const uint32_t absolute_send_time);
149 int32_t RegisterRtpHeaderExtension(const RTPExtensionType type,
152 int32_t DeregisterRtpHeaderExtension(const RTPExtensionType type);
154 uint16_t RtpHeaderExtensionTotalLength() const;
156 uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer) const;
158 uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const;
159 uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const;
160 uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const;
162 bool UpdateAudioLevel(uint8_t *rtp_packet,
163 const uint16_t rtp_packet_length,
164 const RTPHeader &rtp_header,
165 const bool is_voiced,
166 const uint8_t dBov) const;
168 bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms,
169 bool retransmission);
170 int TimeToSendPadding(int bytes);
173 int SelectiveRetransmissions() const;
174 int SetSelectiveRetransmissions(uint8_t settings);
175 void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
176 const uint16_t avg_rtt);
178 void SetStorePacketsStatus(const bool enable,
179 const uint16_t number_to_store);
181 bool StorePackets() const;
183 int32_t ReSendPacket(uint16_t packet_id, uint32_t min_resend_time = 0);
185 bool ProcessNACKBitRate(const uint32_t now);
188 void SetRTXStatus(int mode);
190 void RTXStatus(int* mode, uint32_t* ssrc, int* payload_type) const;
192 void SetRtxSsrc(uint32_t ssrc);
194 void SetRtxPayloadType(int payloadType);
196 // Functions wrapping RTPSenderInterface.
197 virtual int32_t BuildRTPheader(
198 uint8_t *data_buffer, const int8_t payload_type,
199 const bool marker_bit, const uint32_t capture_time_stamp,
200 int64_t capture_time_ms,
201 const bool time_stamp_provided = true,
202 const bool inc_sequence_number = true) OVERRIDE;
204 virtual uint16_t RTPHeaderLength() const OVERRIDE;
205 virtual uint16_t IncrementSequenceNumber() OVERRIDE;
206 virtual uint16_t MaxPayloadLength() const OVERRIDE;
207 virtual uint16_t PacketOverHead() const OVERRIDE;
209 // Current timestamp.
210 virtual uint32_t Timestamp() const OVERRIDE;
211 virtual uint32_t SSRC() const OVERRIDE;
213 virtual int32_t SendToNetwork(
214 uint8_t *data_buffer, int payload_length, int rtp_header_length,
215 int64_t capture_time_ms, StorageType storage,
216 PacedSender::Priority priority) OVERRIDE;
220 // Send a DTMF tone using RFC 2833 (4733).
221 int32_t SendTelephoneEvent(const uint8_t key,
222 const uint16_t time_ms,
223 const uint8_t level);
225 bool SendTelephoneEventActive(int8_t *telephone_event) const;
227 // Set audio packet size, used to determine when it's time to send a DTMF
228 // packet in silence (CNG).
229 int32_t SetAudioPacketSize(const uint16_t packet_size_samples);
231 // Store the audio level in d_bov for
232 // header-extension-for-audio-level-indication.
233 int32_t SetAudioLevel(const uint8_t level_d_bov);
235 // Set payload type for Redundant Audio Data RFC 2198.
236 int32_t SetRED(const int8_t payload_type);
238 // Get payload type for Redundant Audio Data RFC 2198.
239 int32_t RED(int8_t *payload_type) const;
242 VideoCodecInformation *CodecInformationVideo();
244 RtpVideoCodecTypes VideoCodecType() const;
246 uint32_t MaxConfiguredBitrateVideo() const;
248 int32_t SendRTPIntraRequest();
251 int32_t SetGenericFECStatus(const bool enable,
252 const uint8_t payload_type_red,
253 const uint8_t payload_type_fec);
255 int32_t GenericFECStatus(bool *enable, uint8_t *payload_type_red,
256 uint8_t *payload_type_fec) const;
258 int32_t SetFecParameters(const FecProtectionParams *delta_params,
259 const FecProtectionParams *key_params);
261 virtual void RegisterFrameCountObserver(FrameCountObserver* observer);
262 virtual FrameCountObserver* GetFrameCountObserver() const;
264 int SendPadData(int payload_type, uint32_t timestamp, int64_t capture_time_ms,
265 int32_t bytes, StorageType store,
266 bool force_full_size_packets, bool only_pad_after_markerbit);
268 // Called on update of RTP statistics.
269 void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
270 StreamDataCountersCallback* GetRtpStatisticsCallback() const;
272 // Called on new send bitrate estimate.
273 void RegisterBitrateObserver(BitrateStatisticsObserver* observer);
274 BitrateStatisticsObserver* GetBitrateObserver() const;
276 uint32_t BitrateSent() const;
278 virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE;
281 int32_t CheckPayloadType(const int8_t payload_type,
282 RtpVideoCodecTypes *video_type);
285 // Maps capture time in milliseconds to send-side delay in milliseconds.
286 // Send-side delay is the difference between transmission time and capture
288 typedef std::map<int64_t, int> SendDelayMap;
290 int CreateRTPHeader(uint8_t* header, int8_t payload_type,
291 uint32_t ssrc, bool marker_bit,
292 uint32_t timestamp, uint16_t sequence_number,
293 const uint32_t* csrcs, uint8_t csrcs_length) const;
295 void UpdateNACKBitRate(const uint32_t bytes, const uint32_t now);
297 bool PrepareAndSendPacket(uint8_t* buffer,
299 int64_t capture_time_ms,
303 int SendRedundantPayloads(int payload_type, int bytes);
305 bool SendPaddingAccordingToBitrate(int8_t payload_type,
306 uint32_t capture_timestamp,
307 int64_t capture_time_ms);
308 int BuildPaddingPacket(uint8_t* packet, int header_length, int32_t bytes);
310 void BuildRtxPacket(uint8_t* buffer, uint16_t* length,
311 uint8_t* buffer_rtx);
313 bool SendPacketToNetwork(const uint8_t *packet, uint32_t size);
315 void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
317 void UpdateTransmissionTimeOffset(uint8_t *rtp_packet,
318 const uint16_t rtp_packet_length,
319 const RTPHeader &rtp_header,
320 const int64_t time_diff_ms) const;
321 void UpdateAbsoluteSendTime(uint8_t *rtp_packet,
322 const uint16_t rtp_packet_length,
323 const RTPHeader &rtp_header,
324 const int64_t now_ms) const;
326 void UpdateRtpStats(const uint8_t* buffer,
328 const RTPHeader& header,
331 bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
334 Bitrate bitrate_sent_;
337 const bool audio_configured_;
338 RTPSenderAudio *audio_;
339 RTPSenderVideo *video_;
341 PacedSender *paced_sender_;
342 CriticalSectionWrapper *send_critsect_;
344 Transport *transport_;
345 bool sending_media_ GUARDED_BY(send_critsect_);
347 uint16_t max_payload_length_;
348 uint16_t packet_over_head_;
350 int8_t payload_type_ GUARDED_BY(send_critsect_);
351 std::map<int8_t, ModuleRTPUtility::Payload *> payload_type_map_;
353 RtpHeaderExtensionMap rtp_header_extension_map_;
354 int32_t transmission_time_offset_;
355 uint32_t absolute_send_time_;
358 uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
359 int32_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
360 Bitrate nack_bitrate_;
362 RTPPacketHistory packet_history_;
365 scoped_ptr<CriticalSectionWrapper> statistics_crit_;
366 SendDelayMap send_delays_;
367 std::map<FrameType, uint32_t> frame_counts_;
368 FrameCountObserver* frame_count_observer_;
369 StreamDataCounters rtp_stats_;
370 StreamDataCounters rtx_rtp_stats_;
371 StreamDataCountersCallback* rtp_stats_callback_;
372 BitrateStatisticsObserver* bitrate_callback_;
375 bool start_time_stamp_forced_;
376 uint32_t start_time_stamp_;
377 SSRCDatabase &ssrc_db_;
378 uint32_t remote_ssrc_;
379 bool sequence_number_forced_;
380 uint16_t sequence_number_;
381 uint16_t sequence_number_rtx_;
385 int64_t capture_time_ms_;
386 int64_t last_timestamp_time_ms_;
387 bool last_packet_marker_bit_;
389 uint32_t csrcs_[kRtpCsrcSize];
393 int payload_type_rtx_;
395 // Note: Don't access this variable directly, always go through
396 // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember
397 // that by the time the function returns there is no guarantee
398 // that the target bitrate is still valid.
399 scoped_ptr<CriticalSectionWrapper> target_bitrate_critsect_;
400 uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_);
403 } // namespace webrtc
405 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_