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_EXAMPLES_CALL_CALLCLIENT_H_
29 #define TALK_EXAMPLES_CALL_CALLCLIENT_H_
30 
31 #include <map>
32 #include <string>
33 #include <vector>
34 
35 #include "talk/p2p/base/session.h"
36 #include "talk/session/phone/mediachannel.h"
37 #include "talk/session/phone/mediamessages.h"
38 #include "talk/session/phone/mediasessionclient.h"
39 #include "talk/xmpp/hangoutpubsubclient.h"
40 #include "talk/xmpp/xmppclient.h"
41 #include "talk/examples/call/status.h"
42 #include "talk/examples/call/console.h"
43 
44 namespace buzz {
45 class PresencePushTask;
46 class PresenceOutTask;
47 class MucInviteRecvTask;
48 class MucInviteSendTask;
49 class FriendInviteSendTask;
50 class DiscoInfoQueryTask;
51 class Muc;
52 class Status;
53 class IqTask;
54 class MucRoomConfigTask;
55 class MucRoomLookupTask;
56 class MucStatus;
57 class XmlElement;
58 class HangoutPubSubClient;
59 struct AvailableMediaEntry;
60 struct MucRoomInfo;
61 }
62 
63 namespace talk_base {
64 class Thread;
65 class NetworkManager;
66 }
67 
68 namespace cricket {
69 class PortAllocator;
70 class MediaEngineInterface;
71 class MediaSessionClient;
72 class Call;
73 class SessionManagerTask;
74 struct CallOptions;
75 struct MediaStreams;
76 struct StreamParams;
77 }
78 
79 struct RosterItem {
80   buzz::Jid jid;
81   buzz::Status::Show show;
82   std::string status;
83 };
84 
85 struct StaticRenderedView {
StaticRenderedViewStaticRenderedView86   StaticRenderedView(const cricket::StaticVideoView& view,
87                      cricket::VideoRenderer* renderer) :
88       view(view),
89       renderer(renderer) {
90   }
91 
92   cricket::StaticVideoView view;
93   cricket::VideoRenderer* renderer;
94 };
95 
96 typedef std::vector<StaticRenderedView> StaticRenderedViews;
97 
98 class CallClient: public sigslot::has_slots<> {
99  public:
100   CallClient(buzz::XmppClient* xmpp_client,
101              const std::string& caps_node,
102              const std::string& version);
103   ~CallClient();
104 
media_client()105   cricket::MediaSessionClient* media_client() const { return media_client_; }
SetMediaEngine(cricket::MediaEngineInterface * media_engine)106   void SetMediaEngine(cricket::MediaEngineInterface* media_engine) {
107     media_engine_ = media_engine;
108   }
SetAutoAccept(bool auto_accept)109   void SetAutoAccept(bool auto_accept) {
110     auto_accept_ = auto_accept;
111   }
SetPmucDomain(const std::string & pmuc_domain)112   void SetPmucDomain(const std::string &pmuc_domain) {
113     pmuc_domain_ = pmuc_domain;
114   }
SetRender(bool render)115   void SetRender(bool render) {
116     render_ = render;
117   }
SetDataChannelEnabled(bool data_channel_enabled)118   void SetDataChannelEnabled(bool data_channel_enabled) {
119     data_channel_enabled_ = data_channel_enabled;
120   }
SetConsole(Console * console)121   void SetConsole(Console *console) {
122     console_ = console;
123   }
SetPriority(int priority)124   void SetPriority(int priority) {
125     my_status_.set_priority(priority);
126   }
SetOnlineStatus(const std::string & str)127   void SetOnlineStatus(const std::string& str) {
128     SetOnlineStatus(str, &my_status_);
129   }
130   void SetOnlineStatus(const std::string& str, buzz::Status* status);
SendStatus()131   void SendStatus() {
132     SendStatus(my_status_);
133   }
134   void SendStatus(const buzz::Status& status);
135 
136   void ParseLine(const std::string &str);
137 
138   void SendChat(const std::string& to, const std::string msg);
139   void SendData(const std::string& stream_name,
140                 const std::string& text);
141   void InviteFriend(const std::string& user);
142   void JoinMuc(const buzz::Jid& room_jid);
143   void JoinMuc(const std::string& room_jid_str);
144   void LookupAndJoinMuc(const std::string& room_name);
145   void InviteToMuc(const std::string& user, const std::string& room);
146   bool InMuc();
147   const buzz::Jid* FirstMucJid();
148   void LeaveMuc(const std::string& room);
149   void SetNick(const std::string& muc_nick);
SetPortAllocatorFlags(uint32 flags)150   void SetPortAllocatorFlags(uint32 flags) { portallocator_flags_ = flags; }
SetAllowLocalIps(bool allow_local_ips)151   void SetAllowLocalIps(bool allow_local_ips) {
152     allow_local_ips_ = allow_local_ips;
153   }
154 
SetInitialProtocol(cricket::SignalingProtocol initial_protocol)155   void SetInitialProtocol(cricket::SignalingProtocol initial_protocol) {
156     initial_protocol_ = initial_protocol;
157   }
158 
SetSecurePolicy(cricket::SecureMediaPolicy secure_policy)159   void SetSecurePolicy(cricket::SecureMediaPolicy secure_policy) {
160     secure_policy_ = secure_policy;
161   }
162 
163   typedef std::map<buzz::Jid, buzz::Muc*> MucMap;
164 
mucs()165   const MucMap& mucs() const {
166     return mucs_;
167   }
168 
169  private:
170   void AddStream(uint32 audio_src_id, uint32 video_src_id);
171   void RemoveStream(uint32 audio_src_id, uint32 video_src_id);
172   void OnStateChange(buzz::XmppEngine::State state);
173 
174   void InitMedia();
175   void InitPresence();
176   void OnRequestSignaling();
177   void OnSessionCreate(cricket::Session* session, bool initiate);
178   void OnCallCreate(cricket::Call* call);
179   void OnCallDestroy(cricket::Call* call);
180   void OnSessionState(cricket::Call* call,
181                       cricket::Session* session,
182                       cricket::Session::State state);
183   void OnStatusUpdate(const buzz::Status& status);
184   void OnMucInviteReceived(const buzz::Jid& inviter, const buzz::Jid& room,
185       const std::vector<buzz::AvailableMediaEntry>& avail);
186   void OnMucJoined(const buzz::Jid& endpoint);
187   void OnMucStatusUpdate(const buzz::Jid& jid, const buzz::MucStatus& status);
188   void OnMucLeft(const buzz::Jid& endpoint, int error);
189   void OnPresenterStateChange(const std::string& nick,
190                               bool was_presenting, bool is_presenting);
191   void OnAudioMuteStateChange(const std::string& nick,
192                               bool was_muted, bool is_muted);
193   void OnRecordingStateChange(const std::string& nick,
194                               bool was_recording, bool is_recording);
195   void OnRemoteMuted(const std::string& mutee_nick,
196                      const std::string& muter_nick,
197                      bool should_mute_locally);
198   void OnMediaBlocked(const std::string& blockee_nick,
199                       const std::string& blocker_nick);
200   void OnHangoutRequestError(const std::string& node,
201                              const buzz::XmlElement* stanza);
202   void OnHangoutPublishAudioMuteError(const std::string& task_id,
203                                       const buzz::XmlElement* stanza);
204   void OnHangoutPublishPresenterError(const std::string& task_id,
205                                       const buzz::XmlElement* stanza);
206   void OnHangoutPublishRecordingError(const std::string& task_id,
207                                       const buzz::XmlElement* stanza);
208   void OnHangoutRemoteMuteError(const std::string& task_id,
209                                 const std::string& mutee_nick,
210                                 const buzz::XmlElement* stanza);
211   void OnDevicesChange();
212   void OnMediaStreamsUpdate(cricket::Call* call,
213                             cricket::Session* session,
214                             const cricket::MediaStreams& added,
215                             const cricket::MediaStreams& removed);
216   void OnSpeakerChanged(cricket::Call* call,
217                         cricket::Session* session,
218                         const cricket::StreamParams& speaker_stream);
219   void OnRoomLookupResponse(buzz::MucRoomLookupTask* task,
220                             const buzz::MucRoomInfo& room_info);
221   void OnRoomLookupError(buzz::IqTask* task,
222                          const buzz::XmlElement* stanza);
223   void OnRoomConfigResult(buzz::MucRoomConfigTask* task);
224   void OnRoomConfigError(buzz::IqTask* task,
225                          const buzz::XmlElement* stanza);
226   void OnDataReceived(cricket::Call*,
227                       const cricket::ReceiveDataParams& params,
228                       const std::string& data);
229   buzz::Jid GenerateRandomMucJid();
230 
231   void AddStaticRenderedView(
232       cricket::Session* session,
233       uint32 ssrc, int width, int height, int framerate,
234       int x_offset, int y_offset);
235   bool RemoveStaticRenderedView(uint32 ssrc);
236   void RemoveAllStaticRenderedViews();
237   void SendViewRequest(cricket::Session* session);
238 
239   static const std::string strerror(buzz::XmppEngine::Error err);
240 
241   void PrintRoster();
242   void MakeCallTo(const std::string& name, const cricket::CallOptions& options);
243   void PlaceCall(const buzz::Jid& jid, const cricket::CallOptions& options);
244   void Accept(const cricket::CallOptions& options);
245   void Reject();
246   void Quit();
247 
248   void GetDevices();
249   void PrintDevices(const std::vector<std::string>& names);
250 
251   void SetVolume(const std::string& level);
252 
253   typedef std::map<std::string, RosterItem> RosterMap;
254 
255   Console *console_;
256   buzz::XmppClient* xmpp_client_;
257   talk_base::Thread* worker_thread_;
258   talk_base::NetworkManager* network_manager_;
259   cricket::PortAllocator* port_allocator_;
260   cricket::SessionManager* session_manager_;
261   cricket::SessionManagerTask* session_manager_task_;
262   cricket::MediaEngineInterface* media_engine_;
263   cricket::DataEngineInterface* data_engine_;
264   cricket::MediaSessionClient* media_client_;
265   MucMap mucs_;
266 
267   cricket::Call* call_;
268   cricket::Session *session_;
269   buzz::HangoutPubSubClient* hangout_pubsub_client_;
270   bool incoming_call_;
271   bool auto_accept_;
272   std::string pmuc_domain_;
273   bool render_;
274   bool data_channel_enabled_;
275   cricket::VideoRenderer* local_renderer_;
276   cricket::VideoRenderer* remote_renderer_;
277   StaticRenderedViews static_rendered_views_;
278   uint32 static_views_accumulated_count_;
279 
280   buzz::Status my_status_;
281   buzz::PresencePushTask* presence_push_;
282   buzz::PresenceOutTask* presence_out_;
283   buzz::MucInviteRecvTask* muc_invite_recv_;
284   buzz::MucInviteSendTask* muc_invite_send_;
285   buzz::FriendInviteSendTask* friend_invite_send_;
286   RosterMap* roster_;
287   uint32 portallocator_flags_;
288 
289   bool allow_local_ips_;
290   cricket::SignalingProtocol initial_protocol_;
291   cricket::SecureMediaPolicy secure_policy_;
292   std::string last_sent_to_;
293 };
294 
295 #endif  // TALK_EXAMPLES_CALL_CALLCLIENT_H_
296