Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / session / media / channelmanager.h
1 /*
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
34 #include "talk/media/base/capturemanager.h"
35 #include "talk/media/base/mediaengine.h"
36 #include "webrtc/p2p/base/session.h"
37 #include "talk/session/media/voicechannel.h"
38 #include "webrtc/base/criticalsection.h"
39 #include "webrtc/base/fileutils.h"
40 #include "webrtc/base/sigslotrepeater.h"
41 #include "webrtc/base/thread.h"
42
43 namespace cricket {
44
45 const int kDefaultAudioDelayOffset = 0;
46
47 class Soundclip;
48 class VideoProcessor;
49 class VoiceChannel;
50 class VoiceProcessor;
51
52 // ChannelManager allows the MediaEngine to run on a separate thread, and takes
53 // care of marshalling calls between threads. It also creates and keeps track of
54 // voice and video channels; by doing so, it can temporarily pause all the
55 // channels when a new audio or video device is chosen. The voice and video
56 // channels are stored in separate vectors, to easily allow operations on just
57 // voice or just video channels.
58 // ChannelManager also allows the application to discover what devices it has
59 // using device manager.
60 class ChannelManager : public rtc::MessageHandler,
61                        public sigslot::has_slots<> {
62  public:
63 #if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
64   // Creates the channel manager, and specifies the worker thread to use.
65   explicit ChannelManager(rtc::Thread* worker);
66 #endif
67
68   // For testing purposes. Allows the media engine and data media
69   // engine and dev manager to be mocks.  The ChannelManager takes
70   // ownership of these objects.
71   ChannelManager(MediaEngineInterface* me,
72                  DataEngineInterface* dme,
73                  DeviceManagerInterface* dm,
74                  CaptureManager* cm,
75                  rtc::Thread* worker);
76   // Same as above, but gives an easier default DataEngine.
77   ChannelManager(MediaEngineInterface* me,
78                  DeviceManagerInterface* dm,
79                  rtc::Thread* worker);
80   ~ChannelManager();
81
82   // Accessors for the worker thread, allowing it to be set after construction,
83   // but before Init. set_worker_thread will return false if called after Init.
84   rtc::Thread* worker_thread() const { return worker_thread_; }
85   bool set_worker_thread(rtc::Thread* thread) {
86     if (initialized_) return false;
87     worker_thread_ = thread;
88     return true;
89   }
90
91   // Gets capabilities. Can be called prior to starting the media engine.
92   int GetCapabilities();
93
94   // Retrieves the list of supported audio & video codec types.
95   // Can be called before starting the media engine.
96   void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
97   void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
98   void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
99   void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
100   void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
101
102   // Indicates whether the media engine is started.
103   bool initialized() const { return initialized_; }
104   // Starts up the media engine.
105   bool Init();
106   // Shuts down the media engine.
107   void Terminate();
108
109   // The operations below all occur on the worker thread.
110
111   // Creates a voice channel, to be associated with the specified session.
112   VoiceChannel* CreateVoiceChannel(
113       BaseSession* session, const std::string& content_name, bool rtcp);
114   // Destroys a voice channel created with the Create API.
115   void DestroyVoiceChannel(VoiceChannel* voice_channel);
116   // TODO(pbos): Remove as soon as all call sites specify VideoOptions.
117   VideoChannel* CreateVideoChannel(BaseSession* session,
118                                    const std::string& content_name,
119                                    bool rtcp,
120                                    VoiceChannel* voice_channel);
121   // Creates a video channel, synced with the specified voice channel, and
122   // associated with the specified session.
123   VideoChannel* CreateVideoChannel(BaseSession* session,
124                                    const std::string& content_name,
125                                    bool rtcp,
126                                    const VideoOptions& options,
127                                    VoiceChannel* voice_channel);
128   // Destroys a video channel created with the Create API.
129   void DestroyVideoChannel(VideoChannel* video_channel);
130   DataChannel* CreateDataChannel(
131       BaseSession* session, const std::string& content_name,
132       bool rtcp, DataChannelType data_channel_type);
133   // Destroys a data channel created with the Create API.
134   void DestroyDataChannel(DataChannel* data_channel);
135
136   // Creates a soundclip.
137   Soundclip* CreateSoundclip();
138   // Destroys a soundclip created with the Create API.
139   void DestroySoundclip(Soundclip* soundclip);
140
141   // Indicates whether any channels exist.
142   bool has_channels() const {
143     return (!voice_channels_.empty() || !video_channels_.empty() ||
144             !soundclips_.empty());
145   }
146
147   // Configures the audio and video devices. A null pointer can be passed to
148   // GetAudioOptions() for any parameter of no interest.
149   bool GetAudioOptions(std::string* wave_in_device,
150                        std::string* wave_out_device,
151                        AudioOptions* options);
152   bool SetAudioOptions(const std::string& wave_in_device,
153                        const std::string& wave_out_device,
154                        const AudioOptions& options);
155   // Sets Engine-specific audio options according to enabled experiments.
156   bool SetEngineAudioOptions(const AudioOptions& options);
157   bool GetOutputVolume(int* level);
158   bool SetOutputVolume(int level);
159   bool IsSameCapturer(const std::string& capturer_name,
160                       VideoCapturer* capturer);
161   // TODO(noahric): Nearly everything called "device" in this API is actually a
162   // device name, so this should really be GetCaptureDeviceName, and the
163   // next method should be GetCaptureDevice.
164   bool GetCaptureDevice(std::string* cam_device);
165   // Gets the current capture Device.
166   bool GetVideoCaptureDevice(Device* device);
167   // Create capturer based on what has been set in SetCaptureDevice().
168   VideoCapturer* CreateVideoCapturer();
169   // Create capturer from a screen.
170   VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
171   bool SetCaptureDevice(const std::string& cam_device);
172   bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
173   // RTX will be enabled/disabled in engines that support it. The supporting
174   // engines will start offering an RTX codec. Must be called before Init().
175   bool SetVideoRtxEnabled(bool enable);
176
177   // Starts/stops the local microphone and enables polling of the input level.
178   bool SetLocalMonitor(bool enable);
179   bool monitoring() const { return monitoring_; }
180   bool capturing() const { return capturing_; }
181
182   // Configures the logging output of the mediaengine(s).
183   void SetVoiceLogging(int level, const char* filter);
184   void SetVideoLogging(int level, const char* filter);
185
186   // The channel manager handles the Tx side for Video processing,
187   // as well as Tx and Rx side for Voice processing.
188   // (The Rx Video processing will go throug the simplerenderingmanager,
189   //  to be implemented).
190   bool RegisterVideoProcessor(VideoCapturer* capturer,
191                               VideoProcessor* processor);
192   bool UnregisterVideoProcessor(VideoCapturer* capturer,
193                                 VideoProcessor* processor);
194   bool RegisterVoiceProcessor(uint32 ssrc,
195                               VoiceProcessor* processor,
196                               MediaProcessorDirection direction);
197   bool UnregisterVoiceProcessor(uint32 ssrc,
198                                 VoiceProcessor* processor,
199                                 MediaProcessorDirection direction);
200   // The following are done in the new "CaptureManager" style that
201   // all local video capturers, processors, and managers should move to.
202   // TODO(pthatcher): Make methods nicer by having start return a handle that
203   // can be used for stop and restart, rather than needing to pass around
204   // formats a a pseudo-handle.
205   bool StartVideoCapture(VideoCapturer* video_capturer,
206                          const VideoFormat& video_format);
207   // When muting, produce black frames then pause the camera.
208   // When unmuting, start the camera. Camera starts unmuted.
209   bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
210   bool StopVideoCapture(VideoCapturer* video_capturer,
211                         const VideoFormat& video_format);
212   bool RestartVideoCapture(VideoCapturer* video_capturer,
213                            const VideoFormat& previous_format,
214                            const VideoFormat& desired_format,
215                            CaptureManager::RestartOptions options);
216
217   bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
218   bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
219   bool IsScreencastRunning() const;
220
221   // The operations below occur on the main thread.
222
223   bool GetAudioInputDevices(std::vector<std::string>* names);
224   bool GetAudioOutputDevices(std::vector<std::string>* names);
225   bool GetVideoCaptureDevices(std::vector<std::string>* names);
226   void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
227                                       const VideoFormat& max_format);
228
229   // Starts AEC dump using existing file.
230   bool StartAecDump(rtc::PlatformFile file);
231
232   sigslot::repeater0<> SignalDevicesChange;
233   sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
234
235   // Returns the current selected device. Note: Subtly different from
236   // GetCaptureDevice(). See member video_device_ for more details.
237   // This API is mainly a hook used by unittests.
238   const std::string& video_device_name() const { return video_device_name_; }
239
240   // TODO(hellner): Remove this function once the engine capturer has been
241   // removed.
242   VideoFormat GetStartCaptureFormat();
243
244  protected:
245   // Adds non-transient parameters which can only be changed through the
246   // options store.
247   bool SetAudioOptions(const std::string& wave_in_device,
248                        const std::string& wave_out_device,
249                        const AudioOptions& options,
250                        int delay_offset);
251   int audio_delay_offset() const { return audio_delay_offset_; }
252   // This is here so that ChannelManager subclasses can set the video
253   // capturer factories to use.
254   DeviceManagerInterface* device_manager() { return device_manager_.get(); }
255
256  private:
257   typedef std::vector<VoiceChannel*> VoiceChannels;
258   typedef std::vector<VideoChannel*> VideoChannels;
259   typedef std::vector<DataChannel*> DataChannels;
260   typedef std::vector<Soundclip*> Soundclips;
261
262   void Construct(MediaEngineInterface* me,
263                  DataEngineInterface* dme,
264                  DeviceManagerInterface* dm,
265                  CaptureManager* cm,
266                  rtc::Thread* worker_thread);
267   void Terminate_w();
268   VoiceChannel* CreateVoiceChannel_w(
269       BaseSession* session, const std::string& content_name, bool rtcp);
270   void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
271   VideoChannel* CreateVideoChannel_w(BaseSession* session,
272                                      const std::string& content_name,
273                                      bool rtcp,
274                                      const VideoOptions& options,
275                                      VoiceChannel* voice_channel);
276   void DestroyVideoChannel_w(VideoChannel* video_channel);
277   DataChannel* CreateDataChannel_w(
278       BaseSession* session, const std::string& content_name,
279       bool rtcp, DataChannelType data_channel_type);
280   void DestroyDataChannel_w(DataChannel* data_channel);
281   Soundclip* CreateSoundclip_w();
282   void DestroySoundclip_w(Soundclip* soundclip);
283   bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
284                          const Device* in_dev, const Device* out_dev);
285   bool SetEngineAudioOptions_w(const AudioOptions& options);
286   bool SetCaptureDevice_w(const Device* cam_device);
287   void OnVideoCaptureStateChange(VideoCapturer* capturer,
288                                  CaptureState result);
289   bool RegisterVideoProcessor_w(VideoCapturer* capturer,
290                                 VideoProcessor* processor);
291   bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
292                                   VideoProcessor* processor);
293   bool IsScreencastRunning_w() const;
294   virtual void OnMessage(rtc::Message *message);
295
296   rtc::scoped_ptr<MediaEngineInterface> media_engine_;
297   rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
298   rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
299   rtc::scoped_ptr<CaptureManager> capture_manager_;
300   bool initialized_;
301   rtc::Thread* main_thread_;
302   rtc::Thread* worker_thread_;
303
304   VoiceChannels voice_channels_;
305   VideoChannels video_channels_;
306   DataChannels data_channels_;
307   Soundclips soundclips_;
308
309   std::string audio_in_device_;
310   std::string audio_out_device_;
311   AudioOptions audio_options_;
312   int audio_delay_offset_;
313   int audio_output_volume_;
314   std::string camera_device_;
315   VideoEncoderConfig default_video_encoder_config_;
316   VideoRenderer* local_renderer_;
317   bool enable_rtx_;
318
319   bool capturing_;
320   bool monitoring_;
321
322   // String containing currently set device. Note that this string is subtly
323   // different from camera_device_. E.g. camera_device_ will list unplugged
324   // but selected devices while this sting will be empty or contain current
325   // selected device.
326   // TODO(hellner): refactor the code such that there is no need to keep two
327   // strings for video devices that have subtle differences in behavior.
328   std::string video_device_name_;
329 };
330
331 }  // namespace cricket
332
333 #endif  // TALK_SESSION_MEDIA_CHANNELMANAGER_H_