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