aboutsummaryrefslogtreecommitdiff
path: root/talk/examples/call/callclient.h
blob: 075dc6efd83caccaaa492a4d712422ea8b51b270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * libjingle
 * Copyright 2004--2005, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TALK_EXAMPLES_CALL_CALLCLIENT_H_
#define TALK_EXAMPLES_CALL_CALLCLIENT_H_

#include <map>
#include <string>
#include <vector>

#include "talk/examples/call/console.h"
#include "talk/media/base/mediachannel.h"
#include "webrtc/p2p/base/session.h"
#include "talk/session/media/mediamessages.h"
#include "talk/session/media/mediasessionclient.h"
#include "webrtc/libjingle/xmpp/hangoutpubsubclient.h"
#include "webrtc/libjingle/xmpp/presencestatus.h"
#include "webrtc/libjingle/xmpp/xmppclient.h"
#include "webrtc/base/scoped_ptr.h"
#include "webrtc/base/sslidentity.h"

namespace buzz {
class PresencePushTask;
class PresenceOutTask;
class MucInviteRecvTask;
class MucInviteSendTask;
class FriendInviteSendTask;
class DiscoInfoQueryTask;
class Muc;
class PresenceStatus;
class IqTask;
class MucRoomConfigTask;
class MucRoomLookupTask;
class MucPresenceStatus;
class XmlElement;
class HangoutPubSubClient;
struct AvailableMediaEntry;
struct MucRoomInfo;
}  // namespace buzz

namespace rtc {
class Thread;
class NetworkManager;
}  // namespace rtc

namespace cricket {
class PortAllocator;
class MediaEngineInterface;
class MediaSessionClient;
class Call;
class SessionManagerTask;
struct CallOptions;
struct MediaStreams;
struct StreamParams;
}  // namespace cricket

struct RosterItem {
  buzz::Jid jid;
  buzz::PresenceStatus::Show show;
  std::string status;
};

struct StaticRenderedView {
  StaticRenderedView(const cricket::StaticVideoView& view,
                     cricket::VideoRenderer* renderer) :
      view(view),
      renderer(renderer) {
  }

  cricket::StaticVideoView view;
  cricket::VideoRenderer* renderer;
};

// Maintain a mapping of (session, ssrc) to rendered view.
typedef std::map<std::pair<cricket::Session*, uint32>,
                 StaticRenderedView> StaticRenderedViews;

class CallClient: public sigslot::has_slots<> {
 public:
  CallClient(buzz::XmppClient* xmpp_client,
             const std::string& caps_node,
             const std::string& version);
  ~CallClient();

  cricket::MediaSessionClient* media_client() const { return media_client_; }
  void SetMediaEngine(cricket::MediaEngineInterface* media_engine) {
    media_engine_ = media_engine;
  }
  void SetAutoAccept(bool auto_accept) {
    auto_accept_ = auto_accept;
  }
  void SetPmucDomain(const std::string &pmuc_domain) {
    pmuc_domain_ = pmuc_domain;
  }
  void SetRender(bool render) {
    render_ = render;
  }
  void SetDataChannelType(cricket::DataChannelType data_channel_type) {
    data_channel_type_ = data_channel_type;
  }
  void SetMultiSessionEnabled(bool multisession_enabled) {
    multisession_enabled_ = multisession_enabled;
  }
  void SetConsole(Console *console) {
    console_ = console;
  }
  void SetPriority(int priority) {
    my_status_.set_priority(priority);
  }
  void SendStatus() {
    SendStatus(my_status_);
  }
  void SendStatus(const buzz::PresenceStatus& status);

  void ParseLine(const std::string &str);

  void SendChat(const std::string& to, const std::string msg);
  void SendData(const std::string& stream_name,
                const std::string& text);
  void InviteFriend(const std::string& user);
  void JoinMuc(const buzz::Jid& room_jid);
  void JoinMuc(const std::string& room_jid_str);
  void LookupAndJoinMuc(const std::string& room_name);
  void InviteToMuc(const std::string& user, const std::string& room);
  bool InMuc();
  const buzz::Jid* FirstMucJid();
  void LeaveMuc(const std::string& room);
  void SetNick(const std::string& muc_nick);
  void SetPortAllocatorFlags(uint32 flags) { portallocator_flags_ = flags; }
  void SetAllowLocalIps(bool allow_local_ips) {
    allow_local_ips_ = allow_local_ips;
  }

