Upstream version 9.38.198.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(uint8_t* data_buffer,
47                                  const int8_t payload_type,
48                                  const bool marker_bit,
49                                  const uint32_t capture_timestamp,
50                                  int64_t capture_time_ms,
51                                  const bool timestamp_provided = true,
52                                  const bool inc_sequence_number = true) = 0;
53
54   virtual uint16_t RTPHeaderLength() const = 0;
55   virtual uint16_t IncrementSequenceNumber() = 0;
56   virtual uint16_t SequenceNumber() const = 0;
57   virtual uint16_t MaxPayloadLength() const = 0;
58   virtual uint16_t MaxDataPayloadLength() const = 0;
59   virtual uint16_t PacketOverHead() const = 0;
60   virtual uint16_t ActualSendBitrateKbit() const = 0;
61
62   virtual int32_t SendToNetwork(
63       uint8_t *data_buffer, int payload_length, int rtp_header_length,
64       int64_t capture_time_ms, StorageType storage,
65       PacedSender::Priority priority) = 0;
66 };
67
68 class RTPSender : public RTPSenderInterface, public Bitrate::Observer {
69  public:
70   RTPSender(const int32_t id, const bool audio, Clock *clock,
71             Transport *transport, RtpAudioFeedback *audio_feedback,
72             PacedSender *paced_sender,
73             BitrateStatisticsObserver* bitrate_callback,
74             FrameCountObserver* frame_count_observer,
75             SendSideDelayObserver* send_side_delay_observer);
76   virtual ~RTPSender();
77
78   void ProcessBitrate();
79
80   virtual uint16_t ActualSendBitrateKbit() const OVERRIDE;
81
82   uint32_t VideoBitrateSent() const;
83   uint32_t FecOverheadRate() const;
84   uint32_t NackOverheadRate() const;
85
86   // Returns true if the statistics have been calculated, and false if no frame
87   // was sent within the statistics window.
88   bool GetSendSideDelay(int* avg_send_delay_ms, int* max_send_delay_ms) const;
89
90   void SetTargetBitrate(uint32_t bitrate);
91   uint32_t GetTargetBitrate();
92
93   virtual uint16_t MaxDataPayloadLength() const
94       OVERRIDE;  // with RTP and FEC headers.
95
96   int32_t RegisterPayload(
97       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
98       const int8_t payload_type, const uint32_t frequency,
99       const uint8_t channels, const uint32_t rate);
100
101   int32_t DeRegisterSendPayload(const int8_t payload_type);
102
103   int8_t SendPayloadType() const;
104
105   int SendPayloadFrequency() const;
106
107   void SetSendingStatus(bool enabled);
108
109   void SetSendingMediaStatus(const bool enabled);
110   bool SendingMedia() const;
111
112   void GetDataCounters(StreamDataCounters* rtp_stats,
113                        StreamDataCounters* rtx_stats) const;
114
115   void ResetDataCounters();
116
117   uint32_t StartTimestamp() const;
118   void SetStartTimestamp(uint32_t timestamp, bool force);
119
120   uint32_t GenerateNewSSRC();
121   void SetSSRC(const uint32_t ssrc);
122
123   virtual uint16_t SequenceNumber() const OVERRIDE;
124   void SetSequenceNumber(uint16_t seq);
125
126   int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const;
127
128   void SetCSRCStatus(const bool include);
129
130   void SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
131                 const uint8_t arr_length);
132
133   int32_t SetMaxPayloadLength(const uint16_t length,
134                               const uint16_t packet_over_head);
135
136   int32_t SendOutgoingData(const FrameType frame_type,
137                            const int8_t payload_type,
138                            const uint32_t timestamp,
139                            int64_t capture_time_ms,
140                            const uint8_t* payload_data,
141                            const uint32_t payload_size,
142                            const RTPFragmentationHeader* fragmentation,
143                            VideoCodecInformation* codec_info = NULL,
144                            const RTPVideoTypeHeader* rtp_type_hdr = NULL);
145
146   // RTP header extension
147   int32_t SetTransmissionTimeOffset(
148       const int32_t transmission_time_offset);
149   int32_t SetAbsoluteSendTime(
150       const uint32_t absolute_send_time);
151
152   int32_t RegisterRtpHeaderExtension(const RTPExtensionType type,
153                                      const uint8_t id);
154
155   int32_t DeregisterRtpHeaderExtension(const RTPExtensionType type);
156
157   uint16_t RtpHeaderExtensionTotalLength() const;
158
159   uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer) const;
160
161   uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const;
162   uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const;
163   uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const;
164
165   bool UpdateAudioLevel(uint8_t *rtp_packet,
166                         const uint16_t rtp_packet_length,
167                         const RTPHeader &rtp_header,
168                         const bool is_voiced,
169                         const uint8_t dBov) const;
170
171   bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms,
172                         bool retransmission);
173   int TimeToSendPadding(int bytes);
174
175   // NACK.
176   int SelectiveRetransmissions() const;
177   int SetSelectiveRetransmissions(uint8_t settings);
178   void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
179                       const uint16_t avg_rtt);
180
181   void SetStorePacketsStatus(const bool enable,
182                              const uint16_t number_to_store);
183
184   bool StorePackets() const;
185
186   int32_t ReSendPacket(uint16_t packet_id, uint32_t min_resend_time = 0);
187
188   bool ProcessNACKBitRate(const uint32_t now);
189
190   // RTX.
191   void SetRTXStatus(int mode);
192
193   void RTXStatus(int* mode, uint32_t* ssrc, int* payload_type) const;
194
195   uint32_t RtxSsrc() const;
196   void SetRtxSsrc(uint32_t ssrc);
197
198   void SetRtxPayloadType(int payloadType);
199
200   // Functions wrapping RTPSenderInterface.
201   virtual int32_t BuildRTPheader(
202       uint8_t* data_buffer,
203       const int8_t payload_type,
204       const bool marker_bit,
205       const uint32_t capture_timestamp,
206       int64_t capture_time_ms,
207       const bool timestamp_provided = true,
208       const bool inc_sequence_number = true) OVERRIDE;
209
210   virtual uint16_t RTPHeaderLength() const OVERRIDE;
211   virtual uint16_t IncrementSequenceNumber() OVERRIDE;
212   virtual uint16_t MaxPayloadLength() const OVERRIDE;
213   virtual uint16_t PacketOverHead() const OVERRIDE;
214
215   // Current timestamp.
216   virtual uint32_t Timestamp() const OVERRIDE;
217   virtual uint32_t SSRC() const OVERRIDE;
218
219   virtual int32_t SendToNetwork(
220       uint8_t *data_buffer, int payload_length, int rtp_header_length,
221       int64_t capture_time_ms, StorageType storage,
222       PacedSender::Priority priority) OVERRIDE;
223
224   // Audio.
225
226   // Send a DTMF tone using RFC 2833 (4733).
227   int32_t SendTelephoneEvent(const uint8_t key,
228                              const uint16_t time_ms,
229                              const uint8_t level);
230
231   bool SendTelephoneEventActive(int8_t *telephone_event) const;
232
233   // Set audio packet size, used to determine when it's time to send a DTMF
234   // packet in silence (CNG).
235   int32_t SetAudioPacketSize(const uint16_t packet_size_samples);
236
237   // Store the audio level in d_bov for
238   // header-extension-for-audio-level-indication.
239   int32_t SetAudioLevel(const uint8_t level_d_bov);
240
241   // Set payload type for Redundant Audio Data RFC 2198.
242   int32_t SetRED(const int8_t payload_type);
243
244   // Get payload type for Redundant Audio Data RFC 2198.
245   int32_t RED(int8_t *payload_type) const;
246
247   // Video.
248   VideoCodecInformation *CodecInformationVideo();
249
250   RtpVideoCodecTypes VideoCodecType() const;
251
252   uint32_t MaxConfiguredBitrateVideo() const;
253
254   int32_t SendRTPIntraRequest();
255
256   // FEC.
257   int32_t SetGenericFECStatus(const bool enable,
258                               const uint8_t payload_type_red,
259                               const uint8_t payload_type_fec);
260
261   int32_t GenericFECStatus(bool *enable, uint8_t *payload_type_red,
262                            uint8_t *payload_type_fec) const;
263
264   int32_t SetFecParameters(const FecProtectionParams *delta_params,
265                            const FecProtectionParams *key_params);
266
267   int SendPadData(uint32_t timestamp,
268                   int64_t capture_time_ms,
269                   int32_t bytes);
270
271   // Called on update of RTP statistics.
272   void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
273   StreamDataCountersCallback* GetRtpStatisticsCallback() const;
274
275   uint32_t BitrateSent() const;
276
277   virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE;
278
279   void SetRtpState(const RtpState& rtp_state);
280   RtpState GetRtpState() const;
281   void SetRtxRtpState(const RtpState& rtp_state);
282   RtpState GetRtxRtpState() const;
283
284  protected:
285   int32_t CheckPayloadType(const int8_t payload_type,
286                            RtpVideoCodecTypes *video_type);
287
288  private:
289   // Maps capture time in milliseconds to send-side delay in milliseconds.
290   // Send-side delay is the difference between transmission time and capture
291   // time.
292   typedef std::map<int64_t, int> SendDelayMap;
293
294   int CreateRTPHeader(uint8_t* header, int8_t payload_type,
295                       uint32_t ssrc, bool marker_bit,
296                       uint32_t timestamp, uint16_t sequence_number,
297                       const uint32_t* csrcs, uint8_t csrcs_length) const;
298
299   void UpdateNACKBitRate(const uint32_t bytes, const uint32_t now);
300
301   bool PrepareAndSendPacket(uint8_t* buffer,
302                             uint16_t length,
303                             int64_t capture_time_ms,
304                             bool send_over_rtx,
305                             bool is_retransmit);
306
307   // Return the number of bytes sent.
308   int TrySendRedundantPayloads(int bytes);
309   int TrySendPadData(int bytes);
310
311   int BuildPaddingPacket(uint8_t* packet, int header_length, int32_t bytes);
312
313   void BuildRtxPacket(uint8_t* buffer, uint16_t* length,
314                       uint8_t* buffer_rtx);
315
316   bool SendPacketToNetwork(const uint8_t *packet, uint32_t size);
317
318   void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
319
320   void UpdateTransmissionTimeOffset(uint8_t *rtp_packet,
321                                     const uint16_t rtp_packet_length,
322                                     const RTPHeader &rtp_header,
323                                     const int64_t time_diff_ms) const;
324   void UpdateAbsoluteSendTime(uint8_t *rtp_packet,
325                               const uint16_t rtp_packet_length,
326                               const RTPHeader &rtp_header,
327                               const int64_t now_ms) const;
328
329   void UpdateRtpStats(const uint8_t* buffer,
330                       uint32_t size,
331                       const RTPHeader& header,
332                       bool is_rtx,
333                       bool is_retransmit);
334   bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
335
336   Clock* clock_;
337   Bitrate bitrate_sent_;
338
339   int32_t id_;
340   const bool audio_configured_;
341   RTPSenderAudio *audio_;
342   RTPSenderVideo *video_;
343
344   PacedSender *paced_sender_;
345   CriticalSectionWrapper *send_critsect_;
346
347   Transport *transport_;
348   bool sending_media_ GUARDED_BY(send_critsect_);
349
350   uint16_t max_payload_length_;
351   uint16_t packet_over_head_;
352
353   int8_t payload_type_ GUARDED_BY(send_critsect_);
354   std::map<int8_t, RtpUtility::Payload*> payload_type_map_;
355
356   RtpHeaderExtensionMap rtp_header_extension_map_;
357   int32_t transmission_time_offset_;
358   uint32_t absolute_send_time_;
359
360   // NACK
361   uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
362   int32_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
363   Bitrate nack_bitrate_;
364
365   RTPPacketHistory packet_history_;
366
367   // Statistics
368   scoped_ptr<CriticalSectionWrapper> statistics_crit_;
369   SendDelayMap send_delays_ GUARDED_BY(statistics_crit_);
370   std::map<FrameType, uint32_t> frame_counts_ GUARDED_BY(statistics_crit_);
371   StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_);
372   StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_);
373   StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_);
374   BitrateStatisticsObserver* const bitrate_callback_;
375   FrameCountObserver* const frame_count_observer_;
376   SendSideDelayObserver* const send_side_delay_observer_;
377
378   // RTP variables
379   bool start_timestamp_forced_ GUARDED_BY(send_critsect_);
380   uint32_t start_timestamp_ GUARDED_BY(send_critsect_);
381   SSRCDatabase& ssrc_db_ GUARDED_BY(send_critsect_);
382   uint32_t remote_ssrc_ GUARDED_BY(send_critsect_);
383   bool sequence_number_forced_ GUARDED_BY(send_critsect_);
384   uint16_t sequence_number_ GUARDED_BY(send_critsect_);
385   uint16_t sequence_number_rtx_ GUARDED_BY(send_critsect_);
386   bool ssrc_forced_ GUARDED_BY(send_critsect_);
387   uint32_t ssrc_ GUARDED_BY(send_critsect_);
388   uint32_t timestamp_ GUARDED_BY(send_critsect_);
389   int64_t capture_time_ms_ GUARDED_BY(send_critsect_);
390   int64_t last_timestamp_time_ms_ GUARDED_BY(send_critsect_);
391   bool media_has_been_sent_ GUARDED_BY(send_critsect_);
392   bool last_packet_marker_bit_ GUARDED_BY(send_critsect_);
393   uint8_t num_csrcs_ GUARDED_BY(send_critsect_);
394   uint32_t csrcs_[kRtpCsrcSize] GUARDED_BY(send_critsect_);
395   bool include_csrcs_ GUARDED_BY(send_critsect_);
396   int rtx_ GUARDED_BY(send_critsect_);
397   uint32_t ssrc_rtx_ GUARDED_BY(send_critsect_);
398   int payload_type_rtx_ GUARDED_BY(send_critsect_);
399
400   // Note: Don't access this variable directly, always go through
401   // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember
402   // that by the time the function returns there is no guarantee
403   // that the target bitrate is still valid.
404   scoped_ptr<CriticalSectionWrapper> target_bitrate_critsect_;
405   uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_);
406 };
407
408 }  // namespace webrtc
409
410 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_