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( 100 webrtc::Call* call, 101 const cricket::MediaConfig& media_config, 102 webrtc::RtpTransportInternal* rtp_transport, 103 rtc::Thread* signaling_thread, 104 const std::string& content_name, 105 bool srtp_required, 106 const webrtc::CryptoOptions& crypto_options, 107 rtc::UniqueRandomIdGenerator* ssrc_generator, 108 const AudioOptions& options); 109 // Destroys a voice channel created by CreateVoiceChannel. 110 void DestroyVoiceChannel(VoiceChannel* voice_channel); 111 112 // Creates a video channel, synced with the specified voice channel, and 113 // associated with the specified session. 114 // Version of the above that takes PacketTransportInternal. 115 VideoChannel* CreateVideoChannel( 116 webrtc::Call* call, 117 const cricket::MediaConfig& media_config, 118 webrtc::RtpTransportInternal* rtp_transport, 119 rtc::Thread* signaling_thread, 120 const std::string& content_name, 121 bool srtp_required, 122 const webrtc::CryptoOptions& crypto_options, 123 rtc::UniqueRandomIdGenerator* ssrc_generator, 124 const VideoOptions& options, 125 webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory); 126 // Destroys a video channel created by CreateVideoChannel. 127 void DestroyVideoChannel(VideoChannel* video_channel); 128 129 RtpDataChannel* CreateRtpDataChannel( 130 const cricket::MediaConfig& media_config, 131 webrtc::RtpTransportInternal* rtp_transport, 132 rtc::Thread* signaling_thread, 133 const std::string& content_name, 134 bool srtp_required, 135 const webrtc::CryptoOptions& crypto_options, 136 rtc::UniqueRandomIdGenerator* ssrc_generator); 137 // Destroys a data channel created by CreateRtpDataChannel. 138 void DestroyRtpDataChannel(RtpDataChannel* data_channel); 139 140 // Indicates whether any channels exist. has_channels()141 bool has_channels() const { 142 return (!voice_channels_.empty() || !video_channels_.empty() || 143 !data_channels_.empty()); 144 } 145 146 // RTX will be enabled/disabled in engines that support it. The supporting 147 // engines will start offering an RTX codec. Must be called before Init(). 148 bool SetVideoRtxEnabled(bool enable); 149 150 // Starts/stops the local microphone and enables polling of the input level. capturing()151 bool capturing() const { return capturing_; } 152 153 // The operations below occur on the main thread. 154 155 // Starts AEC dump using existing file, with a specified maximum file size in 156 // bytes. When the limit is reached, logging will stop and the file will be 157 // closed. If max_size_bytes is set to <= 0, no limit will be used. 158 bool StartAecDump(webrtc::FileWrapper file, int64_t max_size_bytes); 159 160 // Stops recording AEC dump. 161 void StopAecDump(); 162 163 private: 164 std::unique_ptr<MediaEngineInterface> media_engine_; // Nullable. 165 std::unique_ptr<DataEngineInterface> data_engine_; // Non-null. 166 bool initialized_ = false; 167 rtc::Thread* main_thread_; 168 rtc::Thread* worker_thread_; 169 rtc::Thread* network_thread_; 170 171 // Vector contents are non-null. 172 std::vector<std::unique_ptr<VoiceChannel>> voice_channels_; 173 std::vector<std::unique_ptr<VideoChannel>> video_channels_; 174 std::vector<std::unique_ptr<RtpDataChannel>> data_channels_; 175 176 bool enable_rtx_ = false; 177 bool capturing_ = false; 178 }; 179 180 } // namespace cricket 181 182 #endif // PC_CHANNEL_MANAGER_H_ 183