blob: bd1bc2f46985ecd37794507a3ddf08c78aaebdce [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
11#ifndef TALK_SESSION_MEDIA_CHANNELMANAGER_H_
12#define TALK_SESSION_MEDIA_CHANNELMANAGER_H_
13
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);
perkj74622e02016-02-26 02:54:38 -0800132 // When muting, produce black frames then pause the camera.
133 // When unmuting, start the camera. Camera starts unmuted.
134 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 bool StopVideoCapture(VideoCapturer* video_capturer,
136 const VideoFormat& video_format);
137 bool RestartVideoCapture(VideoCapturer* video_capturer,
138 const VideoFormat& previous_format,
139 const VideoFormat& desired_format,
140 CaptureManager::RestartOptions options);
141
nissee73afba2016-01-28 04:47:08 -0800142 virtual void AddVideoSink(VideoCapturer* video_capturer,
143 rtc::VideoSinkInterface<VideoFrame>* sink);
144 virtual void RemoveVideoSink(VideoCapturer* video_capturer,
145 rtc::VideoSinkInterface<VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 bool IsScreencastRunning() const;
147
148 // The operations below occur on the main thread.
149
ivocd66b44d2016-01-15 03:06:36 -0800150 // Starts AEC dump using existing file, with a specified maximum file size in
151 // bytes. When the limit is reached, logging will stop and the file will be
152 // closed. If max_size_bytes is set to <= 0, no limit will be used.
153 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000154
ivoc797ef122015-10-22 03:25:41 -0700155 // Stops recording AEC dump.
156 void StopAecDump();
157
ivoc112a3d82015-10-16 02:22:18 -0700158 // Starts RtcEventLog using existing file.
159 bool StartRtcEventLog(rtc::PlatformFile file);
160
161 // Stops logging RtcEventLog.
162 void StopRtcEventLog();
163
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 private:
167 typedef std::vector<VoiceChannel*> VoiceChannels;
168 typedef std::vector<VideoChannel*> VideoChannels;
169 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
171 void Construct(MediaEngineInterface* me,
172 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000175 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000176 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200178 VoiceChannel* CreateVoiceChannel_w(
179 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700180 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200181 const std::string& content_name,
182 bool rtcp,
183 const AudioOptions& options);
184 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
185 VideoChannel* CreateVideoChannel_w(
186 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700187 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200188 const std::string& content_name,
189 bool rtcp,
190 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700192 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
193 const std::string& content_name,
194 bool rtcp,
195 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 void DestroyDataChannel_w(DataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 void OnVideoCaptureStateChange(VideoCapturer* capturer,
198 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000199 void GetSupportedFormats_w(
200 VideoCapturer* capturer,
201 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000203 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000205 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
206 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000207 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000209 rtc::Thread* main_thread_;
210 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
212 VoiceChannels voice_channels_;
213 VideoChannels video_channels_;
214 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 int audio_output_volume_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 bool enable_rtx_;
218
219 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220};
221
222} // namespace cricket
223
224#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_