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 AUDIO_AUDIO_SEND_STREAM_H_
12 #define AUDIO_AUDIO_SEND_STREAM_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "audio/time_interval.h"
18 #include "call/audio_send_stream.h"
19 #include "call/audio_state.h"
20 #include "call/bitrate_allocator.h"
21 #include "modules/rtp_rtcp/include/rtp_rtcp.h"
22 #include "rtc_base/constructormagic.h"
23 #include "rtc_base/thread_checker.h"
24 #include "voice_engine/transport_feedback_packet_loss_tracker.h"
25 
26 namespace webrtc {
27 class VoiceEngine;
28 class RtcEventLog;
29 class RtcpBandwidthObserver;
30 class RtcpRttStats;
31 class RtpTransportControllerSendInterface;
32 
33 namespace voe {
34 class ChannelProxy;
35 }  // namespace voe
36 
37 namespace internal {
38 class AudioSendStream final : public webrtc::AudioSendStream,
39                               public webrtc::BitrateAllocatorObserver,
40                               public webrtc::PacketFeedbackObserver {
41  public:
42   AudioSendStream(const webrtc::AudioSendStream::Config& config,
43                   const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
44                   rtc::TaskQueue* worker_queue,
45                   RtpTransportControllerSendInterface* transport,
46                   BitrateAllocator* bitrate_allocator,
47                   RtcEventLog* event_log,
48                   RtcpRttStats* rtcp_rtt_stats,
49                   const rtc::Optional<RtpState>& suspended_rtp_state);
50   ~AudioSendStream() override;
51 
52   // webrtc::AudioSendStream implementation.
53   const webrtc::AudioSendStream::Config& GetConfig() const override;
54   void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
55   void Start() override;
56   void Stop() override;
57   bool SendTelephoneEvent(int payload_type, int payload_frequency, int event,
58                           int duration_ms) override;
59   void SetMuted(bool muted) override;
60   webrtc::AudioSendStream::Stats GetStats() const override;
61   webrtc::AudioSendStream::Stats GetStats(
62       bool has_remote_tracks) const override;
63 
64   void SignalNetworkState(NetworkState state);
65   bool DeliverRtcp(const uint8_t* packet, size_t length);
66 
67   // Implements BitrateAllocatorObserver.
68   uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
69                             uint8_t fraction_loss,
70                             int64_t rtt,
71                             int64_t bwe_period_ms) override;
72 
73   // From PacketFeedbackObserver.
74   void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
75   void OnPacketFeedbackVector(
76       const std::vector<PacketFeedback>& packet_feedback_vector) override;
77 
78   void SetTransportOverhead(int transport_overhead_per_packet);
79 
80   RtpState GetRtpState() const;
81   const TimeInterval& GetActiveLifetime() const;
82 
83  private:
84   class TimedTransport;
85 
86   VoiceEngine* voice_engine() const;
87 
88   // These are all static to make it less likely that (the old) config_ is
89   // accessed unintentionally.
90   static void ConfigureStream(AudioSendStream* stream,
91                               const Config& new_config,
92                               bool first_time);
93   static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config);
94   static bool ReconfigureSendCodec(AudioSendStream* stream,
95                                    const Config& new_config);
96   static void ReconfigureANA(AudioSendStream* stream, const Config& new_config);
97   static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config);
98   static void ReconfigureBitrateObserver(AudioSendStream* stream,
99                                          const Config& new_config);
100 
101   void ConfigureBitrateObserver(int min_bitrate_bps, int max_bitrate_bps);
102   void RemoveBitrateObserver();
103 
104   void RegisterCngPayloadType(int payload_type, int clockrate_hz);
105 
106   rtc::ThreadChecker worker_thread_checker_;
107   rtc::ThreadChecker pacer_thread_checker_;
108   rtc::TaskQueue* worker_queue_;
109   webrtc::AudioSendStream::Config config_;
110   rtc::scoped_refptr<webrtc::AudioState> audio_state_;
111   std::unique_ptr<voe::ChannelProxy> channel_proxy_;
112   RtcEventLog* const event_log_;
113 
114   BitrateAllocator* const bitrate_allocator_;
115   RtpTransportControllerSendInterface* const transport_;
116 
117   rtc::CriticalSection packet_loss_tracker_cs_;
118   TransportFeedbackPacketLossTracker packet_loss_tracker_
119       RTC_GUARDED_BY(&packet_loss_tracker_cs_);
120 
121   RtpRtcp* rtp_rtcp_module_;
122   rtc::Optional<RtpState> const suspended_rtp_state_;
123 
124   std::unique_ptr<TimedTransport> timed_send_transport_adapter_;
125   TimeInterval active_lifetime_;
126 
127   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
128 };
129 }  // namespace internal
130 }  // namespace webrtc
131 
132 #endif  // AUDIO_AUDIO_SEND_STREAM_H_
133