• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "talk/session/media/voicechannel.h"
37 #include "webrtc/base/criticalsection.h"
38 #include "webrtc/base/fileutils.h"
39 #include "webrtc/base/sigslotrepeater.h"
40 #include "webrtc/base/thread.h"
41 
42 namespace webrtc {
43 class MediaControllerInterface;
44 }
45 namespace cricket {
46 
47 class VoiceChannel;
48 
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.
57 class ChannelManager : public rtc::MessageHandler,
58                        public sigslot::has_slots<> {
59  public:
60   // 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,
65                  CaptureManager* cm,
66                  rtc::Thread* worker);
67   // Same as above, but gives an easier default DataEngine.
68   ChannelManager(MediaEngineInterface* me,
69                  rtc::Thread* worker);
70   ~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.
worker_thread()74   rtc::Thread* worker_thread() const { return worker_thread_; }
set_worker_thread(rtc::Thread * thread)75   bool set_worker_thread(rtc::Thread* thread) {
76     if (initialized_) return false;
77     worker_thread_ = thread;
78     return true;
79   }
80 
media_engine()81   MediaEngineInterface* media_engine() { return media_engine_.get(); }
82 
83   // 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.
initialized()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.
99   // Creates a voice channel, to be associated with the specified session.
100   VoiceChannel* CreateVoiceChannel(
101       webrtc::MediaControllerInterface* media_controller,
102       TransportController* transport_controller,
103       const std::string& content_name,
104       bool rtcp,
105       const AudioOptions& options);
106   // Destroys a voice channel created with the Create API.
107   void DestroyVoiceChannel(VoiceChannel* voice_channel);
108   // Creates a video channel, synced with the specified voice channel, and
109   // associated with the specified session.
110   VideoChannel* CreateVideoChannel(
111       webrtc::MediaControllerInterface* media_controller,
112       TransportController* transport_controller,
113       const std::string& content_name,
114       bool rtcp,
115       const VideoOptions& options);
116   // Destroys a video channel created with the Create API.
117   void DestroyVideoChannel(VideoChannel* video_channel);
118   DataChannel* CreateDataChannel(TransportController* transport_controller,
119                                  const std::string& content_name,
120                                  bool rtcp,
121                                  DataChannelType data_channel_type);
122   // Destroys a data channel created with the Create API.
123   void DestroyDataChannel(DataChannel* data_channel);
124 
125   // Indicates whether any channels exist.
has_channels()126   bool has_channels() const {
127     return (!voice_channels_.empty() || !video_channels_.empty());
128   }
129 
130   bool GetOutputVolume(int* level);
131   bool SetOutputVolume(int level);
132   // 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.
capturing()137   bool capturing() const { return capturing_; }
138 
139   // Gets capturer's supported formats in a thread safe manner
140   std::vector<cricket::VideoFormat> GetSupportedFormats(
141       VideoCapturer* capturer) const;
142   // 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 
165   // Starts AEC dump using existing file.
166   bool StartAecDump(rtc::PlatformFile file);
167 
168   // Stops recording AEC dump.
169   void StopAecDump();
170 
171   // Starts RtcEventLog using existing file.
172   bool StartRtcEventLog(rtc::PlatformFile file);
173 
174   // Stops logging RtcEventLog.
175   void StopRtcEventLog();
176 
177   sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
178 
179  private:
180   typedef std::vector<VoiceChannel*> VoiceChannels;
181   typedef std::vector<VideoChannel*> VideoChannels;
182   typedef std::vector<DataChannel*> DataChannels;
183 
184   void Construct(MediaEngineInterface* me,
185                  DataEngineInterface* dme,
186                  CaptureManager* cm,
187                  rtc::Thread* worker_thread);
188   bool InitMediaEngine_w();
189   void DestructorDeletes_w();
190   void Terminate_w();
191   VoiceChannel* CreateVoiceChannel_w(
192       webrtc::MediaControllerInterface* media_controller,
193       TransportController* transport_controller,
194       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,
200       TransportController* transport_controller,
201       const std::string& content_name,
202       bool rtcp,
203       const VideoOptions& options);
204   void DestroyVideoChannel_w(VideoChannel* video_channel);
205   DataChannel* CreateDataChannel_w(TransportController* transport_controller,
206                                    const std::string& content_name,
207                                    bool rtcp,
208                                    DataChannelType data_channel_type);
209   void DestroyDataChannel_w(DataChannel* data_channel);
210   void OnVideoCaptureStateChange(VideoCapturer* capturer,
211                                  CaptureState result);
212   void GetSupportedFormats_w(
213       VideoCapturer* capturer,
214       std::vector<cricket::VideoFormat>* out_formats) const;
215   bool IsScreencastRunning_w() const;
216   virtual void OnMessage(rtc::Message *message);
217 
218   rtc::scoped_ptr<MediaEngineInterface> media_engine_;
219   rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
220   rtc::scoped_ptr<CaptureManager> capture_manager_;
221   bool initialized_;
222   rtc::Thread* main_thread_;
223   rtc::Thread* worker_thread_;
224 
225   VoiceChannels voice_channels_;
226   VideoChannels video_channels_;
227   DataChannels data_channels_;
228 
229   int audio_output_volume_;
230   VideoRenderer* local_renderer_;
231   bool enable_rtx_;
232 
233   bool capturing_;
234 };
235 
236 }  // namespace cricket
237 
238 #endif  // TALK_SESSION_MEDIA_CHANNELMANAGER_H_
239