xref: /aosp_15_r20/external/webrtc/sdk/objc/unittests/objc_video_decoder_factory_tests.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
2 *  Copyright 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#import <Foundation/Foundation.h>
12#import <OCMock/OCMock.h>
13#import <XCTest/XCTest.h>
14
15#include "sdk/objc/native/src/objc_video_decoder_factory.h"
16
17#import "base/RTCMacros.h"
18#import "base/RTCVideoDecoder.h"
19#import "base/RTCVideoDecoderFactory.h"
20#include "media/base/codec.h"
21#include "modules/video_coding/include/video_codec_interface.h"
22#include "modules/video_coding/include/video_error_codes.h"
23#include "rtc_base/gunit.h"
24
25id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateDecoderFactoryReturning(int return_code) {
26  id decoderMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoder)));
27  OCMStub([decoderMock startDecodeWithNumberOfCores:1]).andReturn(return_code);
28  OCMStub([decoderMock decode:[OCMArg any]
29                    missingFrames:NO
30                codecSpecificInfo:[OCMArg any]
31                     renderTimeMs:0])
32      .andReturn(return_code);
33  OCMStub([decoderMock releaseDecoder]).andReturn(return_code);
34
35  id decoderFactoryMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoderFactory)));
36  RTC_OBJC_TYPE(RTCVideoCodecInfo)* supported =
37      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264" parameters:nil];
38  OCMStub([decoderFactoryMock supportedCodecs]).andReturn(@[ supported ]);
39  OCMStub([decoderFactoryMock createDecoder:[OCMArg any]]).andReturn(decoderMock);
40  return decoderFactoryMock;
41}
42
43id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateOKDecoderFactory() {
44  return CreateDecoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK);
45}
46
47id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateErrorDecoderFactory() {
48  return CreateDecoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR);
49}
50
51std::unique_ptr<webrtc::VideoDecoder> GetObjCDecoder(
52    id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> factory) {
53  webrtc::ObjCVideoDecoderFactory decoder_factory(factory);
54  return decoder_factory.CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName));
55}
56
57#pragma mark -
58
59@interface ObjCVideoDecoderFactoryTests : XCTestCase
60@end
61
62@implementation ObjCVideoDecoderFactoryTests
63
64- (void)testConfigureReturnsTrueOnSuccess {
65  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
66
67  webrtc::VideoDecoder::Settings settings;
68  EXPECT_TRUE(decoder->Configure(settings));
69}
70
71- (void)testConfigureReturnsFalseOnFail {
72  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
73
74  webrtc::VideoDecoder::Settings settings;
75  EXPECT_FALSE(decoder->Configure(settings));
76}
77
78- (void)testDecodeReturnsOKOnSuccess {
79  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
80
81  webrtc::EncodedImage encoded_image;
82  encoded_image.SetEncodedData(webrtc::EncodedImageBuffer::Create());
83
84  EXPECT_EQ(decoder->Decode(encoded_image, false, 0), WEBRTC_VIDEO_CODEC_OK);
85}
86
87- (void)testDecodeReturnsErrorOnFail {
88  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
89
90  webrtc::EncodedImage encoded_image;
91  encoded_image.SetEncodedData(webrtc::EncodedImageBuffer::Create());
92
93  EXPECT_EQ(decoder->Decode(encoded_image, false, 0), WEBRTC_VIDEO_CODEC_ERROR);
94}
95
96- (void)testReleaseDecodeReturnsOKOnSuccess {
97  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
98
99  EXPECT_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
100}
101
102- (void)testReleaseDecodeReturnsErrorOnFail {
103  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
104
105  EXPECT_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_ERROR);
106}
107@end
108