3 * Copyright 2011 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // This file contains structures for describing SSRCs from a media source such
29 // as a MediaStreamTrack when it is sent across an RTP session. Multiple media
30 // sources may be sent across the same RTP session, each of them will be
31 // described by one StreamParams object
32 // SsrcGroup is used to describe the relationship between the SSRCs that
33 // are used for this media source.
34 // E.x: Consider a source that is sent as 3 simulcast streams
35 // Let the simulcast elements have SSRC 10, 20, 30.
36 // Let each simulcast element use FEC and let the protection packets have
38 // To describe this 4 SsrcGroups are needed,
39 // StreamParams would then contain ssrc = {10,11,20,21,30,31} and
40 // ssrc_groups = {{SIM,{10,20,30}, {FEC,{10,11}, {FEC, {20,21}, {FEC {30,31}}}
41 // Please see RFC 5576.
43 #ifndef TALK_MEDIA_BASE_STREAMPARAMS_H_
44 #define TALK_MEDIA_BASE_STREAMPARAMS_H_
51 #include "webrtc/base/basictypes.h"
55 extern const char kFecSsrcGroupSemantics[];
56 extern const char kFidSsrcGroupSemantics[];
57 extern const char kSimSsrcGroupSemantics[];
60 SsrcGroup(const std::string& usage, const std::vector<uint32>& ssrcs)
61 : semantics(usage), ssrcs(ssrcs) {
64 bool operator==(const SsrcGroup& other) const {
65 return (semantics == other.semantics && ssrcs == other.ssrcs);
67 bool operator!=(const SsrcGroup &other) const {
68 return !(*this == other);
71 bool has_semantics(const std::string& semantics) const;
73 std::string ToString() const;
75 std::string semantics; // e.g FIX, FEC, SIM.
76 std::vector<uint32> ssrcs; // SSRCs of this type.
80 static StreamParams CreateLegacy(uint32 ssrc) {
82 stream.ssrcs.push_back(ssrc);
86 bool operator==(const StreamParams& other) const {
87 return (groupid == other.groupid &&
89 ssrcs == other.ssrcs &&
90 ssrc_groups == other.ssrc_groups &&
92 display == other.display &&
93 cname == other.cname &&
94 sync_label == other.sync_label);
96 bool operator!=(const StreamParams &other) const {
97 return !(*this == other);
100 uint32 first_ssrc() const {
107 bool has_ssrcs() const {
108 return !ssrcs.empty();
110 bool has_ssrc(uint32 ssrc) const {
111 return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
113 void add_ssrc(uint32 ssrc) {
114 ssrcs.push_back(ssrc);
116 bool has_ssrc_groups() const {
117 return !ssrc_groups.empty();
119 bool has_ssrc_group(const std::string& semantics) const {
120 return (get_ssrc_group(semantics) != NULL);
122 const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
123 for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
124 it != ssrc_groups.end(); ++it) {
125 if (it->has_semantics(semantics)) {
132 // Convenience function to add an FID ssrc for a primary_ssrc
133 // that's already been added.
134 inline bool AddFidSsrc(uint32 primary_ssrc, uint32 fid_ssrc) {
135 return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
138 // Convenience function to lookup the FID ssrc for a primary_ssrc.
139 // Returns false if primary_ssrc not found or FID not defined for it.
140 inline bool GetFidSsrc(uint32 primary_ssrc, uint32* fid_ssrc) const {
141 return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
144 // Convenience to get all the SIM SSRCs if there are SIM ssrcs, or
145 // the first SSRC otherwise.
146 void GetPrimarySsrcs(std::vector<uint32>* ssrcs) const;
148 // Convenience to get all the FID SSRCs for the given primary ssrcs.
149 // If a given primary SSRC does not have a FID SSRC, the list of FID
150 // SSRCS will be smaller than the list of primary SSRCs.
151 void GetFidSsrcs(const std::vector<uint32>& primary_ssrcs,
152 std::vector<uint32>* fid_ssrcs) const;
154 std::string ToString() const;
156 // Resource of the MUC jid of the participant of with this stream.
157 // For 1:1 calls, should be left empty (which means remote streams
158 // and local streams should not be mixed together).
160 // Unique per-groupid, not across all groupids
162 std::vector<uint32> ssrcs; // All SSRCs for this source
163 std::vector<SsrcGroup> ssrc_groups; // e.g. FID, FEC, SIM
164 // Examples: "camera", "screencast"
166 // Friendly name describing stream
168 std::string cname; // RTCP CNAME
169 std::string sync_label; // Friendly name of cname.
172 bool AddSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
173 uint32 secondary_ssrc);
174 bool GetSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
175 uint32* secondary_ssrc) const;
178 // A Stream can be selected by either groupid+id or ssrc.
179 struct StreamSelector {
180 explicit StreamSelector(uint32 ssrc) :
184 StreamSelector(const std::string& groupid,
185 const std::string& streamid) :
191 bool Matches(const StreamParams& stream) const {
193 return stream.groupid == groupid && stream.id == streamid;
195 return stream.has_ssrc(ssrc);
201 std::string streamid;
204 typedef std::vector<StreamParams> StreamParamsVec;
206 // Finds the stream in streams. Returns true if found.
207 bool GetStream(const StreamParamsVec& streams,
208 const StreamSelector& selector,
209 StreamParams* stream_out);
210 bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
211 StreamParams* stream_out);
212 bool GetStreamByIds(const StreamParamsVec& streams,
213 const std::string& groupid,
214 const std::string& id,
215 StreamParams* stream_out);
217 // Removes the stream from streams. Returns true if a stream is
218 // found and removed.
219 bool RemoveStream(StreamParamsVec* streams,
220 const StreamSelector& selector);
221 bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc);
222 bool RemoveStreamByIds(StreamParamsVec* streams,
223 const std::string& groupid,
224 const std::string& id);
226 // Checks if |sp| defines parameters for a single primary stream. There may
227 // be an RTX stream associated with the primary stream. Leaving as non-static so
228 // we can test this function.
229 bool IsOneSsrcStream(const StreamParams& sp);
231 // Checks if |sp| defines parameters for one Simulcast stream. There may be RTX
232 // streams associated with the simulcast streams. Leaving as non-static so we
233 // can test this function.
234 bool IsSimulcastStream(const StreamParams& sp);
236 } // namespace cricket
238 #endif // TALK_MEDIA_BASE_STREAMPARAMS_H_