  void SetSignalingProtocol(cricket::SignalingProtocol protocol) {
    signaling_protocol_ = protocol;
  }
  void SetTransportProtocol(cricket::TransportProtocol protocol) {
    transport_protocol_ = protocol;
  }
  void SetSecurePolicy(cricket::SecurePolicy sdes_policy,
                       cricket::SecurePolicy dtls_policy) {
    sdes_policy_ = sdes_policy;
    dtls_policy_ = dtls_policy;
  }
  void SetSslIdentity(rtc::SSLIdentity* identity) {
    ssl_identity_.reset(identity);
  }

  typedef std::map<buzz::Jid, buzz::Muc*> MucMap;

  const MucMap& mucs() const {
    return mucs_;
  }

  void SetShowRosterMessages(bool show_roster_messages) {
    show_roster_messages_ = show_roster_messages;
  }

 private:
  void AddStream(uint32 audio_src_id, uint32 video_src_id);
  void RemoveStream(uint32 audio_src_id, uint32 video_src_id);
  void OnStateChange(buzz::XmppEngine::State state);

  void InitMedia();
  void InitPresence();
  void StartXmppPing();
  void OnPingTimeout();
  void OnRequestSignaling();
  void OnSessionCreate(cricket::Session* session, bool initiate);
  void OnCallCreate(cricket::Call* call);
  void OnCallDestroy(cricket::Call* call);
  void OnSessionState(cricket::Call* call,
                      cricket::Session* session,
                      cricket::Session::State state);
  void OnStatusUpdate(const buzz::PresenceStatus& status);
  void OnMucInviteReceived(const buzz::Jid& inviter, const buzz::Jid& room,
      const std::vector<buzz::AvailableMediaEntry>& avail);
  void OnMucJoined(const buzz::Jid& endpoint);
  void OnMucStatusUpdate(const buzz::Jid& jid,
                         const buzz::MucPresenceStatus& status);
  void OnMucLeft(const buzz::Jid& endpoint, int error);
  void OnPresenterStateChange(const std::string& nick,
                              bool was_presenting, bool is_presenting);
  void OnAudioMuteStateChange(const std::string& nick,
                              bool was_muted, bool is_muted);
  void OnRecordingStateChange(const std::string& nick,
                              bool was_recording, bool is_recording);
  void OnRemoteMuted(const std::string& mutee_nick,
                     const std::string& muter_nick,
                     bool should_mute_locally);
  void OnMediaBlocked(const std::string& blockee_nick,
                      const std::string& blocker_nick);
  void OnHangoutRequestError(const std::string& node,
                             const buzz::XmlElement* stanza);
  void OnHangoutPublishAudioMuteError(const std::string& task_id,
                                      const buzz::XmlElement* stanza);
  void OnHangoutPublishPresenterError(const std::string& task_id,
                                      const buzz::XmlElement* stanza);
  void OnHangoutPublishRecordingError(const std::string& task_id,
                                      const buzz::XmlElement* stanza);
  void OnHangoutRemoteMuteError(const std::string& task_id,
                                const std::string& mutee_nick,
                                const buzz::XmlElement* stanza);
  void OnDevicesChange();
  void OnMediaStreamsUpdate(cricket::Call* call,
                            cricket::Session* session,
                            const cricket::MediaStreams& added,
                            const cricket::MediaStreams& removed);
  void OnSpeakerChanged(cricket::Call* call,
                        cricket::Session* session,
                        const cricket::StreamParams& speaker_stream);
  void OnRoomLookupResponse(buzz::MucRoomLookupTask* task,
                            const buzz::MucRoomInfo& room_info);
  void OnRoomLookupError(buzz::IqTask* task,
                         const buzz::XmlElement* stanza);
  void OnRoomConfigResult(buzz::MucRoomConfigTask* task);
  void OnRoomConfigError(buzz::IqTask* task,
                         const buzz::XmlElement* stanza);
  void OnDataReceived(cricket::Call*,
                      const cricket::ReceiveDataParams& params,
                      const rtc::Buffer& payload);
  buzz::Jid GenerateRandomMucJid();

