blob: 7a903751479008c5343d3eac907106432b692a4b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_CODEC_H_
12#define WEBRTC_MEDIA_BASE_CODEC_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <map>
15#include <set>
16#include <string>
17#include <vector>
18
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070019#include "webrtc/api/rtpparameters.h"
magjed1e45cc62016-10-28 07:43:45 -070020#include "webrtc/common_types.h"
kjellanderf4752772016-03-02 05:42:30 -080021#include "webrtc/media/base/mediaconstants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
25typedef std::map<std::string, std::string> CodecParameterMap;
26
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +000027extern const int kMaxPayloadId;
28
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029class FeedbackParam {
30 public:
31 FeedbackParam(const std::string& id, const std::string& param)
32 : id_(id),
33 param_(param) {
34 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000035 explicit FeedbackParam(const std::string& id)
36 : id_(id),
37 param_(kParamValueEmpty) {
38 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 bool operator==(const FeedbackParam& other) const;
40
41 const std::string& id() const { return id_; }
42 const std::string& param() const { return param_; }
43
44 private:
45 std::string id_; // e.g. "nack", "ccm"
46 std::string param_; // e.g. "", "rpsi", "fir"
47};
48
49class FeedbackParams {
50 public:
51 bool operator==(const FeedbackParams& other) const;
52
53 bool Has(const FeedbackParam& param) const;
54 void Add(const FeedbackParam& param);
55
56 void Intersect(const FeedbackParams& from);
57
58 const std::vector<FeedbackParam>& params() const { return params_; }
59 private:
60 bool HasDuplicateEntries() const;
61
62 std::vector<FeedbackParam> params_;
63};
64
65struct Codec {
66 int id;
67 std::string name;
68 int clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 CodecParameterMap params;
70 FeedbackParams feedback_params;
71
72 // Creates a codec with the given parameters.
deadbeef67cf2c12016-04-13 10:07:16 -070073 Codec(int id, const std::string& name, int clockrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000075 Codec();
76 Codec(const Codec& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +020077 virtual ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
79 // Indicates if this codec is compatible with the specified codec.
80 bool Matches(const Codec& codec) const;
81
82 // Find the parameter for |name| and write the value to |out|.
83 bool GetParam(const std::string& name, std::string* out) const;
84 bool GetParam(const std::string& name, int* out) const;
85
86 void SetParam(const std::string& name, const std::string& value);
87 void SetParam(const std::string& name, int value);
88
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000089 // It is safe to input a non-existent parameter.
90 // Returns true if the parameter existed, false if it did not exist.
91 bool RemoveParam(const std::string& name);
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 bool HasFeedbackParam(const FeedbackParam& param) const;
94 void AddFeedbackParam(const FeedbackParam& param);
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 // Filter |this| feedbacks params such that only those shared by both |this|
97 // and |other| are kept.
98 void IntersectFeedbackParams(const Codec& other);
99
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700100 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
101
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000102 Codec& operator=(const Codec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000104 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 bool operator!=(const Codec& c) const {
107 return !(*this == c);
108 }
109};
110
111struct AudioCodec : public Codec {
112 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800113 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
115 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800116 AudioCodec(int id,
117 const std::string& name,
118 int clockrate,
119 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700120 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000122 AudioCodec();
123 AudioCodec(const AudioCodec& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200124 virtual ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
126 // Indicates if this codec is compatible with the specified codec.
127 bool Matches(const AudioCodec& codec) const;
128
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 std::string ToString() const;
130
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700131 webrtc::RtpCodecParameters ToCodecParameters() const override;
132
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000133 AudioCodec& operator=(const AudioCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000135 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
137 bool operator!=(const AudioCodec& c) const {
138 return !(*this == c);
139 }
140};
141
142struct VideoCodec : public Codec {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800144 VideoCodec(int id, const std::string& name);
magjed1e45cc62016-10-28 07:43:45 -0700145 // Creates a codec with the given name and empty id.
146 explicit VideoCodec(const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000148 VideoCodec();
149 VideoCodec(const VideoCodec& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200150 virtual ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 std::string ToString() const;
153
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000154 VideoCodec& operator=(const VideoCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000156 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157
158 bool operator!=(const VideoCodec& c) const {
159 return !(*this == c);
160 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000161
162 static VideoCodec CreateRtxCodec(int rtx_payload_type,
163 int associated_payload_type);
164
165 enum CodecType {
166 CODEC_VIDEO,
167 CODEC_RED,
168 CODEC_ULPFEC,
brandtr87d7d772016-11-07 03:03:41 -0800169 CODEC_FLEXFEC,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000170 CODEC_RTX,
171 };
172
173 CodecType GetCodecType() const;
174 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
175 // don't make sense (such as max < min bitrate), and error is logged and
176 // ValidateCodecFormat returns false.
177 bool ValidateCodecFormat() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178};
179
180struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700181 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000182 DataCodec();
183 DataCodec(const DataCodec& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200184 virtual ~DataCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000186 DataCodec& operator=(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
188 std::string ToString() const;
189};
190
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000191// Get the codec setting associated with |payload_type|. If there
192// is no codec associated with that payload type it returns false.
193template <class Codec>
194bool FindCodecById(const std::vector<Codec>& codecs,
195 int payload_type,
196 Codec* codec_out) {
197 for (const auto& codec : codecs) {
198 if (codec.id == payload_type) {
199 *codec_out = codec;
200 return true;
201 }
202 }
203 return false;
204}
205
Shao Changbine62202f2015-04-21 20:24:50 +0800206bool CodecNamesEq(const std::string& name1, const std::string& name2);
magjed1e45cc62016-10-28 07:43:45 -0700207bool CodecNamesEq(const char* name1, const char* name2);
208webrtc::VideoCodecType CodecTypeFromName(const std::string& name);
stefanba4c0e42016-02-04 04:12:24 -0800209bool HasNack(const Codec& codec);
210bool HasRemb(const Codec& codec);
211bool HasTransportCc(const Codec& codec);
magjed1e45cc62016-10-28 07:43:45 -0700212bool IsCodecSupported(const std::vector<VideoCodec>& supported_codecs,
213 const VideoCodec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215} // namespace cricket
216
kjellandera96e2d72016-02-04 23:52:28 -0800217#endif // WEBRTC_MEDIA_BASE_CODEC_H_