blob: 052c363486aa0708bcc0bfcca70fe360269d5b1c [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
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000018#include "webrtc/base/fileutils.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000019#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/media/base/mediaengine.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010021#include "webrtc/pc/voicechannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
Fredrik Solenberg709ed672015-09-15 12:26:33 +020023namespace webrtc {
24class MediaControllerInterface;
25}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026namespace cricket {
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30// ChannelManager allows the MediaEngine to run on a separate thread, and takes
31// care of marshalling calls between threads. It also creates and keeps track of
32// voice and video channels; by doing so, it can temporarily pause all the
33// channels when a new audio or video device is chosen. The voice and video
34// channels are stored in separate vectors, to easily allow operations on just
35// voice or just video channels.
36// ChannelManager also allows the application to discover what devices it has
37// using device manager.
perkjc11b1842016-03-07 17:34:13 -080038class ChannelManager {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 // For testing purposes. Allows the media engine and data media
deadbeef112b2e92017-02-10 20:13:37 -080041 // engine and dev manager to be mocks.
42 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
43 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020044 rtc::Thread* worker_and_network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 // Same as above, but gives an easier default DataEngine.
deadbeef112b2e92017-02-10 20:13:37 -080046 ChannelManager(std::unique_ptr<MediaEngineInterface> me,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020047 rtc::Thread* worker,
48 rtc::Thread* network);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 ~ChannelManager();
50
51 // Accessors for the worker thread, allowing it to be set after construction,
52 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053 rtc::Thread* worker_thread() const { return worker_thread_; }
54 bool set_worker_thread(rtc::Thread* thread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020055 if (initialized_) {
56 return false;
57 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 worker_thread_ = thread;
59 return true;
60 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020061 rtc::Thread* network_thread() const { return network_thread_; }
62 bool set_network_thread(rtc::Thread* thread) {
63 if (initialized_) {
64 return false;
65 }
66 network_thread_ = thread;
67 return true;
68 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
Fredrik Solenberg709ed672015-09-15 12:26:33 +020070 MediaEngineInterface* media_engine() { return media_engine_.get(); }
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 // Retrieves the list of supported audio & video codec types.
73 // Can be called before starting the media engine.
ossudedfd282016-06-14 07:12:39 -070074 void GetSupportedAudioSendCodecs(std::vector<AudioCodec>* codecs) const;
75 void GetSupportedAudioReceiveCodecs(std::vector<AudioCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
magjed3cf8ece2016-11-10 03:36:53 -080077 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
79 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
80
81 // Indicates whether the media engine is started.
82 bool initialized() const { return initialized_; }
83 // Starts up the media engine.
84 bool Init();
85 // Shuts down the media engine.
86 void Terminate();
87
88 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020090 VoiceChannel* CreateVoiceChannel(
91 webrtc::MediaControllerInterface* media_controller,
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,
skvlad6c87a672016-05-17 17:49:52 -070096 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -080097 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -080098 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020099 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200101 void DestroyVoiceChannel(VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 // Creates a video channel, synced with the specified voice channel, and
103 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200104 VideoChannel* CreateVideoChannel(
105 webrtc::MediaControllerInterface* media_controller,
zhihuangb2cdd932017-01-19 16:54:25 -0800106 DtlsTransportInternal* rtp_transport,
107 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800108 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200109 const std::string& content_name,
skvlad6c87a672016-05-17 17:49:52 -0700110 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -0800111 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800112 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200113 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 // Destroys a video channel created with the Create API.
115 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800116 RtpDataChannel* CreateRtpDataChannel(
zhihuangebbe4f22016-12-06 10:45:42 -0800117 webrtc::MediaControllerInterface* media_controller,
zhihuangb2cdd932017-01-19 16:54:25 -0800118 DtlsTransportInternal* rtp_transport,
119 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800120 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800121 const std::string& content_name,
122 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -0800123 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800124 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Destroys a data channel created with the Create API.
deadbeef953c2ce2017-01-09 14:53:41 -0800126 void DestroyRtpDataChannel(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 // Indicates whether any channels exist.
129 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200130 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 }
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // RTX will be enabled/disabled in engines that support it. The supporting
134 // engines will start offering an RTX codec. Must be called before Init().
135 bool SetVideoRtxEnabled(bool enable);
136
jbauchcb560652016-08-04 05:20:32 -0700137 // Define crypto options to set on newly created channels. Doesn't change
138 // options on already created channels.
139 bool SetCryptoOptions(const rtc::CryptoOptions& crypto_options);
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 bool capturing() const { return capturing_; }
143
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 // The operations below occur on the main thread.
145
ivocd66b44d2016-01-15 03:06:36 -0800146 // Starts AEC dump using existing file, with a specified maximum file size in
147 // bytes. When the limit is reached, logging will stop and the file will be
148 // closed. If max_size_bytes is set to <= 0, no limit will be used.
149 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000150
ivoc797ef122015-10-22 03:25:41 -0700151 // Stops recording AEC dump.
152 void StopAecDump();
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 private:
155 typedef std::vector<VoiceChannel*> VoiceChannels;
156 typedef std::vector<VideoChannel*> VideoChannels;
deadbeef953c2ce2017-01-09 14:53:41 -0800157 typedef std::vector<RtpDataChannel*> RtpDataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
deadbeef112b2e92017-02-10 20:13:37 -0800159 void Construct(std::unique_ptr<MediaEngineInterface> me,
160 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200161 rtc::Thread* worker_thread,
162 rtc::Thread* network_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000163 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000164 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 void Terminate_w();
jbauchcb560652016-08-04 05:20:32 -0700166 bool SetCryptoOptions_w(const rtc::CryptoOptions& crypto_options);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200167 VoiceChannel* CreateVoiceChannel_w(
168 webrtc::MediaControllerInterface* media_controller,
zhihuangb2cdd932017-01-19 16:54:25 -0800169 DtlsTransportInternal* rtp_transport,
170 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800171 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200172 const std::string& content_name,
skvlad6c87a672016-05-17 17:49:52 -0700173 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -0800174 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800175 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200176 const AudioOptions& options);
177 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
178 VideoChannel* CreateVideoChannel_w(
179 webrtc::MediaControllerInterface* media_controller,
zhihuangb2cdd932017-01-19 16:54:25 -0800180 DtlsTransportInternal* rtp_transport,
181 DtlsTransportInternal* rtcp_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,
skvlad6c87a672016-05-17 17:49:52 -0700184 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -0800185 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800186 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200187 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800189 RtpDataChannel* CreateRtpDataChannel_w(
deadbeef7af91dd2016-12-13 11:29:11 -0800190 webrtc::MediaControllerInterface* media_controller,
zhihuangb2cdd932017-01-19 16:54:25 -0800191 DtlsTransportInternal* rtp_transport,
192 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800193 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800194 const std::string& content_name,
195 const std::string* bundle_transport_name,
deadbeefac22f702017-01-12 21:59:29 -0800196 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800197 bool srtp_required);
198 void DestroyRtpDataChannel_w(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199
kwiberg31022942016-03-11 14:18:21 -0800200 std::unique_ptr<MediaEngineInterface> media_engine_;
201 std::unique_ptr<DataEngineInterface> data_media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000203 rtc::Thread* main_thread_;
204 rtc::Thread* worker_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200205 rtc::Thread* network_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
207 VoiceChannels voice_channels_;
208 VideoChannels video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800209 RtpDataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 bool enable_rtx_;
jbauchcb560652016-08-04 05:20:32 -0700212 rtc::CryptoOptions crypto_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
214 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215};
216
217} // namespace cricket
218
Perfb45d172016-02-29 12:07:35 +0100219#endif // WEBRTC_PC_CHANNELMANAGER_H_