1 /*
2  *  Copyright 2004 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 PC_CHANNEL_MANAGER_H_
12 #define PC_CHANNEL_MANAGER_H_
13 
14 #include <stdint.h>
15 
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 #include "api/audio_options.h"
21 #include "api/crypto/crypto_options.h"
22 #include "call/call.h"
23 #include "media/base/codec.h"
24 #include "media/base/media_channel.h"
25 #include "media/base/media_config.h"
26 #include "media/base/media_engine.h"
27 #include "pc/channel.h"
28 #include "pc/rtp_transport_internal.h"
29 #include "pc/session_description.h"
30 #include "rtc_base/system/file_wrapper.h"
31 #include "rtc_base/thread.h"
32 
33 namespace cricket {
34 
35 // ChannelManager allows the MediaEngine to run on a separate thread, and takes
36 // care of marshalling calls between threads. It also creates and keeps track of
37 // voice and video channels; by doing so, it can temporarily pause all the
38 // channels when a new audio or video device is chosen. The voice and video
39 // channels are stored in separate vectors, to easily allow operations on just
40 // voice or just video channels.
41 // ChannelManager also allows the application to discover what devices it has
42 // using device manager.
43 class ChannelManager final {
44  public:
45   // Construct a ChannelManager with the specified media engine and data engine.
46   ChannelManager(std::unique_ptr<MediaEngineInterface> media_engine,
47                  std::unique_ptr<DataEngineInterface> data_engine,
48                  rtc::Thread* worker_thread,
49                  rtc::Thread* network_thread);
50   ~ChannelManager();
51 
52   // Accessors for the worker thread, allowing it to be set after construction,
53   // but before Init. set_worker_thread will return false if called after Init.
worker_thread()54   rtc::Thread* worker_thread() const { return worker_thread_; }
set_worker_thread(rtc::Thread * thread)55   bool set_worker_thread(rtc::Thread* thread) {
56     if (initialized_) {
57       return false;
58     }
59     worker_thread_ = thread;
60     return true;
61   }
network_thread()62   rtc::Thread* network_thread() const { return network_thread_; }
set_network_thread(rtc::Thread * thread)63   bool set_network_thread(rtc::Thread* thread) {
64     if (initialized_) {
65       return false;
66     }
67     network_thread_ = thread;
68     return true;
69   }
70 
media_engine()71   MediaEngineInterface* media_engine() { return media_engine_.get(); }
72 
73   // Retrieves the list of supported audio & video codec types.
74   // Can be called before starting the media engine.
75   void GetSupportedAudioSendCodecs(std::vector<AudioCodec>* codecs) const;
76   void GetSupportedAudioReceiveCodecs(std::vector<AudioCodec>* codecs) const;
77   void GetSupportedVideoSendCodecs(std::vector<VideoCodec>* codecs) const;
78   void GetSupportedVideoReceiveCodecs(std::vector<VideoCodec>* codecs) const;
79   void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
80   RtpHeaderExtensions GetDefaultEnabledAudioRtpHeaderExtensions() const;
81   std::vector<webrtc::RtpHeaderExtensionCapability>
82   GetSupportedAudioRtpHeaderExtensions() const;
83   RtpHeaderExtensions GetDefaultEnabledVideoRtpHeaderExtensions() const;
84   std::vector<webrtc::RtpHeaderExtensionCapability>
85   GetSupportedVideoRtpHeaderExtensions() const;
86 
87   // Indicates whether the media engine is started.
initialized()88   bool initialized() const { return initialized_; }
89   // Starts up the media engine.
90   bool Init();
91   // Shuts down the media engine.
92   void Terminate();
93 
94   // The operations below all occur on the worker thread.
95   // ChannelManager retains ownership of the created channels, so clients should
96   // call the appropriate Destroy*Channel method when done.
97 
98   // Creates a voice channel, to be associated with the specified session.
99   VoiceChannel* CreateVoiceChannel(webrtc::Call* call,
100                                    const cricket::MediaConfig& media_config,
101                                    webrtc::RtpTransportInternal* rtp_transport,
102                                    rtc::Thread* signaling_thread,
103                                    const std::string& content_name,
104                                    bool srtp_required,
105                                    const webrtc::CryptoOptions& crypto_options,
106                                    rtc::UniqueRandomIdGenerator* ssrc_generator,
107                                    const AudioOptions& options);
108   // Destroys a voice channel created by CreateVoiceChannel.
109   void DestroyVoiceChannel(VoiceChannel* voice_channel);
110 
111   // Creates a video channel, synced with the specified voice channel, and
112   // associated with the specified session.
113   // Version of the above that takes PacketTransportInternal.
114   VideoChannel* CreateVideoChannel(
115       webrtc::Call* call,
116       const cricket::MediaConfig& media_config,
117       webrtc::RtpTransportInternal* rtp_transport,
118       rtc::Thread* signaling_thread,
119       const std::string& content_name,
120       bool srtp_required,
121       const webrtc::CryptoOptions& crypto_options,
122       rtc::UniqueRandomIdGenerator* ssrc_generator,
123       const VideoOptions& options,
124       webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory);
125   // Destroys a video channel created by CreateVideoChannel.
126   void DestroyVideoChannel(VideoChannel* video_channel);
127 
128   RtpDataChannel* CreateRtpDataChannel(
129       const cricket::MediaConfig& media_config,
130       webrtc::RtpTransportInternal* rtp_transport,
131       rtc::Thread* signaling_thread,
132       const std::string& content_name,
133       bool srtp_required,
134       const webrtc::CryptoOptions& crypto_options,
135       rtc::UniqueRandomIdGenerator* ssrc_generator);
136   // Destroys a data channel created by CreateRtpDataChannel.
137   void DestroyRtpDataChannel(RtpDataChannel* data_channel);
138 
139   // Indicates whether any channels exist.
has_channels()140   bool has_channels() const {
141     return (!voice_channels_.empty() || !video_channels_.empty() ||
142             !data_channels_.empty());
143   }
144 
145   // RTX will be enabled/disabled in engines that support it. The supporting
146   // engines will start offering an RTX codec. Must be called before Init().
147   bool SetVideoRtxEnabled(bool enable);
148 
149   // Starts/stops the local microphone and enables polling of the input level.
capturing()150   bool capturing() const { return capturing_; }
151 
152   // The operations below occur on the main thread.
153 
154   // Starts AEC dump using existing file, with a specified maximum file size in
155   // bytes. When the limit is reached, logging will stop and the file will be
156   // closed. If max_size_bytes is set to <= 0, no limit will be used.
157   bool StartAecDump(webrtc::FileWrapper file, int64_t max_size_bytes);
158 
159   // Stops recording AEC dump.
160   void StopAecDump();
161 
162  private:
163   std::unique_ptr<MediaEngineInterface> media_engine_;  // Nullable.
164   std::unique_ptr<DataEngineInterface> data_engine_;    // Non-null.
165   bool initialized_ = false;
166   rtc::Thread* main_thread_;
167   rtc::Thread* worker_thread_;
168   rtc::Thread* network_thread_;
169 
170   // Vector contents are non-null.
171   std::vector<std::unique_ptr<VoiceChannel>> voice_channels_;
172   std::vector<std::unique_ptr<VideoChannel>> video_channels_;
173   std::vector<std::unique_ptr<RtpDataChannel>> data_channels_;
174 
175   bool enable_rtx_ = false;
176   bool capturing_ = false;
177 };
178 
179 }  // namespace cricket
180 
181 #endif  // PC_CHANNEL_MANAGER_H_
182