  // Depending on |enable|, render (or don't) all the streams in |session|.
  void RenderAllStreams(cricket::Call* call,
                        cricket::Session* session,
                        bool enable);

  // Depending on |enable|, render (or don't) the streams in |video_streams|.
  void RenderStreams(cricket::Call* call,
                     cricket::Session* session,
                     const std::vector<cricket::StreamParams>& video_streams,
                     bool enable);

  // Depending on |enable|, render (or don't) the supplied |stream|.
  void RenderStream(cricket::Call* call,
                    cricket::Session* session,
                    const cricket::StreamParams& stream,
                    bool enable);
  void AddStaticRenderedView(
      cricket::Session* session,
      uint32 ssrc, int width, int height, int framerate,
      int x_offset, int y_offset);
  bool RemoveStaticRenderedView(uint32 ssrc);
  void RemoveCallsStaticRenderedViews(cricket::Call* call);
  void SendViewRequest(cricket::Call* call, cricket::Session* session);
  bool SelectFirstDesktopScreencastId(cricket::ScreencastId* screencastid);

  static const std::string strerror(buzz::XmppEngine::Error err);

  void PrintRoster();
  bool FindJid(const std::string& name,
               buzz::Jid* found_jid,
               cricket::CallOptions* options);
  bool PlaceCall(const std::string& name, cricket::CallOptions options);
  bool InitiateAdditionalSession(const std::string& name,
                                 cricket::CallOptions options);
  void TerminateAndRemoveSession(cricket::Call* call, const std::string& id);
  void PrintCalls();
  void SwitchToCall(uint32 call_id);
  void Accept(const cricket::CallOptions& options);
  void Reject();
  void Quit();

  void GetDevices();
  void PrintDevices(const std::vector<std::string>& names);

  void SetVolume(const std::string& level);

  cricket::Session* GetFirstSession() { return sessions_[call_->id()][0]; }
  void AddSession(cricket::Session* session) {
    sessions_[call_->id()].push_back(session);
  }

  void PrintStats() const;
  void SetupAcceptedCall();

  typedef std::map<std::string, RosterItem> RosterMap;

  Console *console_;
  buzz::XmppClient* xmpp_client_;
  rtc::Thread* worker_thread_;
  rtc::NetworkManager* network_manager_;
  cricket::PortAllocator* port_allocator_;
  cricket::SessionManager* session_manager_;
  cricket::SessionManagerTask* session_manager_task_;
  cricket::MediaEngineInterface* media_engine_;
  cricket::DataEngineInterface* data_engine_;
  cricket::MediaSessionClient* media_client_;
  MucMap mucs_;

  cricket::Call* call_;
  typedef std::map<uint32, std::vector<cricket::Session *> > SessionMap;
  SessionMap sessions_;

  buzz::HangoutPubSubClient* hangout_pubsub_client_;
  bool incoming_call_;
  bool auto_accept_;
  std::string pmuc_domain_;
  bool render_;
  cricket::DataChannelType data_channel_type_;
  bool multisession_enabled_;
  cricket::VideoRenderer* local_renderer_;
  StaticRenderedViews static_rendered_views_;
  uint32 static_views_accumulated_count_;
  uint32 screencast_ssrc_;

  buzz::PresenceStatus my_status_;
  buzz::PresencePushTask* presence_push_;
  buzz::PresenceOutTask* presence_out_;
  buzz::MucInviteRecvTask* muc_invite_recv_;
  buzz::MucInviteSendTask* muc_invite_send_;
  buzz::FriendInviteSendTask* friend_invite_send_;
  RosterMap* roster_;
  uint32 portallocator_flags_;

  bool allow_local_ips_;
  cricket::SignalingProtocol signaling_protocol_;
  cricket::TransportProtocol transport_protocol_;
  cricket::SecurePolicy sdes_policy_;
  cricket::SecurePolicy dtls_policy_;
  rtc::scoped_ptr<rtc::SSLIdentity> ssl_identity_;
  std::string last_sent_to_;

  bool show_roster_messages_;
};

#endif  // TALK_EXAMPLES_CALL_CALLCLIENT_H_