blob: f1285329c89b7b89689c5701294f58d8c604520f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#ifndef MEDIA_BASE_STREAMPARAMS_H_
47#define MEDIA_BASE_STREAMPARAMS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
Yves Gerey3e707812018-11-28 16:47:49 +010049#include <stddef.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050#include <algorithm>
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
Amit Hilbuchc57d5732018-12-11 15:30:11 -080055#include "media/base/riddescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/constructormagic.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58namespace cricket {
59
60extern const char kFecSsrcGroupSemantics[];
brandtr9688e382016-11-22 00:59:48 -080061extern const char kFecFrSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062extern const char kFidSsrcGroupSemantics[];
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000063extern const char kSimSsrcGroupSemantics[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65struct SsrcGroup {
Paulina Hensman11b34f42018-04-09 14:24:52 +020066 SsrcGroup(const std::string& usage, const std::vector<uint32_t>& ssrcs);
67 SsrcGroup(const SsrcGroup&);
68 SsrcGroup(SsrcGroup&&);
69 ~SsrcGroup();
70 SsrcGroup& operator=(const SsrcGroup&);
71 SsrcGroup& operator=(SsrcGroup&&);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 bool operator==(const SsrcGroup& other) const {
74 return (semantics == other.semantics && ssrcs == other.ssrcs);
75 }
Yves Gerey665174f2018-06-19 15:03:05 +020076 bool operator!=(const SsrcGroup& other) const { return !(*this == other); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78 bool has_semantics(const std::string& semantics) const;
79
80 std::string ToString() const;
81
Yves Gerey665174f2018-06-19 15:03:05 +020082 std::string semantics; // e.g FIX, FEC, SIM.
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 std::vector<uint32_t> ssrcs; // SSRCs of this type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084};
85
Seth Hampson7435b842018-04-10 10:52:10 -070086// StreamParams is used to represent a sender/track in a SessionDescription.
87// In Plan B, this means that multiple StreamParams can exist within one
88// MediaContentDescription, while in UnifiedPlan this means that there is one
89// StreamParams per MediaContentDescription.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090struct StreamParams {
Paulina Hensman11b34f42018-04-09 14:24:52 +020091 StreamParams();
92 StreamParams(const StreamParams&);
93 StreamParams(StreamParams&&);
94 ~StreamParams();
95 StreamParams& operator=(const StreamParams&);
96 StreamParams& operator=(StreamParams&&);
97
Peter Boström0c4e06b2015-10-07 12:23:21 +020098 static StreamParams CreateLegacy(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 StreamParams stream;
100 stream.ssrcs.push_back(ssrc);
101 return stream;
102 }
103
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800104 bool operator==(const StreamParams& other) const;
Yves Gerey665174f2018-06-19 15:03:05 +0200105 bool operator!=(const StreamParams& other) const { return !(*this == other); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
Peter Boström0c4e06b2015-10-07 12:23:21 +0200107 uint32_t first_ssrc() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 if (ssrcs.empty()) {
109 return 0;
110 }
111
112 return ssrcs[0];
113 }
Yves Gerey665174f2018-06-19 15:03:05 +0200114 bool has_ssrcs() const { return !ssrcs.empty(); }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115 bool has_ssrc(uint32_t ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
117 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200118 void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
Yves Gerey665174f2018-06-19 15:03:05 +0200119 bool has_ssrc_groups() const { return !ssrc_groups.empty(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 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.
brandtr9688e382016-11-22 00:59:48 -0800135 bool AddFidSsrc(uint32_t primary_ssrc, uint32_t fid_ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 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.
brandtr9688e382016-11-22 00:59:48 -0800141 bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
143 }
144
brandtr9688e382016-11-22 00:59:48 -0800145 // Convenience function to add an FEC-FR ssrc for a primary_ssrc
146 // that's already been added.
147 bool AddFecFrSsrc(uint32_t primary_ssrc, uint32_t fecfr_ssrc) {
148 return AddSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
149 }
150
151 // Convenience function to lookup the FEC-FR ssrc for a primary_ssrc.
152 // Returns false if primary_ssrc not found or FEC-FR not defined for it.
153 bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
154 return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
155 }
156
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000157 // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
158 // the first SSRC otherwise.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000160
161 // Convenience to get all the FID SSRCs for the given primary ssrcs.
162 // If a given primary SSRC does not have a FID SSRC, the list of FID
163 // SSRCS will be smaller than the list of primary SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
165 std::vector<uint32_t>* fid_ssrcs) const;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000166
Seth Hampson845e8782018-03-02 11:34:10 -0800167 // Stream ids serialized to SDP.
168 std::vector<std::string> stream_ids() const;
169 void set_stream_ids(const std::vector<std::string>& stream_ids);
Steve Antonac7539e2018-02-26 17:20:29 -0800170
Seth Hampson845e8782018-03-02 11:34:10 -0800171 // Returns the first stream id or "" if none exist. This method exists only
Steve Anton5a26a3a2018-02-28 11:38:47 -0800172 // as temporary backwards compatibility with the old sync_label.
Seth Hampson845e8782018-03-02 11:34:10 -0800173 std::string first_stream_id() const;
Steve Anton5a26a3a2018-02-28 11:38:47 -0800174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 std::string ToString() const;
176
177 // Resource of the MUC jid of the participant of with this stream.
178 // For 1:1 calls, should be left empty (which means remote streams
Seth Hampson7435b842018-04-10 10:52:10 -0700179 // and local streams should not be mixed together). This is not used
180 // internally and should be deprecated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 std::string groupid;
Seth Hampson7435b842018-04-10 10:52:10 -0700182 // A unique identifier of the StreamParams object. When the SDP is created,
183 // this comes from the track ID of the sender that the StreamParams object
184 // is associated with.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 std::string id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700186 // There may be no SSRCs stored in unsignaled case when stream_ids are
187 // signaled with a=msid lines.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 std::vector<uint32_t> ssrcs; // All SSRCs for this source
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 std::vector<SsrcGroup> ssrc_groups; // e.g. FID, FEC, SIM
Yves Gerey665174f2018-06-19 15:03:05 +0200190 std::string cname; // RTCP CNAME
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800192 // RID functionality according to
193 // https://tools.ietf.org/html/draft-ietf-mmusic-rid-15
194 // Each layer can be represented by a RID identifier and can also have
195 // restrictions (such as max-width, max-height, etc.)
196 // If the track has multiple layers (ex. Simulcast), each layer will be
197 // represented by a RID.
198 bool has_rids() const { return !rids_.empty(); }
199 const std::vector<RidDescription>& rids() const { return rids_; }
200 void set_rids(const std::vector<RidDescription>& rids) { rids_ = rids; }
201
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200203 bool AddSecondarySsrc(const std::string& semantics,
204 uint32_t primary_ssrc,
205 uint32_t secondary_ssrc);
206 bool GetSecondarySsrc(const std::string& semantics,
207 uint32_t primary_ssrc,
208 uint32_t* secondary_ssrc) const;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700209
Seth Hampson7435b842018-04-10 10:52:10 -0700210 // The stream IDs of the sender that the StreamParams object is associated
211 // with. In Plan B this should always be size of 1, while in Unified Plan this
212 // could be none or multiple stream IDs.
Seth Hampson5b4f0752018-04-02 16:31:36 -0700213 std::vector<std::string> stream_ids_;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800214
215 std::vector<RidDescription> rids_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216};
217
218// A Stream can be selected by either groupid+id or ssrc.
219struct StreamSelector {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220 explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
Yves Gerey665174f2018-06-19 15:03:05 +0200222 StreamSelector(const std::string& groupid, const std::string& streamid)
223 : ssrc(0), groupid(groupid), streamid(streamid) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224
Seth Hampson23ffbe72018-03-30 16:59:31 -0700225 explicit StreamSelector(const std::string& streamid)
226 : ssrc(0), streamid(streamid) {}
227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 bool Matches(const StreamParams& stream) const {
229 if (ssrc == 0) {
230 return stream.groupid == groupid && stream.id == streamid;
231 } else {
232 return stream.has_ssrc(ssrc);
233 }
234 }
235
Peter Boström0c4e06b2015-10-07 12:23:21 +0200236 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 std::string groupid;
238 std::string streamid;
239};
240
241typedef std::vector<StreamParams> StreamParamsVec;
242
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000243// A collection of audio and video and data streams. Most of the
244// methods are merely for convenience. Many of these methods are keyed
245// by ssrc, which is the source identifier in the RTP spec
246// (http://tools.ietf.org/html/rfc3550).
247// TODO(pthatcher): Add basic unit test for these.
248// See https://code.google.com/p/webrtc/issues/detail?id=4107
249struct MediaStreams {
250 public:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200251 MediaStreams();
252 ~MediaStreams();
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000253 void CopyFrom(const MediaStreams& sources);
254
255 bool empty() const {
256 return audio_.empty() && video_.empty() && data_.empty();
257 }
258
259 std::vector<StreamParams>* mutable_audio() { return &audio_; }
260 std::vector<StreamParams>* mutable_video() { return &video_; }
261 std::vector<StreamParams>* mutable_data() { return &data_; }
262 const std::vector<StreamParams>& audio() const { return audio_; }
263 const std::vector<StreamParams>& video() const { return video_; }
264 const std::vector<StreamParams>& data() const { return data_; }
265
266 // Gets a stream, returning true if found.
Yves Gerey665174f2018-06-19 15:03:05 +0200267 bool GetAudioStream(const StreamSelector& selector, StreamParams* stream);
268 bool GetVideoStream(const StreamSelector& selector, StreamParams* stream);
269 bool GetDataStream(const StreamSelector& selector, StreamParams* stream);
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000270 // Adds a stream.
271 void AddAudioStream(const StreamParams& stream);
272 void AddVideoStream(const StreamParams& stream);
273 void AddDataStream(const StreamParams& stream);
274 // Removes a stream, returning true if found and removed.
275 bool RemoveAudioStream(const StreamSelector& selector);
276 bool RemoveVideoStream(const StreamSelector& selector);
277 bool RemoveDataStream(const StreamSelector& selector);
278
279 private:
280 std::vector<StreamParams> audio_;
281 std::vector<StreamParams> video_;
282 std::vector<StreamParams> data_;
283
henrikg3c089d72015-09-16 05:37:44 -0700284 RTC_DISALLOW_COPY_AND_ASSIGN(MediaStreams);
pthatcher@webrtc.orge2b75852014-12-16 21:09:08 +0000285};
286
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000287template <class Condition>
288const StreamParams* GetStream(const StreamParamsVec& streams,
289 Condition condition) {
290 StreamParamsVec::const_iterator found =
291 std::find_if(streams.begin(), streams.end(), condition);
292 return found == streams.end() ? nullptr : &(*found);
293}
294
deadbeef2f425aa2017-04-14 10:41:32 -0700295template <class Condition>
296StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
297 StreamParamsVec::iterator found =
298 std::find_if(streams.begin(), streams.end(), condition);
299 return found == streams.end() ? nullptr : &(*found);
300}
301
Seth Hampson5897a6e2018-04-03 11:16:33 -0700302inline bool HasStreamWithNoSsrcs(const StreamParamsVec& streams) {
303 return GetStream(streams,
304 [](const StreamParams& sp) { return !sp.has_ssrcs(); });
305}
306
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000307inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308 uint32_t ssrc) {
Yves Gerey665174f2018-06-19 15:03:05 +0200309 return GetStream(
310 streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000311}
312
313inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
314 const std::string& groupid,
315 const std::string& id) {
deadbeef2f425aa2017-04-14 10:41:32 -0700316 return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
317 return sp.groupid == groupid && sp.id == id;
318 });
319}
320
321inline StreamParams* GetStreamByIds(StreamParamsVec& streams,
322 const std::string& groupid,
323 const std::string& id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200324 return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
325 return sp.groupid == groupid && sp.id == id;
326 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000327}
328
329inline const StreamParams* GetStream(const StreamParamsVec& streams,
330 const StreamSelector& selector) {
Yves Gerey665174f2018-06-19 15:03:05 +0200331 return GetStream(streams, [&selector](const StreamParams& sp) {
332 return selector.Matches(sp);
333 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000334}
335
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000336template <class Condition>
337bool RemoveStream(StreamParamsVec* streams, Condition condition) {
338 auto iter(std::remove_if(streams->begin(), streams->end(), condition));
339 if (iter == streams->end())
340 return false;
341 streams->erase(iter, streams->end());
342 return true;
343}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344
345// Removes the stream from streams. Returns true if a stream is
346// found and removed.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000347inline bool RemoveStream(StreamParamsVec* streams,
Yves Gerey665174f2018-06-19 15:03:05 +0200348 const StreamSelector& selector) {
349 return RemoveStream(streams, [&selector](const StreamParams& sp) {
350 return selector.Matches(sp);
351 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000352}
Peter Boström0c4e06b2015-10-07 12:23:21 +0200353inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
Yves Gerey665174f2018-06-19 15:03:05 +0200354 return RemoveStream(
355 streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000356}
357inline bool RemoveStreamByIds(StreamParamsVec* streams,
358 const std::string& groupid,
359 const std::string& id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200360 return RemoveStream(streams, [&groupid, &id](const StreamParams& sp) {
361 return sp.groupid == groupid && sp.id == id;
362 });
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000363}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000365// Checks if |sp| defines parameters for a single primary stream. There may
brandtr9688e382016-11-22 00:59:48 -0800366// be an RTX stream or a FlexFEC stream (or both) associated with the primary
367// stream. Leaving as non-static so we can test this function.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000368bool IsOneSsrcStream(const StreamParams& sp);
369
370// Checks if |sp| defines parameters for one Simulcast stream. There may be RTX
371// streams associated with the simulcast streams. Leaving as non-static so we
372// can test this function.
373bool IsSimulcastStream(const StreamParams& sp);
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375} // namespace cricket
376
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200377#endif // MEDIA_BASE_STREAMPARAMS_H_