blob: b8c37706dfdb1e801783562662c0e6004b6722a1 [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.
Amit Hilbuchc57d5732018-12-11 15:30:11 -080025// A spec-compliant way to achieve this is to use RIDs and Simulcast attribute
26// instead of the ssrc-group. In this method, the StreamParam object will
27// have multiple RidDescriptions, each corresponding to a simulcast layer
28// and the media section will have a simulcast attribute that indicates
29// that these layers are for the same source. This also removes the extra
30// lines for redundancy streams, as the same RIDs appear in the redundancy
31// packets.
32// Note: in the spec compliant simulcast scenario, some of the RIDs might be
33// alternatives for one another (such as different encodings for same data).
34// In the context of the StreamParams class, the notion of alternatives does
35// not exist and all the RIDs will describe different layers of the same source.
36// When the StreamParams class is used to configure the media engine, simulcast
37// considerations will be used to remove the alternative layers outside of this
38// class.
39// As an example, let the simulcast layers have RID 10, 20, 30.
40// StreamParams would contain rid = { 10, 20, 30 }.
41// MediaSection would contain SimulcastDescription specifying these rids.
42// a=simulcast:send 10;20;30 (or a=simulcast:send 10,20;30 or similar).
43// See https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13
44// and https://tools.ietf.org/html/draft-ietf-mmusic-rid-15.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
Steve Anton10542f22019-01-11 09:11:00 -080046#ifndef MEDIA_BASE_STREAM_PARAMS_H_
47#define MEDIA_BASE_STREAM_PARAMS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
Yves Gerey3e707812018-11-28 16:47:49 +010049#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020050
Yves Gerey3e707812018-11-28 16:47:49 +010051#include <cstdint>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000052#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053#include <vector>
54
Steve Anton2c9ebef2019-01-28 17:27:58 -080055#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "media/base/rid_description.h"
57#include "rtc_base/constructor_magic.h"
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080058#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60namespace cricket {
61
62extern const char kFecSsrcGroupSemantics[];
brandtr9688e382016-11-22 00:59:48 -080063extern const char kFecFrSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064extern const char kFidSsrcGroupSemantics[];
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000065extern const char kSimSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67struct SsrcGroup {
Paulina Hensman11b34f42018-04-09 14:24:52 +020068 SsrcGroup(const std::string& usage, const std::vector<uint32_t>& ssrcs);
69 SsrcGroup(const SsrcGroup&);
70 SsrcGroup(SsrcGroup&&);
71 ~SsrcGroup();
72 SsrcGroup& operator=(const SsrcGroup&);
73 SsrcGroup& operator=(SsrcGroup&&);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75 bool operator==(const SsrcGroup& other) const {
76 return (semantics == other.semantics && ssrcs == other.ssrcs);
77 }
Yves Gerey665174f2018-06-19 15:03:05 +020078 bool operator!=(const SsrcGroup& other) const { return !(*this == other); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
80 bool has_semantics(const std::string& semantics) const;
81
82 std::string ToString() const;
83
Yves Gerey665174f2018-06-19 15:03:05 +020084 std::string semantics; // e.g FIX, FEC, SIM.
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 std::vector<uint32_t> ssrcs; // SSRCs of this type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
Seth Hampson7435b842018-04-10 10:52:10 -070088// StreamParams is used to represent a sender/track in a SessionDescription.
89// In Plan B, this means that multiple StreamParams can exist within one
90// MediaContentDescription, while in UnifiedPlan this means that there is one
91// StreamParams per MediaContentDescription.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092struct StreamParams {
Paulina Hensman11b34f42018-04-09 14:24:52 +020093 StreamParams();
94 StreamParams(const StreamParams&);
95 StreamParams(StreamParams&&);
96 ~StreamParams();
97 StreamParams& operator=(const StreamParams&);
98 StreamParams& operator=(StreamParams&&);
99
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 static StreamParams CreateLegacy(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 StreamParams stream;
102 stream.ssrcs.push_back(ssrc);
103 return stream;
104 }
105
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800106 bool operator==(const StreamParams& other) const;
Yves Gerey665174f2018-06-19 15:03:05 +0200107 bool operator!=(const StreamParams& other) const { return !(*this == other); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108
Peter Boström0c4e06b2015-10-07 12:23:21 +0200109 uint32_t first_ssrc() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 if (ssrcs.empty()) {
111 return 0;
112 }
113
114 return ssrcs[0];
115 }
Yves Gerey665174f2018-06-19 15:03:05 +0200116 bool has_ssrcs() const { return !ssrcs.empty(); }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200117 bool has_ssrc(uint32_t ssrc) const {
Steve Anton2c9ebef2019-01-28 17:27:58 -0800118 return absl::c_linear_search(ssrcs, ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200120 void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
Yves Gerey665174f2018-06-19 15:03:05 +0200121 bool has_ssrc_groups() const { return !ssrc_groups.empty(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 bool has_ssrc_group(const std::string& semantics) const {
123 return (get_ssrc_group(semantics) != NULL);
124 }
125 const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
Steve Antonc6643142019-02-15 10:50:44 -0800126 for (const SsrcGroup& ssrc_group : ssrc_groups) {
127 if (ssrc_group.has_semantics(semantics)) {
128 return &ssrc_group;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 }
130 }
131 return NULL;
132 }
133
134 // Convenience function to add an FID ssrc for a primary_ssrc
135 // that's already been added.
brandtr9688e382016-11-22 00:59:48 -0800136 bool AddFidSsrc(uint32_t primary_ssrc, uint32_t fid_ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
138 }
139
140 // Convenience function to lookup the FID ssrc for a primary_ssrc.
141 // Returns false if primary_ssrc not found or FID not defined for it.
brandtr9688e382016-11-22 00:59:48 -0800142 bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
144 }
145
brandtr9688e382016-11-22 00:59:48 -0800146 // Convenience function to add an FEC-FR ssrc for a primary_ssrc
147 // that's already been added.
148 bool AddFecFrSsrc(uint32_t primary_ssrc, uint32_t fecfr_ssrc) {
149 return AddSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
150 }
151
152 // Convenience function to lookup the FEC-FR ssrc for a primary_ssrc.
153 // Returns false if primary_ssrc not found or FEC-FR not defined for it.
154 bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
155 return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
156 }
157
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800158 // Convenience function to populate the StreamParams with the requested number
159 // of SSRCs along with accompanying FID and FEC-FR ssrcs if requested.
160 // SSRCs are generated using the given generator.
161 void GenerateSsrcs(int num_layers,
162 bool generate_fid,
163 bool generate_fec_fr,
164 rtc::UniqueRandomIdGenerator* ssrc_generator);
165
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000166 // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
167 // the first SSRC otherwise.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200168 void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000169
170 // Convenience to get all the FID SSRCs for the given primary ssrcs.
171 // If a given primary SSRC does not have a FID SSRC, the list of FID
172 // SSRCS will be smaller than the list of primary SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200173 void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
174 std::vector<uint32_t>* fid_ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000175
Seth Hampson845e8782018-03-02 11:34:10 -0800176 // Stream ids serialized to SDP.
177 std::vector<std::string> stream_ids() const;
178 void set_stream_ids(const std::vector<std::string>& stream_ids);
Steve Antonac7539e2018-02-26 17:20:29 -0800179
Seth Hampson845e8782018-03-02 11:34:10 -0800180 // Returns the first stream id or "" if none exist. This method exists only
Steve Anton5a26a3a2018-02-28 11:38:47 -0800181 // as temporary backwards compatibility with the old sync_label.
Seth Hampson845e8782018-03-02 11:34:10 -0800182 std::string first_stream_id() const;
Steve Anton5a26a3a2018-02-28 11:38:47 -0800183
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 std::string ToString() const;
185
186 // Resource of the MUC jid of the participant of with this stream.
187 // For 1:1 calls, should be left empty (which means remote streams
Seth Hampson7435b842018-04-10 10:52:10 -0700188 // and local streams should not be mixed together). This is not used
189 // internally and should be deprecated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 std::string groupid;
Seth Hampson7435b842018-04-10 10:52:10 -0700191 // A unique identifier of the StreamParams object. When the SDP is created,
192 // this comes from the track ID of the sender that the StreamParams object
193 // is associated with.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 std::string id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700195 // There may be no SSRCs stored in unsignaled case when stream_ids are
196 // signaled with a=msid lines.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200197 std::vector<uint32_t> ssrcs; // All SSRCs for this source
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 std::vector<SsrcGroup> ssrc_groups; // e.g. FID, FEC, SIM
Yves Gerey665174f2018-06-19 15:03:05 +0200199 std::string cname; // RTCP CNAME
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800201 // RID functionality according to
202 // https://tools.ietf.org/html/draft-ietf-mmusic-rid-15
203 // Each layer can be represented by a RID identifier and can also have
204 // restrictions (such as max-width, max-height, etc.)
205 // If the track has multiple layers (ex. Simulcast), each layer will be
206 // represented by a RID.
207 bool has_rids() const { return !rids_.empty(); }
208 const std::vector<RidDescription>& rids() const { return rids_; }
209 void set_rids(const std::vector<RidDescription>& rids) { rids_ = rids; }
210
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200212 bool AddSecondarySsrc(const std::string& semantics,
213 uint32_t primary_ssrc,
214 uint32_t secondary_ssrc);
215 bool GetSecondarySsrc(const std::string& semantics,
216 uint32_t primary_ssrc,
217 uint32_t* secondary_ssrc) const;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700218
Seth Hampson7435b842018-04-10 10:52:10 -0700219 // The stream IDs of the sender that the StreamParams object is associated
220 // with. In Plan B this should always be size of 1, while in Unified Plan this
221 // could be none or multiple stream IDs.
Seth Hampson5b4f0752018-04-02 16:31:36 -0700222 std::vector<std::string> stream_ids_;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800223
224 std::vector<RidDescription> rids_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225};
226
227// A Stream can be selected by either groupid+id or ssrc.
228struct StreamSelector {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200229 explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
Yves Gerey665174f2018-06-19 15:03:05 +0200231 StreamSelector(const std::string& groupid, const std::string& streamid)
232 : ssrc(0), groupid(groupid), streamid(streamid) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
Seth Hampson23ffbe72018-03-30 16:59:31 -0700234 explicit StreamSelector(const std::string& streamid)
235 : ssrc(0), streamid(streamid) {}
236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 bool Matches(const StreamParams& stream) const {
238 if (ssrc == 0) {
239 return stream.groupid == groupid && stream.id == streamid;
240 } else {
241 return stream.has_ssrc(ssrc);
242 }
243 }
244
Peter Boström0c4e06b2015-10-07 12:23:21 +0200245 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 std::string groupid;
247 std::string streamid;
248};
249
250typedef std::vector<StreamParams> StreamParamsVec;
251
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000252template <class Condition>
253const StreamParams* GetStream(const StreamParamsVec& streams,
254 Condition condition) {
Steve Anton2c9ebef2019-01-28 17:27:58 -0800255 auto found = absl::c_find_if(streams, condition);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000256 return found == streams.end() ? nullptr : &(*found);
257}
258
deadbeef2f425aa2017-04-14 10:41:32 -0700259template <class Condition>
260StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
Steve Anton2c9ebef2019-01-28 17:27:58 -0800261 auto found = absl::c_find_if(streams, condition);
deadbeef2f425aa2017-04-14 10:41:32 -0700262 return found == streams.end() ? nullptr : &(*found);
263}
264
Seth Hampson5897a6e2018-04-03 11:16:33 -0700265inline bool HasStreamWithNoSsrcs(const StreamParamsVec& streams) {
266 return GetStream(streams,
267 [](const StreamParams& sp) { return !sp.has_ssrcs(); });
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) {
Yves Gerey665174f2018-06-19 15:03:05 +0200272 return GetStream(
273 streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000274}
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) {
Yves Gerey665174f2018-06-19 15:03:05 +0200287 return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
288 return sp.groupid == groupid && sp.id == id;
289 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000290}
291
292inline const StreamParams* GetStream(const StreamParamsVec& streams,
293 const StreamSelector& selector) {
Yves Gerey665174f2018-06-19 15:03:05 +0200294 return GetStream(streams, [&selector](const StreamParams& sp) {
295 return selector.Matches(sp);
296 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000297}
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,
Yves Gerey665174f2018-06-19 15:03:05 +0200311 const StreamSelector& selector) {
312 return RemoveStream(streams, [&selector](const StreamParams& sp) {
313 return selector.Matches(sp);
314 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000315}
Peter Boström0c4e06b2015-10-07 12:23:21 +0200316inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
Yves Gerey665174f2018-06-19 15:03:05 +0200317 return RemoveStream(
318 streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000319}
320inline bool RemoveStreamByIds(StreamParamsVec* streams,
321 const std::string& groupid,
322 const std::string& id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200323 return RemoveStream(streams, [&groupid, &id](const StreamParams& sp) {
324 return sp.groupid == groupid && sp.id == id;
325 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000326}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328} // namespace cricket
329
Steve Anton10542f22019-01-11 09:11:00 -0800330#endif // MEDIA_BASE_STREAM_PARAMS_H_