diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn index a044c469e0..206bd13b2b 100644 --- a/sdk/BUILD.gn +++ b/sdk/BUILD.gn @@ -1173,6 +1173,7 @@ if (is_ios || is_mac) { "../api:scoped_refptr", "../api/audio_codecs:builtin_audio_decoder_factory", "../api/audio_codecs:builtin_audio_encoder_factory", + "../api/environment:environment_factory", "../api/task_queue:default_task_queue_factory", "../api/video:video_frame", "../api/video_codecs:video_codecs_api", @@ -1539,6 +1540,9 @@ if (is_ios || is_mac) { rtc_library("wrapped_native_codec_objc") { sources = [ + "objc/api/video_codec/RTCNativeVideoDecoder.h", + "objc/api/video_codec/RTCNativeVideoDecoder.mm", + "objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h", "objc/api/video_codec/RTCWrappedNativeVideoDecoder.h", "objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm", "objc/api/video_codec/RTCWrappedNativeVideoEncoder.h", @@ -1551,6 +1555,7 @@ if (is_ios || is_mac) { deps = [ ":base_objc", ":helpers_objc", + "../api/environment", "../api/video_codecs:video_codecs_api", "../media:codec", "../media:rtc_media_base", @@ -1662,6 +1667,7 @@ if (is_ios || is_mac) { ":vpx_codec_constants", ":wrapped_native_codec_objc", "../api:make_ref_counted", + "../api/environment", "../api/video:video_frame", "../api/video:video_rtp_headers", "../api/video_codecs:video_codecs_api", diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h new file mode 100644 index 0000000000..c88642fbb3 --- /dev/null +++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#import + +#import "base/RTCMacros.h" +#import "base/RTCVideoDecoder.h" + +// NativeVideoDecoder pretends to conform to RTCVideoDecoder protocol, but +// expects its methods won't be called. +@interface RTC_OBJC_TYPE (RTCNativeVideoDecoder) : NSObject + +@end diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm new file mode 100644 index 0000000000..8ade15b3c9 --- /dev/null +++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#import + +#import "RTCWrappedNativeVideoDecoder.h" +#import "base/RTCMacros.h" +#import "helpers/NSString+StdString.h" + +@implementation RTC_OBJC_TYPE (RTCNativeVideoDecoder) + +- (void)setCallback:(RTCVideoDecoderCallback)callback { + RTC_DCHECK_NOTREACHED(); +} + +- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores { + RTC_DCHECK_NOTREACHED(); + return 0; +} + +- (NSInteger)releaseDecoder { + RTC_DCHECK_NOTREACHED(); + return 0; +} + +// TODO: bugs.webrtc.org/15444 - Remove obsolete missingFrames param. +- (NSInteger)decode:(RTC_OBJC_TYPE(RTCEncodedImage) *)encodedImage + missingFrames:(BOOL)missingFrames + codecSpecificInfo:(nullable id)info + renderTimeMs:(int64_t)renderTimeMs { + RTC_DCHECK_NOTREACHED(); + return 0; +} + +- (NSString *)implementationName { + RTC_DCHECK_NOTREACHED(); + return nil; +} + +@end diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h b/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h new file mode 100644 index 0000000000..e19343d45f --- /dev/null +++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#import + +#import "base/RTCMacros.h" + +#include + +#include "api/environment/environment.h" +#include "api/video_codecs/video_decoder.h" + +@protocol RTC_OBJC_TYPE +(RTCNativeVideoDecoderBuilder) + + - (std::unique_ptr)build : (const webrtc::Environment&)env; + +@end diff --git a/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm b/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm index c150cf6d3a..66d3d0f0a9 100644 --- a/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm +++ b/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm @@ -12,16 +12,28 @@ #import #import "RTCMacros.h" +#import "RTCNativeVideoDecoder.h" +#import "RTCNativeVideoDecoderBuilder+Native.h" #import "RTCVideoDecoderVP8.h" -#import "RTCWrappedNativeVideoDecoder.h" #include "modules/video_coding/codecs/vp8/include/vp8.h" -@implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8) - -+ (id)vp8Decoder { - return [[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) alloc] - initWithNativeDecoder:std::unique_ptr(webrtc::VP8Decoder::Create())]; -} - +@interface RTC_OBJC_TYPE (RTCVideoDecoderVP8Builder) + : RTC_OBJC_TYPE(RTCNativeVideoDecoder) @end + + @implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8Builder) + + - (std::unique_ptr)build:(const webrtc::Environment&)env { + return webrtc::CreateVp8Decoder(env); + } + + @end + + @implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8) + + + (id)vp8Decoder { + return [[RTC_OBJC_TYPE(RTCVideoDecoderVP8Builder) alloc] init]; + } + + @end diff --git a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h index 3a9b39e959..93ffa2ff57 100644 --- a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h +++ b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h @@ -10,13 +10,15 @@ #import +#import "RTCNativeVideoDecoder.h" #import "base/RTCMacros.h" #import "base/RTCVideoDecoder.h" #include "api/video_codecs/video_decoder.h" #include "media/base/codec.h" -@interface RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) : NSObject +// TODO: bugs.webrtc.org/15791 - Remove in favor of the RTCNativeVideoDecoderBuilder +@interface RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) : RTC_OBJC_TYPE (RTCNativeVideoDecoder) - (instancetype)initWithNativeDecoder:(std::unique_ptr)decoder; diff --git a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm index 29d2265e20..b812d9450d 100644 --- a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm +++ b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm @@ -12,7 +12,6 @@ #import "RTCWrappedNativeVideoDecoder.h" #import "base/RTCMacros.h" -#import "helpers/NSString+StdString.h" @implementation RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) { std::unique_ptr _wrappedDecoder; @@ -30,34 +29,4 @@ return std::move(_wrappedDecoder); } -#pragma mark - RTC_OBJC_TYPE(RTCVideoDecoder) - -- (void)setCallback:(RTCVideoDecoderCallback)callback { - RTC_DCHECK_NOTREACHED(); -} - -- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores { - RTC_DCHECK_NOTREACHED(); - return 0; -} - -- (NSInteger)releaseDecoder { - RTC_DCHECK_NOTREACHED(); - return 0; -} - -// TODO(bugs.webrtc.org/15444): Remove obsolete missingFrames param. -- (NSInteger)decode:(RTC_OBJC_TYPE(RTCEncodedImage) *)encodedImage - missingFrames:(BOOL)missingFrames - codecSpecificInfo:(nullable id)info - renderTimeMs:(int64_t)renderTimeMs { - RTC_DCHECK_NOTREACHED(); - return 0; -} - -- (NSString *)implementationName { - RTC_DCHECK_NOTREACHED(); - return nil; -} - @end diff --git a/sdk/objc/native/src/objc_video_decoder_factory.h b/sdk/objc/native/src/objc_video_decoder_factory.h index 19c997e503..de10fbcc62 100644 --- a/sdk/objc/native/src/objc_video_decoder_factory.h +++ b/sdk/objc/native/src/objc_video_decoder_factory.h @@ -13,6 +13,7 @@ #import "base/RTCMacros.h" +#include "api/environment/environment.h" #include "api/video_codecs/video_decoder_factory.h" #include "media/base/codec.h" @@ -29,7 +30,8 @@ class ObjCVideoDecoderFactory : public VideoDecoderFactory { id wrapped_decoder_factory() const; std::vector GetSupportedFormats() const override; - std::unique_ptr CreateVideoDecoder(const SdpVideoFormat& format) override; + std::unique_ptr Create(const Environment& env, + const SdpVideoFormat& format) override; private: id decoder_factory_; diff --git a/sdk/objc/native/src/objc_video_decoder_factory.mm b/sdk/objc/native/src/objc_video_decoder_factory.mm index bf5e898a6d..8939940deb 100644 --- a/sdk/objc/native/src/objc_video_decoder_factory.mm +++ b/sdk/objc/native/src/objc_video_decoder_factory.mm @@ -18,6 +18,7 @@ #import "components/video_codec/RTCCodecSpecificInfoH264.h" #import "sdk/objc/api/peerconnection/RTCEncodedImage+Private.h" #import "sdk/objc/api/peerconnection/RTCVideoCodecInfo+Private.h" +#import "sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h" #import "sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h" #import "sdk/objc/helpers/NSString+StdString.h" @@ -42,8 +43,7 @@ class ObjCVideoDecoder : public VideoDecoder { [decoder_ startDecodeWithNumberOfCores:settings.number_of_cores()] == WEBRTC_VIDEO_CODEC_OK; } - int32_t Decode(const EncodedImage &input_image, - int64_t render_time_ms = -1) override { + int32_t Decode(const EncodedImage &input_image, int64_t render_time_ms = -1) override { RTC_OBJC_TYPE(RTCEncodedImage) *encodedImage = [[RTC_OBJC_TYPE(RTCEncodedImage) alloc] initWithNativeEncodedImage:input_image]; @@ -91,14 +91,16 @@ id ObjCVideoDecoderFactory::wrapped_decod return decoder_factory_; } -std::unique_ptr ObjCVideoDecoderFactory::CreateVideoDecoder( - const SdpVideoFormat &format) { +std::unique_ptr ObjCVideoDecoderFactory::Create(const Environment &env, + const SdpVideoFormat &format) { NSString *codecName = [NSString stringWithUTF8String:format.name.c_str()]; for (RTC_OBJC_TYPE(RTCVideoCodecInfo) * codecInfo in decoder_factory_.supportedCodecs) { if ([codecName isEqualToString:codecInfo.name]) { id decoder = [decoder_factory_ createDecoder:codecInfo]; - if ([decoder isKindOfClass:[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) class]]) { + if ([decoder conformsToProtocol:@protocol(RTC_OBJC_TYPE(RTCNativeVideoDecoderBuilder))]) { + return [((id)decoder) build:env]; + } else if ([decoder isKindOfClass:[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) class]]) { return [(RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) *)decoder releaseWrappedDecoder]; } else { return std::unique_ptr(new ObjCVideoDecoder(decoder)); diff --git a/sdk/objc/unittests/objc_video_decoder_factory_tests.mm b/sdk/objc/unittests/objc_video_decoder_factory_tests.mm index f44d831d29..5b2b401a03 100644 --- a/sdk/objc/unittests/objc_video_decoder_factory_tests.mm +++ b/sdk/objc/unittests/objc_video_decoder_factory_tests.mm @@ -14,6 +14,7 @@ #include "sdk/objc/native/src/objc_video_decoder_factory.h" +#include "api/environment/environment_factory.h" #import "base/RTCMacros.h" #import "base/RTCVideoDecoder.h" #import "base/RTCVideoDecoderFactory.h" @@ -26,9 +27,9 @@ id CreateDecoderFactoryReturning(int retu id decoderMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoder))); OCMStub([decoderMock startDecodeWithNumberOfCores:1]).andReturn(return_code); OCMStub([decoderMock decode:[OCMArg any] - missingFrames:NO - codecSpecificInfo:[OCMArg any] - renderTimeMs:0]) + missingFrames:NO + codecSpecificInfo:[OCMArg any] + renderTimeMs:0]) .andReturn(return_code); OCMStub([decoderMock releaseDecoder]).andReturn(return_code); @@ -51,7 +52,8 @@ id CreateErrorDecoderFactory() { std::unique_ptr GetObjCDecoder( id factory) { webrtc::ObjCVideoDecoderFactory decoder_factory(factory); - return decoder_factory.CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName)); + return decoder_factory.Create(webrtc::CreateEnvironment(), + webrtc::SdpVideoFormat(cricket::kH264CodecName)); } #pragma mark -