• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017 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 MODULES_RTP_RTCP_SOURCE_RTCP_TRANSCEIVER_IMPL_H_
12 #define MODULES_RTP_RTCP_SOURCE_RTCP_TRANSCEIVER_IMPL_H_
13 
14 #include <list>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "api/array_view.h"
21 #include "api/units/timestamp.h"
22 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/target_bitrate.h"
27 #include "modules/rtp_rtcp/source/rtcp_transceiver_config.h"
28 #include "rtc_base/containers/flat_map.h"
29 #include "rtc_base/task_utils/repeating_task.h"
30 #include "system_wrappers/include/ntp_time.h"
31 
32 namespace webrtc {
33 //
34 // Manage incoming and outgoing rtcp messages for multiple BUNDLED streams.
35 //
36 // This class is not thread-safe.
37 class RtcpTransceiverImpl {
38  public:
39   explicit RtcpTransceiverImpl(const RtcpTransceiverConfig& config);
40   RtcpTransceiverImpl(const RtcpTransceiverImpl&) = delete;
41   RtcpTransceiverImpl& operator=(const RtcpTransceiverImpl&) = delete;
42   ~RtcpTransceiverImpl();
43 
StopPeriodicTask()44   void StopPeriodicTask() { periodic_task_handle_.Stop(); }
45 
46   void AddMediaReceiverRtcpObserver(uint32_t remote_ssrc,
47                                     MediaReceiverRtcpObserver* observer);
48   void RemoveMediaReceiverRtcpObserver(uint32_t remote_ssrc,
49                                        MediaReceiverRtcpObserver* observer);
50 
51   // Returns false on failure, e.g. when there is already an handler for the
52   // `local_ssrc`.
53   bool AddMediaSender(uint32_t local_ssrc, RtpStreamRtcpHandler* handler);
54   bool RemoveMediaSender(uint32_t local_ssrc);
55 
56   void SetReadyToSend(bool ready);
57 
58   void ReceivePacket(rtc::ArrayView<const uint8_t> packet, Timestamp now);
59 
60   void SendCompoundPacket();
61 
62   void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs);
63   void UnsetRemb();
64   // Temporary helpers to send pre-built TransportFeedback rtcp packet.
sender_ssrc()65   uint32_t sender_ssrc() const { return config_.feedback_ssrc; }
66   void SendRawPacket(rtc::ArrayView<const uint8_t> packet);
67 
68   void SendNack(uint32_t ssrc, std::vector<uint16_t> sequence_numbers);
69 
70   void SendPictureLossIndication(uint32_t ssrc);
71   // If new_request is true then requested sequence no. will increase for each
72   // requested ssrc.
73   void SendFullIntraRequest(rtc::ArrayView<const uint32_t> ssrcs,
74                             bool new_request);
75 
76   // SendCombinedRtcpPacket ignores rtcp mode and does not send a compound
77   // message. https://tools.ietf.org/html/rfc4585#section-3.1
78   void SendCombinedRtcpPacket(
79       std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets);
80 
81  private:
82   class PacketSender;
83   struct RemoteSenderState;
84   struct LocalSenderState;
85   struct RrtrTimes {
86     // Received remote NTP timestamp in compact representation.
87     uint32_t received_remote_mid_ntp_time;
88 
89     // Local NTP time when the report was received in compact representation.
90     uint32_t local_receive_mid_ntp_time;
91   };
92 
93   void HandleReceivedPacket(const rtcp::CommonHeader& rtcp_packet_header,
94                             Timestamp now,
95                             std::vector<rtcp::ReportBlock>& report_blocks);
96   // Individual rtcp packet handlers.
97   void HandleBye(const rtcp::CommonHeader& rtcp_packet_header);
98   void HandleSenderReport(const rtcp::CommonHeader& rtcp_packet_header,
99                           Timestamp now,
100                           std::vector<rtcp::ReportBlock>& report_blocks);
101   void HandleReceiverReport(const rtcp::CommonHeader& rtcp_packet_header,
102                             std::vector<rtcp::ReportBlock>& report_blocks);
103   void CallbackOnReportBlocks(
104       uint32_t sender_ssrc,
105       rtc::ArrayView<const rtcp::ReportBlock> report_blocks);
106   void HandlePayloadSpecificFeedback(
107       const rtcp::CommonHeader& rtcp_packet_header,
108       Timestamp now);
109   void HandleRtpFeedback(const rtcp::CommonHeader& rtcp_packet_header,
110                          Timestamp now);
111   void HandleFir(const rtcp::CommonHeader& rtcp_packet_header);
112   void HandlePli(const rtcp::CommonHeader& rtcp_packet_header);
113   void HandleRemb(const rtcp::CommonHeader& rtcp_packet_header, Timestamp now);
114   void HandleNack(const rtcp::CommonHeader& rtcp_packet_header);
115   void HandleTransportFeedback(const rtcp::CommonHeader& rtcp_packet_header,
116                                Timestamp now);
117   void HandleExtendedReports(const rtcp::CommonHeader& rtcp_packet_header,
118                              Timestamp now);
119   // Extended Reports blocks handlers.
120   void HandleDlrr(const rtcp::Dlrr& dlrr, Timestamp now);
121   void HandleTargetBitrate(const rtcp::TargetBitrate& target_bitrate,
122                            uint32_t remote_ssrc);
123   void ProcessReportBlocks(
124       Timestamp now,
125       rtc::ArrayView<const rtcp::ReportBlock> report_blocks);
126 
127   void ReschedulePeriodicCompoundPackets();
128   void SchedulePeriodicCompoundPackets(TimeDelta delay);
129   // Appends RTCP sender and receiver reports to the `sender`.
130   // Both sender and receiver reports may have attached report blocks.
131   // Uses up to `config_.max_packet_size - reserved_bytes.per_packet`
132   // Returns list of sender ssrc in sender reports.
133   struct ReservedBytes {
134     size_t per_packet = 0;
135     size_t per_sender = 0;
136   };
137   std::vector<uint32_t> FillReports(Timestamp now,
138                                     ReservedBytes reserved_bytes,
139                                     PacketSender& rtcp_sender);
140 
141   // Creates compound RTCP packet, as defined in
142   // https://tools.ietf.org/html/rfc5506#section-2
143   void CreateCompoundPacket(Timestamp now,
144                             size_t reserved_bytes,
145                             PacketSender& rtcp_sender);
146 
147   // Sends RTCP packets.
148   void SendPeriodicCompoundPacket();
149   void SendImmediateFeedback(const rtcp::RtcpPacket& rtcp_packet);
150   // Generate Report Blocks to be send in Sender or Receiver Reports.
151   std::vector<rtcp::ReportBlock> CreateReportBlocks(Timestamp now,
152                                                     size_t num_max_blocks);
153 
154   const RtcpTransceiverConfig config_;
155 
156   bool ready_to_send_;
157   absl::optional<rtcp::Remb> remb_;
158   // TODO(danilchap): Remove entries from remote_senders_ that are no longer
159   // needed.
160   flat_map<uint32_t, RemoteSenderState> remote_senders_;
161   std::list<LocalSenderState> local_senders_;
162   flat_map<uint32_t, std::list<LocalSenderState>::iterator>
163       local_senders_by_ssrc_;
164   flat_map<uint32_t, RrtrTimes> received_rrtrs_;
165   RepeatingTaskHandle periodic_task_handle_;
166 };
167 
168 }  // namespace webrtc
169 
170 #endif  // MODULES_RTP_RTCP_SOURCE_RTCP_TRANSCEIVER_IMPL_H_
171