blob: 94d1e0ac977d846f0232533dd8d90e3cf4916fd9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_CHANNELMANAGER_H_
12#define PC_CHANNELMANAGER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
kwiberg31022942016-03-11 14:18:21 -080014#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "media/base/mediaengine.h"
19#include "pc/voicechannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26// ChannelManager allows the MediaEngine to run on a separate thread, and takes
27// care of marshalling calls between threads. It also creates and keeps track of
28// voice and video channels; by doing so, it can temporarily pause all the
29// channels when a new audio or video device is chosen. The voice and video
30// channels are stored in separate vectors, to easily allow operations on just
31// voice or just video channels.
32// ChannelManager also allows the application to discover what devices it has
33// using device manager.
perkjc11b1842016-03-07 17:34:13 -080034class ChannelManager {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 // For testing purposes. Allows the media engine and data media
deadbeef112b2e92017-02-10 20:13:37 -080037 // engine and dev manager to be mocks.
38 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
39 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040 rtc::Thread* worker_and_network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 // Same as above, but gives an easier default DataEngine.
deadbeef112b2e92017-02-10 20:13:37 -080042 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020043 rtc::Thread* worker,
44 rtc::Thread* network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 ~ChannelManager();
46
47 // Accessors for the worker thread, allowing it to be set after construction,
48 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049 rtc::Thread* worker_thread() const { return worker_thread_; }
50 bool set_worker_thread(rtc::Thread* thread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020051 if (initialized_) {
52 return false;
53 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 worker_thread_ = thread;
55 return true;
56 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020057 rtc::Thread* network_thread() const { return network_thread_; }
58 bool set_network_thread(rtc::Thread* thread) {
59 if (initialized_) {
60 return false;
61 }
62 network_thread_ = thread;
63 return true;
64 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
Fredrik Solenberg709ed672015-09-15 12:26:33 +020066 MediaEngineInterface* media_engine() { return media_engine_.get(); }
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 // Retrieves the list of supported audio & video codec types.
69 // Can be called before starting the media engine.
ossudedfd282016-06-14 07:12:39 -070070 void GetSupportedAudioSendCodecs(std::vector<AudioCodec>* codecs) const;
71 void GetSupportedAudioReceiveCodecs(std::vector<AudioCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
magjed3cf8ece2016-11-10 03:36:53 -080073 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
75 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
76
77 // Indicates whether the media engine is started.
78 bool initialized() const { return initialized_; }
79 // Starts up the media engine.
80 bool Init();
81 // Shuts down the media engine.
82 void Terminate();
83
84 // The operations below all occur on the worker thread.
Steve Anton774115c2017-08-30 10:48:46 -070085 // ChannelManager retains ownership of the created channels, so clients should
86 // call the appropriate Destroy*Channel method when done.
87
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020089 VoiceChannel* CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -070090 webrtc::Call* call,
91 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -080092 DtlsTransportInternal* rtp_transport,
93 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -080094 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020095 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -080096 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020097 const AudioOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -080098 // Version of the above that takes PacketTransportInternal.
99 VoiceChannel* CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -0700100 webrtc::Call* call,
101 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800102 rtc::PacketTransportInternal* rtp_transport,
103 rtc::PacketTransportInternal* rtcp_transport,
104 rtc::Thread* signaling_thread,
105 const std::string& content_name,
106 bool srtp_required,
107 const AudioOptions& options);
Steve Anton774115c2017-08-30 10:48:46 -0700108 // Destroys a voice channel created by CreateVoiceChannel.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200109 void DestroyVoiceChannel(VoiceChannel* voice_channel);
Steve Anton774115c2017-08-30 10:48:46 -0700110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 // Creates a video channel, synced with the specified voice channel, and
112 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200113 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700114 webrtc::Call* call,
115 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800116 DtlsTransportInternal* rtp_transport,
117 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800118 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200119 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800120 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200121 const VideoOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -0800122 // Version of the above that takes PacketTransportInternal.
123 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700124 webrtc::Call* call,
125 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800126 rtc::PacketTransportInternal* rtp_transport,
127 rtc::PacketTransportInternal* rtcp_transport,
128 rtc::Thread* signaling_thread,
129 const std::string& content_name,
130 bool srtp_required,
131 const VideoOptions& options);
Steve Anton774115c2017-08-30 10:48:46 -0700132 // Destroys a video channel created by CreateVideoChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 void DestroyVideoChannel(VideoChannel* video_channel);
Steve Anton774115c2017-08-30 10:48:46 -0700134
deadbeef953c2ce2017-01-09 14:53:41 -0800135 RtpDataChannel* CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -0700136 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800137 DtlsTransportInternal* rtp_transport,
138 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800139 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800140 const std::string& content_name,
deadbeef953c2ce2017-01-09 14:53:41 -0800141 bool srtp_required);
Steve Anton774115c2017-08-30 10:48:46 -0700142 // Destroys a data channel created by CreateRtpDataChannel.
deadbeef953c2ce2017-01-09 14:53:41 -0800143 void DestroyRtpDataChannel(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 // Indicates whether any channels exist.
146 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200147 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 // RTX will be enabled/disabled in engines that support it. The supporting
151 // engines will start offering an RTX codec. Must be called before Init().
152 bool SetVideoRtxEnabled(bool enable);
153
154 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 bool capturing() const { return capturing_; }
156
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 // The operations below occur on the main thread.
158
ivocd66b44d2016-01-15 03:06:36 -0800159 // Starts AEC dump using existing file, with a specified maximum file size in
160 // bytes. When the limit is reached, logging will stop and the file will be
161 // closed. If max_size_bytes is set to <= 0, no limit will be used.
162 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000163
ivoc797ef122015-10-22 03:25:41 -0700164 // Stops recording AEC dump.
165 void StopAecDump();
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 private:
deadbeef112b2e92017-02-10 20:13:37 -0800168 void Construct(std::unique_ptr<MediaEngineInterface> me,
169 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200170 rtc::Thread* worker_thread,
171 rtc::Thread* network_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000172 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000173 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200175 VoiceChannel* CreateVoiceChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700176 webrtc::Call* call,
177 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800178 DtlsTransportInternal* rtp_dtls_transport,
179 DtlsTransportInternal* rtcp_dtls_transport,
180 rtc::PacketTransportInternal* rtp_packet_transport,
181 rtc::PacketTransportInternal* rtcp_packet_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800182 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200183 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800184 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200185 const AudioOptions& options);
186 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
187 VideoChannel* CreateVideoChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700188 webrtc::Call* call,
189 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800190 DtlsTransportInternal* rtp_dtls_transport,
191 DtlsTransportInternal* rtcp_dtls_transport,
192 rtc::PacketTransportInternal* rtp_packet_transport,
193 rtc::PacketTransportInternal* rtcp_packet_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800194 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200195 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800196 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200197 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800199 RtpDataChannel* CreateRtpDataChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700200 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800201 DtlsTransportInternal* rtp_transport,
202 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800203 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800204 const std::string& content_name,
deadbeef953c2ce2017-01-09 14:53:41 -0800205 bool srtp_required);
206 void DestroyRtpDataChannel_w(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207
kwiberg31022942016-03-11 14:18:21 -0800208 std::unique_ptr<MediaEngineInterface> media_engine_;
209 std::unique_ptr<DataEngineInterface> data_media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000211 rtc::Thread* main_thread_;
212 rtc::Thread* worker_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200213 rtc::Thread* network_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
Steve Anton774115c2017-08-30 10:48:46 -0700215 std::vector<std::unique_ptr<VoiceChannel>> voice_channels_;
216 std::vector<std::unique_ptr<VideoChannel>> video_channels_;
217 std::vector<std::unique_ptr<RtpDataChannel>> data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 bool enable_rtx_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221};
222
223} // namespace cricket
224
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200225#endif // PC_CHANNELMANAGER_H_