Home | History | Annotate | Download | only in base
      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 <set>
     48 #include <string>
     49 #include <vector>
     50 
     51 #include "webrtc/base/basictypes.h"
     52 
     53 namespace cricket {
     54 
     55 extern const char kFecSsrcGroupSemantics[];
     56 extern const char kFidSsrcGroupSemantics[];
     57 extern const char kSimSsrcGroupSemantics[];
     58 
     59 struct 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 
     79 struct 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   // 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;
    147 
    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;
    153 
    154   std::string ToString() const;
    155 
    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).
    159   std::string groupid;
    160   // Unique per-groupid, not across all groupids
    161   std::string id;
    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"
    165   std::string type;
    166   // Friendly name describing stream
    167   std::string display;
    168   std::string cname;  // RTCP CNAME
    169   std::string sync_label;  // Friendly name of cname.
    170 
    171  private:
    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;
    176 };
    177 
    178 // A Stream can be selected by either groupid+id or ssrc.
    179 struct StreamSelector {
    180   explicit StreamSelector(uint32 ssrc) :
    181       ssrc(ssrc) {
    182   }
    183 
    184   StreamSelector(const std::string& groupid,
    185                  const std::string& streamid) :
    186       ssrc(0),
    187       groupid(groupid),
    188       streamid(streamid) {
    189   }
    190 
    191   bool Matches(const StreamParams& stream) const {
    192     if (ssrc == 0) {
    193       return stream.groupid == groupid && stream.id == streamid;
    194     } else {
    195       return stream.has_ssrc(ssrc);
    196     }
    197   }
    198 
    199   uint32 ssrc;
    200   std::string groupid;
    201   std::string streamid;
    202 };
    203 
    204 typedef std::vector<StreamParams> StreamParamsVec;
    205 
    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);
    216 
    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);
    225 
    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);
    230 
    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);
    235 
    236 }  // namespace cricket
    237 
    238 #endif  // TALK_MEDIA_BASE_STREAMPARAMS_H_
    239