blob: 6e24fa3b96d9bfe1f6fca045edf48da0f1aaaf92 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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// This file contains structures for describing SSRCs from a media source such
29// as a MediaStreamTrack when it is sent across an RTP session. Multiple media
30// sources may be sent across the same RTP session, each of them will be
31// described by one StreamParams object
32// SsrcGroup is used to describe the relationship between the SSRCs that
33// are used for this media source.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000034// E.x: Consider a source that is sent as 3 simulcast streams
35// Let the simulcast elements have SSRC 10, 20, 30.
36// Let each simulcast element use FEC and let the protection packets have
37// SSRC 11,21,31.
38// To describe this 4 SsrcGroups are needed,
39// StreamParams would then contain ssrc = {10,11,20,21,30,31} and
40// ssrc_groups = {{SIM,{10,20,30}, {FEC,{10,11}, {FEC, {20,21}, {FEC {30,31}}}
41// Please see RFC 5576.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43#ifndef TALK_MEDIA_BASE_STREAMPARAMS_H_
44#define TALK_MEDIA_BASE_STREAMPARAMS_H_
45
46#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047#include <set>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049#include <vector>
50
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051#include "webrtc/base/basictypes.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53namespace cricket {
54
55extern const char kFecSsrcGroupSemantics[];
56extern const char kFidSsrcGroupSemantics[];
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000057extern const char kSimSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59struct SsrcGroup {
60 SsrcGroup(const std::string& usage, const std::vector<uint32>& ssrcs)
61 : semantics(usage), ssrcs(ssrcs) {
62 }
63
64 bool operator==(const SsrcGroup& other) const {
65 return (semantics == other.semantics && ssrcs == other.ssrcs);
66 }
67 bool operator!=(const SsrcGroup &other) const {
68 return !(*this == other);
69 }
70
71 bool has_semantics(const std::string& semantics) const;
72
73 std::string ToString() const;
74
75 std::string semantics; // e.g FIX, FEC, SIM.
76 std::vector<uint32> ssrcs; // SSRCs of this type.
77};
78
79struct StreamParams {
80 static StreamParams CreateLegacy(uint32 ssrc) {
81 StreamParams stream;
82 stream.ssrcs.push_back(ssrc);
83 return stream;
84 }
85
86 bool operator==(const StreamParams& other) const {
87 return (groupid == other.groupid &&
88 id == other.id &&
89 ssrcs == other.ssrcs &&
90 ssrc_groups == other.ssrc_groups &&
91 type == other.type &&
92 display == other.display &&
93 cname == other.cname &&
94 sync_label == other.sync_label);
95 }
96 bool operator!=(const StreamParams &other) const {
97 return !(*this == other);
98 }
99
100 uint32 first_ssrc() const {
101 if (ssrcs.empty()) {
102 return 0;
103 }
104
105 return ssrcs[0];
106 }
107 bool has_ssrcs() const {
108 return !ssrcs.empty();
109 }
110 bool has_ssrc(uint32 ssrc) const {
111 return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
112 }
113 void add_ssrc(uint32 ssrc) {
114 ssrcs.push_back(ssrc);
115 }
116 bool has_ssrc_groups() const {
117 return !ssrc_groups.empty();
118 }
119 bool has_ssrc_group(const std::string& semantics) const {
120 return (get_ssrc_group(semantics) != NULL);
121 }
122 const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
123 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
124 it != ssrc_groups.end(); ++it) {
125 if (it->has_semantics(semantics)) {
126 return &(*it);
127 }
128 }
129 return NULL;
130 }
131
132 // Convenience function to add an FID ssrc for a primary_ssrc
133 // that's already been added.
134 inline bool AddFidSsrc(uint32 primary_ssrc, uint32 fid_ssrc) {
135 return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
136 }
137
138 // Convenience function to lookup the FID ssrc for a primary_ssrc.
139 // Returns false if primary_ssrc not found or FID not defined for it.
140 inline bool GetFidSsrc(uint32 primary_ssrc, uint32* fid_ssrc) const {
141 return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
142 }
143
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000144 // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
145 // the first SSRC otherwise.
146 void GetPrimarySsrcs(std::vector<uint32>* ssrcs) const;
147
148 // Convenience to get all the FID SSRCs for the given primary ssrcs.
149 // If a given primary SSRC does not have a FID SSRC, the list of FID
150 // SSRCS will be smaller than the list of primary SSRCs.
151 void GetFidSsrcs(const std::vector<uint32>& primary_ssrcs,
152 std::vector<uint32>* fid_ssrcs) const;
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 std::string ToString() const;
155
156 // Resource of the MUC jid of the participant of with this stream.
157 // For 1:1 calls, should be left empty (which means remote streams
158 // and local streams should not be mixed together).
159 std::string groupid;
160 // Unique per-groupid, not across all groupids
161 std::string id;
162 std::vector<uint32> ssrcs; // All SSRCs for this source
163 std::vector<SsrcGroup> ssrc_groups; // e.g. FID, FEC, SIM
164 // Examples: "camera", "screencast"
165 std::string type;
166 // Friendly name describing stream
167 std::string display;
168 std::string cname; // RTCP CNAME
169 std::string sync_label; // Friendly name of cname.
170
171 private:
172 bool AddSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
173 uint32 secondary_ssrc);
174 bool GetSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
175 uint32* secondary_ssrc) const;
176};
177
178// A Stream can be selected by either groupid+id or ssrc.
179struct StreamSelector {
180 explicit StreamSelector(uint32 ssrc) :
181 ssrc(ssrc) {
182 }
183
184 StreamSelector(const std::string& groupid,
185 const std::string& streamid) :
186 ssrc(0),
187 groupid(groupid),
188 streamid(streamid) {
189 }
190
191 bool Matches(const StreamParams& stream) const {
192 if (ssrc == 0) {
193 return stream.groupid == groupid && stream.id == streamid;
194 } else {
195 return stream.has_ssrc(ssrc);
196 }
197 }
198
199 uint32 ssrc;
200 std::string groupid;
201 std::string streamid;
202};
203
204typedef std::vector<StreamParams> StreamParamsVec;
205
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000206// A collection of audio and video and data streams. Most of the
207// methods are merely for convenience. Many of these methods are keyed
208// by ssrc, which is the source identifier in the RTP spec
209// (http://tools.ietf.org/html/rfc3550).
210// TODO(pthatcher): Add basic unit test for these.
211// See https://code.google.com/p/webrtc/issues/detail?id=4107
212struct MediaStreams {
213 public:
214 MediaStreams() {}
215 void CopyFrom(const MediaStreams& sources);
216
217 bool empty() const {
218 return audio_.empty() && video_.empty() && data_.empty();
219 }
220
221 std::vector<StreamParams>* mutable_audio() { return &audio_; }
222 std::vector<StreamParams>* mutable_video() { return &video_; }
223 std::vector<StreamParams>* mutable_data() { return &data_; }
224 const std::vector<StreamParams>& audio() const { return audio_; }
225 const std::vector<StreamParams>& video() const { return video_; }
226 const std::vector<StreamParams>& data() const { return data_; }
227
228 // Gets a stream, returning true if found.
229 bool GetAudioStream(
230 const StreamSelector& selector, StreamParams* stream);
231 bool GetVideoStream(
232 const StreamSelector& selector, StreamParams* stream);
233 bool GetDataStream(
234 const StreamSelector& selector, StreamParams* stream);
235 // Adds a stream.
236 void AddAudioStream(const StreamParams& stream);
237 void AddVideoStream(const StreamParams& stream);
238 void AddDataStream(const StreamParams& stream);
239 // Removes a stream, returning true if found and removed.
240 bool RemoveAudioStream(const StreamSelector& selector);
241 bool RemoveVideoStream(const StreamSelector& selector);
242 bool RemoveDataStream(const StreamSelector& selector);
243
244 private:
245 std::vector<StreamParams> audio_;
246 std::vector<StreamParams> video_;
247 std::vector<StreamParams> data_;
248
249 DISALLOW_COPY_AND_ASSIGN(MediaStreams);
250};
251
252// A request for a specific format of a specific stream.
253struct StaticVideoView {
254 StaticVideoView(const StreamSelector& selector,
255 int width, int height, int framerate)
256 : selector(selector),
257 width(width),
258 height(height),
259 framerate(framerate),
260 preference(0) {
261 }
262
263 StreamSelector selector;
264 int width;
265 int height;
266 int framerate;
267 int preference;
268};
269
270typedef std::vector<StaticVideoView> StaticVideoViews;
271
272// A request for several streams in various formats.
273struct ViewRequest {
274 StaticVideoViews static_video_views;
275};
276
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000277template <class Condition>
278const StreamParams* GetStream(const StreamParamsVec& streams,
279 Condition condition) {
280 StreamParamsVec::const_iterator found =
281 std::find_if(streams.begin(), streams.end(), condition);
282 return found == streams.end() ? nullptr : &(*found);
283}
284
285inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
286 uint32 ssrc) {
287 return GetStream(streams,
288 [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
289}
290
291inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
292 const std::string& groupid,
293 const std::string& id) {
294 return GetStream(streams,
295 [&groupid, &id](const StreamParams& sp) {
296 return sp.groupid == groupid && sp.id == id;
297 });
298}
299
300inline const StreamParams* GetStream(const StreamParamsVec& streams,
301 const StreamSelector& selector) {
302 return GetStream(streams,
303 [&selector](const StreamParams& sp) { return selector.Matches(sp); });
304}
305
306////////////////////////////////////////////////////////////////////////////////
307// Deprecated methods that will be removed one of these days.
308// Please use the methods with the same name above.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309bool GetStream(const StreamParamsVec& streams,
310 const StreamSelector& selector,
311 StreamParams* stream_out);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000312inline bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
313 StreamParams* stream_out) {
314 return GetStream(streams, StreamSelector(ssrc), stream_out);
315}
316inline bool GetStreamByIds(const StreamParamsVec& streams,
317 const std::string& groupid,
318 const std::string& id,
319 StreamParams* stream_out) {
320 return GetStream(streams, StreamSelector(groupid, id), stream_out);
321}
322// End deprecated methods.
323////////////////////////////////////////////////////////////////////////////////
324
325template <class Condition>
326bool RemoveStream(StreamParamsVec* streams, Condition condition) {
327 auto iter(std::remove_if(streams->begin(), streams->end(), condition));
328 if (iter == streams->end())
329 return false;
330 streams->erase(iter, streams->end());
331 return true;
332}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
334// Removes the stream from streams. Returns true if a stream is
335// found and removed.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000336inline bool RemoveStream(StreamParamsVec* streams,
337 const StreamSelector& selector) {
338 return RemoveStream(streams,
339 [&selector](const StreamParams& sp) { return selector.Matches(sp); });
340}
341inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc) {
342 return RemoveStream(streams,
343 [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
344}
345inline bool RemoveStreamByIds(StreamParamsVec* streams,
346 const std::string& groupid,
347 const std::string& id) {
348 return RemoveStream(streams,
349 [&groupid, &id](const StreamParams& sp) {
350 return sp.groupid == groupid && sp.id == id;
351 });
352}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000354// Checks if |sp| defines parameters for a single primary stream. There may
355// be an RTX stream associated with the primary stream. Leaving as non-static so
356// we can test this function.
357bool IsOneSsrcStream(const StreamParams& sp);
358
359// Checks if |sp| defines parameters for one Simulcast stream. There may be RTX
360// streams associated with the simulcast streams. Leaving as non-static so we
361// can test this function.
362bool IsSimulcastStream(const StreamParams& sp);
363
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364} // namespace cricket
365
366#endif // TALK_MEDIA_BASE_STREAMPARAMS_H_