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