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
14#include "sdk/objc/native/src/objc_video_decoder_factory.h"
15
16#import "base/RTCMacros.h"
17#import "base/RTCVideoDecoder.h"
18#import "base/RTCVideoDecoderFactory.h"
19#include "media/base/codec.h"
20#include "modules/video_coding/include/video_codec_interface.h"
21#include "modules/video_coding/include/video_error_codes.h"
22#include "rtc_base/gunit.h"
23
24id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateDecoderFactoryReturning(int return_code) {
25  id decoderMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoder)));
26  OCMStub([decoderMock startDecodeWithNumberOfCores:1]).andReturn(return_code);
27  OCMStub([decoderMock decode:[OCMArg any]
28                    missingFrames:NO
29                codecSpecificInfo:[OCMArg any]
30                     renderTimeMs:0])
31      .andReturn(return_code);
32  OCMStub([decoderMock releaseDecoder]).andReturn(return_code);
33
34  id decoderFactoryMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoderFactory)));
35  RTC_OBJC_TYPE(RTCVideoCodecInfo)* supported =
36      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264" parameters:nil];
37  OCMStub([decoderFactoryMock supportedCodecs]).andReturn(@[ supported ]);
38  OCMStub([decoderFactoryMock createDecoder:[OCMArg any]]).andReturn(decoderMock);
39  return decoderFactoryMock;
40}
41
42id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateOKDecoderFactory() {
43  return CreateDecoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK);
44}
45
46id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> CreateErrorDecoderFactory() {
47  return CreateDecoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR);
48}
49
50std::unique_ptr<webrtc::VideoDecoder> GetObjCDecoder(
51    id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> factory) {
52  webrtc::ObjCVideoDecoderFactory decoder_factory(factory);
53  return decoder_factory.CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName));
54}
55
56#pragma mark -
57
58TEST(ObjCVideoDecoderFactoryTest, InitDecodeReturnsOKOnSuccess) {
59  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
60
61  auto* settings = new webrtc::VideoCodec();
62  EXPECT_EQ(decoder->InitDecode(settings, 1), WEBRTC_VIDEO_CODEC_OK);
63}
64
65TEST(ObjCVideoDecoderFactoryTest, InitDecodeReturnsErrorOnFail) {
66  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
67
68  auto* settings = new webrtc::VideoCodec();
69  EXPECT_EQ(decoder->InitDecode(settings, 1), WEBRTC_VIDEO_CODEC_ERROR);
70}
71
72TEST(ObjCVideoDecoderFactoryTest, DecodeReturnsOKOnSuccess) {
73  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
74
75  webrtc::EncodedImage encoded_image;
76  encoded_image.SetEncodedData(webrtc::EncodedImageBuffer::Create());
77
78  EXPECT_EQ(decoder->Decode(encoded_image, false, 0), WEBRTC_VIDEO_CODEC_OK);
79}
80
81TEST(ObjCVideoDecoderFactoryTest, DecodeReturnsErrorOnFail) {
82  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
83
84  webrtc::EncodedImage encoded_image;
85  encoded_image.SetEncodedData(webrtc::EncodedImageBuffer::Create());
86
87  EXPECT_EQ(decoder->Decode(encoded_image, false, 0), WEBRTC_VIDEO_CODEC_ERROR);
88}
89
90TEST(ObjCVideoDecoderFactoryTest, ReleaseDecodeReturnsOKOnSuccess) {
91  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateOKDecoderFactory());
92
93  EXPECT_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
94}
95
96TEST(ObjCVideoDecoderFactoryTest, ReleaseDecodeReturnsErrorOnFail) {
97  std::unique_ptr<webrtc::VideoDecoder> decoder = GetObjCDecoder(CreateErrorDecoderFactory());
98
99  EXPECT_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_ERROR);
100}
101