blob: de8555208fc03376a476a22abb990c9109580338 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_SESSION_MEDIA_CHANNELMANAGER_H_
29#define TALK_SESSION_MEDIA_CHANNELMANAGER_H_
30
31#include <string>
32#include <vector>
33
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/voicechannel.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000035#include "webrtc/base/criticalsection.h"
36#include "webrtc/base/fileutils.h"
37#include "webrtc/base/sigslotrepeater.h"
38#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/media/base/capturemanager.h"
40#include "webrtc/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
Fredrik Solenberg709ed672015-09-15 12:26:33 +020042namespace webrtc {
43class MediaControllerInterface;
44}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045namespace cricket {
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49// ChannelManager allows the MediaEngine to run on a separate thread, and takes
50// care of marshalling calls between threads. It also creates and keeps track of
51// voice and video channels; by doing so, it can temporarily pause all the
52// channels when a new audio or video device is chosen. The voice and video
53// channels are stored in separate vectors, to easily allow operations on just
54// voice or just video channels.
55// ChannelManager also allows the application to discover what devices it has
56// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 public sigslot::has_slots<> {
59 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 // For testing purposes. Allows the media engine and data media
61 // engine and dev manager to be mocks. The ChannelManager takes
62 // ownership of these objects.
63 ChannelManager(MediaEngineInterface* me,
64 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 // Same as above, but gives an easier default DataEngine.
68 ChannelManager(MediaEngineInterface* me,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 ~ChannelManager();
71
72 // Accessors for the worker thread, allowing it to be set after construction,
73 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::Thread* worker_thread() const { return worker_thread_; }
75 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 if (initialized_) return false;
77 worker_thread_ = thread;
78 return true;
79 }
80
Fredrik Solenberg709ed672015-09-15 12:26:33 +020081 MediaEngineInterface* media_engine() { return media_engine_.get(); }
82
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 // Retrieves the list of supported audio & video codec types.
84 // Can be called before starting the media engine.
85 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
86 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
87 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
88 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
89 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
90
91 // Indicates whether the media engine is started.
92 bool initialized() const { return initialized_; }
93 // Starts up the media engine.
94 bool Init();
95 // Shuts down the media engine.
96 void Terminate();
97
98 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200100 VoiceChannel* CreateVoiceChannel(
101 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700102 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200103 const std::string& content_name,
104 bool rtcp,
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(
111 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700112 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200113 const std::string& content_name,
114 bool rtcp,
115 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 // Destroys a video channel created with the Create API.
117 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700118 DataChannel* CreateDataChannel(TransportController* transport_controller,
119 const std::string& content_name,
120 bool rtcp,
121 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 // Destroys a data channel created with the Create API.
123 void DestroyDataChannel(DataChannel* data_channel);
124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Indicates whether any channels exist.
126 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200127 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 bool GetOutputVolume(int* level);
131 bool SetOutputVolume(int level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // RTX will be enabled/disabled in engines that support it. The supporting
133 // engines will start offering an RTX codec. Must be called before Init().
134 bool SetVideoRtxEnabled(bool enable);
135
136 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 bool capturing() const { return capturing_; }
138
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000139 // Gets capturer's supported formats in a thread safe manner
140 std::vector<cricket::VideoFormat> GetSupportedFormats(
141 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 // The following are done in the new "CaptureManager" style that
143 // all local video capturers, processors, and managers should move to.
144 // TODO(pthatcher): Make methods nicer by having start return a handle that
145 // can be used for stop and restart, rather than needing to pass around
146 // formats a a pseudo-handle.
147 bool StartVideoCapture(VideoCapturer* video_capturer,
148 const VideoFormat& video_format);
149 // When muting, produce black frames then pause the camera.
150 // When unmuting, start the camera. Camera starts unmuted.
151 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
152 bool StopVideoCapture(VideoCapturer* video_capturer,
153 const VideoFormat& video_format);
154 bool RestartVideoCapture(VideoCapturer* video_capturer,
155 const VideoFormat& previous_format,
156 const VideoFormat& desired_format,
157 CaptureManager::RestartOptions options);
158
nissee73afba2016-01-28 04:47:08 -0800159 virtual void AddVideoSink(VideoCapturer* video_capturer,
160 rtc::VideoSinkInterface<VideoFrame>* sink);
161 virtual void RemoveVideoSink(VideoCapturer* video_capturer,
162 rtc::VideoSinkInterface<VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 bool IsScreencastRunning() const;
164
165 // The operations below occur on the main thread.
166
ivocd66b44d2016-01-15 03:06:36 -0800167 // Starts AEC dump using existing file, with a specified maximum file size in
168 // bytes. When the limit is reached, logging will stop and the file will be
169 // closed. If max_size_bytes is set to <= 0, no limit will be used.
170 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000171
ivoc797ef122015-10-22 03:25:41 -0700172 // Stops recording AEC dump.
173 void StopAecDump();
174
ivoc112a3d82015-10-16 02:22:18 -0700175 // Starts RtcEventLog using existing file.
176 bool StartRtcEventLog(rtc::PlatformFile file);
177
178 // Stops logging RtcEventLog.
179 void StopRtcEventLog();
180
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 private:
184 typedef std::vector<VoiceChannel*> VoiceChannels;
185 typedef std::vector<VideoChannel*> VideoChannels;
186 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
188 void Construct(MediaEngineInterface* me,
189 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000191 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000192 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000193 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200195 VoiceChannel* CreateVoiceChannel_w(
196 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700197 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200198 const std::string& content_name,
199 bool rtcp,
200 const AudioOptions& options);
201 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
202 VideoChannel* CreateVideoChannel_w(
203 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700204 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200205 const std::string& content_name,
206 bool rtcp,
207 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700209 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
210 const std::string& content_name,
211 bool rtcp,
212 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 void DestroyDataChannel_w(DataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 void OnVideoCaptureStateChange(VideoCapturer* capturer,
215 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000216 void GetSupportedFormats_w(
217 VideoCapturer* capturer,
218 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
223 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000224 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000226 rtc::Thread* main_thread_;
227 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 VoiceChannels voice_channels_;
230 VideoChannels video_channels_;
231 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 int audio_output_volume_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 VideoRenderer* local_renderer_;
235 bool enable_rtx_;
236
237 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238};
239
240} // namespace cricket
241
242#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_