1 /*
2  * libjingle
3  * Copyright 2004--2005, 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_CALL_H_
29 #define TALK_SESSION_PHONE_CALL_H_
30 
31 #include <string>
32 #include <map>
33 #include <vector>
34 #include <deque>
35 #include "talk/base/messagequeue.h"
36 #include "talk/p2p/base/session.h"
37 #include "talk/p2p/client/socketmonitor.h"
38 #include "talk/xmpp/jid.h"
39 #include "talk/session/phone/audiomonitor.h"
40 #include "talk/session/phone/currentspeakermonitor.h"
41 #include "talk/session/phone/dataengine.h"
42 #include "talk/session/phone/mediamessages.h"
43 #include "talk/session/phone/mediasession.h"
44 #include "talk/session/phone/streamparams.h"
45 
46 namespace cricket {
47 
48 class MediaSessionClient;
49 class BaseChannel;
50 class VoiceChannel;
51 class VideoChannel;
52 class DataChannel;
53 
54 // Can't typedef this easily since it's forward declared as struct elsewhere.
55 struct CallOptions : public MediaSessionOptions {
56 };
57 
58 class Call : public talk_base::MessageHandler, public sigslot::has_slots<> {
59  public:
60   explicit Call(MediaSessionClient* session_client);
61   ~Call();
62 
63   Session* InitiateSession(const buzz::Jid &jid, const CallOptions& options);
64   void AcceptSession(Session* session, const CallOptions& options);
65   void RejectSession(Session* session);
66   void TerminateSession(Session* session);
67   void Terminate();
68   bool SendViewRequest(Session* session,
69                        const ViewRequest& view_request);
70   void SetLocalRenderer(VideoRenderer* renderer);
71   void SetVideoRenderer(Session* session, uint32 ssrc,
72                         VideoRenderer* renderer);
73   void StartConnectionMonitor(Session* session, int cms);
74   void StopConnectionMonitor(Session* session);
75   void StartAudioMonitor(Session* session, int cms);
76   void StopAudioMonitor(Session* session);
77   bool IsAudioMonitorRunning(Session* session);
78   void StartSpeakerMonitor(Session* session);
79   void StopSpeakerMonitor(Session* session);
80   void Mute(bool mute);
81   void MuteVideo(bool mute);
82   void SendData(Session* session,
83                 const DataMediaChannel::SendDataParams& params,
84                 const std::string& data);
85   void PressDTMF(int event);
86 
87   const std::vector<Session*> &sessions();
88   uint32 id();
has_video()89   bool has_video() const { return has_video_; }
has_data()90   bool has_data() const { return has_data_; }
muted()91   bool muted() const { return muted_; }
video_muted()92   bool video_muted() const { return video_muted_; }
data_recv_streams()93   const std::vector<StreamParams>& data_recv_streams() const {
94     return recv_streams_.data();
95   }
96 
97   // Setting this to false will cause the call to have a longer timeout and
98   // for the SignalSetupToCallVoicemail to never fire.
set_send_to_voicemail(bool send_to_voicemail)99   void set_send_to_voicemail(bool send_to_voicemail) {
100     send_to_voicemail_ = send_to_voicemail;
101   }
send_to_voicemail()102   bool send_to_voicemail() { return send_to_voicemail_; }
103 
104   // Sets a flag on the chatapp that will redirect the call to voicemail once
105   // the call has been terminated
106   sigslot::signal0<> SignalSetupToCallVoicemail;
107   sigslot::signal2<Call*, Session*> SignalAddSession;
108   sigslot::signal2<Call*, Session*> SignalRemoveSession;
109   sigslot::signal3<Call*, Session*, Session::State>
110       SignalSessionState;
111   sigslot::signal3<Call*, Session*, Session::Error>
112       SignalSessionError;
113   sigslot::signal3<Call*, Session*, const std::string &>
114       SignalReceivedTerminateReason;
115   sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
116       SignalConnectionMonitor;
117   sigslot::signal2<Call*, const VoiceMediaInfo&> SignalMediaMonitor;
118   sigslot::signal2<Call*, const AudioInfo&> SignalAudioMonitor;
119   // Empty nick on StreamParams means "unknown".
120   // No ssrcs in StreamParams means "no current speaker".
121   sigslot::signal3<Call*,
122                    Session*,
123                    const StreamParams&> SignalSpeakerMonitor;
124   sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
125       SignalVideoConnectionMonitor;
126   sigslot::signal2<Call*, const VideoMediaInfo&> SignalVideoMediaMonitor;
127   // Gives added streams and removed streams, in that order.
128   sigslot::signal4<Call*,
129                    Session*,
130                    const MediaStreams&,
131                    const MediaStreams&> SignalMediaStreamsUpdate;
132   sigslot::signal3<Call*,
133                    const ReceiveDataParams&,
134                    const std::string&> SignalDataReceived;
135 
136  private:
137   void OnMessage(talk_base::Message* message);
138   void OnSessionState(BaseSession* session, BaseSession::State state);
139   void OnSessionError(BaseSession* session, Session::Error error);
140   void OnSessionInfoMessage(
141       Session* session, const buzz::XmlElement* action_elem);
142   void OnViewRequest(
143       Session* session, const ViewRequest& view_request);
144   void OnRemoteDescriptionUpdate(
145       BaseSession* session, const ContentInfos& updated_contents);
146   void OnReceivedTerminateReason(Session* session, const std::string &reason);
147   void IncomingSession(Session* session, const SessionDescription* offer);
148   // Returns true on success.
149   bool AddSession(Session* session, const SessionDescription* offer);
150   void RemoveSession(Session* session);
151   void EnableChannels(bool enable);
152   void Join(Call* call, bool enable);
153   void OnConnectionMonitor(VoiceChannel* channel,
154                            const std::vector<ConnectionInfo> &infos);
155   void OnMediaMonitor(VoiceChannel* channel, const VoiceMediaInfo& info);
156   void OnAudioMonitor(VoiceChannel* channel, const AudioInfo& info);
157   void OnSpeakerMonitor(CurrentSpeakerMonitor* monitor, uint32 ssrc);
158   void OnConnectionMonitor(VideoChannel* channel,
159                            const std::vector<ConnectionInfo> &infos);
160   void OnMediaMonitor(VideoChannel* channel, const VideoMediaInfo& info);
161   void OnDataReceived(DataChannel* channel,
162                       const ReceiveDataParams& params,
163                       const std::string& data);
164   VoiceChannel* GetVoiceChannel(Session* session);
165   VideoChannel* GetVideoChannel(Session* session);
166   DataChannel* GetDataChannel(Session* session);
167   bool UpdateVoiceChannelRemoteContent(Session* session,
168                                        const AudioContentDescription* audio);
169   bool UpdateVideoChannelRemoteContent(Session* session,
170                                        const VideoContentDescription* video);
171   bool UpdateDataChannelRemoteContent(Session* session,
172                                       const DataContentDescription* data);
173   void UpdateRecvStreams(const std::vector<StreamParams>& update_streams,
174                          BaseChannel* channel,
175                          std::vector<StreamParams>* recv_streams,
176                          std::vector<StreamParams>* added_streams,
177                          std::vector<StreamParams>* removed_streams);
178   void AddRecvStreams(const std::vector<StreamParams>& added_streams,
179                       BaseChannel* channel,
180                       std::vector<StreamParams>* recv_streams);
181   void AddRecvStream(const StreamParams& stream,
182                      BaseChannel* channel,
183                      std::vector<StreamParams>* recv_streams);
184   void RemoveRecvStreams(const std::vector<StreamParams>& removed_streams,
185                          BaseChannel* channel,
186                          std::vector<StreamParams>* recv_streams);
187   void RemoveRecvStream(const StreamParams& stream,
188                         BaseChannel* channel,
189                         std::vector<StreamParams>* recv_streams);
190 
191   void ContinuePlayDTMF();
192 
193   uint32 id_;
194   MediaSessionClient* session_client_;
195   std::vector<Session*> sessions_;
196   MediaStreams recv_streams_;
197   std::map<std::string, VoiceChannel*> voice_channel_map_;
198   std::map<std::string, VideoChannel*> video_channel_map_;
199   std::map<std::string, DataChannel*> data_channel_map_;
200   std::map<std::string, CurrentSpeakerMonitor*> speaker_monitor_map_;
201   VideoRenderer* local_renderer_;
202   bool has_video_;
203   bool has_data_;
204   bool muted_;
205   bool video_muted_;
206   bool send_to_voicemail_;
207 
208   // DTMF tones have to be queued up so that we don't flood the call.  We
209   // keep a deque (doubely ended queue) of them around.  While one is playing we
210   // set the playing_dtmf_ bit and schedule a message in XX msec to clear that
211   // bit or start the next tone playing.
212   std::deque<int> queued_dtmf_;
213   bool playing_dtmf_;
214 
215   friend class MediaSessionClient;
216 };
217 
218 }  // namespace cricket
219 
220 #endif  // TALK_SESSION_PHONE_CALL_H_
221