blob: 9862fe03244c9adf62c24ac4068af8eb230c9b14 [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
14#include <string>
15#include <vector>
16
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/criticalsection.h"
18#include "webrtc/base/fileutils.h"
19#include "webrtc/base/sigslotrepeater.h"
20#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/media/base/capturemanager.h"
22#include "webrtc/media/base/mediaengine.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010023#include "webrtc/pc/voicechannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
Fredrik Solenberg709ed672015-09-15 12:26:33 +020025namespace webrtc {
26class MediaControllerInterface;
27}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028namespace cricket {
29
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32// ChannelManager allows the MediaEngine to run on a separate thread, and takes
33// care of marshalling calls between threads. It also creates and keeps track of
34// voice and video channels; by doing so, it can temporarily pause all the
35// channels when a new audio or video device is chosen. The voice and video
36// channels are stored in separate vectors, to easily allow operations on just
37// voice or just video channels.
38// ChannelManager also allows the application to discover what devices it has
39// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 public sigslot::has_slots<> {
42 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 // For testing purposes. Allows the media engine and data media
44 // engine and dev manager to be mocks. The ChannelManager takes
45 // ownership of these objects.
46 ChannelManager(MediaEngineInterface* me,
47 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 // Same as above, but gives an easier default DataEngine.
51 ChannelManager(MediaEngineInterface* me,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 ~ChannelManager();
54
55 // Accessors for the worker thread, allowing it to be set after construction,
56 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057 rtc::Thread* worker_thread() const { return worker_thread_; }
58 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 if (initialized_) return false;
60 worker_thread_ = thread;
61 return true;
62 }
63
Fredrik Solenberg709ed672015-09-15 12:26:33 +020064 MediaEngineInterface* media_engine() { return media_engine_.get(); }
65
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 // Retrieves the list of supported audio & video codec types.
67 // Can be called before starting the media engine.
68 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
69 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
70 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
71 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
72 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
73
74 // Indicates whether the media engine is started.
75 bool initialized() const { return initialized_; }
76 // Starts up the media engine.
77 bool Init();
78 // Shuts down the media engine.
79 void Terminate();
80
81 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020083 VoiceChannel* CreateVoiceChannel(
84 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -070085 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020086 const std::string& content_name,
87 bool rtcp,
88 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020090 void DestroyVoiceChannel(VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 // Creates a video channel, synced with the specified voice channel, and
92 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020093 VideoChannel* CreateVideoChannel(
94 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -070095 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +020096 const std::string& content_name,
97 bool rtcp,
98 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 // Destroys a video channel created with the Create API.
100 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700101 DataChannel* CreateDataChannel(TransportController* transport_controller,
102 const std::string& content_name,
103 bool rtcp,
104 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 // Destroys a data channel created with the Create API.
106 void DestroyDataChannel(DataChannel* data_channel);
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 // Indicates whether any channels exist.
109 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200110 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 }
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 bool GetOutputVolume(int* level);
114 bool SetOutputVolume(int level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 // RTX will be enabled/disabled in engines that support it. The supporting
116 // engines will start offering an RTX codec. Must be called before Init().
117 bool SetVideoRtxEnabled(bool enable);
118
119 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 bool capturing() const { return capturing_; }
121
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000122 // Gets capturer's supported formats in a thread safe manner
123 std::vector<cricket::VideoFormat> GetSupportedFormats(
124 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // The following are done in the new "CaptureManager" style that
126 // all local video capturers, processors, and managers should move to.
127 // TODO(pthatcher): Make methods nicer by having start return a handle that
128 // can be used for stop and restart, rather than needing to pass around
129 // formats a a pseudo-handle.
130 bool StartVideoCapture(VideoCapturer* video_capturer,
131 const VideoFormat& video_format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 bool StopVideoCapture(VideoCapturer* video_capturer,
133 const VideoFormat& video_format);
134 bool RestartVideoCapture(VideoCapturer* video_capturer,
135 const VideoFormat& previous_format,
136 const VideoFormat& desired_format,
137 CaptureManager::RestartOptions options);
138
nissee73afba2016-01-28 04:47:08 -0800139 virtual void AddVideoSink(VideoCapturer* video_capturer,
140 rtc::VideoSinkInterface<VideoFrame>* sink);
141 virtual void RemoveVideoSink(VideoCapturer* video_capturer,
142 rtc::VideoSinkInterface<VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 bool IsScreencastRunning() const;
144
145 // The operations below occur on the main thread.
146
ivocd66b44d2016-01-15 03:06:36 -0800147 // Starts AEC dump using existing file, with a specified maximum file size in
148 // bytes. When the limit is reached, logging will stop and the file will be
149 // closed. If max_size_bytes is set to <= 0, no limit will be used.
150 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000151
ivoc797ef122015-10-22 03:25:41 -0700152 // Stops recording AEC dump.
153 void StopAecDump();
154
ivoc112a3d82015-10-16 02:22:18 -0700155 // Starts RtcEventLog using existing file.
156 bool StartRtcEventLog(rtc::PlatformFile file);
157
158 // Stops logging RtcEventLog.
159 void StopRtcEventLog();
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 private:
164 typedef std::vector<VoiceChannel*> VoiceChannels;
165 typedef std::vector<VideoChannel*> VideoChannels;
166 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168 void Construct(MediaEngineInterface* me,
169 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 rtc::Thread* worker_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(
176 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700177 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200178 const std::string& content_name,
179 bool rtcp,
180 const AudioOptions& options);
181 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
182 VideoChannel* CreateVideoChannel_w(
183 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700184 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200185 const std::string& content_name,
186 bool rtcp,
187 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700189 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
190 const std::string& content_name,
191 bool rtcp,
192 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 void DestroyDataChannel_w(DataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 void OnVideoCaptureStateChange(VideoCapturer* capturer,
195 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000196 void GetSupportedFormats_w(
197 VideoCapturer* capturer,
198 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000202 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
203 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 rtc::Thread* main_thread_;
207 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 VoiceChannels voice_channels_;
210 VideoChannels video_channels_;
211 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 int audio_output_volume_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 bool enable_rtx_;
215
216 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217};
218
219} // namespace cricket
220
Perfb45d172016-02-29 12:07:35 +0100221#endif // WEBRTC_PC_CHANNELMANAGER_H_