123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #ifndef MEDIA_BASE_STREAM_PARAMS_H_
- #define MEDIA_BASE_STREAM_PARAMS_H_
- #include <stddef.h>
- #include <cstdint>
- #include <string>
- #include <vector>
- #include "absl/algorithm/container.h"
- #include "media/base/rid_description.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/unique_id_generator.h"
- namespace cricket {
- extern const char kFecSsrcGroupSemantics[];
- extern const char kFecFrSsrcGroupSemantics[];
- extern const char kFidSsrcGroupSemantics[];
- extern const char kSimSsrcGroupSemantics[];
- struct SsrcGroup {
- SsrcGroup(const std::string& usage, const std::vector<uint32_t>& ssrcs);
- SsrcGroup(const SsrcGroup&);
- SsrcGroup(SsrcGroup&&);
- ~SsrcGroup();
- SsrcGroup& operator=(const SsrcGroup&);
- SsrcGroup& operator=(SsrcGroup&&);
- bool operator==(const SsrcGroup& other) const {
- return (semantics == other.semantics && ssrcs == other.ssrcs);
- }
- bool operator!=(const SsrcGroup& other) const { return !(*this == other); }
- bool has_semantics(const std::string& semantics) const;
- std::string ToString() const;
- std::string semantics;
- std::vector<uint32_t> ssrcs;
- };
- struct StreamParams {
- StreamParams();
- StreamParams(const StreamParams&);
- StreamParams(StreamParams&&);
- ~StreamParams();
- StreamParams& operator=(const StreamParams&);
- StreamParams& operator=(StreamParams&&);
- static StreamParams CreateLegacy(uint32_t ssrc) {
- StreamParams stream;
- stream.ssrcs.push_back(ssrc);
- return stream;
- }
- bool operator==(const StreamParams& other) const;
- bool operator!=(const StreamParams& other) const { return !(*this == other); }
- uint32_t first_ssrc() const {
- if (ssrcs.empty()) {
- return 0;
- }
- return ssrcs[0];
- }
- bool has_ssrcs() const { return !ssrcs.empty(); }
- bool has_ssrc(uint32_t ssrc) const {
- return absl::c_linear_search(ssrcs, ssrc);
- }
- void add_ssrc(uint32_t ssrc) { ssrcs.push_back(ssrc); }
- bool has_ssrc_groups() const { return !ssrc_groups.empty(); }
- bool has_ssrc_group(const std::string& semantics) const {
- return (get_ssrc_group(semantics) != NULL);
- }
- const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
- for (const SsrcGroup& ssrc_group : ssrc_groups) {
- if (ssrc_group.has_semantics(semantics)) {
- return &ssrc_group;
- }
- }
- return NULL;
- }
-
-
- bool AddFidSsrc(uint32_t primary_ssrc, uint32_t fid_ssrc) {
- return AddSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
- }
-
-
- bool GetFidSsrc(uint32_t primary_ssrc, uint32_t* fid_ssrc) const {
- return GetSecondarySsrc(kFidSsrcGroupSemantics, primary_ssrc, fid_ssrc);
- }
-
-
- bool AddFecFrSsrc(uint32_t primary_ssrc, uint32_t fecfr_ssrc) {
- return AddSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
- }
-
-
- bool GetFecFrSsrc(uint32_t primary_ssrc, uint32_t* fecfr_ssrc) const {
- return GetSecondarySsrc(kFecFrSsrcGroupSemantics, primary_ssrc, fecfr_ssrc);
- }
-
-
-
- void GenerateSsrcs(int num_layers,
- bool generate_fid,
- bool generate_fec_fr,
- rtc::UniqueRandomIdGenerator* ssrc_generator);
-
-
- void GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const;
-
-
-
- void GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
- std::vector<uint32_t>* fid_ssrcs) const;
-
- std::vector<std::string> stream_ids() const;
- void set_stream_ids(const std::vector<std::string>& stream_ids);
-
-
- std::string first_stream_id() const;
- std::string ToString() const;
-
-
-
-
- std::string groupid;
-
-
-
- std::string id;
-
-
- std::vector<uint32_t> ssrcs;
- std::vector<SsrcGroup> ssrc_groups;
- std::string cname;
-
-
-
-
-
-
- bool has_rids() const { return !rids_.empty(); }
- const std::vector<RidDescription>& rids() const { return rids_; }
- void set_rids(const std::vector<RidDescription>& rids) { rids_ = rids; }
- private:
- bool AddSecondarySsrc(const std::string& semantics,
- uint32_t primary_ssrc,
- uint32_t secondary_ssrc);
- bool GetSecondarySsrc(const std::string& semantics,
- uint32_t primary_ssrc,
- uint32_t* secondary_ssrc) const;
-
-
-
- std::vector<std::string> stream_ids_;
- std::vector<RidDescription> rids_;
- };
- struct StreamSelector {
- explicit StreamSelector(uint32_t ssrc) : ssrc(ssrc) {}
- StreamSelector(const std::string& groupid, const std::string& streamid)
- : ssrc(0), groupid(groupid), streamid(streamid) {}
- explicit StreamSelector(const std::string& streamid)
- : ssrc(0), streamid(streamid) {}
- bool Matches(const StreamParams& stream) const {
- if (ssrc == 0) {
- return stream.groupid == groupid && stream.id == streamid;
- } else {
- return stream.has_ssrc(ssrc);
- }
- }
- uint32_t ssrc;
- std::string groupid;
- std::string streamid;
- };
- typedef std::vector<StreamParams> StreamParamsVec;
- template <class Condition>
- const StreamParams* GetStream(const StreamParamsVec& streams,
- Condition condition) {
- auto found = absl::c_find_if(streams, condition);
- return found == streams.end() ? nullptr : &(*found);
- }
- template <class Condition>
- StreamParams* GetStream(StreamParamsVec& streams, Condition condition) {
- auto found = absl::c_find_if(streams, condition);
- return found == streams.end() ? nullptr : &(*found);
- }
- inline bool HasStreamWithNoSsrcs(const StreamParamsVec& streams) {
- return GetStream(streams,
- [](const StreamParams& sp) { return !sp.has_ssrcs(); });
- }
- inline const StreamParams* GetStreamBySsrc(const StreamParamsVec& streams,
- uint32_t ssrc) {
- return GetStream(
- streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
- }
- inline const StreamParams* GetStreamByIds(const StreamParamsVec& streams,
- const std::string& groupid,
- const std::string& id) {
- return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
- return sp.groupid == groupid && sp.id == id;
- });
- }
- inline StreamParams* GetStreamByIds(StreamParamsVec& streams,
- const std::string& groupid,
- const std::string& id) {
- return GetStream(streams, [&groupid, &id](const StreamParams& sp) {
- return sp.groupid == groupid && sp.id == id;
- });
- }
- inline const StreamParams* GetStream(const StreamParamsVec& streams,
- const StreamSelector& selector) {
- return GetStream(streams, [&selector](const StreamParams& sp) {
- return selector.Matches(sp);
- });
- }
- template <class Condition>
- bool RemoveStream(StreamParamsVec* streams, Condition condition) {
- auto iter(std::remove_if(streams->begin(), streams->end(), condition));
- if (iter == streams->end())
- return false;
- streams->erase(iter, streams->end());
- return true;
- }
- inline bool RemoveStream(StreamParamsVec* streams,
- const StreamSelector& selector) {
- return RemoveStream(streams, [&selector](const StreamParams& sp) {
- return selector.Matches(sp);
- });
- }
- inline bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32_t ssrc) {
- return RemoveStream(
- streams, [&ssrc](const StreamParams& sp) { return sp.has_ssrc(ssrc); });
- }
- inline bool RemoveStreamByIds(StreamParamsVec* streams,
- const std::string& groupid,
- const std::string& id) {
- return RemoveStream(streams, [&groupid, &id](const StreamParams& sp) {
- return sp.groupid == groupid && sp.id == id;
- });
- }
- }
- #endif
|