blob: eef90fd41843704f4c3208472ecc7fb3731690d9 [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.
Steve Anton774115c2017-08-30 10:48:46 -070086 // ChannelManager retains ownership of the created channels, so clients should
87 // call the appropriate Destroy*Channel method when done.
88
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(
nisseeaabdf62017-05-05 02:23:02 -070091 webrtc::Call* call,
92 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -080093 DtlsTransportInternal* rtp_transport,
94 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -080095 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020096 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -080097 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020098 const AudioOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -080099 // Version of the above that takes PacketTransportInternal.
100 VoiceChannel* CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -0700101 webrtc::Call* call,
102 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800103 rtc::PacketTransportInternal* rtp_transport,
104 rtc::PacketTransportInternal* rtcp_transport,
105 rtc::Thread* signaling_thread,
106 const std::string& content_name,
107 bool srtp_required,
108 const AudioOptions& options);
Steve Anton774115c2017-08-30 10:48:46 -0700109 // Destroys a voice channel created by CreateVoiceChannel.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200110 void DestroyVoiceChannel(VoiceChannel* voice_channel);
Steve Anton774115c2017-08-30 10:48:46 -0700111
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Creates a video channel, synced with the specified voice channel, and
113 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200114 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700115 webrtc::Call* call,
116 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800117 DtlsTransportInternal* rtp_transport,
118 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800119 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200120 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800121 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200122 const VideoOptions& options);
deadbeefe814a0d2017-02-25 18:15:09 -0800123 // Version of the above that takes PacketTransportInternal.
124 VideoChannel* CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700125 webrtc::Call* call,
126 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800127 rtc::PacketTransportInternal* rtp_transport,
128 rtc::PacketTransportInternal* rtcp_transport,
129 rtc::Thread* signaling_thread,
130 const std::string& content_name,
131 bool srtp_required,
132 const VideoOptions& options);
Steve Anton774115c2017-08-30 10:48:46 -0700133 // Destroys a video channel created by CreateVideoChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 void DestroyVideoChannel(VideoChannel* video_channel);
Steve Anton774115c2017-08-30 10:48:46 -0700135
deadbeef953c2ce2017-01-09 14:53:41 -0800136 RtpDataChannel* CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -0700137 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800138 DtlsTransportInternal* rtp_transport,
139 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800140 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800141 const std::string& content_name,
deadbeef953c2ce2017-01-09 14:53:41 -0800142 bool srtp_required);
Steve Anton774115c2017-08-30 10:48:46 -0700143 // Destroys a data channel created by CreateRtpDataChannel.
deadbeef953c2ce2017-01-09 14:53:41 -0800144 void DestroyRtpDataChannel(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // Indicates whether any channels exist.
147 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200148 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 }
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 // RTX will be enabled/disabled in engines that support it. The supporting
152 // engines will start offering an RTX codec. Must be called before Init().
153 bool SetVideoRtxEnabled(bool enable);
154
155 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 bool capturing() const { return capturing_; }
157
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 // The operations below occur on the main thread.
159
ivocd66b44d2016-01-15 03:06:36 -0800160 // Starts AEC dump using existing file, with a specified maximum file size in
161 // bytes. When the limit is reached, logging will stop and the file will be
162 // closed. If max_size_bytes is set to <= 0, no limit will be used.
163 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000164
ivoc797ef122015-10-22 03:25:41 -0700165 // Stops recording AEC dump.
166 void StopAecDump();
167
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 private:
deadbeef112b2e92017-02-10 20:13:37 -0800169 void Construct(std::unique_ptr<MediaEngineInterface> me,
170 std::unique_ptr<DataEngineInterface> dme,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200171 rtc::Thread* worker_thread,
172 rtc::Thread* network_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000173 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000174 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200176 VoiceChannel* CreateVoiceChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700177 webrtc::Call* call,
178 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800179 DtlsTransportInternal* rtp_dtls_transport,
180 DtlsTransportInternal* rtcp_dtls_transport,
181 rtc::PacketTransportInternal* rtp_packet_transport,
182 rtc::PacketTransportInternal* rtcp_packet_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800183 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200184 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800185 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200186 const AudioOptions& options);
187 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
188 VideoChannel* CreateVideoChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700189 webrtc::Call* call,
190 const cricket::MediaConfig& media_config,
deadbeefe814a0d2017-02-25 18:15:09 -0800191 DtlsTransportInternal* rtp_dtls_transport,
192 DtlsTransportInternal* rtcp_dtls_transport,
193 rtc::PacketTransportInternal* rtp_packet_transport,
194 rtc::PacketTransportInternal* rtcp_packet_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800195 rtc::Thread* signaling_thread,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200196 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800197 bool srtp_required,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200198 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800200 RtpDataChannel* CreateRtpDataChannel_w(
nisseeaabdf62017-05-05 02:23:02 -0700201 const cricket::MediaConfig& media_config,
zhihuangb2cdd932017-01-19 16:54:25 -0800202 DtlsTransportInternal* rtp_transport,
203 DtlsTransportInternal* rtcp_transport,
zhihuangf5b251b2017-01-12 19:37:48 -0800204 rtc::Thread* signaling_thread,
zhihuangebbe4f22016-12-06 10:45:42 -0800205 const std::string& content_name,
deadbeef953c2ce2017-01-09 14:53:41 -0800206 bool srtp_required);
207 void DestroyRtpDataChannel_w(RtpDataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
kwiberg31022942016-03-11 14:18:21 -0800209 std::unique_ptr<MediaEngineInterface> media_engine_;
210 std::unique_ptr<DataEngineInterface> data_media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000212 rtc::Thread* main_thread_;
213 rtc::Thread* worker_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200214 rtc::Thread* network_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
Steve Anton774115c2017-08-30 10:48:46 -0700216 std::vector<std::unique_ptr<VoiceChannel>> voice_channels_;
217 std::vector<std::unique_ptr<VideoChannel>> video_channels_;
218 std::vector<std::unique_ptr<RtpDataChannel>> data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 bool enable_rtx_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222};
223
224} // namespace cricket
225
Perfb45d172016-02-29 12:07:35 +0100226#endif // WEBRTC_PC_CHANNELMANAGER_H_