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
49namespace cricket {
50
51class MediaSessionClient;
52class BaseChannel;
53class VoiceChannel;
54class VideoChannel;
55class DataChannel;
56
57// Can't typedef this easily since it's forward declared as struct elsewhere.
58struct CallOptions : public MediaSessionOptions {
59};
60
61class 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  VoiceChannel* GetVoiceChannel(Session* session) const;
121  VideoChannel* GetVideoChannel(Session* session) const;
122  DataChannel* GetDataChannel(Session* session) const;
123  // Public just for unit tests
124  VideoContentDescription* CreateVideoStreamUpdate(const StreamParams& stream);
125  // Takes ownership of video.
126  void SendVideoStreamUpdate(Session* session, VideoContentDescription* video);
127
128  // Setting this to false will cause the call to have a longer timeout and
129  // for the SignalSetupToCallVoicemail to never fire.
130  void set_send_to_voicemail(bool send_to_voicemail) {
131    send_to_voicemail_ = send_to_voicemail;
132  }
133  bool send_to_voicemail() { return send_to_voicemail_; }
134  const VoiceMediaInfo& last_voice_media_info() const {
135    return last_voice_media_info_;
136  }
137
138  // Sets a flag on the chatapp that will redirect the call to voicemail once
139  // the call has been terminated
140  sigslot::signal0<> SignalSetupToCallVoicemail;
141  sigslot::signal2<Call*, Session*> SignalAddSession;
142  sigslot::signal2<Call*, Session*> SignalRemoveSession;
143  sigslot::signal3<Call*, Session*, Session::State>
144      SignalSessionState;
145  sigslot::signal3<Call*, Session*, Session::Error>
146      SignalSessionError;
147  sigslot::signal3<Call*, Session*, const std::string &>
148      SignalReceivedTerminateReason;
149  sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
150      SignalConnectionMonitor;
151  sigslot::signal2<Call*, const VoiceMediaInfo&> SignalMediaMonitor;
152  sigslot::signal2<Call*, const AudioInfo&> SignalAudioMonitor;
153  // Empty nick on StreamParams means "unknown".
154  // No ssrcs in StreamParams means "no current speaker".
155  sigslot::signal3<Call*,
156                   Session*,
157                   const StreamParams&> SignalSpeakerMonitor;
158  sigslot::signal2<Call*, const std::vector<ConnectionInfo> &>
159      SignalVideoConnectionMonitor;
160  sigslot::signal2<Call*, const VideoMediaInfo&> SignalVideoMediaMonitor;
161  // Gives added streams and removed streams, in that order.
162  sigslot::signal4<Call*,
163                   Session*,
164                   const MediaStreams&,
165                   const MediaStreams&> SignalMediaStreamsUpdate;
166  sigslot::signal3<Call*,
167                   const ReceiveDataParams&,
168                   const talk_base::Buffer&> SignalDataReceived;
169
170 private:
171  void OnMessage(talk_base::Message* message);
172  void OnSessionState(BaseSession* base_session, BaseSession::State state);
173  void OnSessionError(BaseSession* base_session, Session::Error error);
174  void OnSessionInfoMessage(
175      Session* session, const buzz::XmlElement* action_elem);
176  void OnViewRequest(
177      Session* session, const ViewRequest& view_request);
178  void OnRemoteDescriptionUpdate(
179      BaseSession* base_session, const ContentInfos& updated_contents);
180  void OnReceivedTerminateReason(Session* session, const std::string &reason);
181  void IncomingSession(Session* session, const SessionDescription* offer);
182  // Returns true on success.
183  bool AddSession(Session* session, const SessionDescription* offer);
184  void RemoveSession(Session* session);
185  void EnableChannels(bool enable);
186  void EnableSessionChannels(Session* session, bool enable);
187  void Join(Call* call, bool enable);
188  void OnConnectionMonitor(VoiceChannel* channel,
189                           const std::vector<ConnectionInfo> &infos);
190  void OnMediaMonitor(VoiceChannel* channel, const VoiceMediaInfo& info);
191  void OnAudioMonitor(VoiceChannel* channel, const AudioInfo& info);
192  void OnSpeakerMonitor(CurrentSpeakerMonitor* monitor, uint32 ssrc);
193  void OnConnectionMonitor(VideoChannel* channel,
194                           const std::vector<ConnectionInfo> &infos);
195  void OnMediaMonitor(VideoChannel* channel, const VideoMediaInfo& info);
196  void OnDataReceived(DataChannel* channel,
197                      const ReceiveDataParams& params,
198                      const talk_base::Buffer& payload);
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