blob: e8c01650566a2e544293e13a07558f31f58bb56b [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
Perfb45d172016-02-29 12:07:35 +010011#ifndef WEBRTC_PC_CHANNELMANAGER_H_
12#define WEBRTC_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
kjellandera96e2d72016-02-04 23:52:28 -080018#include "webrtc/media/base/mediaengine.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010019#include "webrtc/pc/voicechannel.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/fileutils.h"
21#include "webrtc/rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
27// ChannelManager allows the MediaEngine to run on a separate thread, and takes
28// care of marshalling calls between threads. It also creates and keeps track of
29// voice and video channels; by doing so, it can temporarily pause all the
30// channels when a new audio or video device is chosen. The voice and video
31// channels are stored in separate vectors, to easily allow operations on just
32// voice or just video channels.
33// ChannelManager also allows the application to discover what devices it has
34// using device manager.
perkjc11b1842016-03-07 17:34:13 -080035class ChannelManager {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037 // For testing purposes. Allows the media engine and data media
deadbeef112b2e92017-02-10 20:13:37 -080038 // engine and dev manager to be mocks.
39 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
40 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020041 rtc::Thread* worker_and_network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042 // Same as above, but gives an easier default DataEngine.
deadbeef112b2e92017-02-10 20:13:37 -080043 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020044 rtc::Thread* worker,
45 rtc::Thread* network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046 ~ChannelManager();
47
48 // Accessors for the worker thread, allowing it to be set after construction,
49 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050 rtc::Thread* worker_thread() const { return worker_thread_; }
51 bool set_worker_thread(rtc::Thread* thread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020052 if (initialized_) {
53 return false;
54 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 worker_thread_ = thread;
56 return true;
57 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020058 rtc::Thread* network_thread() const { return network_thread_; }
59 bool set_network_thread(rtc::Thread* thread) {
60 if (initialized_) {
61 return false;
62 }
63 network_thread_ = thread;
64 return true;
65 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
Fredrik Solenberg709ed672015-09-15 12:26:33 +020067 MediaEngineInterface* media_engine() { return media_engine_.get(); }
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 // Retrieves the list of supported audio & video codec types.
70 // Can be called before starting the media engine.
ossudedfd282016-06-14 07:12:39 -070071 void GetSupportedAudioSendCodecs(std::vector<AudioCodec>* codecs) const;
72 void GetSupportedAudioReceiveCodecs(std::vector<AudioCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
magjed3cf8ece2016-11-10 03:36:53 -080074 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
76 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
77
78 // Indicates whether the media engine is started.
79 bool initialized() const { return initialized_; }
80 // Starts up the media engine.
81 bool Init();
82 // Shuts down the media engine.
83 void Terminate();
84
85 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020087 VoiceChannel* CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -070088 webrtc::Call* call,
89 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -080090 DtlsTransportInternal* rtp_transport,
91 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -080092 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020093 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -080094 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020095 const AudioOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -080096 // Version of the above that takes PacketTransportInternal.
97 VoiceChannel* CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -070098 webrtc::Call* call,
99 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800100 rtc::PacketTransportInternal* rtp_transport,
101 rtc::PacketTransportInternal* rtcp_transport,
102 rtc::Thread* signaling_thread,
103 const std::string& content_name,
104 bool srtp_required,
105 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200107 void DestroyVoiceChannel(VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 // Creates a video channel, synced with the specified voice channel, and
109 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200110 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700111 webrtc::Call* call,
112 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800113 DtlsTransportInternal* rtp_transport,
114 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800115 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200116 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800117 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200118 const VideoOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -0800119 // Version of the above that takes PacketTransportInternal.
120 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700121 webrtc::Call* call,
122 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800123 rtc::PacketTransportInternal* rtp_transport,
124 rtc::PacketTransportInternal* rtcp_transport,
125 rtc::Thread* signaling_thread,
126 const std::string& content_name,
127 bool srtp_required,
128 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Destroys a video channel created with the Create API.
130 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800131 RtpDataChannel* CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -0700132 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800133 DtlsTransportInternal* rtp_transport,
134 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800135 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800136 const std::string& content_name,
deadbeef953c2ce2017-01-09 14:53:41 -0800137 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 // Destroys a data channel created with the Create API.
deadbeef953c2ce2017-01-09 14:53:41 -0800139 void DestroyRtpDataChannel(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 // Indicates whether any channels exist.
142 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200143 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 }
145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 bool capturing() const { return capturing_; }
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 // The operations below occur on the main thread.
154
ivocd66b44d2016-01-15 03:06:36 -0800155 // 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(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000159
ivoc797ef122015-10-22 03:25:41 -0700160 // Stops recording AEC dump.
161 void StopAecDump();
162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 private:
164 typedef std::vector<VoiceChannel*> VoiceChannels;
165 typedef std::vector<VideoChannel*> VideoChannels;
deadbeef953c2ce2017-01-09 14:53:41 -0800166 typedef std::vector<RtpDataChannel*> RtpDataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
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
215 VoiceChannels voice_channels_;
216 VideoChannels video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800217 RtpDataChannels 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
Perfb45d172016-02-29 12:07:35 +0100225#endif // WEBRTC_PC_CHANNELMANAGER_H_