| 6 | #include "media/base/codec.h" |
| 7 | #include "absl/strings/match.h" |
| 8 | #include "rtc_base/logging.h" |
| 9 | #include "system_wrappers/include/field_trial.h" |
| 10 |
| 11 | namespace tgcalls { |
| 12 | namespace { |
| 13 |
| 14 | using VideoFormat = webrtc::SdpVideoFormat; |
| 15 |
| 16 | bool CompareFormats(const VideoFormat &a, const VideoFormat &b) { |
| 17 | if (a.name < b.name) { |
| 18 | return true; |
| 19 | } else if (b.name < a.name) { |
| 20 | return false; |
| 21 | } else { |
| 22 | return a.parameters < b.parameters; |
| 23 | } |
| 24 | } |
| 25 |
| 26 | int FormatPriority(const VideoFormat &format, const std::vector<std::string> &preferredCodecs, std::shared_ptr<PlatformContext> platformContext) { |
| 27 | static const auto kCodecs = { |
| 28 | std::string(cricket::kAv1CodecName), |
| 29 | #ifndef WEBRTC_DISABLE_H265 |
| 30 | std::string(cricket::kH265CodecName), |
| 31 | #endif |
| 32 | std::string(cricket::kH264CodecName), |
| 33 | std::string(cricket::kVp8CodecName), |
| 34 | std::string(cricket::kVp9CodecName), |
| 35 | }; |
| 36 | static const auto kSupported = [platformContext] { |
| 37 | const auto platform = PlatformInterface::SharedInstance(); |
| 38 |
| 39 | auto result = std::vector<std::string>(); |
| 40 | result.reserve(kCodecs.size()); |
| 41 | for (const auto &codec : kCodecs) { |
| 42 | if (platform->supportsEncoding(codec, platformContext)) { |
| 43 | result.push_back(codec); |
| 44 | } |
| 45 | } |
| 46 | return result; |
| 47 | }(); |
| 48 |
| 49 | for (int i = 0; i < preferredCodecs.size(); i++) { |
| 50 | for (const auto &name : kSupported) { |
| 51 | if (absl::EqualsIgnoreCase(format.name, preferredCodecs[i]) && absl::EqualsIgnoreCase(format.name, name)) { |
| 52 | return i; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 |
| 57 | auto result = (int)preferredCodecs.size(); |
| 58 | for (const auto &name : kSupported) { |
| 59 | if (absl::EqualsIgnoreCase(format.name, name)) { |
| 60 | return result; |
| 61 | } |
| 62 | ++result; |
| 63 | } |
| 64 | return -1; |
| 65 | } |
| 66 |
| 67 | bool ComparePriorities(const VideoFormat &a, const VideoFormat &b, const std::vector<std::string> &preferredCodecs, std::shared_ptr<PlatformContext> platformContext) { |
| 68 | return FormatPriority(a, preferredCodecs, platformContext) < FormatPriority(b, preferredCodecs, platformContext); |
| 69 | } |
| 70 |
| 71 | std::vector<VideoFormat> FilterAndSortEncoders(std::vector<VideoFormat> list, const std::vector<std::string> &preferredCodecs, std::shared_ptr<PlatformContext> platformContext) { |
| 72 | const auto listBegin = begin(list); |
| 73 | const auto listEnd = end(list); |
| 74 | std::sort(listBegin, listEnd, [&preferredCodecs, platformContext](const VideoFormat &lhs, const VideoFormat &rhs) { |
| 75 | return ComparePriorities(lhs, rhs, preferredCodecs, platformContext); |
| 76 | }); |
| 77 | auto eraseFrom = listBegin; |
| 78 | auto eraseTill = eraseFrom; |
| 79 | while (eraseTill != listEnd && FormatPriority(*eraseTill, preferredCodecs, platformContext) == -1) { |
| 80 | ++eraseTill; |
| 81 | } |
| 82 | if (eraseTill != eraseFrom) { |
| 83 | list.erase(eraseFrom, eraseTill); |
| 84 | } |
| 85 | return list; |
| 86 | } |
| 87 |
| 88 | std::vector<VideoFormat> AppendUnique( |
| 89 | std::vector<VideoFormat> list, |
| 90 | std::vector<VideoFormat> other) { |
| 91 | if (list.empty()) { |
| 92 | return other; |
| 93 | } |
| 94 | list.reserve(list.size() + other.size()); |
| 95 | const auto oldBegin = &list[0]; |
| 96 | const auto oldEnd = oldBegin + list.size(); |
| 97 | for (auto &format : other) { |
| 98 | if (std::find(oldBegin, oldEnd, format) == oldEnd) { |
| 99 | list.push_back(std::move(format)); |
| 100 | } |
| 101 | } |
| 102 | return list; |
| 103 | } |
| 104 |
| 105 | std::vector<VideoFormat>::const_iterator FindEqualFormat( |
| 106 | const std::vector<VideoFormat> &list, |
| 107 | const VideoFormat &format) { |
| 108 | return std::find_if(list.begin(), list.end(), [&](const VideoFormat &other) { |
| 109 | return format.IsSameCodec(other); |
| 110 | }); |
| 111 | } |
| 112 |
| 113 | void AddDefaultFeedbackParams(cricket::VideoCodec *codec) { |
| 114 | // Don't add any feedback params for RED and ULPFEC. |
| 115 | if (codec->name == cricket::kRedCodecName || codec->name == cricket::kUlpfecCodecName) |
| 116 | return; |
| 117 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamRemb, cricket::kParamValueEmpty)); |
| 118 | codec->AddFeedbackParam( |
| 119 | cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc, cricket::kParamValueEmpty)); |
| 120 | // Don't add any more feedback params for FLEXFEC. |
| 121 | if (codec->name == cricket::kFlexfecCodecName) |
| 122 | return; |
| 123 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamCcm, cricket::kRtcpFbCcmParamFir)); |
| 124 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kParamValueEmpty)); |
| 125 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kRtcpFbNackParamPli)); |
| 126 | if (codec->name == cricket::kVp8CodecName && |
| 127 | webrtc::field_trial::IsEnabled("WebRTC-RtcpLossNotification")) { |
| 128 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamLntf, cricket::kParamValueEmpty)); |
| 129 | } |
| 130 | } |
| 131 |
| 132 | std::vector<VideoFormat> RemoveScalabilityModes( |
| 133 | std::vector<VideoFormat> list) { |
| 134 | auto changed = false; |
| 135 | for (auto &entry : list) { |
| 136 | if (!entry.scalability_modes.empty()) { |
| 137 | entry.scalability_modes = {}; |
| 138 | changed = true; |
| 139 | } |
| 140 | } |
| 141 | if (changed && list.size() > 1) { |
| 142 | for (auto i = list.end() - 1; i != list.begin(); --i) { |
| 143 | if (std::find(list.begin(), i, *i) != i) { |
| 144 | i = list.erase(i); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | return list; |
| 149 | } |
| 150 |
| 151 | } // namespace |
| 152 |
| 153 | VideoFormatsMessage ComposeSupportedFormats( |
| 154 | std::vector<VideoFormat> encoders, |
| 155 | std::vector<VideoFormat> decoders, |
| 156 | const std::vector<std::string> &preferredCodecs, |
| 157 | std::shared_ptr<PlatformContext> platformContext) { |
| 158 | // We don't pass scalability_modes through signaling, |
| 159 | // So we have to remove them here, otherwise lists are different. |
| 160 | encoders = RemoveScalabilityModes(std::move(encoders)); |
| 161 | decoders = RemoveScalabilityModes(std::move(decoders)); |
| 162 |
| 163 | encoders = FilterAndSortEncoders(std::move(encoders), preferredCodecs, platformContext); |
| 164 |
| 165 | auto result = VideoFormatsMessage(); |
| 166 | result.encodersCount = (int)encoders.size(); |
| 167 | result.formats = AppendUnique(std::move(encoders), std::move(decoders)); |
| 168 | for (const auto &format : result.formats) { |
| 169 | RTC_LOG(LS_INFO) << "Format: " << format.ToString(); |
| 170 | } |
| 171 | RTC_LOG(LS_INFO) << "First " << result.encodersCount << " formats are supported encoders."; |
| 172 | return result; |
| 173 | } |
| 174 |
| 175 | CommonFormats ComputeCommonFormats( |
| 176 | const VideoFormatsMessage &my, |
| 177 | VideoFormatsMessage their) { |
| 178 | assert(my.encodersCount <= my.formats.size()); |
| 179 | assert(their.encodersCount <= their.formats.size()); |
| 180 |
| 181 | for (const auto &format : their.formats) { |
| 182 | RTC_LOG(LS_INFO) << "Their format: " << format.ToString(); |
| 183 | } |
| 184 | RTC_LOG(LS_INFO) << "Their first " << their.encodersCount << " formats are supported encoders."; |
| 185 |
| 186 | const auto myEncodersBegin = begin(my.formats); |
| 187 | const auto myEncodersEnd = myEncodersBegin + my.encodersCount; |
| 188 | const auto theirEncodersBegin = begin(their.formats); |
| 189 | const auto theirEncodersEnd = theirEncodersBegin + their.encodersCount; |
| 190 |
| 191 | auto result = CommonFormats(); |
| 192 | const auto addUnique = [&](const VideoFormat &format) { |
| 193 | const auto already = std::find( |
| 194 | result.list.begin(), |
| 195 | result.list.end(), |
| 196 | format); |
| 197 | if (already == result.list.end()) { |
| 198 | result.list.push_back(format); |
| 199 | } |
| 200 | }; |
| 201 | const auto addCommonAndFindFirst = [&]( |
| 202 | std::vector<VideoFormat>::const_iterator begin, |
| 203 | std::vector<VideoFormat>::const_iterator end, |
| 204 | const std::vector<VideoFormat> &decoders) { |
| 205 | auto first = VideoFormat(std::string()); |
| 206 | for (auto i = begin; i != end; ++i) { |
| 207 | const auto &format = *i; |
| 208 | const auto j = FindEqualFormat(decoders, format); |
| 209 | if (j != decoders.end()) { |
| 210 | if (first.name.empty()) { |
| 211 | first = format; |
| 212 | } |
| 213 | addUnique(format); |
| 214 | addUnique(*j); |
| 215 | }; |
| 216 | } |
| 217 | return first; |
| 218 | }; |
| 219 |
| 220 | result.list.reserve(my.formats.size() + their.formats.size()); |
| 221 | auto myEncoderFormat = addCommonAndFindFirst( |
| 222 | myEncodersBegin, |
| 223 | myEncodersEnd, |
| 224 | their.formats); |
| 225 | auto theirEncoderFormat = addCommonAndFindFirst( |
| 226 | theirEncodersBegin, |
| 227 | theirEncodersEnd, |
| 228 | my.formats); |
| 229 | std::sort(begin(result.list), end(result.list), CompareFormats); |
| 230 | if (!myEncoderFormat.name.empty()) { |
| 231 | const auto i = std::find(begin(result.list), end(result.list), myEncoderFormat); |
| 232 | assert(i != end(result.list)); |
| 233 | result.myEncoderIndex = (i - begin(result.list)); |
| 234 | } |
| 235 |
| 236 | for (const auto &format : result.list) { |
| 237 | RTC_LOG(LS_INFO) << "Common format: " << format.ToString(); |
| 238 | } |
| 239 | RTC_LOG(LS_INFO) << "My encoder: " << (result.myEncoderIndex >= 0 ? result.list[result.myEncoderIndex].ToString() : "(null)"); |
| 240 | RTC_LOG(LS_INFO) << "Their encoder: " << (!theirEncoderFormat.name.empty() ? theirEncoderFormat.ToString() : "(null)"); |
| 241 |
| 242 | return result; |
| 243 | } |
| 244 |
| 245 | CommonCodecs AssignPayloadTypesAndDefaultCodecs(CommonFormats &&formats) { |
| 246 | if (formats.list.empty()) { |
| 247 | return CommonCodecs(); |
| 248 | } |
| 249 |
| 250 | constexpr int kFirstDynamicPayloadType = 96; |
| 251 | constexpr int kLastDynamicPayloadType = 127; |
| 252 |
| 253 | int payload_type = kFirstDynamicPayloadType; |
| 254 |
| 255 | formats.list.push_back(webrtc::SdpVideoFormat(cricket::kRedCodecName)); |
| 256 | formats.list.push_back(webrtc::SdpVideoFormat(cricket::kUlpfecCodecName)); |
| 257 |
| 258 | if (true) { |
| 259 | webrtc::SdpVideoFormat flexfec_format(cricket::kFlexfecCodecName); |
| 260 | // This value is currently arbitrarily set to 10 seconds. (The unit |
| 261 | // is microseconds.) This parameter MUST be present in the SDP, but |
| 262 | // we never use the actual value anywhere in our code however. |
| 263 | // TODO(brandtr): Consider honouring this value in the sender and receiver. |
| 264 | flexfec_format.parameters = { {cricket::kFlexfecFmtpRepairWindow, "10000000"} }; |
| 265 | formats.list.push_back(flexfec_format); |
| 266 | } |
| 267 |
| 268 | auto inputIndex = 0; |
| 269 | auto result = CommonCodecs(); |
| 270 | result.list.reserve(2 * formats.list.size() - 2); |
| 271 | for (const auto &format : formats.list) { |
| 272 | cricket::VideoCodec codec = cricket::CreateVideoCodec(format); |
| 273 | codec.id = payload_type; |
| 274 | AddDefaultFeedbackParams(&codec); |
| 275 |
| 276 | if (inputIndex++ == formats.myEncoderIndex) { |
| 277 | result.myEncoderIndex = (int)result.list.size(); |
| 278 | } |
| 279 | result.list.push_back(codec); |
| 280 |
| 281 | // Increment payload type. |
| 282 | ++payload_type; |
| 283 | if (payload_type > kLastDynamicPayloadType) { |
| 284 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 285 | break; |
| 286 | } |
| 287 |
| 288 | // Add associated RTX codec for non-FEC codecs. |
| 289 | if (!absl::EqualsIgnoreCase(codec.name, cricket::kUlpfecCodecName) && |
| 290 | !absl::EqualsIgnoreCase(codec.name, cricket::kFlexfecCodecName)) { |
| 291 | result.list.push_back(cricket::CreateVideoRtxCodec(payload_type, codec.id)); |
| 292 |
| 293 | // Increment payload type. |
| 294 | ++payload_type; |
| 295 | if (payload_type > kLastDynamicPayloadType) { |
| 296 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | return result; |
| 302 | } |
| 303 |
| 304 | } // namespace tgcalls |
| 305 |