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_H_
12 #define MODULES_RTP_RTCP_SOURCE_RTCP_TRANSCEIVER_H_
13 
14 #include <functional>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "api/task_queue/task_queue_base.h"
20 #include "modules/rtp_rtcp/source/rtcp_transceiver_config.h"
21 #include "modules/rtp_rtcp/source/rtcp_transceiver_impl.h"
22 #include "rtc_base/copy_on_write_buffer.h"
23 
24 namespace webrtc {
25 //
26 // Manage incoming and outgoing rtcp messages for multiple BUNDLED streams.
27 //
28 // This class is thread-safe wrapper of RtcpTransceiverImpl
29 class RtcpTransceiver : public RtcpFeedbackSenderInterface {
30  public:
31   explicit RtcpTransceiver(const RtcpTransceiverConfig& config);
32   RtcpTransceiver(const RtcpTransceiver&) = delete;
33   RtcpTransceiver& operator=(const RtcpTransceiver&) = delete;
34   // Note that interfaces provided in constructor still might be used after the
35   // destructor. However they can only be used on the confic.task_queue.
36   // Use Stop function to get notified when they are no longer used or
37   // ensure those objects outlive the task queue.
38   ~RtcpTransceiver() override;
39 
40   // Start asynchronious destruction of the RtcpTransceiver.
41   // It is safe to call destructor right after Stop exits.
42   // No other methods can be called.
43   // Note that interfaces provided in constructor or registered with AddObserver
44   // still might be used by the transceiver on the task queue
45   // until |on_destroyed| runs.
46   void Stop(std::function<void()> on_destroyed);
47 
48   // Registers observer to be notified about incoming rtcp packets.
49   // Calls to observer will be done on the |config.task_queue|.
50   void AddMediaReceiverRtcpObserver(uint32_t remote_ssrc,
51                                     MediaReceiverRtcpObserver* observer);
52   // Deregisters the observer. Might return before observer is deregistered.
53   // Runs |on_removed| when observer is deregistered.
54   void RemoveMediaReceiverRtcpObserver(uint32_t remote_ssrc,
55                                        MediaReceiverRtcpObserver* observer,
56                                        std::function<void()> on_removed);
57 
58   // Enables/disables sending rtcp packets eventually.
59   // Packets may be sent after the SetReadyToSend(false) returns, but no new
60   // packets will be scheduled.
61   void SetReadyToSend(bool ready);
62 
63   // Handles incoming rtcp packets.
64   void ReceivePacket(rtc::CopyOnWriteBuffer packet);
65 
66   // Sends RTCP packets starting with a sender or receiver report.
67   void SendCompoundPacket();
68 
69   // (REMB) Receiver Estimated Max Bitrate.
70   // Includes REMB in following compound packets and sends a REMB message
71   // immediately if 'RtcpTransceiverConfig::send_remb_on_change' is set.
72   void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
73   // Stops sending REMB in following compound packets.
74   void UnsetRemb() override;
75 
76   // TODO(bugs.webrtc.org/8239): Remove SendCombinedRtcpPacket
77   // and move generating of the TransportFeedback message inside
78   // RtcpTransceiverImpl when there is one RtcpTransceiver per rtp transport.
79   void SendCombinedRtcpPacket(
80       std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
81 
82   // Reports missing packets, https://tools.ietf.org/html/rfc4585#section-6.2.1
83   void SendNack(uint32_t ssrc, std::vector<uint16_t> sequence_numbers);
84 
85   // Requests new key frame.
86   // using PLI, https://tools.ietf.org/html/rfc4585#section-6.3.1.1
87   void SendPictureLossIndication(uint32_t ssrc);
88   // using FIR, https://tools.ietf.org/html/rfc5104#section-4.3.1.2
89   // Use the SendFullIntraRequest(ssrcs, true) instead.
90   void SendFullIntraRequest(std::vector<uint32_t> ssrcs);
91   // If new_request is true then requested sequence no. will increase for each
92   // requested ssrc.
93   void SendFullIntraRequest(std::vector<uint32_t> ssrcs, bool new_request);
94 
95  private:
96   TaskQueueBase* const task_queue_;
97   std::unique_ptr<RtcpTransceiverImpl> rtcp_transceiver_;
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // MODULES_RTP_RTCP_SOURCE_RTCP_TRANSCEIVER_H_
103