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