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/voicechannel.h"
41
42namespace cricket {
43
44class MediaSessionClient;
45struct CallOptions;
46
47class Call : public talk_base::MessageHandler, public sigslot::has_slots<> {
48 public:
49  Call(MediaSessionClient* session_client);
50  ~Call();
51
52  Session *InitiateSession(const buzz::Jid &jid, const CallOptions& options);
53  void AcceptSession(BaseSession *session, const CallOptions& options);
54  void RejectSession(BaseSession *session);
55  void TerminateSession(BaseSession *session);
56  void Terminate();
57  void SetLocalRenderer(VideoRenderer* renderer);
58  void SetVideoRenderer(BaseSession *session, uint32 ssrc,
59                        VideoRenderer* renderer);
60  void AddStream(BaseSession *session, uint32 voice_ssrc, uint32 video_ssrc);
61  void RemoveStream(BaseSession *session, uint32 voice_ssrc, uint32 video_ssrc);
62  void StartConnectionMonitor(BaseSession *session, int cms);
63  void StopConnectionMonitor(BaseSession *session);
64  void StartAudioMonitor(BaseSession *session, int cms);
65  void StopAudioMonitor(BaseSession *session);
66  void Mute(bool mute);
67  void PressDTMF(int event);
68
69  const std::vector<Session *> &sessions();
70  uint32 id();
71  bool video() const { return video_; }
72  bool muted() const { return muted_; }
73
74  // Setting this to false will cause the call to have a longer timeout and
75  // for the SignalSetupToCallVoicemail to never fire.
76  void set_send_to_voicemail(bool send_to_voicemail) {
77    send_to_voicemail_ = send_to_voicemail;
78  }
79  bool send_to_voicemail() { return send_to_voicemail_; }
80
81  // Sets a flag on the chatapp that will redirect the call to voicemail once
82  // the call has been terminated
83  sigslot::signal0<> SignalSetupToCallVoicemail;
84  sigslot::signal2<Call *, Session *> SignalAddSession;
85  sigslot::signal2<Call *, Session *> SignalRemoveSession;
86  sigslot::signal3<Call *, BaseSession *, BaseSession::State>
87      SignalSessionState;
88  sigslot::signal3<Call *, BaseSession *, Session::Error>
89      SignalSessionError;
90  sigslot::signal3<Call *, Session *, const std::string &>
91      SignalReceivedTerminateReason;
92  sigslot::signal2<Call *, const std::vector<ConnectionInfo> &>
93      SignalConnectionMonitor;
94  sigslot::signal2<Call *, const VoiceMediaInfo&> SignalMediaMonitor;
95  sigslot::signal2<Call *, const AudioInfo&> SignalAudioMonitor;
96  sigslot::signal2<Call *, const std::vector<ConnectionInfo> &>
97      SignalVideoConnectionMonitor;
98  sigslot::signal2<Call *, const VideoMediaInfo&> SignalVideoMediaMonitor;
99
100 private:
101  void OnMessage(talk_base::Message *message);
102  void OnSessionState(BaseSession *session, BaseSession::State state);
103  void OnSessionError(BaseSession *session, Session::Error error);
104  void OnReceivedTerminateReason(Session *session, const std::string &reason);
105  void IncomingSession(Session *session, const SessionDescription* offer);
106  // Returns true on success.
107  bool AddSession(Session *session, const SessionDescription* offer);
108  void RemoveSession(Session *session);
109  void EnableChannels(bool enable);
110  void Join(Call *call, bool enable);
111  void OnConnectionMonitor(VoiceChannel *channel,
112                           const std::vector<ConnectionInfo> &infos);
113  void OnMediaMonitor(VoiceChannel *channel, const VoiceMediaInfo& info);
114  void OnAudioMonitor(VoiceChannel *channel, const AudioInfo& info);
115  void OnConnectionMonitor(VideoChannel *channel,
116                           const std::vector<ConnectionInfo> &infos);
117  void OnMediaMonitor(VideoChannel *channel, const VideoMediaInfo& info);
118  VoiceChannel* GetVoiceChannel(BaseSession* session);
119  VideoChannel* GetVideoChannel(BaseSession* session);
120  void ContinuePlayDTMF();
121
122  uint32 id_;
123  MediaSessionClient *session_client_;
124  std::vector<Session *> sessions_;
125  std::map<std::string, VoiceChannel *> voice_channel_map_;
126  std::map<std::string, VideoChannel *> video_channel_map_;
127  VideoRenderer* local_renderer_;
128  bool video_;
129  bool muted_;
130  bool send_to_voicemail_;
131
132  // DTMF tones have to be queued up so that we don't flood the call.  We
133  // keep a deque (doubely ended queue) of them around.  While one is playing we
134  // set the playing_dtmf_ bit and schedule a message in XX msec to clear that
135  // bit or start the next tone playing.
136  std::deque<int> queued_dtmf_;
137  bool playing_dtmf_;
138
139  friend class MediaSessionClient;
140};
141
142}  // namespace cricket
143
144#endif  // TALK_SESSION_PHONE_CALL_H_
145