Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtp_sender.h
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_SENDER_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
13
14 #include <assert.h>
15 #include <math.h>
16
17 #include <map>
18
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"
29
30 #define MAX_INIT_RTP_SEQ_NUMBER 32767  // 2^15 -1.
31
32 namespace webrtc {
33
34 class CriticalSectionWrapper;
35 class RTPSenderAudio;
36 class RTPSenderVideo;
37
38 class RTPSenderInterface {
39  public:
40   RTPSenderInterface() {}
41   virtual ~RTPSenderInterface() {}
42
43   virtual uint32_t SSRC() const = 0;
44   virtual uint32_t Timestamp() const = 0;
45
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;
52
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;
60
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;
65 };
66
67 class RTPSender : public RTPSenderInterface, public Bitrate::Observer {
68  public:
69   RTPSender(const int32_t id, const bool audio, Clock *clock,
70             Transport *transport, RtpAudioFeedback *audio_feedback,
71             PacedSender *paced_sender);
72   virtual ~RTPSender();
73
74   void ProcessBitrate();
75
76   virtual uint16_t ActualSendBitrateKbit() const OVERRIDE;
77
78   uint32_t VideoBitrateSent() const;
79   uint32_t FecOverheadRate() const;
80   uint32_t NackOverheadRate() const;
81
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;
85
86   void SetTargetBitrate(uint32_t bitrate);
87   uint32_t GetTargetBitrate();
88
89   virtual uint16_t MaxDataPayloadLength() const
90       OVERRIDE;  // with RTP and FEC headers.
91
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);
96
97   int32_t DeRegisterSendPayload(const int8_t payload_type);
98
99   int8_t SendPayloadType() const;
100
101   int SendPayloadFrequency() const;
102
103   void SetSendingStatus(bool enabled);
104
105   void SetSendingMediaStatus(const bool enabled);
106   bool SendingMedia() const;
107
108   // Number of sent RTP packets.
109   uint32_t Packets() const;
110
111   // Number of sent RTP bytes.
112   uint32_t Bytes() const;
113
114   void ResetDataCounters();
115
116   uint32_t StartTimestamp() const;
117   void SetStartTimestamp(uint32_t timestamp, bool force);
118
119   uint32_t GenerateNewSSRC();
120   void SetSSRC(const uint32_t ssrc);
121
122   virtual uint16_t SequenceNumber() const OVERRIDE;
123   void SetSequenceNumber(uint16_t seq);
124
125   int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const;
126
127   void SetCSRCStatus(const bool include);
128
129   void SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
130                 const uint8_t arr_length);
131
132   int32_t SetMaxPayloadLength(const uint16_t length,
133                               const uint16_t packet_over_head);
134
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);
142
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);
148
149   int32_t RegisterRtpHeaderExtension(const RTPExtensionType type,
150                                      const uint8_t id);
151
152   int32_t DeregisterRtpHeaderExtension(const RTPExtensionType type);
153
154   uint16_t RtpHeaderExtensionTotalLength() const;
155
156   uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer) const;
157
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;
161
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;
167
168   bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms,
169                         bool retransmission);
170   int TimeToSendPadding(int bytes);
171
172   // NACK.
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);
177
178   void SetStorePacketsStatus(const bool enable,
179                              const uint16_t number_to_store);
180
181   bool StorePackets() const;
182
183   int32_t ReSendPacket(uint16_t packet_id, uint32_t min_resend_time = 0);
184
185   bool ProcessNACKBitRate(const uint32_t now);
186
187   // RTX.
188   void SetRTXStatus(int mode);
189
190   void RTXStatus(int* mode, uint32_t* ssrc, int* payload_type) const;
191
192   void SetRtxSsrc(uint32_t ssrc);
193
194   void SetRtxPayloadType(int payloadType);
195
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;
203
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;
208
209   // Current timestamp.
210   virtual uint32_t Timestamp() const OVERRIDE;
211   virtual uint32_t SSRC() const OVERRIDE;
212
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;
217
218   // Audio.
219
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);
224
225   bool SendTelephoneEventActive(int8_t *telephone_event) const;
226
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);
230
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);
234
235   // Set payload type for Redundant Audio Data RFC 2198.
236   int32_t SetRED(const int8_t payload_type);
237
238   // Get payload type for Redundant Audio Data RFC 2198.
239   int32_t RED(int8_t *payload_type) const;
240
241   // Video.
242   VideoCodecInformation *CodecInformationVideo();
243
244   RtpVideoCodecTypes VideoCodecType() const;
245
246   uint32_t MaxConfiguredBitrateVideo() const;
247
248   int32_t SendRTPIntraRequest();
249
250   // FEC.
251   int32_t SetGenericFECStatus(const bool enable,
252                               const uint8_t payload_type_red,
253                               const uint8_t payload_type_fec);
254
255   int32_t GenericFECStatus(bool *enable, uint8_t *payload_type_red,
256                            uint8_t *payload_type_fec) const;
257
258   int32_t SetFecParameters(const FecProtectionParams *delta_params,
259                            const FecProtectionParams *key_params);
260
261   virtual void RegisterFrameCountObserver(FrameCountObserver* observer);
262   virtual FrameCountObserver* GetFrameCountObserver() const;
263
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);
267
268   // Called on update of RTP statistics.
269   void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
270   StreamDataCountersCallback* GetRtpStatisticsCallback() const;
271
272   // Called on new send bitrate estimate.
273   void RegisterBitrateObserver(BitrateStatisticsObserver* observer);
274   BitrateStatisticsObserver* GetBitrateObserver() const;
275
276   uint32_t BitrateSent() const;
277
278   virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE;
279
280  protected:
281   int32_t CheckPayloadType(const int8_t payload_type,
282                            RtpVideoCodecTypes *video_type);
283
284  private:
285   // Maps capture time in milliseconds to send-side delay in milliseconds.
286   // Send-side delay is the difference between transmission time and capture
287   // time.
288   typedef std::map<int64_t, int> SendDelayMap;
289
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;
294
295   void UpdateNACKBitRate(const uint32_t bytes, const uint32_t now);
296
297   bool PrepareAndSendPacket(uint8_t* buffer,
298                             uint16_t length,
299                             int64_t capture_time_ms,
300                             bool send_over_rtx,
301                             bool is_retransmit);
302
303   int SendRedundantPayloads(int payload_type, int bytes);
304
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);
309
310   void BuildRtxPacket(uint8_t* buffer, uint16_t* length,
311                       uint8_t* buffer_rtx);
312
313   bool SendPacketToNetwork(const uint8_t *packet, uint32_t size);
314
315   void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
316
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;
325
326   void UpdateRtpStats(const uint8_t* buffer,
327                       uint32_t size,
328                       const RTPHeader& header,
329                       bool is_rtx,
330                       bool is_retransmit);
331   bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
332
333   Clock* clock_;
334   Bitrate bitrate_sent_;
335
336   int32_t id_;
337   const bool audio_configured_;
338   RTPSenderAudio *audio_;
339   RTPSenderVideo *video_;
340
341   PacedSender *paced_sender_;
342   CriticalSectionWrapper *send_critsect_;
343
344   Transport *transport_;
345   bool sending_media_ GUARDED_BY(send_critsect_);
346
347   uint16_t max_payload_length_;
348   uint16_t packet_over_head_;
349
350   int8_t payload_type_ GUARDED_BY(send_critsect_);
351   std::map<int8_t, ModuleRTPUtility::Payload *> payload_type_map_;
352
353   RtpHeaderExtensionMap rtp_header_extension_map_;
354   int32_t transmission_time_offset_;
355   uint32_t absolute_send_time_;
356
357   // NACK
358   uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
359   int32_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
360   Bitrate nack_bitrate_;
361
362   RTPPacketHistory packet_history_;
363
364   // Statistics
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_;
373
374   // RTP variables
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_;
382   bool ssrc_forced_;
383   uint32_t ssrc_;
384   uint32_t timestamp_;
385   int64_t capture_time_ms_;
386   int64_t last_timestamp_time_ms_;
387   bool last_packet_marker_bit_;
388   uint8_t num_csrcs_;
389   uint32_t csrcs_[kRtpCsrcSize];
390   bool include_csrcs_;
391   int rtx_;
392   uint32_t ssrc_rtx_;
393   int payload_type_rtx_;
394
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_);
401 };
402
403 }  // namespace webrtc
404
405 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_