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