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_CALL_H_
     29 #define TALK_SESSION_MEDIA_CALL_H_
     30 
     31 #include <deque>
     32 #include <map>
     33 #include <string>
     34 #include <vector>
     35 
     36 #include "talk/base/messagequeue.h"
     37 #include "talk/media/base/mediachannel.h"
     38 #include "talk/media/base/screencastid.h"
     39 #include "talk/media/base/streamparams.h"
     40 #include "talk/media/base/videocommon.h"
     41 #include "talk/p2p/base/session.h"
     42 #include "talk/p2p/client/socketmonitor.h"
     43 #include "talk/session/media/audiomonitor.h"
     44 #include "talk/session/media/currentspeakermonitor.h"
     45 #include "talk/session/media/mediamessages.h"
     46 #include "talk/session/media/mediasession.h"
     47 #include "talk/xmpp/jid.h"
     48 
     49 namespace cricket {
     50 
     51 struct AudioInfo;
     52 class Call;
     53 class MediaSessionClient;
     54 class BaseChannel;
     55 class VoiceChannel;
     56 class VideoChannel;
     57 class DataChannel;
     58 
     59 // Can't typedef this easily since it's forward declared as struct elsewhere.
     60 struct CallOptions : public MediaSessionOptions {
     61 };
     62 
     63 // CurrentSpeakerMonitor used to have a dependency on Call. To remove this
     64 // dependency, we create AudioSourceContext. CurrentSpeakerMonitor depends on
     65 // AudioSourceContext.
     66 // AudioSourceProxy acts as a proxy so that when SignalAudioMonitor
     67 // in Call is triggered, SignalAudioMonitor in AudioSourceContext is triggered.
     68 // Likewise, when OnMediaStreamsUpdate in Call is triggered,
     69 // OnMediaStreamsUpdate in AudioSourceContext is triggered.
     70 class AudioSourceProxy: public AudioSourceContext, public sigslot::has_slots<> {
     71  public:
     72   explicit AudioSourceProxy(Call* call);
     73 
     74  private:
     75   void OnAudioMonitor(Call* call, const AudioInfo& info);
     76   void OnMediaStreamsUpdate(Call* call, cricket::Session*,
     77       const cricket::MediaStreams&, const cricket::MediaStreams&);
     78 
     79   AudioSourceContext* audio_source_context_;
     80   Call* call_;
     81 };
     82 
     83 class Call : public talk_base::MessageHandler, public sigslot::has_slots<> {
     84  public:
     85   explicit Call(MediaSessionClient* session_client);
     86   ~Call();
     87 
     88   // |initiator| can be empty.
     89   Session* InitiateSession(const buzz::Jid& to, const buzz::Jid& initiator,
     90                            const CallOptions& options);
     91   Session* InitiateSession(const std::string& id, const buzz::Jid& to,
     92                            const CallOptions& options);
     93   void AcceptSession(Session* session, const CallOptions& options);
     94   void RejectSession(Session* session);
     95   void TerminateSession(Session* session);
     96   void Terminate();
     97   bool SendViewRequest(Session* session,
     98                        const ViewRequest& view_request);
     99   void SetLocalRenderer(VideoRenderer* renderer);
    100   void SetVideoRenderer(Session* session, uint32 ssrc,
    101                         VideoRenderer* renderer);
    102   void StartConnectionMonitor(Session* session, int cms);
    103   void StopConnectionMonitor(Session* session);
    104   void StartAudioMonitor(Session* session, int cms);
    105   void StopAudioMonitor(Session* session);
    106   bool IsAudioMonitorRunning(Session* session);
    107   void StartSpeakerMonitor(Session* session);
    108   void StopSpeakerMonitor(Session* session);
    109   void Mute(bool mute);
    110   void MuteVideo(bool mute);
    111   bool SendData(Session* session,
    112                 const SendDataParams& params,
    113                 const talk_base::Buffer& payload,
    114                 SendDataResult* result);
    115   void PressDTMF(int event);
    116   bool StartScreencast(Session* session,
    117                        const std::string& stream_name, uint32 ssrc,
    118                        const ScreencastId& screencastid, int fps);
    119   bool StopScreencast(Session* session,
    120                       const std::string& stream_name, uint32 ssrc);
    121 
    122   std::vector<Session*> sessions();
    123   uint32 id();
    124   bool has_video() const { return has_video_; }
    125   bool has_data() const { return has_data_; }
    126   bool muted() const { return muted_; }
    127   bool video() const { return has_video_; }
    128   bool secure() const;
    129   bool video_muted() const { return video_muted_; }
    130   const std::vector<StreamParams>* GetDataRecvStreams(Session* session) const {
    131     MediaStreams* recv_streams = GetMediaStreams(session);
    132     return recv_streams ? &recv_streams->data() : NULL;
    133   }
    134   const std::vector<StreamParams>* GetVideoRecvStreams(Session* session) const {
    135     MediaStreams* recv_streams = GetMediaStreams(session);
    136     return recv_streams ? &recv_streams->video() : NULL;
    137   }
    138   const std::vector<StreamParams>* GetAudioRecvStreams(Session* session) const {
    139     MediaStreams* recv_streams = GetMediaStreams(session);
    140     return recv_streams ? &recv_streams->audio() : NULL;
    141   }
    142   VoiceChannel* GetVoiceChannel(Session* session) const;
    143   VideoChannel* GetVideoChannel(Session* session) const;
    144   DataChannel* GetDataChannel(Session* session) const;
    145   // Public just for unit tests
    146   VideoContentDescription* CreateVideoStreamUpdate(const StreamParams& stream);
    147   // Takes ownership of video.
    148   void SendVideoStreamUpdate(Session* session, VideoContentDescription* video);
    149 
    150   // Setting this to false will cause the call to have a longer timeout and
    151   // for the SignalSetupToCallVoicemail to never fire.
    152   void set_send_to_voicemail(bool send_to_voicemail) {
    153     send_to_voicemail_ = send_to_voicemail;
    154   }
    155   bool send_to_voicemail() { return send_to_voicemail_; }
    156   const VoiceMediaInfo& last_voice_media_info() const {
    157     return last_voice_media_info_;
    158   }
    159 
    160   // Sets a flag on the chatapp that will redirect the call to voicemail once
    161   // the call has been terminated
    162   sigslot::signal0<> SignalSetupToCallVoicemail;
    163   sigslot::signal2<Call*, Session*> SignalAddSession;
    164   sigslot::signal2<Call*, Session*> SignalRemoveSession;
    165   sigslot::signal3<Call*, Session*, Session::State>
    166       SignalSessionState;
    167   sigslot::signal3<Call*, Session*, Session::Error>
    168       SignalSessionError;
    169   sigslot::signal3<Call*, Session*, const std::string &>
    170       SignalReceivedTerminateReason;
    171   sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
    172       SignalConnectionMonitor;
    173   sigslot::signal2<Call*, const VoiceMediaInfo&> SignalMediaMonitor;
    174   sigslot::signal2<Call*, const AudioInfo&> SignalAudioMonitor;
    175   // Empty nick on StreamParams means "unknown".
    176   // No ssrcs in StreamParams means "no current speaker".
    177   sigslot::signal3<Call*,
    178                    Session*,
    179                    const StreamParams&> SignalSpeakerMonitor;
    180   sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
    181       SignalVideoConnectionMonitor;
    182   sigslot::signal2<Call*, const VideoMediaInfo&> SignalVideoMediaMonitor;
    183   // Gives added streams and removed streams, in that order.
    184   sigslot::signal4<Call*,
    185                    Session*,
    186                    const MediaStreams&,
    187                    const MediaStreams&> SignalMediaStreamsUpdate;
    188   sigslot::signal3<Call*,
    189                    const ReceiveDataParams&,
    190                    const talk_base::Buffer&> SignalDataReceived;
    191 
    192   AudioSourceProxy* GetAudioSourceProxy();
    193 
    194  private:
    195   void OnMessage(talk_base::Message* message);
    196   void OnSessionState(BaseSession* base_session, BaseSession::State state);
    197   void OnSessionError(BaseSession* base_session, Session::Error error);
    198   void OnSessionInfoMessage(
    199       Session* session, const buzz::XmlElement* action_elem);
    200   void OnViewRequest(
    201       Session* session, const ViewRequest& view_request);
    202   void OnRemoteDescriptionUpdate(
    203       BaseSession* base_session, const ContentInfos& updated_contents);
    204   void OnReceivedTerminateReason(Session* session, const std::string &reason);
    205   void IncomingSession(Session* session, const SessionDescription* offer);
    206   // Returns true on success.
    207   bool AddSession(Session* session, const SessionDescription* offer);
    208   void RemoveSession(Session* session);
    209   void EnableChannels(bool enable);
    210   void EnableSessionChannels(Session* session, bool enable);
    211   void Join(Call* call, bool enable);
    212   void OnConnectionMonitor(VoiceChannel* channel,
    213                            const std::vector<ConnectionInfo> &infos);
    214   void OnMediaMonitor(VoiceChannel* channel, const VoiceMediaInfo& info);
    215   void OnAudioMonitor(VoiceChannel* channel, const AudioInfo& info);
    216   void OnSpeakerMonitor(CurrentSpeakerMonitor* monitor, uint32 ssrc);
    217   void OnConnectionMonitor(VideoChannel* channel,
    218                            const std::vector<ConnectionInfo> &infos);
    219   void OnMediaMonitor(VideoChannel* channel, const VideoMediaInfo& info);
    220   void OnDataReceived(DataChannel* channel,
    221                       const ReceiveDataParams& params,
    222                       const talk_base::Buffer& payload);
    223   MediaStreams* GetMediaStreams(Session* session) const;
    224   void UpdateRemoteMediaStreams(Session* session,
    225                                 const ContentInfos& updated_contents,
    226                                 bool update_channels);
    227   bool UpdateVoiceChannelRemoteContent(Session* session,
    228                                        const AudioContentDescription* audio);
    229   bool UpdateVideoChannelRemoteContent(Session* session,
    230                                        const VideoContentDescription* video);
    231   bool UpdateDataChannelRemoteContent(Session* session,
    232                                       const DataContentDescription* data);
    233   void UpdateRecvStreams(const std::vector<StreamParams>& update_streams,
    234                          BaseChannel* channel,
    235                          std::vector<StreamParams>* recv_streams,
    236                          std::vector<StreamParams>* added_streams,
    237                          std::vector<StreamParams>* removed_streams);
    238   void AddRecvStreams(const std::vector<StreamParams>& added_streams,
    239                       BaseChannel* channel,
    240                       std::vector<StreamParams>* recv_streams);
    241   void AddRecvStream(const StreamParams& stream,
    242                      BaseChannel* channel,
    243                      std::vector<StreamParams>* recv_streams);
    244   void RemoveRecvStreams(const std::vector<StreamParams>& removed_streams,
    245                          BaseChannel* channel,
    246                          std::vector<StreamParams>* recv_streams);
    247   void RemoveRecvStream(const StreamParams& stream,
    248                         BaseChannel* channel,
    249                         std::vector<StreamParams>* recv_streams);
    250   void ContinuePlayDTMF();
    251   bool StopScreencastWithoutSendingUpdate(Session* session, uint32 ssrc);
    252   bool StopAllScreencastsWithoutSendingUpdate(Session* session);
    253   bool SessionDescriptionContainsCrypto(const SessionDescription* sdesc) const;
    254   Session* InternalInitiateSession(const std::string& id,
    255                                    const buzz::Jid& to,
    256                                    const std::string& initiator_name,
    257                                    const CallOptions& options);
    258 
    259   uint32 id_;
    260   MediaSessionClient* session_client_;
    261 
    262   struct StartedCapture {
    263     StartedCapture(cricket::VideoCapturer* capturer,
    264                    const cricket::VideoFormat& format) :
    265         capturer(capturer),
    266         format(format) {
    267     }
    268     cricket::VideoCapturer* capturer;
    269     cricket::VideoFormat format;
    270   };
    271   typedef std::map<uint32, StartedCapture> StartedScreencastMap;
    272 
    273   struct MediaSession {
    274     Session* session;
    275     VoiceChannel* voice_channel;
    276     VideoChannel* video_channel;
    277     DataChannel* data_channel;
    278     MediaStreams* recv_streams;
    279     StartedScreencastMap started_screencasts;
    280   };
    281 
    282   // Create a map of media sessions, keyed off session->id().
    283   typedef std::map<std::string, MediaSession> MediaSessionMap;
    284   MediaSessionMap media_session_map_;
    285 
    286   std::map<std::string, CurrentSpeakerMonitor*> speaker_monitor_map_;
    287   VideoRenderer* local_renderer_;
    288   bool has_video_;
    289   bool has_data_;
    290   bool muted_;
    291   bool video_muted_;
    292   bool send_to_voicemail_;
    293 
    294   // DTMF tones have to be queued up so that we don't flood the call.  We
    295   // keep a deque (doubely ended queue) of them around.  While one is playing we
    296   // set the playing_dtmf_ bit and schedule a message in XX msec to clear that
    297   // bit or start the next tone playing.
    298   std::deque<int> queued_dtmf_;
    299   bool playing_dtmf_;
    300 
    301   VoiceMediaInfo last_voice_media_info_;
    302 
    303   talk_base::scoped_ptr<AudioSourceProxy> audio_source_proxy_;
    304 
    305   friend class MediaSessionClient;
    306 };
    307 
    308 }  // namespace cricket
    309 
    310 #endif  // TALK_SESSION_MEDIA_CALL_H_
    311