codec_list.h (3021B)
1 /* 2 * Copyright (c) 2024 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MEDIA_BASE_CODEC_LIST_H_ 12 #define MEDIA_BASE_CODEC_LIST_H_ 13 14 #include <cstddef> 15 #include <vector> 16 17 #include "api/rtc_error.h" 18 #include "media/base/codec.h" 19 20 namespace webrtc { 21 22 class CodecList { 23 public: 24 using iterator = std::vector<Codec>::iterator; 25 using const_iterator = std::vector<Codec>::const_iterator; 26 using value_type = Codec; 27 28 CodecList() = default; 29 // Copy and assign are available. 30 CodecList(const CodecList&) = default; 31 CodecList& operator=(const CodecList&) = default; 32 CodecList(CodecList&&) = default; 33 CodecList& operator=(CodecList&&) = default; 34 bool operator==(const CodecList& o) const { return codecs_ == o.codecs_; } 35 36 // Creates a codec list on untrusted data. If successful, the 37 // resulting CodecList satisfies all the CodecList invariants. 38 static RTCErrorOr<CodecList> Create(const std::vector<Codec>& codecs); 39 // Creates a codec list on trusted data. Only for use when 40 // the codec list is generated from internal code. 41 static CodecList CreateFromTrustedData(const std::vector<Codec>& codecs) { 42 return CodecList(codecs); 43 } 44 // Vector-compatible API to access the codecs. 45 iterator begin() { return codecs_.begin(); } 46 iterator end() { return codecs_.end(); } 47 const_iterator begin() const { return codecs_.begin(); } 48 const_iterator end() const { return codecs_.end(); } 49 const Codec& operator[](size_t i) const { return codecs_[i]; } 50 Codec& operator[](size_t i) { return codecs_[i]; } 51 void push_back(const Codec& codec) { 52 codecs_.push_back(codec); 53 CheckConsistency(); 54 } 55 bool empty() const { return codecs_.empty(); } 56 void clear() { codecs_.clear(); } 57 size_t size() const { return codecs_.size(); } 58 // Access to the whole codec list 59 const std::vector<Codec>& codecs() const { return codecs_; } 60 std::vector<Codec>& writable_codecs() { return codecs_; } 61 // Verify consistency of the codec list. 62 // Examples: checking that all RTX codecs have APT pointing 63 // to a codec in the list. 64 // The function will CHECK or DCHECK on inconsistencies. 65 void CheckConsistency(); 66 67 template <typename Sink> 68 friend void AbslStringify(Sink& sink, const CodecList& list) { 69 absl::Format(&sink, "\n--- Codec list of size %d\n", list.size()); 70 for (Codec codec : list) { 71 absl::Format(&sink, "%v\n", codec); 72 } 73 sink.Append("--- End\n"); 74 } 75 76 private: 77 // Creates a codec list on trusted data. 78 explicit CodecList(const std::vector<Codec>& codecs) { 79 codecs_ = codecs; 80 CheckConsistency(); 81 } 82 83 std::vector<Codec> codecs_; 84 }; 85 86 } // namespace webrtc 87 88 89 #endif // MEDIA_BASE_CODEC_LIST_H_