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