Home | History | Annotate | Download | only in media
      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/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   // Creates a video channel, synced with the specified voice channel, and
    117   // associated with the specified session.
    118   VideoChannel* CreateVideoChannel(
    119       BaseSession* session, const std::string& content_name, bool rtcp,
    120       VoiceChannel* voice_channel);
    121   // Destroys a video channel created with the Create API.
    122   void DestroyVideoChannel(VideoChannel* video_channel);
    123   DataChannel* CreateDataChannel(
    124       BaseSession* session, const std::string& content_name,
    125       bool rtcp, DataChannelType data_channel_type);
    126   // Destroys a data channel created with the Create API.
    127   void DestroyDataChannel(DataChannel* data_channel);
    128 
    129   // Creates a soundclip.
    130   Soundclip* CreateSoundclip();
    131   // Destroys a soundclip created with the Create API.
    132   void DestroySoundclip(Soundclip* soundclip);
    133 
    134   // Indicates whether any channels exist.
    135   bool has_channels() const {
    136     return (!voice_channels_.empty() || !video_channels_.empty() ||
    137             !soundclips_.empty());
    138   }
    139 
    140   // Configures the audio and video devices. A null pointer can be passed to
    141   // GetAudioOptions() for any parameter of no interest.
    142   bool GetAudioOptions(std::string* wave_in_device,
    143                        std::string* wave_out_device,
    144                        AudioOptions* options);
    145   bool SetAudioOptions(const std::string& wave_in_device,
    146                        const std::string& wave_out_device,
    147                        const AudioOptions& options);
    148   // Sets Engine-specific audio options according to enabled experiments.
    149   bool SetEngineAudioOptions(const AudioOptions& options);
    150   bool GetOutputVolume(int* level);
    151   bool SetOutputVolume(int level);
    152   bool IsSameCapturer(const std::string& capturer_name,
    153                       VideoCapturer* capturer);
    154   // TODO(noahric): Nearly everything called "device" in this API is actually a
    155   // device name, so this should really be GetCaptureDeviceName, and the
    156   // next method should be GetCaptureDevice.
    157   bool GetCaptureDevice(std::string* cam_device);
    158   // Gets the current capture Device.
    159   bool GetVideoCaptureDevice(Device* device);
    160   // Create capturer based on what has been set in SetCaptureDevice().
    161   VideoCapturer* CreateVideoCapturer();
    162   // Create capturer from a screen.
    163   VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
    164   bool SetCaptureDevice(const std::string& cam_device);
    165   bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
    166   // RTX will be enabled/disabled in engines that support it. The supporting
    167   // engines will start offering an RTX codec. Must be called before Init().
    168   bool SetVideoRtxEnabled(bool enable);
    169 
    170   // Starts/stops the local microphone and enables polling of the input level.
    171   bool SetLocalMonitor(bool enable);
    172   bool monitoring() const { return monitoring_; }
    173   bool capturing() const { return capturing_; }
    174 
    175   // Configures the logging output of the mediaengine(s).
    176   void SetVoiceLogging(int level, const char* filter);
    177   void SetVideoLogging(int level, const char* filter);
    178 
    179   // The channel manager handles the Tx side for Video processing,
    180   // as well as Tx and Rx side for Voice processing.
    181   // (The Rx Video processing will go throug the simplerenderingmanager,
    182   //  to be implemented).
    183   bool RegisterVideoProcessor(VideoCapturer* capturer,
    184                               VideoProcessor* processor);
    185   bool UnregisterVideoProcessor(VideoCapturer* capturer,
    186                                 VideoProcessor* processor);
    187   bool RegisterVoiceProcessor(uint32 ssrc,
    188                               VoiceProcessor* processor,
    189                               MediaProcessorDirection direction);
    190   bool UnregisterVoiceProcessor(uint32 ssrc,
    191                                 VoiceProcessor* processor,
    192                                 MediaProcessorDirection direction);
    193   // The following are done in the new "CaptureManager" style that
    194   // all local video capturers, processors, and managers should move to.
    195   // TODO(pthatcher): Make methods nicer by having start return a handle that
    196   // can be used for stop and restart, rather than needing to pass around
    197   // formats a a pseudo-handle.
    198   bool StartVideoCapture(VideoCapturer* video_capturer,
    199                          const VideoFormat& video_format);
    200   // When muting, produce black frames then pause the camera.
    201   // When unmuting, start the camera. Camera starts unmuted.
    202   bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
    203   bool StopVideoCapture(VideoCapturer* video_capturer,
    204                         const VideoFormat& video_format);
    205   bool RestartVideoCapture(VideoCapturer* video_capturer,
    206                            const VideoFormat& previous_format,
    207                            const VideoFormat& desired_format,
    208                            CaptureManager::RestartOptions options);
    209 
    210   bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
    211   bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
    212   bool IsScreencastRunning() const;
    213 
    214   // The operations below occur on the main thread.
    215 
    216   bool GetAudioInputDevices(std::vector<std::string>* names);
    217   bool GetAudioOutputDevices(std::vector<std::string>* names);
    218   bool GetVideoCaptureDevices(std::vector<std::string>* names);
    219   void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
    220                                       const VideoFormat& max_format);
    221 
    222   // Starts AEC dump using existing file.
    223   bool StartAecDump(rtc::PlatformFile file);
    224 
    225   sigslot::repeater0<> SignalDevicesChange;
    226   sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
    227 
    228   // Returns the current selected device. Note: Subtly different from
    229   // GetCaptureDevice(). See member video_device_ for more details.
    230   // This API is mainly a hook used by unittests.
    231   const std::string& video_device_name() const { return video_device_name_; }
    232 
    233   // TODO(hellner): Remove this function once the engine capturer has been
    234   // removed.
    235   VideoFormat GetStartCaptureFormat();
    236 
    237  protected:
    238   // Adds non-transient parameters which can only be changed through the
    239   // options store.
    240   bool SetAudioOptions(const std::string& wave_in_device,
    241                        const std::string& wave_out_device,
    242                        const AudioOptions& options,
    243                        int delay_offset);
    244   int audio_delay_offset() const { return audio_delay_offset_; }
    245   // This is here so that ChannelManager subclasses can set the video
    246   // capturer factories to use.
    247   DeviceManagerInterface* device_manager() { return device_manager_.get(); }
    248 
    249  private:
    250   typedef std::vector<VoiceChannel*> VoiceChannels;
    251   typedef std::vector<VideoChannel*> VideoChannels;
    252   typedef std::vector<DataChannel*> DataChannels;
    253   typedef std::vector<Soundclip*> Soundclips;
    254 
    255   void Construct(MediaEngineInterface* me,
    256                  DataEngineInterface* dme,
    257                  DeviceManagerInterface* dm,
    258                  CaptureManager* cm,
    259                  rtc::Thread* worker_thread);
    260   void Terminate_w();
    261   VoiceChannel* CreateVoiceChannel_w(
    262       BaseSession* session, const std::string& content_name, bool rtcp);
    263   void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
    264   VideoChannel* CreateVideoChannel_w(
    265       BaseSession* session, const std::string& content_name, bool rtcp,
    266       VoiceChannel* voice_channel);
    267   void DestroyVideoChannel_w(VideoChannel* video_channel);
    268   DataChannel* CreateDataChannel_w(
    269       BaseSession* session, const std::string& content_name,
    270       bool rtcp, DataChannelType data_channel_type);
    271   void DestroyDataChannel_w(DataChannel* data_channel);
    272   Soundclip* CreateSoundclip_w();
    273   void DestroySoundclip_w(Soundclip* soundclip);
    274   bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
    275                          const Device* in_dev, const Device* out_dev);
    276   bool SetEngineAudioOptions_w(const AudioOptions& options);
    277   bool SetCaptureDevice_w(const Device* cam_device);
    278   void OnVideoCaptureStateChange(VideoCapturer* capturer,
    279                                  CaptureState result);
    280   bool RegisterVideoProcessor_w(VideoCapturer* capturer,
    281                                 VideoProcessor* processor);
    282   bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
    283                                   VideoProcessor* processor);
    284   bool IsScreencastRunning_w() const;
    285   virtual void OnMessage(rtc::Message *message);
    286 
    287   rtc::scoped_ptr<MediaEngineInterface> media_engine_;
    288   rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
    289   rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
    290   rtc::scoped_ptr<CaptureManager> capture_manager_;
    291   bool initialized_;
    292   rtc::Thread* main_thread_;
    293   rtc::Thread* worker_thread_;
    294 
    295   VoiceChannels voice_channels_;
    296   VideoChannels video_channels_;
    297   DataChannels data_channels_;
    298   Soundclips soundclips_;
    299 
    300   std::string audio_in_device_;
    301   std::string audio_out_device_;
    302   AudioOptions audio_options_;
    303   int audio_delay_offset_;
    304   int audio_output_volume_;
    305   std::string camera_device_;
    306   VideoEncoderConfig default_video_encoder_config_;
    307   VideoRenderer* local_renderer_;
    308   bool enable_rtx_;
    309 
    310   bool capturing_;
    311   bool monitoring_;
    312 
    313   // String containing currently set device. Note that this string is subtly
    314   // different from camera_device_. E.g. camera_device_ will list unplugged
    315   // but selected devices while this sting will be empty or contain current
    316   // selected device.
    317   // TODO(hellner): refactor the code such that there is no need to keep two
    318   // strings for video devices that have subtle differences in behavior.
    319   std::string video_device_name_;
    320 };
    321 
    322 }  // namespace cricket
    323 
    324 #endif  // TALK_SESSION_MEDIA_CHANNELMANAGER_H_
    325