3 * Copyright 2010, 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 WEBRTC_P2P_BASE_SESSIONMESSAGES_H_
29 #define WEBRTC_P2P_BASE_SESSIONMESSAGES_H_
35 #include "webrtc/p2p/base/constants.h"
36 #include "webrtc/p2p/base/parsing.h"
37 #include "webrtc/p2p/base/sessiondescription.h" // Needed to delete contents.
38 #include "webrtc/p2p/base/transportinfo.h"
39 #include "webrtc/libjingle/xmllite/xmlelement.h"
40 #include "webrtc/base/basictypes.h"
48 class TransportParser;
50 typedef std::vector<Candidate> Candidates;
51 typedef std::map<std::string, ContentParser*> ContentParserMap;
52 typedef std::map<std::string, TransportParser*> TransportParserMap;
57 ACTION_SESSION_INITIATE,
59 ACTION_SESSION_ACCEPT,
60 ACTION_SESSION_REJECT,
61 ACTION_SESSION_TERMINATE,
63 ACTION_TRANSPORT_INFO,
64 ACTION_TRANSPORT_ACCEPT,
66 ACTION_DESCRIPTION_INFO,
69 // Abstraction of a <jingle> element within an <iq> stanza, per XMPP
70 // standard XEP-166. Can be serialized into multiple protocols,
71 // including the standard (Jingle) and the draft standard (Gingle).
72 // In general, used to communicate actions related to a p2p session,
73 // such accept, initiate, terminate, etc.
75 struct SessionMessage {
76 SessionMessage() : action_elem(NULL), stanza(NULL) {}
78 SessionMessage(SignalingProtocol protocol, ActionType type,
79 const std::string& sid, const std::string& initiator) :
80 protocol(protocol), type(type), sid(sid), initiator(initiator),
81 action_elem(NULL), stanza(NULL) {}
86 SignalingProtocol protocol;
88 std::string sid; // session id
89 std::string initiator;
91 // Used for further parsing when necessary.
92 // Represents <session> or <jingle>.
93 const buzz::XmlElement* action_elem;
94 // Mostly used for debugging.
95 const buzz::XmlElement* stanza;
98 // TODO: Break up this class so we don't have to typedef it into
100 struct ContentMessage {
101 ContentMessage() : owns_contents(false) {}
105 for (ContentInfos::iterator content = contents.begin();
106 content != contents.end(); content++) {
107 delete content->description;
112 // Caller takes ownership of contents.
113 ContentInfos ClearContents() {
116 owns_contents = false;
121 ContentInfos contents;
122 TransportInfos transports;
123 ContentGroups groups;
126 typedef ContentMessage SessionInitiate;
127 typedef ContentMessage SessionAccept;
128 // Note that a DescriptionInfo does not have TransportInfos.
129 typedef ContentMessage DescriptionInfo;
131 struct SessionTerminate {
132 SessionTerminate() {}
134 explicit SessionTerminate(const std::string& reason) :
138 std::string debug_reason;
141 struct SessionRedirect {
145 // Used during parsing and writing to map component to channel name
146 // and back. This is primarily for converting old G-ICE candidate
147 // signalling to new ICE candidate classes.
148 class CandidateTranslator {
150 virtual bool GetChannelNameFromComponent(
151 int component, std::string* channel_name) const = 0;
152 virtual bool GetComponentFromChannelName(
153 const std::string& channel_name, int* component) const = 0;
156 // Content name => translator
157 typedef std::map<std::string, CandidateTranslator*> CandidateTranslatorMap;
159 bool IsSessionMessage(const buzz::XmlElement* stanza);
160 bool ParseSessionMessage(const buzz::XmlElement* stanza,
163 // Will return an error if there is more than one content type.
164 bool ParseContentType(SignalingProtocol protocol,
165 const buzz::XmlElement* action_elem,
166 std::string* content_type,
168 void WriteSessionMessage(const SessionMessage& msg,
169 const XmlElements& action_elems,
170 buzz::XmlElement* stanza);
171 bool ParseSessionInitiate(SignalingProtocol protocol,
172 const buzz::XmlElement* action_elem,
173 const ContentParserMap& content_parsers,
174 const TransportParserMap& transport_parsers,
175 const CandidateTranslatorMap& translators,
176 SessionInitiate* init,
178 bool WriteSessionInitiate(SignalingProtocol protocol,
179 const ContentInfos& contents,
180 const TransportInfos& tinfos,
181 const ContentParserMap& content_parsers,
182 const TransportParserMap& transport_parsers,
183 const CandidateTranslatorMap& translators,
184 const ContentGroups& groups,
187 bool ParseSessionAccept(SignalingProtocol protocol,
188 const buzz::XmlElement* action_elem,
189 const ContentParserMap& content_parsers,
190 const TransportParserMap& transport_parsers,
191 const CandidateTranslatorMap& translators,
192 SessionAccept* accept,
194 bool WriteSessionAccept(SignalingProtocol protocol,
195 const ContentInfos& contents,
196 const TransportInfos& tinfos,
197 const ContentParserMap& content_parsers,
198 const TransportParserMap& transport_parsers,
199 const CandidateTranslatorMap& translators,
200 const ContentGroups& groups,
203 bool ParseSessionTerminate(SignalingProtocol protocol,
204 const buzz::XmlElement* action_elem,
205 SessionTerminate* term,
207 void WriteSessionTerminate(SignalingProtocol protocol,
208 const SessionTerminate& term,
210 bool ParseDescriptionInfo(SignalingProtocol protocol,
211 const buzz::XmlElement* action_elem,
212 const ContentParserMap& content_parsers,
213 const TransportParserMap& transport_parsers,
214 const CandidateTranslatorMap& translators,
215 DescriptionInfo* description_info,
217 bool WriteDescriptionInfo(SignalingProtocol protocol,
218 const ContentInfos& contents,
219 const ContentParserMap& content_parsers,
222 // Since a TransportInfo is not a transport-info message, and a
223 // transport-info message is just a collection of TransportInfos, we
224 // say Parse/Write TransportInfos for transport-info messages.
225 bool ParseTransportInfos(SignalingProtocol protocol,
226 const buzz::XmlElement* action_elem,
227 const ContentInfos& contents,
228 const TransportParserMap& trans_parsers,
229 const CandidateTranslatorMap& translators,
230 TransportInfos* tinfos,
232 bool WriteTransportInfos(SignalingProtocol protocol,
233 const TransportInfos& tinfos,
234 const TransportParserMap& trans_parsers,
235 const CandidateTranslatorMap& translators,
238 // Handles both Gingle and Jingle syntax.
239 bool FindSessionRedirect(const buzz::XmlElement* stanza,
240 SessionRedirect* redirect);
241 } // namespace cricket
243 #endif // WEBRTC_P2P_BASE_SESSIONMESSAGES_H_