1 /*
2  * libjingle
3  * Copyright 2011, 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_XMPP_HANGOUTPUBSUBCLIENT_H_
29 #define TALK_XMPP_HANGOUTPUBSUBCLIENT_H_
30 
31 #include <map>
32 #include <string>
33 #include <vector>
34 
35 #include "talk/base/scoped_ptr.h"
36 #include "talk/base/sigslot.h"
37 #include "talk/base/sigslotrepeater.h"
38 #include "talk/xmpp/jid.h"
39 #include "talk/xmpp/pubsubclient.h"
40 
41 // Gives a high-level API for MUC call PubSub needs such as
42 // presenter state, recording state, mute state, and remote mute.
43 
44 namespace buzz {
45 
46 class Jid;
47 class XmlElement;
48 class XmppTaskParentInterface;
49 
50 // To handle retracts correctly, we need to remember certain details
51 // about an item.  We could just cache the entire XML element, but
52 // that would take more memory and require re-parsing.
53 struct StateItemInfo {
54   std::string published_nick;
55   std::string publisher_nick;
56 };
57 
58 // Represents a PubSub state change.  Usually, the key is the nick,
59 // but not always.  It's a per-state-type thing.  Currently documented
60 // at https://docs.google.com/a/google.com/document/d/
61 // 1QyHu_ufyVdf0VICdfc_DtJbrOdrdIUm4eM73RZqnivI/edit?hl=en_US
62 template <typename C>
63 struct PubSubStateChange {
64   // The nick of the user changing the state.
65   std::string publisher_nick;
66   // The nick of the user whose state is changing.
67   std::string published_nick;
68   C old_state;
69   C new_state;
70 };
71 
72 template <typename C> class PubSubStateClient;
73 
74 // A client tied to a specific MUC jid and local nick.  Provides ways
75 // to get updates and publish state and events.  Must call
76 // RequestAll() to start getting updates.
77 class HangoutPubSubClient : public sigslot::has_slots<> {
78  public:
79   HangoutPubSubClient(XmppTaskParentInterface* parent,
80                       const Jid& mucjid,
81                       const std::string& nick);
82   ~HangoutPubSubClient();
mucjid()83   const Jid& mucjid() const { return mucjid_; }
nick()84   const std::string& nick() const { return nick_; }
85 
86   // Requests all of the different states and subscribes for updates.
87   // Responses and updates will be signalled via the various signals.
88   void RequestAll();
89   // Signal (nick, was_presenting, is_presenting)
90   sigslot::signal3<const std::string&, bool, bool> SignalPresenterStateChange;
91   // Signal (nick, was_muted, is_muted)
92   sigslot::signal3<const std::string&, bool, bool> SignalAudioMuteStateChange;
93   // Signal (nick, was_recording, is_recording)
94   sigslot::signal3<const std::string&, bool, bool> SignalRecordingStateChange;
95   // Signal (mutee_nick, muter_nick, should_mute_locally)
96   sigslot::signal3<const std::string&,
97                    const std::string&,
98                    bool> SignalRemoteMute;
99   // Signal (blockee_nick, blocker_nick)
100   sigslot::signal2<const std::string&, const std::string&> SignalMediaBlock;
101 
102   // Signal (node, error stanza)
103   sigslot::signal2<const std::string&, const XmlElement*> SignalRequestError;
104 
105   // On each of these, provide a task_id_out to get the task_id, which
106   // can be correlated to the error and result signals.
107   void PublishPresenterState(
108       bool presenting, std::string* task_id_out = NULL);
109   void PublishAudioMuteState(
110       bool muted, std::string* task_id_out = NULL);
111   void PublishRecordingState(
112       bool recording, std::string* task_id_out = NULL);
113   void RemoteMute(
114       const std::string& mutee_nick, std::string* task_id_out = NULL);
115   void BlockMedia(
116       const std::string& blockee_nick, std::string* task_id_out = NULL);
117 
118   // Signal task_id
119   sigslot::signal1<const std::string&> SignalPublishAudioMuteResult;
120   sigslot::signal1<const std::string&> SignalPublishPresenterResult;
121   sigslot::signal1<const std::string&> SignalPublishRecordingResult;
122   // Signal (task_id, mutee_nick)
123   sigslot::signal2<const std::string&,
124                    const std::string&> SignalRemoteMuteResult;
125   // Signal (task_id, blockee_nick)
126   sigslot::signal2<const std::string&,
127                    const std::string&> SignalMediaBlockResult;
128 
129   // Signal (task_id, error stanza)
130   sigslot::signal2<const std::string&,
131                    const XmlElement*> SignalPublishAudioMuteError;
132   sigslot::signal2<const std::string&,
133                    const XmlElement*> SignalPublishPresenterError;
134   sigslot::signal2<const std::string&,
135                    const XmlElement*> SignalPublishRecordingError;
136   sigslot::signal2<const std::string&,
137                    const XmlElement*> SignalPublishMediaBlockError;
138   // Signal (task_id, mutee_nick, error stanza)
139   sigslot::signal3<const std::string&,
140                    const std::string&,
141                    const XmlElement*> SignalRemoteMuteError;
142   // Signal (task_id, blockee_nick, error stanza)
143   sigslot::signal3<const std::string&,
144                    const std::string&,
145                    const XmlElement*> SignalMediaBlockError;
146 
147  private:
148   void OnPresenterRequestError(PubSubClient* client,
149                                const XmlElement* stanza);
150   void OnMediaRequestError(PubSubClient* client,
151                            const XmlElement* stanza);
152 
153   void OnPresenterStateChange(const PubSubStateChange<bool>& change);
154   void OnPresenterPublishResult(const std::string& task_id,
155                                const XmlElement* item);
156   void OnPresenterPublishError(const std::string& task_id,
157                                const XmlElement* item,
158                                const XmlElement* stanza);
159   void OnAudioMuteStateChange(const PubSubStateChange<bool>& change);
160   void OnAudioMutePublishResult(const std::string& task_id,
161                                const XmlElement* item);
162   void OnAudioMutePublishError(const std::string& task_id,
163                                const XmlElement* item,
164                                const XmlElement* stanza);
165   void OnRecordingStateChange(const PubSubStateChange<bool>& change);
166   void OnRecordingPublishResult(const std::string& task_id,
167                                const XmlElement* item);
168   void OnRecordingPublishError(const std::string& task_id,
169                                const XmlElement* item,
170                                const XmlElement* stanza);
171   void OnMediaBlockStateChange(const PubSubStateChange<bool>& change);
172   void OnMediaBlockPublishResult(const std::string& task_id,
173                                  const XmlElement* item);
174   void OnMediaBlockPublishError(const std::string& task_id,
175                                 const XmlElement* item,
176                                 const XmlElement* stanza);
177   Jid mucjid_;
178   std::string nick_;
179   talk_base::scoped_ptr<PubSubClient> media_client_;
180   talk_base::scoped_ptr<PubSubClient> presenter_client_;
181   talk_base::scoped_ptr<PubSubStateClient<bool> > presenter_state_client_;
182   talk_base::scoped_ptr<PubSubStateClient<bool> > audio_mute_state_client_;
183   talk_base::scoped_ptr<PubSubStateClient<bool> > recording_state_client_;
184   talk_base::scoped_ptr<PubSubStateClient<bool> > media_block_state_client_;
185 };
186 
187 }  // namespace buzz
188 
189 #endif  // TALK_XMPP_HANGOUTPUBSUBCLIENT_H_
190