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 
     35 #ifndef TALK_MEDIA_BASE_STREAMPARAMS_H_
     36 #define TALK_MEDIA_BASE_STREAMPARAMS_H_
     37 
     38 #include <algorithm>
     39 #include <string>
     40 #include <set>
     41 #include <vector>
     42 
     43 #include "talk/base/basictypes.h"
     44 
     45 namespace cricket {
     46 
     47 extern const char kFecSsrcGroupSemantics[];
     48 extern const char kFidSsrcGroupSemantics[];
     49 
     50 struct SsrcGroup {
     51   SsrcGroup(const std::string& usage, const std::vector<uint32>& ssrcs)
     52       : semantics(usage), ssrcs(ssrcs) {
     53   }
     54 
     55   bool operator==(const SsrcGroup& other) const {
     56     return (semantics == other.semantics && ssrcs == other.ssrcs);
     57   }
     58   bool operator!=(const SsrcGroup &other) const {
     59     return !(*this == other);
     60   }
     61 
     62   bool has_semantics(const std::string& semantics) const;
     63 
     64   std::string ToString() const;
     65 
     66   std::string semantics;  // e.g FIX, FEC, SIM.
     67   std::vector<uint32> ssrcs;  // SSRCs of this type.
     68 };
     69 
     70 struct StreamParams {
     71   static StreamParams CreateLegacy(uint32 ssrc) {
     72     StreamParams stream;
     73     stream.ssrcs.push_back(ssrc);
     74     return stream;
     75   }
     76 
     77   bool operator==(const StreamParams& other) const {
     78     return (groupid == other.groupid &&
     79             id == other.id &&
     80             ssrcs == other.ssrcs &&
     81             ssrc_groups == other.ssrc_groups &&
     82             type == other.type &&
     83             display == other.display &&
     84             cname == other.cname &&
     85             sync_label == other.sync_label);
     86   }
     87   bool operator!=(const StreamParams &other) const {
     88     return !(*this == other);
     89   }
     90 
     91   uint32 first_ssrc() const {
     92     if (ssrcs.empty()) {
     93       return 0;
     94     }
     95 
     96     return ssrcs[0];
     97   }
     98   bool has_ssrcs() const {
     99     return !ssrcs.empty();
    100   }
    101   bool has_ssrc(uint32 ssrc) const {
    102     return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
    103   }
    104   void add_ssrc(uint32 ssrc) {
    105     ssrcs.push_back(ssrc);
    106   }
    107   bool has_ssrc_groups() const {
    108     return !ssrc_groups.empty();
    109   }
    110   bool has_ssrc_group(const std::string& semantics) const {
    111     return (get_ssrc_group(semantics) != NULL);
    112   }
    113   const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
    114     for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
    115          it != ssrc_groups.end(); ++it) {
    116       if (it->has_semantics(semantics)) {
    117         return &(*it);
    118       }
    119     }
    120     return NULL;
    121   }
    122 
    123   // Convenience function to add an FID ssrc for a primary_ssrc
    124   // that's already been added.
    125   inline bool AddFidSsrc(uint32 primary_ssrc, uint32 fid_ssrc) {
    126     return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
    127   }
    128 
    129   // Convenience function to lookup the FID ssrc for a primary_ssrc.
    130   // Returns false if primary_ssrc not found or FID not defined for it.
    131   inline bool GetFidSsrc(uint32 primary_ssrc, uint32* fid_ssrc) const {
    132     return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
    133   }
    134 
    135   std::string ToString() const;
    136 
    137   // Resource of the MUC jid of the participant of with this stream.
    138   // For 1:1 calls, should be left empty (which means remote streams
    139   // and local streams should not be mixed together).
    140   std::string groupid;
    141   // Unique per-groupid, not across all groupids
    142   std::string id;
    143   std::vector<uint32> ssrcs;  // All SSRCs for this source
    144   std::vector<SsrcGroup> ssrc_groups;  // e.g. FID, FEC, SIM
    145   // Examples: "camera", "screencast"
    146   std::string type;
    147   // Friendly name describing stream
    148   std::string display;
    149   std::string cname;  // RTCP CNAME
    150   std::string sync_label;  // Friendly name of cname.
    151 
    152  private:
    153   bool AddSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
    154                         uint32 secondary_ssrc);
    155   bool GetSecondarySsrc(const std::string& semantics, uint32 primary_ssrc,
    156                         uint32* secondary_ssrc) const;
    157 };
    158 
    159 // A Stream can be selected by either groupid+id or ssrc.
    160 struct StreamSelector {
    161   explicit StreamSelector(uint32 ssrc) :
    162       ssrc(ssrc) {
    163   }
    164 
    165   StreamSelector(const std::string& groupid,
    166                  const std::string& streamid) :
    167       ssrc(0),
    168       groupid(groupid),
    169       streamid(streamid) {
    170   }
    171 
    172   bool Matches(const StreamParams& stream) const {
    173     if (ssrc == 0) {
    174       return stream.groupid == groupid && stream.id == streamid;
    175     } else {
    176       return stream.has_ssrc(ssrc);
    177     }
    178   }
    179 
    180   uint32 ssrc;
    181   std::string groupid;
    182   std::string streamid;
    183 };
    184 
    185 typedef std::vector<StreamParams> StreamParamsVec;
    186 
    187 // Finds the stream in streams.  Returns true if found.
    188 bool GetStream(const StreamParamsVec& streams,
    189                const StreamSelector& selector,
    190                StreamParams* stream_out);
    191 bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
    192                      StreamParams* stream_out);
    193 bool GetStreamByIds(const StreamParamsVec& streams,
    194                     const std::string& groupid,
    195                     const std::string& id,
    196                     StreamParams* stream_out);
    197 
    198 // Removes the stream from streams. Returns true if a stream is
    199 // found and removed.
    200 bool RemoveStream(StreamParamsVec* streams,
    201                   const StreamSelector& selector);
    202 bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc);
    203 bool RemoveStreamByIds(StreamParamsVec* streams,
    204                        const std::string& groupid,
    205                        const std::string& id);
    206 
    207 }  // namespace cricket
    208 
    209 #endif  // TALK_MEDIA_BASE_STREAMPARAMS_H_
    210