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 <list>
15 #include <map>
16 #include <utility>
17 #include <vector>
18 
19 #include "webrtc/base/random.h"
20 #include "webrtc/base/thread_annotations.h"
21 #include "webrtc/common_types.h"
22 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
23 #include "webrtc/modules/rtp_rtcp/source/bitrate.h"
24 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
25 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
26 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
27 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
28 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
29 #include "webrtc/transport.h"
30 
31 namespace webrtc {
32 
33 class BitrateAggregator;
34 class CriticalSectionWrapper;
35 class RTPSenderAudio;
36 class RTPSenderVideo;
37 
38 class RTPSenderInterface {
39  public:
RTPSenderInterface()40   RTPSenderInterface() {}
~RTPSenderInterface()41   virtual ~RTPSenderInterface() {}
42 
43   enum CVOMode {
44     kCVONone,
45     kCVOInactive,  // CVO rtp header extension is registered but haven't
46                    // received any frame with rotation pending.
47     kCVOActivated,  // CVO rtp header extension will be present in the rtp
48                     // packets.
49   };
50 
51   virtual uint32_t SSRC() const = 0;
52   virtual uint32_t Timestamp() const = 0;
53 
54   virtual int32_t BuildRTPheader(uint8_t* data_buffer,
55                                  int8_t payload_type,
56                                  bool marker_bit,
57                                  uint32_t capture_timestamp,
58                                  int64_t capture_time_ms,
59                                  bool timestamp_provided = true,
60                                  bool inc_sequence_number = true) = 0;
61 
62   virtual size_t RTPHeaderLength() const = 0;
63   // Returns the next sequence number to use for a packet and allocates
64   // 'packets_to_send' number of sequence numbers. It's important all allocated
65   // sequence numbers are used in sequence to avoid perceived packet loss.
66   virtual uint16_t AllocateSequenceNumber(uint16_t packets_to_send) = 0;
67   virtual uint16_t SequenceNumber() const = 0;
68   virtual size_t MaxPayloadLength() const = 0;
69   virtual size_t MaxDataPayloadLength() const = 0;
70   virtual uint16_t PacketOverHead() const = 0;
71   virtual uint16_t ActualSendBitrateKbit() const = 0;
72 
73   virtual int32_t SendToNetwork(uint8_t* data_buffer,
74                                 size_t payload_length,
75                                 size_t rtp_header_length,
76                                 int64_t capture_time_ms,
77                                 StorageType storage,
78                                 RtpPacketSender::Priority priority) = 0;
79 
80   virtual bool UpdateVideoRotation(uint8_t* rtp_packet,
81                                    size_t rtp_packet_length,
82                                    const RTPHeader& rtp_header,
83                                    VideoRotation rotation) const = 0;
84   virtual bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) = 0;
85   virtual CVOMode ActivateCVORtpHeaderExtension() = 0;
86 };
87 
88 class RTPSender : public RTPSenderInterface {
89  public:
90   RTPSender(bool audio,
91             Clock* clock,
92             Transport* transport,
93             RtpAudioFeedback* audio_feedback,
94             RtpPacketSender* paced_sender,
95             TransportSequenceNumberAllocator* sequence_number_allocator,
96             TransportFeedbackObserver* transport_feedback_callback,
97             BitrateStatisticsObserver* bitrate_callback,
98             FrameCountObserver* frame_count_observer,
99             SendSideDelayObserver* send_side_delay_observer);
100   virtual ~RTPSender();
101 
102   void ProcessBitrate();
103 
104   uint16_t ActualSendBitrateKbit() const override;
105 
106   uint32_t VideoBitrateSent() const;
107   uint32_t FecOverheadRate() const;
108   uint32_t NackOverheadRate() const;
109 
110   void SetTargetBitrate(uint32_t bitrate);
111   uint32_t GetTargetBitrate();
112 
113   // Includes size of RTP and FEC headers.
114   size_t MaxDataPayloadLength() const override;
115 
116   int32_t RegisterPayload(
117       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
118       const int8_t payload_type, const uint32_t frequency,
119       const size_t channels, const uint32_t rate);
120 
121   int32_t DeRegisterSendPayload(const int8_t payload_type);
122 
123   void SetSendPayloadType(int8_t payload_type);
124 
125   int8_t SendPayloadType() const;
126 
127   int SendPayloadFrequency() const;
128 
129   void SetSendingStatus(bool enabled);
130 
131   void SetSendingMediaStatus(bool enabled);
132   bool SendingMedia() const;
133 
134   void GetDataCounters(StreamDataCounters* rtp_stats,
135                        StreamDataCounters* rtx_stats) const;
136 
137   uint32_t StartTimestamp() const;
138   void SetStartTimestamp(uint32_t timestamp, bool force);
139 
140   uint32_t GenerateNewSSRC();
141   void SetSSRC(uint32_t ssrc);
142 
143   uint16_t SequenceNumber() const override;
144   void SetSequenceNumber(uint16_t seq);
145 
146   void SetCsrcs(const std::vector<uint32_t>& csrcs);
147 
148   int32_t SetMaxPayloadLength(size_t length, uint16_t packet_over_head);
149 
150   int32_t SendOutgoingData(FrameType frame_type,
151                            int8_t payload_type,
152                            uint32_t timestamp,
153                            int64_t capture_time_ms,
154                            const uint8_t* payload_data,
155                            size_t payload_size,
156                            const RTPFragmentationHeader* fragmentation,
157                            const RTPVideoHeader* rtp_hdr = NULL);
158 
159   // RTP header extension
160   int32_t SetTransmissionTimeOffset(int32_t transmission_time_offset);
161   int32_t SetAbsoluteSendTime(uint32_t absolute_send_time);
162   void SetVideoRotation(VideoRotation rotation);
163   int32_t SetTransportSequenceNumber(uint16_t sequence_number);
164 
165   int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
166   bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) override;
167   int32_t DeregisterRtpHeaderExtension(RTPExtensionType type);
168 
169   size_t RtpHeaderExtensionTotalLength() const;
170 
171   uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer, bool marker_bit) const;
172 
173   uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const;
174   uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const;
175   uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const;
176   uint8_t BuildVideoRotationExtension(uint8_t* data_buffer) const;
177   uint8_t BuildTransportSequenceNumberExtension(uint8_t* data_buffer,
178                                                 uint16_t sequence_number) const;
179 
180   // Verifies that the specified extension is registered, and that it is
181   // present in rtp packet. If extension is not registered kNotRegistered is
182   // returned. If extension cannot be found in the rtp header, or if it is
183   // malformed, kError is returned. Otherwise *extension_offset is set to the
184   // offset of the extension from the beginning of the rtp packet and kOk is
185   // returned.
186   enum class ExtensionStatus {
187     kNotRegistered,
188     kOk,
189     kError,
190   };
191   ExtensionStatus VerifyExtension(RTPExtensionType extension_type,
192                                   uint8_t* rtp_packet,
193                                   size_t rtp_packet_length,
194                                   const RTPHeader& rtp_header,
195                                   size_t extension_length_bytes,
196                                   size_t* extension_offset) const
197       EXCLUSIVE_LOCKS_REQUIRED(send_critsect_.get());
198 
199   bool UpdateAudioLevel(uint8_t* rtp_packet,
200                         size_t rtp_packet_length,
201                         const RTPHeader& rtp_header,
202                         bool is_voiced,
203                         uint8_t dBov) const;
204 
205   bool UpdateVideoRotation(uint8_t* rtp_packet,
206                            size_t rtp_packet_length,
207                            const RTPHeader& rtp_header,
208                            VideoRotation rotation) const override;
209 
210   bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms,
211                         bool retransmission);
212   size_t TimeToSendPadding(size_t bytes);
213 
214   // NACK.
215   int SelectiveRetransmissions() const;
216   int SetSelectiveRetransmissions(uint8_t settings);
217   void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
218                       int64_t avg_rtt);
219 
220   void SetStorePacketsStatus(bool enable, uint16_t number_to_store);
221 
222   bool StorePackets() const;
223 
224   int32_t ReSendPacket(uint16_t packet_id, int64_t min_resend_time = 0);
225 
226   bool ProcessNACKBitRate(uint32_t now);
227 
228   // RTX.
229   void SetRtxStatus(int mode);
230   int RtxStatus() const;
231 
232   uint32_t RtxSsrc() const;
233   void SetRtxSsrc(uint32_t ssrc);
234 
235   void SetRtxPayloadType(int payload_type, int associated_payload_type);
236   std::pair<int, int> RtxPayloadType() const;
237 
238   // Functions wrapping RTPSenderInterface.
239   int32_t BuildRTPheader(uint8_t* data_buffer,
240                          int8_t payload_type,
241                          bool marker_bit,
242                          uint32_t capture_timestamp,
243                          int64_t capture_time_ms,
244                          const bool timestamp_provided = true,
245                          const bool inc_sequence_number = true) override;
246 
247   size_t RTPHeaderLength() const override;
248   uint16_t AllocateSequenceNumber(uint16_t packets_to_send) override;
249   size_t MaxPayloadLength() const override;
250   uint16_t PacketOverHead() const override;
251 
252   // Current timestamp.
253   uint32_t Timestamp() const override;
254   uint32_t SSRC() const override;
255 
256   int32_t SendToNetwork(uint8_t* data_buffer,
257                         size_t payload_length,
258                         size_t rtp_header_length,
259                         int64_t capture_time_ms,
260                         StorageType storage,
261                         RtpPacketSender::Priority priority) override;
262 
263   // Audio.
264 
265   // Send a DTMF tone using RFC 2833 (4733).
266   int32_t SendTelephoneEvent(uint8_t key, uint16_t time_ms, uint8_t level);
267 
268   // Set audio packet size, used to determine when it's time to send a DTMF
269   // packet in silence (CNG).
270   int32_t SetAudioPacketSize(uint16_t packet_size_samples);
271 
272   // Store the audio level in d_bov for
273   // header-extension-for-audio-level-indication.
274   int32_t SetAudioLevel(uint8_t level_d_bov);
275 
276   // Set payload type for Redundant Audio Data RFC 2198.
277   int32_t SetRED(int8_t payload_type);
278 
279   // Get payload type for Redundant Audio Data RFC 2198.
280   int32_t RED(int8_t *payload_type) const;
281 
282   RtpVideoCodecTypes VideoCodecType() const;
283 
284   uint32_t MaxConfiguredBitrateVideo() const;
285 
286   // FEC.
287   void SetGenericFECStatus(bool enable,
288                            uint8_t payload_type_red,
289                            uint8_t payload_type_fec);
290 
291   void GenericFECStatus(bool* enable,
292                         uint8_t* payload_type_red,
293                         uint8_t* payload_type_fec) const;
294 
295   int32_t SetFecParameters(const FecProtectionParams *delta_params,
296                            const FecProtectionParams *key_params);
297 
298   size_t SendPadData(size_t bytes,
299                      bool timestamp_provided,
300                      uint32_t timestamp,
301                      int64_t capture_time_ms);
302 
303   // Called on update of RTP statistics.
304   void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
305   StreamDataCountersCallback* GetRtpStatisticsCallback() const;
306 
307   uint32_t BitrateSent() const;
308 
309   void SetRtpState(const RtpState& rtp_state);
310   RtpState GetRtpState() const;
311   void SetRtxRtpState(const RtpState& rtp_state);
312   RtpState GetRtxRtpState() const;
313   CVOMode ActivateCVORtpHeaderExtension() override;
314 
315  protected:
316   int32_t CheckPayloadType(int8_t payload_type, RtpVideoCodecTypes* video_type);
317 
318  private:
319   // Maps capture time in milliseconds to send-side delay in milliseconds.
320   // Send-side delay is the difference between transmission time and capture
321   // time.
322   typedef std::map<int64_t, int> SendDelayMap;
323 
324   size_t CreateRtpHeader(uint8_t* header,
325                          int8_t payload_type,
326                          uint32_t ssrc,
327                          bool marker_bit,
328                          uint32_t timestamp,
329                          uint16_t sequence_number,
330                          const std::vector<uint32_t>& csrcs) const;
331 
332   void UpdateNACKBitRate(uint32_t bytes, int64_t now);
333 
334   bool PrepareAndSendPacket(uint8_t* buffer,
335                             size_t length,
336                             int64_t capture_time_ms,
337                             bool send_over_rtx,
338                             bool is_retransmit);
339 
340   // Return the number of bytes sent.  Note that both of these functions may
341   // return a larger value that their argument.
342   size_t TrySendRedundantPayloads(size_t bytes);
343 
344   void BuildPaddingPacket(uint8_t* packet,
345                           size_t header_length,
346                           size_t padding_length);
347 
348   void BuildRtxPacket(uint8_t* buffer, size_t* length,
349                       uint8_t* buffer_rtx);
350 
351   bool SendPacketToNetwork(const uint8_t* packet,
352                            size_t size,
353                            const PacketOptions& options);
354 
355   void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
356 
357   // Find the byte position of the RTP extension as indicated by |type| in
358   // |rtp_packet|. Return false if such extension doesn't exist.
359   bool FindHeaderExtensionPosition(RTPExtensionType type,
360                                    const uint8_t* rtp_packet,
361                                    size_t rtp_packet_length,
362                                    const RTPHeader& rtp_header,
363                                    size_t* position) const;
364 
365   void UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
366                                     size_t rtp_packet_length,
367                                     const RTPHeader& rtp_header,
368                                     int64_t time_diff_ms) const;
369   void UpdateAbsoluteSendTime(uint8_t* rtp_packet,
370                               size_t rtp_packet_length,
371                               const RTPHeader& rtp_header,
372                               int64_t now_ms) const;
373   // Update the transport sequence number of the packet using a new sequence
374   // number allocated by SequenceNumberAllocator. Returns the assigned sequence
375   // number, or 0 if extension could not be updated.
376   uint16_t UpdateTransportSequenceNumber(uint8_t* rtp_packet,
377                                          size_t rtp_packet_length,
378                                          const RTPHeader& rtp_header) const;
379 
380   void UpdateRtpStats(const uint8_t* buffer,
381                       size_t packet_length,
382                       const RTPHeader& header,
383                       bool is_rtx,
384                       bool is_retransmit);
385   bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
386 
387   Clock* clock_;
388   int64_t clock_delta_ms_;
389   Random random_ GUARDED_BY(send_critsect_);
390 
391   rtc::scoped_ptr<BitrateAggregator> bitrates_;
392   Bitrate total_bitrate_sent_;
393 
394   const bool audio_configured_;
395   rtc::scoped_ptr<RTPSenderAudio> audio_;
396   rtc::scoped_ptr<RTPSenderVideo> video_;
397 
398   RtpPacketSender* const paced_sender_;
399   TransportSequenceNumberAllocator* const transport_sequence_number_allocator_;
400   TransportFeedbackObserver* const transport_feedback_observer_;
401   int64_t last_capture_time_ms_sent_;
402   rtc::scoped_ptr<CriticalSectionWrapper> send_critsect_;
403 
404   Transport *transport_;
405   bool sending_media_ GUARDED_BY(send_critsect_);
406 
407   size_t max_payload_length_;
408   uint16_t packet_over_head_;
409 
410   int8_t payload_type_ GUARDED_BY(send_critsect_);
411   std::map<int8_t, RtpUtility::Payload*> payload_type_map_;
412 
413   RtpHeaderExtensionMap rtp_header_extension_map_;
414   int32_t transmission_time_offset_;
415   uint32_t absolute_send_time_;
416   VideoRotation rotation_;
417   CVOMode cvo_mode_;
418   uint16_t transport_sequence_number_;
419 
420   // NACK
421   uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
422   size_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
423   Bitrate nack_bitrate_;
424 
425   RTPPacketHistory packet_history_;
426 
427   // Statistics
428   rtc::scoped_ptr<CriticalSectionWrapper> statistics_crit_;
429   SendDelayMap send_delays_ GUARDED_BY(statistics_crit_);
430   FrameCounts frame_counts_ GUARDED_BY(statistics_crit_);
431   StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_);
432   StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_);
433   StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_);
434   FrameCountObserver* const frame_count_observer_;
435   SendSideDelayObserver* const send_side_delay_observer_;
436 
437   // RTP variables
438   bool start_timestamp_forced_ GUARDED_BY(send_critsect_);
439   uint32_t start_timestamp_ GUARDED_BY(send_critsect_);
440   SSRCDatabase& ssrc_db_ GUARDED_BY(send_critsect_);
441   uint32_t remote_ssrc_ GUARDED_BY(send_critsect_);
442   bool sequence_number_forced_ GUARDED_BY(send_critsect_);
443   uint16_t sequence_number_ GUARDED_BY(send_critsect_);
444   uint16_t sequence_number_rtx_ GUARDED_BY(send_critsect_);
445   bool ssrc_forced_ GUARDED_BY(send_critsect_);
446   uint32_t ssrc_ GUARDED_BY(send_critsect_);
447   uint32_t timestamp_ GUARDED_BY(send_critsect_);
448   int64_t capture_time_ms_ GUARDED_BY(send_critsect_);
449   int64_t last_timestamp_time_ms_ GUARDED_BY(send_critsect_);
450   bool media_has_been_sent_ GUARDED_BY(send_critsect_);
451   bool last_packet_marker_bit_ GUARDED_BY(send_critsect_);
452   std::vector<uint32_t> csrcs_ GUARDED_BY(send_critsect_);
453   int rtx_ GUARDED_BY(send_critsect_);
454   uint32_t ssrc_rtx_ GUARDED_BY(send_critsect_);
455   // TODO(changbin): Remove rtx_payload_type_ once interop with old clients that
456   // only understand one RTX PT is no longer needed.
457   int rtx_payload_type_ GUARDED_BY(send_critsect_);
458   // Mapping rtx_payload_type_map_[associated] = rtx.
459   std::map<int8_t, int8_t> rtx_payload_type_map_ GUARDED_BY(send_critsect_);
460 
461   // Note: Don't access this variable directly, always go through
462   // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember
463   // that by the time the function returns there is no guarantee
464   // that the target bitrate is still valid.
465   rtc::scoped_ptr<CriticalSectionWrapper> target_bitrate_critsect_;
466   uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_);
467 };
468 
469 }  // namespace webrtc
470 
471 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
472