• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020 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_VOIP_VOIP_CORE_H_
12 #define AUDIO_VOIP_VOIP_CORE_H_
13 
14 #include <map>
15 #include <memory>
16 #include <queue>
17 #include <unordered_map>
18 #include <vector>
19 
20 #include "api/audio_codecs/audio_decoder_factory.h"
21 #include "api/audio_codecs/audio_encoder_factory.h"
22 #include "api/scoped_refptr.h"
23 #include "api/task_queue/task_queue_factory.h"
24 #include "api/voip/voip_base.h"
25 #include "api/voip/voip_codec.h"
26 #include "api/voip/voip_engine.h"
27 #include "api/voip/voip_network.h"
28 #include "audio/audio_transport_impl.h"
29 #include "audio/voip/audio_channel.h"
30 #include "modules/audio_device/include/audio_device.h"
31 #include "modules/audio_mixer/audio_mixer_impl.h"
32 #include "modules/audio_processing/include/audio_processing.h"
33 #include "modules/utility/include/process_thread.h"
34 #include "rtc_base/synchronization/mutex.h"
35 
36 namespace webrtc {
37 
38 // VoipCore is the implementatino of VoIP APIs listed in api/voip directory.
39 // It manages a vector of AudioChannel objects where each is mapped with a
40 // ChannelId (int) type. ChannelId is the primary key to locate a specific
41 // AudioChannel object to operate requested VoIP API from the caller.
42 //
43 // This class receives required audio components from caller at construction and
44 // owns the life cycle of them to orchestrate the proper destruction sequence.
45 class VoipCore : public VoipEngine,
46                  public VoipBase,
47                  public VoipNetwork,
48                  public VoipCodec {
49  public:
50   ~VoipCore() override = default;
51 
52   // Initialize VoipCore components with provided arguments.
53   // Returns false only when |audio_device_module| fails to initialize which
54   // would presumably render further processing useless.
55   // TODO(natim@webrtc.org): Need to report audio device errors to user layer.
56   bool Init(rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,
57             rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
58             std::unique_ptr<TaskQueueFactory> task_queue_factory,
59             rtc::scoped_refptr<AudioDeviceModule> audio_device_module,
60             rtc::scoped_refptr<AudioProcessing> audio_processing);
61 
62   // Implements VoipEngine interfaces.
Base()63   VoipBase& Base() override { return *this; }
Network()64   VoipNetwork& Network() override { return *this; }
Codec()65   VoipCodec& Codec() override { return *this; }
66 
67   // Implements VoipBase interfaces.
68   absl::optional<ChannelId> CreateChannel(
69       Transport* transport,
70       absl::optional<uint32_t> local_ssrc) override;
71   void ReleaseChannel(ChannelId channel) override;
72   bool StartSend(ChannelId channel) override;
73   bool StopSend(ChannelId channel) override;
74   bool StartPlayout(ChannelId channel) override;
75   bool StopPlayout(ChannelId channel) override;
76 
77   // Implements VoipNetwork interfaces.
78   void ReceivedRTPPacket(ChannelId channel,
79                          rtc::ArrayView<const uint8_t> rtp_packet) override;
80   void ReceivedRTCPPacket(ChannelId channel,
81                           rtc::ArrayView<const uint8_t> rtcp_packet) override;
82 
83   // Implements VoipCodec interfaces.
84   void SetSendCodec(ChannelId channel,
85                     int payload_type,
86                     const SdpAudioFormat& encoder_format) override;
87   void SetReceiveCodecs(
88       ChannelId channel,
89       const std::map<int, SdpAudioFormat>& decoder_specs) override;
90 
91  private:
92   // Fetches the corresponding AudioChannel assigned with given |channel|.
93   // Returns nullptr if not found.
94   rtc::scoped_refptr<AudioChannel> GetChannel(ChannelId channel);
95 
96   // Updates AudioTransportImpl with a new set of actively sending AudioSender
97   // (AudioEgress). This needs to be invoked whenever StartSend/StopSend is
98   // involved by caller. Returns false when the selected audio device fails to
99   // initialize where it can't expect to deliver any audio input sample.
100   bool UpdateAudioTransportWithSenders();
101 
102   // Synchronization for these are handled internally.
103   rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
104   rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
105   std::unique_ptr<TaskQueueFactory> task_queue_factory_;
106 
107   // Synchronization is handled internally by AudioProessing.
108   // Must be placed before |audio_device_module_| for proper destruction.
109   rtc::scoped_refptr<AudioProcessing> audio_processing_;
110 
111   // Synchronization is handled internally by AudioMixer.
112   // Must be placed before |audio_device_module_| for proper destruction.
113   rtc::scoped_refptr<AudioMixer> audio_mixer_;
114 
115   // Synchronization is handled internally by AudioTransportImpl.
116   // Must be placed before |audio_device_module_| for proper destruction.
117   std::unique_ptr<AudioTransportImpl> audio_transport_;
118 
119   // Synchronization is handled internally by AudioDeviceModule.
120   rtc::scoped_refptr<AudioDeviceModule> audio_device_module_;
121 
122   // Synchronization is handled internally by ProcessThread.
123   // Must be placed before |channels_| for proper destruction.
124   std::unique_ptr<ProcessThread> process_thread_;
125 
126   Mutex lock_;
127 
128   // Member to track a next ChannelId for new AudioChannel.
129   int next_channel_id_ RTC_GUARDED_BY(lock_) = 0;
130 
131   // Container to track currently active AudioChannel objects mapped by
132   // ChannelId.
133   std::unordered_map<ChannelId, rtc::scoped_refptr<AudioChannel>> channels_
134       RTC_GUARDED_BY(lock_);
135 };
136 
137 }  // namespace webrtc
138 
139 #endif  // AUDIO_VOIP_VOIP_CORE_H_
140