1 /*
2  *  Copyright (c) 2015 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 VOICE_ENGINE_CHANNEL_PROXY_H_
12 #define VOICE_ENGINE_CHANNEL_PROXY_H_
13 
14 #include "api/audio/audio_mixer.h"
15 #include "api/audio_codecs/audio_encoder.h"
16 #include "api/rtpreceiverinterface.h"
17 #include "call/rtp_packet_sink_interface.h"
18 #include "rtc_base/constructormagic.h"
19 #include "rtc_base/race_checker.h"
20 #include "rtc_base/thread_checker.h"
21 #include "voice_engine/channel.h"
22 #include "voice_engine/channel_manager.h"
23 
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 namespace webrtc {
29 
30 class AudioSinkInterface;
31 class PacketRouter;
32 class RtcEventLog;
33 class RtcpBandwidthObserver;
34 class RtcpRttStats;
35 class RtpPacketObserver;
36 class RtpPacketSender;
37 class RtpPacketReceived;
38 class RtpReceiver;
39 class RtpRtcp;
40 class RtpTransportControllerSendInterface;
41 class Transport;
42 class TransportFeedbackObserver;
43 
44 namespace voe {
45 
46 // This class provides the "view" of a voe::Channel that we need to implement
47 // webrtc::AudioSendStream and webrtc::AudioReceiveStream. It serves two
48 // purposes:
49 //  1. Allow mocking just the interfaces used, instead of the entire
50 //     voe::Channel class.
51 //  2. Provide a refined interface for the stream classes, including assumptions
52 //     on return values and input adaptation.
53 class ChannelProxy : public RtpPacketSinkInterface {
54  public:
55   ChannelProxy();
56   explicit ChannelProxy(const ChannelOwner& channel_owner);
57   virtual ~ChannelProxy();
58 
59   virtual bool SetEncoder(int payload_type,
60                           std::unique_ptr<AudioEncoder> encoder);
61   virtual void ModifyEncoder(
62       rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
63 
64   virtual void SetRTCPStatus(bool enable);
65   virtual void SetLocalMID(const char* mid);
66   virtual void SetLocalSSRC(uint32_t ssrc);
67   virtual void SetRTCP_CNAME(const std::string& c_name);
68   virtual void SetNACKStatus(bool enable, int max_packets);
69   virtual void SetSendAudioLevelIndicationStatus(bool enable, int id);
70   virtual void SetReceiveAudioLevelIndicationStatus(bool enable, int id,
71                                                     bool isLevelSsrc = true);
72   virtual void SetReceiveCsrcAudioLevelIndicationStatus(bool enable, int id);
73   virtual void SetSendMIDStatus(bool enable, int id);
74   virtual void EnableSendTransportSequenceNumber(int id);
75   virtual void EnableReceiveTransportSequenceNumber(int id);
76   virtual void RegisterSenderCongestionControlObjects(
77       RtpTransportControllerSendInterface* transport,
78       RtcpBandwidthObserver* bandwidth_observer);
79   virtual void RegisterReceiverCongestionControlObjects(
80       PacketRouter* packet_router);
81   virtual void ResetSenderCongestionControlObjects();
82   virtual void ResetReceiverCongestionControlObjects();
83   virtual bool GetRTCPPacketTypeCounters(RtcpPacketTypeCounter& stats);
84   virtual bool GetRTCPReceiverStatistics(int64_t* timestamp,
85                                          uint32_t* jitterMs,
86                                          uint32_t* cumulativeLost,
87                                          uint32_t* packetsReceived,
88                                          uint64_t* bytesReceived,
89                                          double* packetsFractionLost,
90                                          int64_t* rtt) const;
91   virtual CallStatistics GetRTCPStatistics() const;
92   virtual int GetRTPStatistics(unsigned int& averageJitterMs,
93                                unsigned int& cumulativeLost) const;
94   virtual std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const;
95   virtual NetworkStatistics GetNetworkStatistics() const;
96   virtual AudioDecodingCallStats GetDecodingCallStatistics() const;
97   virtual ANAStats GetANAStatistics() const;
98   virtual int GetSpeechOutputLevel() const;
99   virtual int GetSpeechOutputLevelFullRange() const;
100   // See description of "totalAudioEnergy" in the WebRTC stats spec:
101   // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
102   virtual double GetTotalOutputEnergy() const;
103   virtual double GetTotalOutputDuration() const;
104   virtual uint32_t GetDelayEstimate() const;
105   virtual bool SetSendTelephoneEventPayloadType(int payload_type,
106                                                 int payload_frequency);
107   virtual bool SendTelephoneEventOutband(int event, int duration_ms);
108   virtual void SetBitrate(int bitrate_bps, int64_t probing_interval_ms);
109   virtual void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
110   virtual void SetSink(std::unique_ptr<AudioSinkInterface> sink);
111   virtual void SetInputMute(bool muted);
112   virtual void RegisterTransport(Transport* transport);
113 
114   // Implements RtpPacketSinkInterface
115   void OnRtpPacket(const RtpPacketReceived& packet) override;
116   virtual bool ReceivedRTCPPacket(const uint8_t* packet, size_t length);
117   virtual const rtc::scoped_refptr<AudioDecoderFactory>&
118       GetAudioDecoderFactory() const;
119   virtual void SetChannelOutputVolumeScaling(float scaling);
120   virtual void SetRtcEventLog(RtcEventLog* event_log);
121   virtual AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
122       int sample_rate_hz,
123       AudioFrame* audio_frame);
124   virtual int PreferredSampleRate() const;
125   virtual void SetTransportOverhead(int transport_overhead_per_packet);
126   virtual void AssociateSendChannel(const ChannelProxy& send_channel_proxy);
127   virtual void DisassociateSendChannel();
128   virtual void GetRtpRtcp(RtpRtcp** rtp_rtcp,
129                           RtpReceiver** rtp_receiver) const;
130   virtual uint32_t GetPlayoutTimestamp() const;
131   virtual void SetMinimumPlayoutDelay(int delay_ms);
132   virtual void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats);
133   virtual bool GetRecCodec(CodecInst* codec_inst) const;
134   virtual void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
135   virtual void OnRecoverableUplinkPacketLossRate(
136       float recoverable_packet_loss_rate);
137   virtual std::vector<webrtc::RtpSource> GetSources() const;
138 
139   virtual void SetRtpPacketObserver(RtpPacketObserver* observer);
140   virtual void SetRtcpEventObserver(RtcpEventObserver* observer);
141 
142  private:
143   Channel* channel() const;
144 
145   // Thread checkers document and lock usage of some methods on voe::Channel to
146   // specific threads we know about. The goal is to eventually split up
147   // voe::Channel into parts with single-threaded semantics, and thereby reduce
148   // the need for locks.
149   rtc::ThreadChecker worker_thread_checker_;
150   rtc::ThreadChecker module_process_thread_checker_;
151   // Methods accessed from audio and video threads are checked for sequential-
152   // only access. We don't necessarily own and control these threads, so thread
153   // checkers cannot be used. E.g. Chromium may transfer "ownership" from one
154   // audio thread to another, but access is still sequential.
155   rtc::RaceChecker audio_thread_race_checker_;
156   rtc::RaceChecker video_capture_thread_race_checker_;
157   ChannelOwner channel_owner_;
158 
159   RTC_DISALLOW_COPY_AND_ASSIGN(ChannelProxy);
160 };
161 }  // namespace voe
162 }  // namespace webrtc
163 
164 #endif  // VOICE_ENGINE_CHANNEL_PROXY_H_
165