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