blob: 2bc516bfaa36edfdb075795be10cec74583d1a7b [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/media/base/capturemanager.h"
35#include "talk/media/base/mediaengine.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000036#include "talk/session/media/voicechannel.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000037#include "webrtc/base/criticalsection.h"
38#include "webrtc/base/fileutils.h"
39#include "webrtc/base/sigslotrepeater.h"
40#include "webrtc/base/thread.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
159 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
160 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
161 bool IsScreencastRunning() const;
162
163 // The operations below occur on the main thread.
164
ivoca4df27b2015-12-19 10:14:10 -0800165 // Starts AEC dump using existing file.
166 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000167
ivoc797ef122015-10-22 03:25:41 -0700168 // Stops recording AEC dump.
169 void StopAecDump();
170
ivoc112a3d82015-10-16 02:22:18 -0700171 // Starts RtcEventLog using existing file.
172 bool StartRtcEventLog(rtc::PlatformFile file);
173
174 // Stops logging RtcEventLog.
175 void StopRtcEventLog();
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
178
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 private:
180 typedef std::vector<VoiceChannel*> VoiceChannels;
181 typedef std::vector<VideoChannel*> VideoChannels;
182 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
184 void Construct(MediaEngineInterface* me,
185 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000188 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000189 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200191 VoiceChannel* CreateVoiceChannel_w(
192 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700193 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200194 const std::string& content_name,
195 bool rtcp,
196 const AudioOptions& options);
197 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
198 VideoChannel* CreateVideoChannel_w(
199 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700200 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200201 const std::string& content_name,
202 bool rtcp,
203 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700205 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
206 const std::string& content_name,
207 bool rtcp,
208 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 void DestroyDataChannel_w(DataChannel* data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 void OnVideoCaptureStateChange(VideoCapturer* capturer,
211 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000212 void GetSupportedFormats_w(
213 VideoCapturer* capturer,
214 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000216 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000218 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
219 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222 rtc::Thread* main_thread_;
223 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224
225 VoiceChannels voice_channels_;
226 VideoChannels video_channels_;
227 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 int audio_output_volume_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 VideoRenderer* local_renderer_;
231 bool enable_rtx_;
232
233 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234};
235
236} // namespace cricket
237
238#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_