Home | History | Annotate | Download | only in phone
      1 /*
      2  * libjingle
      3  * Copyright 2004--2008, 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_PHONE_CHANNELMANAGER_H_
     29 #define TALK_SESSION_PHONE_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/p2p/base/session.h"
     38 #include "talk/session/phone/voicechannel.h"
     39 #include "talk/session/phone/mediaengine.h"
     40 #include "talk/session/phone/devicemanager.h"
     41 
     42 namespace cricket {
     43 
     44 class Soundclip;
     45 class VoiceChannel;
     46 
     47 // ChannelManager allows the MediaEngine to run on a separate thread, and takes
     48 // care of marshalling calls between threads. It also creates and keeps track of
     49 // voice and video channels; by doing so, it can temporarily pause all the
     50 // channels when a new audio or video device is chosen. The voice and video
     51 // channels are stored in separate vectors, to easily allow operations on just
     52 // voice or just video channels.
     53 // ChannelManager also allows the application to discover what devices it has
     54 // using device manager.
     55 class ChannelManager : public talk_base::MessageHandler,
     56                        public sigslot::has_slots<> {
     57  public:
     58   // Creates the channel manager, and specifies the worker thread to use.
     59   explicit ChannelManager(talk_base::Thread* worker);
     60   // For testing purposes. Allows the media engine and dev manager to be mocks.
     61   // The ChannelManager takes ownership of these objects.
     62   ChannelManager(MediaEngine* me, DeviceManager* dm, talk_base::Thread* worker);
     63   ~ChannelManager();
     64 
     65   // Accessors for the worker thread, allowing it to be set after construction,
     66   // but before Init. set_worker_thread will return false if called after Init.
     67   talk_base::Thread* worker_thread() const { return worker_thread_; }
     68   bool set_worker_thread(talk_base::Thread* thread) {
     69     if (initialized_) return false;
     70     worker_thread_ = thread;
     71     return true;
     72   }
     73 
     74   // Gets capabilities. Can be called prior to starting the media engine.
     75   int GetCapabilities();
     76 
     77   // Retrieves the list of supported audio & video codec types.
     78   // Can be called before starting the media engine.
     79   void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
     80   void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
     81 
     82   // Determines if a specific audio or video codec is supported.
     83   // Can be called before starting the media engine.
     84   bool FindAudioCodec(const AudioCodec& codec) const {
     85     return media_engine_->FindAudioCodec(codec);
     86   }
     87   bool FindVideoCodec(const VideoCodec& video_codec) const {
     88     return media_engine_->FindVideoCodec(video_codec);
     89   }
     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   // TODO: Remove this temporary API once Flute is updated.
     96   bool Init(talk_base::Thread* thread) {
     97     return set_worker_thread(thread) && Init();
     98   }
     99   // Shuts down the media engine.
    100   void Terminate();
    101 
    102   // The operations below all occur on the worker thread.
    103 
    104   // Creates a voice channel, to be associated with the specified session.
    105   VoiceChannel* CreateVoiceChannel(
    106       BaseSession* session, const std::string& content_name, bool rtcp);
    107   // Destroys a voice channel created with the Create API.
    108   void DestroyVoiceChannel(VoiceChannel* voice_channel);
    109   // Creates a video channel, synced with the specified voice channel, and
    110   // associated with the specified session.
    111   VideoChannel* CreateVideoChannel(
    112       BaseSession* session, const std::string& content_name, bool rtcp,
    113       VoiceChannel* voice_channel);
    114   // Destroys a video channel created with the Create API.
    115   void DestroyVideoChannel(VideoChannel* video_channel);
    116 
    117   // Creates a soundclip.
    118   Soundclip* CreateSoundclip();
    119   // Destroys a soundclip created with the Create API.
    120   void DestroySoundclip(Soundclip* soundclip);
    121 
    122   // Indicates whether any channels exist.
    123   bool has_channels() const {
    124     return (!voice_channels_.empty() || !video_channels_.empty() ||
    125             !soundclips_.empty());
    126   }
    127 
    128   // Configures the audio and video devices.
    129   bool GetAudioOptions(std::string* wave_in_device,
    130                        std::string* wave_out_device, int* opts);
    131   bool SetAudioOptions(const std::string& wave_in_device,
    132                        const std::string& wave_out_device, int opts);
    133   bool SetOutputVolume(int level);
    134   bool GetVideoOptions(std::string* cam_device);
    135   bool SetVideoOptions(const std::string& cam_device);
    136   bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
    137 
    138   // Starts/stops the local microphone and enables polling of the input level.
    139   bool SetLocalMonitor(bool enable);
    140   bool monitoring() const { return monitoring_; }
    141   // Sets the local renderer where to renderer the local camera.
    142   bool SetLocalRenderer(VideoRenderer* renderer);
    143   // Starts and stops the local camera and renders it to the local renderer.
    144   CaptureResult SetVideoCapture(bool capture);
    145   bool capturing() const { return capturing_; }
    146 
    147   // Configures the logging output of the mediaengine(s).
    148   void SetVoiceLogging(int level, const char* filter);
    149   void SetVideoLogging(int level, const char* filter);
    150 
    151   // The operations below occur on the main thread.
    152 
    153   bool GetAudioInputDevices(std::vector<std::string>* names);
    154   bool GetAudioOutputDevices(std::vector<std::string>* names);
    155   bool GetVideoCaptureDevices(std::vector<std::string>* names);
    156   sigslot::repeater0<> SignalDevicesChange;
    157   sigslot::signal1<CaptureResult> SignalVideoCaptureResult;
    158 
    159  private:
    160   typedef std::vector<VoiceChannel*> VoiceChannels;
    161   typedef std::vector<VideoChannel*> VideoChannels;
    162   typedef std::vector<Soundclip*> Soundclips;
    163 
    164   void Construct();
    165   bool Send(uint32 id, talk_base::MessageData* pdata);
    166   VoiceChannel* CreateVoiceChannel_w(
    167       BaseSession* session, const std::string& content_name, bool rtcp);
    168   void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
    169   VideoChannel* CreateVideoChannel_w(
    170       BaseSession* session, const std::string& content_name, bool rtcp,
    171       VoiceChannel* voice_channel);
    172   void DestroyVideoChannel_w(VideoChannel* video_channel);
    173   Soundclip* CreateSoundclip_w();
    174   void DestroySoundclip_w(Soundclip* soundclip);
    175   bool SetAudioOptions_w(int opts, const Device* in_dev,
    176                          const Device* out_dev);
    177   bool SetOutputVolume_w(int level);
    178   bool SetLocalMonitor_w(bool enable);
    179   bool SetVideoOptions_w(const Device* cam_device);
    180   bool SetDefaultVideoEncoderConfig_w(const VideoEncoderConfig& config);
    181   bool SetLocalRenderer_w(VideoRenderer* renderer);
    182   CaptureResult SetVideoCapture_w(bool capture);
    183   void SetMediaLogging(bool video, int level, const char* filter);
    184   void SetMediaLogging_w(bool video, int level, const char* filter);
    185   void OnVideoCaptureResult(CaptureResult result);
    186   void OnMessage(talk_base::Message *message);
    187 
    188   talk_base::CriticalSection crit_;
    189   talk_base::scoped_ptr<MediaEngine> media_engine_;
    190   talk_base::scoped_ptr<DeviceManager> device_manager_;
    191   bool initialized_;
    192   talk_base::Thread* main_thread_;
    193   talk_base::Thread* worker_thread_;
    194 
    195   VoiceChannels voice_channels_;
    196   VideoChannels video_channels_;
    197   Soundclips soundclips_;
    198 
    199   std::string audio_in_device_;
    200   std::string audio_out_device_;
    201   int audio_options_;
    202   std::string camera_device_;
    203   VideoEncoderConfig default_video_encoder_config_;
    204   VideoRenderer* local_renderer_;
    205 
    206   bool capturing_;
    207   bool monitoring_;
    208 };
    209 
    210 }  // namespace cricket
    211 
    212 #endif  // TALK_SESSION_PHONE_CHANNELMANAGER_H_
    213