1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
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.
17 // 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.
25 // 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.
45 
46 #ifndef MEDIA_BASE_STREAM_PARAMS_H_
47 #define MEDIA_BASE_STREAM_PARAMS_H_
48 
49 #include <stddef.h>
50 
51 #include <cstdint>
52 #include <string>
53 #include <vector>
54 
55 #include "absl/algorithm/container.h"
56 #include "media/base/rid_description.h"
57 #include "rtc_base/constructor_magic.h"
58 #include "rtc_base/unique_id_generator.h"
59 
60 namespace cricket {
61 
62 extern const char kFecSsrcGroupSemantics[];
63 extern const char kFecFrSsrcGroupSemantics[];
64 extern const char kFidSsrcGroupSemantics[];
65 extern const char kSimSsrcGroupSemantics[];
66 
67 struct SsrcGroup {
68   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&&);
74 
75   bool operator==(const SsrcGroup& other) const {
76     return (semantics == other.semantics && ssrcs == other.ssrcs);
77   }
78   bool operator!=(const SsrcGroup& other) const { return !(*this == other); }
79 
80   bool has_semantics(const std::string& semantics) const;
81 
82   std::string ToString() const;
83 
84   std::string semantics;        // e.g FIX, FEC, SIM.
85   std::vector<uint32_t> ssrcs;  // SSRCs of this type.
86 };
87 
88 // 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.
92 struct StreamParams {
93   StreamParams();
94   StreamParams(const StreamParams&);
95   StreamParams(StreamParams&&);
96   ~StreamParams();
97   StreamParams& operator=(const StreamParams&);
98   StreamParams& operator=(StreamParams&&);
99 
CreateLegacyStreamParams100   static StreamParams CreateLegacy(uint32_t ssrc) {
101     StreamParams stream;
102     stream.ssrcs.push_back(ssrc);
103     return stream;
104   }
105 
106   bool operator==(const StreamParams& other) const;
107   bool operator!=(const StreamParams& other) const { return !(*this == other); }
108 
first_ssrcStreamParams109   uint32_t first_ssrc() const {
110     if (ssrcs.empty()) {
111       return 0;
112     }
113 
114     return ssrcs[0];
115   }
has_ssrcsStreamParams116   bool has_ssrcs() const { return !ssrcs.empty(); }
has_ssrcStreamParams117   bool has_ssrc(uint32_t ssrc) const {
118     return absl::c_linear_search(ssrcs, ssrc);
119   }
add_ssrcStreamParams120   void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
has_ssrc_groupsStreamParams121   bool has_ssrc_groups() const { return !ssrc_groups.empty(); }
has_ssrc_groupStreamParams122   bool has_ssrc_group(const std::string& semantics) const {
123     return (get_ssrc_group(semantics) != NULL);
124   }
get_ssrc_groupStreamParams125   const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
126     for (const SsrcGroup& ssrc_group : ssrc_groups) {
127       if (ssrc_group.has_semantics(semantics)) {
128         return &ssrc_group;
129       }
130     }
131     return NULL;
132   }
133 
134   // Convenience function to add an FID ssrc for a primary_ssrc
135   // that's already been added.
AddFidSsrcStreamParams136   bool AddFidSsrc(uint32_t primary_ssrc, uint32_t fid_ssrc) {
137     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.
GetFidSsrcStreamParams142   bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
143     return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
144   }
145 
146   // Convenience function to add an FEC-FR ssrc for a primary_ssrc
147   // that's already been added.
AddFecFrSsrcStreamParams148   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.
GetFecFrSsrcStreamParams154   bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
155     return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
156   }
157 
158   // 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 
166   // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
167   // the first SSRC otherwise.
168   void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
169 
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.
173   void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
174                    std::vector<uint32_t>* fid_ssrcs) const;
175 
176   // 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);
179 
180   // Returns the first stream id or "" if none exist. This method exists only
181   // as temporary backwards compatibility with the old sync_label.
182   std::string first_stream_id() const;
183 
184   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
188   // and local streams should not be mixed together). This is not used
189   // internally and should be deprecated.
190   std::string groupid;
191   // 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.
194   std::string id;
195   // There may be no SSRCs stored in unsignaled case when stream_ids are
196   // signaled with a=msid lines.
197   std::vector<uint32_t> ssrcs;         // All SSRCs for this source
198   std::vector<SsrcGroup> ssrc_groups;  // e.g. FID, FEC, SIM
199   std::string cname;                   // RTCP CNAME
200 
201   // 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.
has_ridsStreamParams207   bool has_rids() const { return !rids_.empty(); }
ridsStreamParams208   const std::vector<RidDescription>& rids() const { return rids_; }
set_ridsStreamParams209   void set_rids(const std::vector<RidDescription>& rids) { rids_ = rids; }
210 
211  private:
212   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;
218 
219   // 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.
222   std::vector<std::string> stream_ids_;
223 
224   std::vector<RidDescription> rids_;
225 };
226 
227 // A Stream can be selected by either groupid+id or ssrc.
228 struct StreamSelector {
StreamSelectorStreamSelector229   explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
230 
StreamSelectorStreamSelector231   StreamSelector(const std::string& groupid, const std::string& streamid)
232       : ssrc(0), groupid(groupid), streamid(streamid) {}
233 
StreamSelectorStreamSelector234   explicit StreamSelector(const std::string& streamid)
235       : ssrc(0), streamid(streamid) {}
236 
MatchesStreamSelector237   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 
245   uint32_t ssrc;
246   std::string groupid;
247   std::string streamid;
248 };
249 
250 typedef std::vector<StreamParams> StreamParamsVec;
251 
252 template <class Condition>
GetStream(const StreamParamsVec & streams,Condition condition)253 const StreamParams* GetStream(const StreamParamsVec& streams,
254                               Condition condition) {
255   auto found = absl::c_find_if(streams, condition);
256   return found == streams.end() ? nullptr : &(*found);
257 }
258 
259 template <class Condition>
GetStream(StreamParamsVec & streams,Condition condition)260 StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
261   auto found = absl::c_find_if(streams, condition);
262   return found == streams.end() ? nullptr : &(*found);
263 }
264 
HasStreamWithNoSsrcs(const StreamParamsVec & streams)265 inline bool HasStreamWithNoSsrcs(const StreamParamsVec& streams) {
266   return GetStream(streams,
267                    [](const StreamParams& sp) { return !sp.has_ssrcs(); });
268 }
269 
GetStreamBySsrc(const StreamParamsVec & streams,uint32_t ssrc)270 inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
271                                            uint32_t ssrc) {
272   return GetStream(
273       streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
274 }
275 
GetStreamByIds(const StreamParamsVec & streams,const std::string & groupid,const std::string & id)276 inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
277                                           const std::string& groupid,
278                                           const std::string& id) {
279   return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
280     return sp.groupid == groupid && sp.id == id;
281   });
282 }
283 
GetStreamByIds(StreamParamsVec & streams,const std::string & groupid,const std::string & id)284 inline StreamParams* GetStreamByIds(StreamParamsVec& streams,
285                                     const std::string& groupid,
286                                     const std::string& id) {
287   return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
288     return sp.groupid == groupid && sp.id == id;
289   });
290 }
291 
GetStream(const StreamParamsVec & streams,const StreamSelector & selector)292 inline const StreamParams* GetStream(const StreamParamsVec& streams,
293                                      const StreamSelector& selector) {
294   return GetStream(streams, [&selector](const StreamParams& sp) {
295     return selector.Matches(sp);
296   });
297 }
298 
299 template <class Condition>
RemoveStream(StreamParamsVec * streams,Condition condition)300 bool 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 }
307 
308 // Removes the stream from streams. Returns true if a stream is
309 // found and removed.
RemoveStream(StreamParamsVec * streams,const StreamSelector & selector)310 inline bool RemoveStream(StreamParamsVec* streams,
311                          const StreamSelector& selector) {
312   return RemoveStream(streams, [&selector](const StreamParams& sp) {
313     return selector.Matches(sp);
314   });
315 }
RemoveStreamBySsrc(StreamParamsVec * streams,uint32_t ssrc)316 inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
317   return RemoveStream(
318       streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
319 }
RemoveStreamByIds(StreamParamsVec * streams,const std::string & groupid,const std::string & id)320 inline bool RemoveStreamByIds(StreamParamsVec* streams,
321                               const std::string& groupid,
322                               const std::string& id) {
323   return RemoveStream(streams, [&groupid, &id](const StreamParams& sp) {
324     return sp.groupid == groupid && sp.id == id;
325   });
326 }
327 
328 }  // namespace cricket
329 
330 #endif  // MEDIA_BASE_STREAM_PARAMS_H_
331