1 /*
2  * libjingle
3  * Copyright 2010, 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 /*
29  * A collection of functions and types for serializing and
30  * deserializing Jingle session messages related to media.
31  * Specificially, the <notify> and <view> messages.  They are not yet
32  * standardized, but their current documentation can be found at:
33  * goto/jinglemuc
34  */
35 
36 #ifndef TALK_SESSION_PHONE_MEDIAMESSAGES_H_
37 #define TALK_SESSION_PHONE_MEDIAMESSAGES_H_
38 
39 #include <string>
40 #include <vector>
41 
42 #include "talk/base/basictypes.h"
43 #include "talk/p2p/base/parsing.h"
44 #include "talk/p2p/base/sessiondescription.h"
45 
46 namespace cricket {
47 
48 struct StreamParams;
49 
50 // A collection of audio and video and data streams. Most of the
51 // methods are merely for convenience. Many of these methods are keyed
52 // by ssrc, which is the source identifier in the RTP spec
53 // (http://tools.ietf.org/html/rfc3550).
54 struct MediaStreams {
55  public:
MediaStreamsMediaStreams56   MediaStreams() {}
57   void CopyFrom(const MediaStreams& sources);
58 
emptyMediaStreams59   bool empty() const {
60     return audio_.empty() && video_.empty() && data_.empty();
61   }
62 
mutable_audioMediaStreams63   std::vector<StreamParams>* mutable_audio() { return &audio_; }
mutable_videoMediaStreams64   std::vector<StreamParams>* mutable_video() { return &video_; }
mutable_dataMediaStreams65   std::vector<StreamParams>* mutable_data() { return &data_; }
audioMediaStreams66   const std::vector<StreamParams>& audio() const { return audio_; }
videoMediaStreams67   const std::vector<StreamParams>& video() const { return video_; }
dataMediaStreams68   const std::vector<StreamParams>& data() const { return data_; }
69 
70   // Remove the streams with the given name.  Names are only unique to
71   // nicks, so you need the nick as well.
72   bool GetAudioStreamByNickAndName(
73       const std::string& nick, const std::string& name, StreamParams* source);
74   bool GetVideoStreamByNickAndName(
75       const std::string& nick, const std::string& name, StreamParams* source);
76   bool GetDataStreamByNickAndName(
77       const std::string& nick, const std::string& name, StreamParams* source);
78   // Get the source with the given ssrc.  Returns true if found.
79   bool GetAudioStreamBySsrc(uint32 ssrc, StreamParams* source);
80   bool GetVideoStreamBySsrc(uint32 ssrc, StreamParams* source);
81   bool GetDataStreamBySsrc(uint32 ssrc, StreamParams* source);
82   // Add a source.
83   void AddAudioStream(const StreamParams& source);
84   void AddVideoStream(const StreamParams& source);
85   void AddDataStream(const StreamParams& source);
86   // Remove the source with the given name.  Names are only unique to
87   // nicks, so you need the nick as well.
88   void RemoveAudioStreamByNickAndName(const std::string& nick,
89                                       const std::string& name);
90   void RemoveVideoStreamByNickAndName(const std::string& nick,
91                                       const std::string& name);
92   void RemoveDataStreamByNickAndName(const std::string& nick,
93                                       const std::string& name);
94 
95  private:
96   std::vector<StreamParams> audio_;
97   std::vector<StreamParams> video_;
98   std::vector<StreamParams> data_;
99 
100   DISALLOW_COPY_AND_ASSIGN(MediaStreams);
101 };
102 
103 // In a <view> message, there are a number of views specified.  This
104 // represents one such view.  We currently only support "static"
105 // views.
106 struct StaticVideoView {
StaticVideoViewStaticVideoView107   StaticVideoView(uint32 ssrc, int width, int height, int framerate)
108       : ssrc(ssrc),
109         width(width),
110         height(height),
111         framerate(framerate),
112         preference(0) {}
113 
114   uint32 ssrc;
115   int width;
116   int height;
117   int framerate;
118   int preference;
119 };
120 
121 typedef std::vector<StaticVideoView> StaticVideoViews;
122 
123 // Represents a whole view request message, which contains many views.
124 struct ViewRequest {
125   StaticVideoViews static_video_views;
126 };
127 
128 // If the parent element (usually <jingle>) is a jingle view.
129 bool IsJingleViewRequest(const buzz::XmlElement* action_elem);
130 
131 // Parses a view request from the parent element (usually
132 // <jingle>). If it fails, it returns false and fills an error
133 // message.
134 bool ParseJingleViewRequest(const buzz::XmlElement* action_elem,
135                             ViewRequest* view_request,
136                             ParseError* error);
137 
138 // Serializes a view request to XML.  If it fails, returns false and
139 // fills in an error message.
140 bool WriteJingleViewRequest(const std::string& content_name,
141                             const ViewRequest& view,
142                             XmlElements* elems,
143                             WriteError* error);
144 
145 // TODO: Get rid of legacy source notify and replace with
146 // description-info as soon as reflector is capable of sending it.
147 bool IsSourcesNotify(const buzz::XmlElement* action_elem);
148 
149 // If the given elem has <streams>.
150 bool HasJingleStreams(const buzz::XmlElement* desc_elem);
151 
152 // Parses streams from a jingle <description>.  If it fails, returns
153 // false and fills an error message.
154 bool ParseJingleStreams(const buzz::XmlElement* desc_elem,
155                         std::vector<StreamParams>* streams,
156                         ParseError* error);
157 
158 // Write a <streams> element to the parent_elem.
159 void WriteJingleStreams(const std::vector<StreamParams>& streams,
160                         buzz::XmlElement* parent_elem);
161 
162 }  // namespace cricket
163 
164 #endif  // TALK_SESSION_PHONE_MEDIAMESSAGES_H_
165