blob: 76f60ec14aacb60fb9652b73924d5f32b999e088 [file] [log] [blame]
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -08001/*
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 Gerey3e707812018-11-28 16:47:49 +010013#include <map>
14#include <string>
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080015#include <utility>
16
Niels Möller039743e2018-10-23 10:07:25 +020017#include "absl/strings/match.h"
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080018#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
25namespace {
26
27bool IsMultiplexCodec(const cricket::VideoCodec& codec) {
Niels Möller039743e2018-10-23 10:07:25 +020028 return absl::EqualsIgnoreCase(codec.name.c_str(),
29 cricket::kMultiplexCodecName);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080030}
31
32} // anonymous namespace
33
34namespace webrtc {
35
36constexpr const char* kMultiplexAssociatedCodecName = cricket::kVp9CodecName;
37
38MultiplexEncoderFactory::MultiplexEncoderFactory(
Tarek Hefny77c8e652018-08-15 10:19:32 -070039 std::unique_ptr<VideoEncoderFactory> factory,
40 bool supports_augmenting_data)
41 : factory_(std::move(factory)),
42 supports_augmenting_data_(supports_augmenting_data) {}
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080043
44std::vector<SdpVideoFormat> MultiplexEncoderFactory::GetSupportedFormats()
45 const {
46 std::vector<SdpVideoFormat> formats = factory_->GetSupportedFormats();
47 for (const auto& format : formats) {
Niels Möller039743e2018-10-23 10:07:25 +020048 if (absl::EqualsIgnoreCase(format.name, kMultiplexAssociatedCodecName)) {
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080049 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
60VideoEncoderFactory::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
68std::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 Hefny77c8e652018-08-15 10:19:32 -070080 return std::unique_ptr<VideoEncoder>(new MultiplexEncoderAdapter(
81 factory_.get(), associated_format, supports_augmenting_data_));
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080082}
83
84MultiplexDecoderFactory::MultiplexDecoderFactory(
Tarek Hefny77c8e652018-08-15 10:19:32 -070085 std::unique_ptr<VideoDecoderFactory> factory,
86 bool supports_augmenting_data)
87 : factory_(std::move(factory)),
88 supports_augmenting_data_(supports_augmenting_data) {}
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080089
90std::vector<SdpVideoFormat> MultiplexDecoderFactory::GetSupportedFormats()
91 const {
92 std::vector<SdpVideoFormat> formats = factory_->GetSupportedFormats();
93 for (const auto& format : formats) {
Niels Möller039743e2018-10-23 10:07:25 +020094 if (absl::EqualsIgnoreCase(format.name, kMultiplexAssociatedCodecName)) {
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080095 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
105std::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 Hefny77c8e652018-08-15 10:19:32 -0700117 return std::unique_ptr<VideoDecoder>(new MultiplexDecoderAdapter(
118 factory_.get(), associated_format, supports_augmenting_data_));
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800119}
120
121} // namespace webrtc