blob: 52a94415b7846af4d59b100bf0aaad4188c1c3b6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains structures for describing SSRCs from a media source such
12// as a MediaStreamTrack when it is sent across an RTP session. Multiple media
13// sources may be sent across the same RTP session, each of them will be
14// described by one StreamParams object
15// SsrcGroup is used to describe the relationship between the SSRCs that
16// are used for this media source.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000017// E.x: Consider a source that is sent as 3 simulcast streams
18// Let the simulcast elements have SSRC 10, 20, 30.
19// Let each simulcast element use FEC and let the protection packets have
20// SSRC 11,21,31.
21// To describe this 4 SsrcGroups are needed,
22// StreamParams would then contain ssrc = {10,11,20,21,30,31} and
23// ssrc_groups = {{SIM,{10,20,30}, {FEC,{10,11}, {FEC, {20,21}, {FEC {30,31}}}
24// Please see RFC 5576.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#ifndef MEDIA_BASE_STREAMPARAMS_H_
27#define MEDIA_BASE_STREAMPARAMS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
pbosc7c26a02017-01-02 08:42:32 -080029#include <stdint.h>
30
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <set>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <vector>
35
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/constructormagic.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38namespace cricket {
39
40extern const char kFecSsrcGroupSemantics[];
brandtr9688e382016-11-22 00:59:48 -080041extern const char kFecFrSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042extern const char kFidSsrcGroupSemantics[];
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000043extern const char kSimSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45struct SsrcGroup {
Peter Boström0c4e06b2015-10-07 12:23:21 +020046 SsrcGroup(const std::string& usage, const std::vector<uint32_t>& ssrcs)
47 : semantics(usage), ssrcs(ssrcs) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49 bool operator==(const SsrcGroup& other) const {
50 return (semantics == other.semantics && ssrcs == other.ssrcs);
51 }
52 bool operator!=(const SsrcGroup &other) const {
53 return !(*this == other);
54 }
55
56 bool has_semantics(const std::string& semantics) const;
57
58 std::string ToString() const;
59
60 std::string semantics; // e.g FIX, FEC, SIM.
Peter Boström0c4e06b2015-10-07 12:23:21 +020061 std::vector<uint32_t> ssrcs; // SSRCs of this type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062};
63
64struct StreamParams {
Peter Boström0c4e06b2015-10-07 12:23:21 +020065 static StreamParams CreateLegacy(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 StreamParams stream;
67 stream.ssrcs.push_back(ssrc);
68 return stream;
69 }
70
71 bool operator==(const StreamParams& other) const {
Steve Anton5a26a3a2018-02-28 11:38:47 -080072 return (groupid == other.groupid && id == other.id &&
73 ssrcs == other.ssrcs && ssrc_groups == other.ssrc_groups &&
74 type == other.type && display == other.display &&
Seth Hampson5b4f0752018-04-02 16:31:36 -070075 cname == other.cname && stream_ids_ == other.stream_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 }
77 bool operator!=(const StreamParams &other) const {
78 return !(*this == other);
79 }
80
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 uint32_t first_ssrc() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 if (ssrcs.empty()) {
83 return 0;
84 }
85
86 return ssrcs[0];
87 }
88 bool has_ssrcs() const {
89 return !ssrcs.empty();
90 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020091 bool has_ssrc(uint32_t ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
93 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 bool has_ssrc_groups() const {
96 return !ssrc_groups.empty();
97 }
98 bool has_ssrc_group(const std::string& semantics) const {
99 return (get_ssrc_group(semantics) != NULL);
100 }
101 const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
102 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
103 it != ssrc_groups.end(); ++it) {
104 if (it->has_semantics(semantics)) {
105 return &(*it);
106 }
107 }
108 return NULL;
109 }
110
111 // Convenience function to add an FID ssrc for a primary_ssrc
112 // that's already been added.
brandtr9688e382016-11-22 00:59:48 -0800113 bool AddFidSsrc(uint32_t primary_ssrc, uint32_t fid_ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
115 }
116
117 // Convenience function to lookup the FID ssrc for a primary_ssrc.
118 // Returns false if primary_ssrc not found or FID not defined for it.
brandtr9688e382016-11-22 00:59:48 -0800119 bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
121 }
122
brandtr9688e382016-11-22 00:59:48 -0800123 // Convenience function to add an FEC-FR ssrc for a primary_ssrc
124 // that's already been added.
125 bool AddFecFrSsrc(uint32_t primary_ssrc, uint32_t fecfr_ssrc) {
126 return AddSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
127 }
128
129 // Convenience function to lookup the FEC-FR ssrc for a primary_ssrc.
130 // Returns false if primary_ssrc not found or FEC-FR not defined for it.
131 bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
132 return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
133 }
134
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000135 // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
136 // the first SSRC otherwise.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000138
139 // Convenience to get all the FID SSRCs for the given primary ssrcs.
140 // If a given primary SSRC does not have a FID SSRC, the list of FID
141 // SSRCS will be smaller than the list of primary SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
143 std::vector<uint32_t>* fid_ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000144
Seth Hampson845e8782018-03-02 11:34:10 -0800145 // Stream ids serialized to SDP.
146 std::vector<std::string> stream_ids() const;
147 void set_stream_ids(const std::vector<std::string>& stream_ids);
Steve Antonac7539e2018-02-26 17:20:29 -0800148
Seth Hampson845e8782018-03-02 11:34:10 -0800149 // Returns the first stream id or "" if none exist. This method exists only
Steve Anton5a26a3a2018-02-28 11:38:47 -0800150 // as temporary backwards compatibility with the old sync_label.
Seth Hampson845e8782018-03-02 11:34:10 -0800151 std::string first_stream_id() const;
Steve Anton5a26a3a2018-02-28 11:38:47 -0800152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 std::string ToString() const;
154
155 // Resource of the MUC jid of the participant of with this stream.
156 // For 1:1 calls, should be left empty (which means remote streams
157 // and local streams should not be mixed together).
158 std::string groupid;
159 // Unique per-groupid, not across all groupids
160 std::string id;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 std::vector<uint32_t> ssrcs; // All SSRCs for this source
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 std::vector<SsrcGroup> ssrc_groups; // e.g. FID, FEC, SIM
163 // Examples: "camera", "screencast"
164 std::string type;
165 // Friendly name describing stream
166 std::string display;
167 std::string cname; // RTCP CNAME
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
169 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 bool AddSecondarySsrc(const std::string& semantics,
171 uint32_t primary_ssrc,
172 uint32_t secondary_ssrc);
173 bool GetSecondarySsrc(const std::string& semantics,
174 uint32_t primary_ssrc,
175 uint32_t* secondary_ssrc) const;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700176
177 std::vector<std::string> stream_ids_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178};
179
180// A Stream can be selected by either groupid+id or ssrc.
181struct StreamSelector {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
184 StreamSelector(const std::string& groupid,
185 const std::string& streamid) :
186 ssrc(0),
187 groupid(groupid),
188 streamid(streamid) {
189 }
190
Seth Hampson23ffbe72018-03-30 16:59:31 -0700191 explicit StreamSelector(const std::string& streamid)
192 : ssrc(0), streamid(streamid) {}
193
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 bool Matches(const StreamParams& stream) const {
195 if (ssrc == 0) {
196 return stream.groupid == groupid && stream.id == streamid;
197 } else {
198 return stream.has_ssrc(ssrc);
199 }
200 }
201
Peter Boström0c4e06b2015-10-07 12:23:21 +0200202 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 std::string groupid;
204 std::string streamid;
205};
206
207typedef std::vector<StreamParams> StreamParamsVec;
208
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000209// A collection of audio and video and data streams. Most of the
210// methods are merely for convenience. Many of these methods are keyed
211// by ssrc, which is the source identifier in the RTP spec
212// (http://tools.ietf.org/html/rfc3550).
213// TODO(pthatcher): Add basic unit test for these.
214// See https://code.google.com/p/webrtc/issues/detail?id=4107
215struct MediaStreams {
216 public:
217 MediaStreams() {}
218 void CopyFrom(const MediaStreams& sources);
219
220 bool empty() const {
221 return audio_.empty() && video_.empty() && data_.empty();
222 }
223
224 std::vector<StreamParams>* mutable_audio() { return &audio_; }
225 std::vector<StreamParams>* mutable_video() { return &video_; }
226 std::vector<StreamParams>* mutable_data() { return &data_; }
227 const std::vector<StreamParams>& audio() const { return audio_; }
228 const std::vector<StreamParams>& video() const { return video_; }
229 const std::vector<StreamParams>& data() const { return data_; }
230
231 // Gets a stream, returning true if found.
232 bool GetAudioStream(
233 const StreamSelector& selector, StreamParams* stream);
234 bool GetVideoStream(
235 const StreamSelector& selector, StreamParams* stream);
236 bool GetDataStream(
237 const StreamSelector& selector, StreamParams* stream);
238 // Adds a stream.
239 void AddAudioStream(const StreamParams& stream);
240 void AddVideoStream(const StreamParams& stream);
241 void AddDataStream(const StreamParams& stream);
242 // Removes a stream, returning true if found and removed.
243 bool RemoveAudioStream(const StreamSelector& selector);
244 bool RemoveVideoStream(const StreamSelector& selector);
245 bool RemoveDataStream(const StreamSelector& selector);
246
247 private:
248 std::vector<StreamParams> audio_;
249 std::vector<StreamParams> video_;
250 std::vector<StreamParams> data_;
251
henrikg3c089d72015-09-16 05:37:44 -0700252 RTC_DISALLOW_COPY_AND_ASSIGN(MediaStreams);
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000253};
254
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000255template <class Condition>
256const StreamParams* GetStream(const StreamParamsVec& streams,
257 Condition condition) {
258 StreamParamsVec::const_iterator found =
259 std::find_if(streams.begin(), streams.end(), condition);
260 return found == streams.end() ? nullptr : &(*found);
261}
262
deadbeef2f425aa2017-04-14 10:41:32 -0700263template <class Condition>
264StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
265 StreamParamsVec::iterator found =
266 std::find_if(streams.begin(), streams.end(), condition);
267 return found == streams.end() ? nullptr : &(*found);
268}
269
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000270inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271 uint32_t ssrc) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272 return GetStream(streams,
273 [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
274}
275
276inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
277 const std::string& groupid,
278 const std::string& id) {
deadbeef2f425aa2017-04-14 10:41:32 -0700279 return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
280 return sp.groupid == groupid && sp.id == id;
281 });
282}
283
284inline StreamParams* GetStreamByIds(StreamParamsVec& streams,
285 const std::string& groupid,
286 const std::string& id) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000287 return GetStream(streams,
288 [&groupid, &id](const StreamParams& sp) {
289 return sp.groupid == groupid && sp.id == id;
290 });
291}
292
293inline const StreamParams* GetStream(const StreamParamsVec& streams,
294 const StreamSelector& selector) {
295 return GetStream(streams,
296 [&selector](const StreamParams& sp) { return selector.Matches(sp); });
297}
298
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000299template <class Condition>
300bool RemoveStream(StreamParamsVec* streams, Condition condition) {
301 auto iter(std::remove_if(streams->begin(), streams->end(), condition));
302 if (iter == streams->end())
303 return false;
304 streams->erase(iter, streams->end());
305 return true;
306}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307
308// Removes the stream from streams. Returns true if a stream is
309// found and removed.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000310inline bool RemoveStream(StreamParamsVec* streams,
311 const StreamSelector& selector) {
312 return RemoveStream(streams,
313 [&selector](const StreamParams& sp) { return selector.Matches(sp); });
314}
Peter Boström0c4e06b2015-10-07 12:23:21 +0200315inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000316 return RemoveStream(streams,
317 [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
318}
319inline bool RemoveStreamByIds(StreamParamsVec* streams,
320 const std::string& groupid,
321 const std::string& id) {
322 return RemoveStream(streams,
323 [&groupid, &id](const StreamParams& sp) {
324 return sp.groupid == groupid && sp.id == id;
325 });
326}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000328// Checks if |sp| defines parameters for a single primary stream. There may
brandtr9688e382016-11-22 00:59:48 -0800329// be an RTX stream or a FlexFEC stream (or both) associated with the primary
330// stream. Leaving as non-static so we can test this function.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000331bool IsOneSsrcStream(const StreamParams& sp);
332
333// Checks if |sp| defines parameters for one Simulcast stream. There may be RTX
334// streams associated with the simulcast streams. Leaving as non-static so we
335// can test this function.
336bool IsSimulcastStream(const StreamParams& sp);
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338} // namespace cricket
339
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200340#endif // MEDIA_BASE_STREAMPARAMS_H_