• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/unique_id_generator.h"
58 
59 namespace cricket {
60 
61 extern const char kFecSsrcGroupSemantics[];
62 extern const char kFecFrSsrcGroupSemantics[];
63 extern const char kFidSsrcGroupSemantics[];
64 extern const char kSimSsrcGroupSemantics[];
65 
66 struct SsrcGroup {
67   SsrcGroup(const std::string& usage, const std::vector<uint32_t>& ssrcs);
68   SsrcGroup(const SsrcGroup&);
69   SsrcGroup(SsrcGroup&&);
70   ~SsrcGroup();
71   SsrcGroup& operator=(const SsrcGroup&);
72   SsrcGroup& operator=(SsrcGroup&&);
73 
74   bool operator==(const SsrcGroup& other) const {
75     return (semantics == other.semantics && ssrcs == other.ssrcs);
76   }
77   bool operator!=(const SsrcGroup& other) const { return !(*this == other); }
78 
79   bool has_semantics(const std::string& semantics) const;
80 
81   std::string ToString() const;
82 
83   std::string semantics;        // e.g FIX, FEC, SIM.
84   std::vector<uint32_t> ssrcs;  // SSRCs of this type.
85 };
86 
87 // StreamParams is used to represent a sender/track in a SessionDescription.
88 // In Plan B, this means that multiple StreamParams can exist within one
89 // MediaContentDescription, while in UnifiedPlan this means that there is one
90 // StreamParams per MediaContentDescription.
91 struct StreamParams {
92   StreamParams();
93   StreamParams(const StreamParams&);
94   StreamParams(StreamParams&&);
95   ~StreamParams();
96   StreamParams& operator=(const StreamParams&);
97   StreamParams& operator=(StreamParams&&);
98 
CreateLegacyStreamParams99   static StreamParams CreateLegacy(uint32_t ssrc) {
100     StreamParams stream;
101     stream.ssrcs.push_back(ssrc);
102     return stream;
103   }
104 
105   bool operator==(const StreamParams& other) const;
106   bool operator!=(const StreamParams& other) const { return !(*this == other); }
107 
first_ssrcStreamParams108   uint32_t first_ssrc() const {
109     if (ssrcs.empty()) {
110       return 0;
111     }
112 
113     return ssrcs[0];
114   }
has_ssrcsStreamParams115   bool has_ssrcs() const { return !ssrcs.empty(); }
has_ssrcStreamParams116   bool has_ssrc(uint32_t ssrc) const {
117     return absl::c_linear_search(ssrcs, ssrc);
118   }
add_ssrcStreamParams119   void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
has_ssrc_groupsStreamParams120   bool has_ssrc_groups() const { return !ssrc_groups.empty(); }
has_ssrc_groupStreamParams121   bool has_ssrc_group(const std::string& semantics) const {
122     return (get_ssrc_group(semantics) != NULL);
123   }
get_ssrc_groupStreamParams124   const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
125     for (const SsrcGroup& ssrc_group : ssrc_groups) {
126       if (ssrc_group.has_semantics(semantics)) {
127         return &ssrc_group;
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.
AddFidSsrcStreamParams135   bool AddFidSsrc(uint32_t primary_ssrc, uint32_t 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.
GetFidSsrcStreamParams141   bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
142     return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
143   }
144 
145   // Convenience function to add an FEC-FR ssrc for a primary_ssrc
146   // that's already been added.
AddFecFrSsrcStreamParams147   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.
GetFecFrSsrcStreamParams153   bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
154     return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
155   }
156 
157   // Convenience function to populate the StreamParams with the requested number
158   // of SSRCs along with accompanying FID and FEC-FR ssrcs if requested.
159   // SSRCs are generated using the given generator.
160   void GenerateSsrcs(int num_layers,
161                      bool generate_fid,
162                      bool generate_fec_fr,
163                      rtc::UniqueRandomIdGenerator* ssrc_generator);
164 
165   // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
166   // the first SSRC otherwise.
167   void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
168 
169   // Convenience to get all the FID SSRCs for the given primary ssrcs.
170   // If a given primary SSRC does not have a FID SSRC, the list of FID
171   // SSRCS will be smaller than the list of primary SSRCs.
172   void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
173                    std::vector<uint32_t>* fid_ssrcs) const;
174 
175   // Stream ids serialized to SDP.
176   std::vector<std::string> stream_ids() const;
177   void set_stream_ids(const std::vector<std::string>& stream_ids);
178 
179   // Returns the first stream id or "" if none exist. This method exists only
180   // as temporary backwards compatibility with the old sync_label.
181   std::string first_stream_id() const;
182 
183   std::string ToString() const;
184 
185   // A unique identifier of the StreamParams object. When the SDP is created,
186   // this comes from the track ID of the sender that the StreamParams object
187   // is associated with.
188   std::string id;
189   // There may be no SSRCs stored in unsignaled case when stream_ids are
190   // signaled with a=msid lines.
191   std::vector<uint32_t> ssrcs;         // All SSRCs for this source
192   std::vector<SsrcGroup> ssrc_groups;  // e.g. FID, FEC, SIM
193   std::string cname;                   // RTCP CNAME
194 
195   // RID functionality according to
196   // https://tools.ietf.org/html/draft-ietf-mmusic-rid-15
197   // Each layer can be represented by a RID identifier and can also have
198   // restrictions (such as max-width, max-height, etc.)
199   // If the track has multiple layers (ex. Simulcast), each layer will be
200   // represented by a RID.
has_ridsStreamParams201   bool has_rids() const { return !rids_.empty(); }
ridsStreamParams202   const std::vector<RidDescription>& rids() const { return rids_; }
set_ridsStreamParams203   void set_rids(const std::vector<RidDescription>& rids) { rids_ = rids; }
204 
205  private:
206   bool AddSecondarySsrc(const std::string& semantics,
207                         uint32_t primary_ssrc,
208                         uint32_t secondary_ssrc);
209   bool GetSecondarySsrc(const std::string& semantics,
210                         uint32_t primary_ssrc,
211                         uint32_t* secondary_ssrc) const;
212 
213   // The stream IDs of the sender that the StreamParams object is associated
214   // with. In Plan B this should always be size of 1, while in Unified Plan this
215   // could be none or multiple stream IDs.
216   std::vector<std::string> stream_ids_;
217 
218   std::vector<RidDescription> rids_;
219 };
220 
221 // A Stream can be selected by either id or ssrc.
222 struct StreamSelector {
StreamSelectorStreamSelector223   explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
224 
StreamSelectorStreamSelector225   explicit StreamSelector(const std::string& streamid)
226       : ssrc(0), streamid(streamid) {}
227 
MatchesStreamSelector228   bool Matches(const StreamParams& stream) const {
229     if (ssrc == 0) {
230       return stream.id == streamid;
231     } else {
232       return stream.has_ssrc(ssrc);
233     }
234   }
235 
236   uint32_t ssrc;
237   std::string streamid;
238 };
239 
240 typedef std::vector<StreamParams> StreamParamsVec;
241 
242 template <class Condition>
GetStream(const StreamParamsVec & streams,Condition condition)243 const StreamParams* GetStream(const StreamParamsVec& streams,
244                               Condition condition) {
245   auto found = absl::c_find_if(streams, condition);
246   return found == streams.end() ? nullptr : &(*found);
247 }
248 
249 template <class Condition>
GetStream(StreamParamsVec & streams,Condition condition)250 StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
251   auto found = absl::c_find_if(streams, condition);
252   return found == streams.end() ? nullptr : &(*found);
253 }
254 
HasStreamWithNoSsrcs(const StreamParamsVec & streams)255 inline bool HasStreamWithNoSsrcs(const StreamParamsVec& streams) {
256   return GetStream(streams,
257                    [](const StreamParams& sp) { return !sp.has_ssrcs(); });
258 }
259 
GetStreamBySsrc(const StreamParamsVec & streams,uint32_t ssrc)260 inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
261                                            uint32_t ssrc) {
262   return GetStream(
263       streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
264 }
265 
GetStreamByIds(const StreamParamsVec & streams,const std::string & id)266 inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
267                                           const std::string& id) {
268   return GetStream(streams,
269                    [&id](const StreamParams& sp) { return sp.id == id; });
270 }
271 
GetStreamByIds(StreamParamsVec & streams,const std::string & id)272 inline StreamParams* GetStreamByIds(StreamParamsVec& streams,
273                                     const std::string& id) {
274   return GetStream(streams,
275                    [&id](const StreamParams& sp) { return sp.id == id; });
276 }
277 
GetStream(const StreamParamsVec & streams,const StreamSelector & selector)278 inline const StreamParams* GetStream(const StreamParamsVec& streams,
279                                      const StreamSelector& selector) {
280   return GetStream(streams, [&selector](const StreamParams& sp) {
281     return selector.Matches(sp);
282   });
283 }
284 
285 template <class Condition>
RemoveStream(StreamParamsVec * streams,Condition condition)286 bool RemoveStream(StreamParamsVec* streams, Condition condition) {
287   auto iter(std::remove_if(streams->begin(), streams->end(), condition));
288   if (iter == streams->end())
289     return false;
290   streams->erase(iter, streams->end());
291   return true;
292 }
293 
294 // Removes the stream from streams. Returns true if a stream is
295 // found and removed.
RemoveStream(StreamParamsVec * streams,const StreamSelector & selector)296 inline bool RemoveStream(StreamParamsVec* streams,
297                          const StreamSelector& selector) {
298   return RemoveStream(streams, [&selector](const StreamParams& sp) {
299     return selector.Matches(sp);
300   });
301 }
RemoveStreamBySsrc(StreamParamsVec * streams,uint32_t ssrc)302 inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
303   return RemoveStream(
304       streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
305 }
RemoveStreamByIds(StreamParamsVec * streams,const std::string & id)306 inline bool RemoveStreamByIds(StreamParamsVec* streams,
307                               const std::string& id) {
308   return RemoveStream(streams,
309                       [&id](const StreamParams& sp) { return sp.id == id; });
310 }
311 
312 }  // namespace cricket
313 
314 #endif  // MEDIA_BASE_STREAM_PARAMS_H_
315