1/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
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
37// SSRC 11,21,31.
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.
42
43#ifndef TALK_MEDIA_BASE_STREAMPARAMS_H_
44#define TALK_MEDIA_BASE_STREAMPARAMS_H_
45
46#include <algorithm>
47#include <string>
48#include <set>
49#include <vector>
50
51#include "talk/base/basictypes.h"
52
53namespace cricket {
54
55extern const char kFecSsrcGroupSemantics[];
56extern const char kFidSsrcGroupSemantics[];
57extern const char kSimSsrcGroupSemantics[];
58
59struct SsrcGroup {
60  SsrcGroup(const std::string& usage, const std::vector<uint32>& ssrcs)
61      : semantics(usage), ssrcs(ssrcs) {
62  }
63
64  bool operator==(const SsrcGroup& other) const {
65    return (semantics == other.semantics && ssrcs == other.ssrcs);
66  }
67  bool operator!=(const SsrcGroup &other) const {
68    return !(*this == other);
69  }
70
71  bool has_semantics(const std::string& semantics) const;
72
73  std::string ToString() const;
74
75  std::string semantics;  // e.g FIX, FEC, SIM.
76  std::vector<uint32> ssrcs;  // SSRCs of this type.
77};
78
79struct StreamParams {
80  static StreamParams CreateLegacy(uint32 ssrc) {
81    StreamParams stream;
82    stream.ssrcs.push_back(ssrc);
83    return stream;
84  }
85
86  bool operator==(const StreamParams& other) const {
87    return (groupid == other.groupid &&
88            id == other.id &&
89            ssrcs == other.ssrcs &&
90            ssrc_groups == other.ssrc_groups &&
91            type == other.type &&
92            display == other.display &&
93            cname == other.cname &&
94            sync_label == other.sync_label);
95  }
96  bool operator!=(const StreamParams &other) const {
97    return !(*this == other);
98  }
99
100  uint32 first_ssrc() const {
101    if (ssrcs.empty()) {
102      return 0;
103    }
104
105    return ssrcs[0];
106  }
107  bool has_ssrcs() const {
108    return !ssrcs.empty();
109  }
110  bool has_ssrc(uint32 ssrc) const {
111    return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
112  }
113  void add_ssrc(uint32 ssrc) {
114    ssrcs.push_back(ssrc);
115  }
116  bool has_ssrc_groups() const {
117    return !ssrc_groups.empty();
118  }
119  bool has_ssrc_group(const std::string& semantics) const {
120    return (get_ssrc_group(semantics) != NULL);
121  }
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)) {
126        return &(*it);
127      }
128    }
129    return NULL;
130  }
131
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);
136  }
137
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);
142  }
143
144  std::string ToString() const;
145
146  // Resource of the MUC jid of the participant of with this stream.
147  // For 1:1 calls, should be left empty (which means remote streams
148  // and local streams should not be mixed together).
149  std::string groupid;
150  // Unique per-groupid, not across all groupids
151  std::string id;
152  std::vector<uint32> ssrcs;  // All SSRCs for this source
153  std::vector<SsrcGroup> ssrc_groups;  // e.g. FID, FEC, SIM
154  // Examples: "camera", "screencast"
155  std::string type;
156  // Friendly name describing stream
157  std::string display;
158  std::string cname;  // RTCP CNAME
159  std::string sync_label;  // Friendly name of cname.
160
161 private:
162  bool AddSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
163                        uint32 secondary_ssrc);
164  bool GetSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
165                        uint32* secondary_ssrc) const;
166};
167
168// A Stream can be selected by either groupid+id or ssrc.
169struct StreamSelector {
170  explicit StreamSelector(uint32 ssrc) :
171      ssrc(ssrc) {
172  }
173
174  StreamSelector(const std::string& groupid,
175                 const std::string& streamid) :
176      ssrc(0),
177      groupid(groupid),
178      streamid(streamid) {
179  }
180
181  bool Matches(const StreamParams& stream) const {
182    if (ssrc == 0) {
183      return stream.groupid == groupid && stream.id == streamid;
184    } else {
185      return stream.has_ssrc(ssrc);
186    }
187  }
188
189  uint32 ssrc;
190  std::string groupid;
191  std::string streamid;
192};
193
194typedef std::vector<StreamParams> StreamParamsVec;
195
196// Finds the stream in streams.  Returns true if found.
197bool GetStream(const StreamParamsVec& streams,
198               const StreamSelector& selector,
199               StreamParams* stream_out);
200bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
201                     StreamParams* stream_out);
202bool GetStreamByIds(const StreamParamsVec& streams,
203                    const std::string& groupid,
204                    const std::string& id,
205                    StreamParams* stream_out);
206
207// Removes the stream from streams. Returns true if a stream is
208// found and removed.
209bool RemoveStream(StreamParamsVec* streams,
210                  const StreamSelector& selector);
211bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc);
212bool RemoveStreamByIds(StreamParamsVec* streams,
213                       const std::string& groupid,
214                       const std::string& id);
215
216// Checks if |sp| defines parameters for a single primary stream. There may
217// be an RTX stream associated with the primary stream. Leaving as non-static so
218// we can test this function.
219bool IsOneSsrcStream(const StreamParams& sp);
220
221// Checks if |sp| defines parameters for one Simulcast stream. There may be RTX
222// streams associated with the simulcast streams. Leaving as non-static so we
223// can test this function.
224bool IsSimulcastStream(const StreamParams& sp);
225
226}  // namespace cricket
227
228#endif  // TALK_MEDIA_BASE_STREAMPARAMS_H_
229