Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #include "media/engine/multiplexcodecfactory.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame^] | 13 | #include <map> |
| 14 | #include <string> |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 18 | #include "api/video_codecs/sdp_video_format.h" |
| 19 | #include "media/base/codec.h" |
| 20 | #include "media/base/mediaconstants.h" |
| 21 | #include "modules/video_coding/codecs/multiplex/include/multiplex_decoder_adapter.h" |
| 22 | #include "modules/video_coding/codecs/multiplex/include/multiplex_encoder_adapter.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | bool IsMultiplexCodec(const cricket::VideoCodec& codec) { |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 28 | return absl::EqualsIgnoreCase(codec.name.c_str(), |
| 29 | cricket::kMultiplexCodecName); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | } // anonymous namespace |
| 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | constexpr const char* kMultiplexAssociatedCodecName = cricket::kVp9CodecName; |
| 37 | |
| 38 | MultiplexEncoderFactory::MultiplexEncoderFactory( |
Tarek Hefny | 77c8e65 | 2018-08-15 10:19:32 -0700 | [diff] [blame] | 39 | std::unique_ptr<VideoEncoderFactory> factory, |
| 40 | bool supports_augmenting_data) |
| 41 | : factory_(std::move(factory)), |
| 42 | supports_augmenting_data_(supports_augmenting_data) {} |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 43 | |
| 44 | std::vector<SdpVideoFormat> MultiplexEncoderFactory::GetSupportedFormats() |
| 45 | const { |
| 46 | std::vector<SdpVideoFormat> formats = factory_->GetSupportedFormats(); |
| 47 | for (const auto& format : formats) { |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 48 | if (absl::EqualsIgnoreCase(format.name, kMultiplexAssociatedCodecName)) { |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 49 | SdpVideoFormat multiplex_format = format; |
| 50 | multiplex_format.parameters[cricket::kCodecParamAssociatedCodecName] = |
| 51 | format.name; |
| 52 | multiplex_format.name = cricket::kMultiplexCodecName; |
| 53 | formats.push_back(multiplex_format); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | return formats; |
| 58 | } |
| 59 | |
| 60 | VideoEncoderFactory::CodecInfo MultiplexEncoderFactory::QueryVideoEncoder( |
| 61 | const SdpVideoFormat& format) const { |
| 62 | if (!IsMultiplexCodec(cricket::VideoCodec(format))) |
| 63 | return factory_->QueryVideoEncoder(format); |
| 64 | return factory_->QueryVideoEncoder( |
| 65 | SdpVideoFormat(kMultiplexAssociatedCodecName)); |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<VideoEncoder> MultiplexEncoderFactory::CreateVideoEncoder( |
| 69 | const SdpVideoFormat& format) { |
| 70 | if (!IsMultiplexCodec(cricket::VideoCodec(format))) |
| 71 | return factory_->CreateVideoEncoder(format); |
| 72 | const auto& it = |
| 73 | format.parameters.find(cricket::kCodecParamAssociatedCodecName); |
| 74 | if (it == format.parameters.end()) { |
| 75 | RTC_LOG(LS_ERROR) << "No assicated codec for multiplex."; |
| 76 | return nullptr; |
| 77 | } |
| 78 | SdpVideoFormat associated_format = format; |
| 79 | associated_format.name = it->second; |
Tarek Hefny | 77c8e65 | 2018-08-15 10:19:32 -0700 | [diff] [blame] | 80 | return std::unique_ptr<VideoEncoder>(new MultiplexEncoderAdapter( |
| 81 | factory_.get(), associated_format, supports_augmenting_data_)); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | MultiplexDecoderFactory::MultiplexDecoderFactory( |
Tarek Hefny | 77c8e65 | 2018-08-15 10:19:32 -0700 | [diff] [blame] | 85 | std::unique_ptr<VideoDecoderFactory> factory, |
| 86 | bool supports_augmenting_data) |
| 87 | : factory_(std::move(factory)), |
| 88 | supports_augmenting_data_(supports_augmenting_data) {} |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 89 | |
| 90 | std::vector<SdpVideoFormat> MultiplexDecoderFactory::GetSupportedFormats() |
| 91 | const { |
| 92 | std::vector<SdpVideoFormat> formats = factory_->GetSupportedFormats(); |
| 93 | for (const auto& format : formats) { |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 94 | if (absl::EqualsIgnoreCase(format.name, kMultiplexAssociatedCodecName)) { |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 95 | SdpVideoFormat multiplex_format = format; |
| 96 | multiplex_format.parameters[cricket::kCodecParamAssociatedCodecName] = |
| 97 | format.name; |
| 98 | multiplex_format.name = cricket::kMultiplexCodecName; |
| 99 | formats.push_back(multiplex_format); |
| 100 | } |
| 101 | } |
| 102 | return formats; |
| 103 | } |
| 104 | |
| 105 | std::unique_ptr<VideoDecoder> MultiplexDecoderFactory::CreateVideoDecoder( |
| 106 | const SdpVideoFormat& format) { |
| 107 | if (!IsMultiplexCodec(cricket::VideoCodec(format))) |
| 108 | return factory_->CreateVideoDecoder(format); |
| 109 | const auto& it = |
| 110 | format.parameters.find(cricket::kCodecParamAssociatedCodecName); |
| 111 | if (it == format.parameters.end()) { |
| 112 | RTC_LOG(LS_ERROR) << "No assicated codec for multiplex."; |
| 113 | return nullptr; |
| 114 | } |
| 115 | SdpVideoFormat associated_format = format; |
| 116 | associated_format.name = it->second; |
Tarek Hefny | 77c8e65 | 2018-08-15 10:19:32 -0700 | [diff] [blame] | 117 | return std::unique_ptr<VideoDecoder>(new MultiplexDecoderAdapter( |
| 118 | factory_.get(), associated_format, supports_augmenting_data_)); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace webrtc |