blob: cef7495bb6aa59311009a9a89976115329b5db33 [file] [log] [blame]
kthelgasonfb143122017-07-25 14:55:581/*
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
Anders Carlsson7bca8ca2018-08-30 07:30:2914#include "sdk/objc/native/src/objc_video_encoder_factory.h"
kthelgasonfb143122017-07-25 14:55:5815
Anders Carlsson7bca8ca2018-08-30 07:30:2916#import "base/RTCVideoEncoder.h"
17#import "base/RTCVideoEncoderFactory.h"
18#import "base/RTCVideoFrameBuffer.h"
19#import "components/video_frame_buffer/RTCCVPixelBuffer.h"
20
Magnus Jedvert8b4e92d2018-04-13 13:36:4321#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "modules/include/module_common_types.h"
23#include "modules/video_coding/include/video_codec_interface.h"
24#include "modules/video_coding/include/video_error_codes.h"
25#include "rtc_base/gunit.h"
Anders Carlsson7bca8ca2018-08-30 07:30:2926#include "sdk/objc/native/src/objc_frame_buffer.h"
kthelgasonfb143122017-07-25 14:55:5827
28id<RTCVideoEncoderFactory> CreateEncoderFactoryReturning(int return_code) {
29 id encoderMock = OCMProtocolMock(@protocol(RTCVideoEncoder));
30 OCMStub([encoderMock startEncodeWithSettings:[OCMArg any] numberOfCores:1])
31 .andReturn(return_code);
32 OCMStub([encoderMock encode:[OCMArg any] codecSpecificInfo:[OCMArg any] frameTypes:[OCMArg any]])
33 .andReturn(return_code);
34 OCMStub([encoderMock releaseEncoder]).andReturn(return_code);
magjed73c0eb52017-08-07 13:55:2835 OCMStub([encoderMock setBitrate:0 framerate:0]).andReturn(return_code);
kthelgasonfb143122017-07-25 14:55:5836
37 id encoderFactoryMock = OCMProtocolMock(@protocol(RTCVideoEncoderFactory));
andersc81bc5232017-08-18 13:34:0938 RTCVideoCodecInfo *supported = [[RTCVideoCodecInfo alloc] initWithName:@"H264" parameters:nil];
kthelgasonfb143122017-07-25 14:55:5839 OCMStub([encoderFactoryMock supportedCodecs]).andReturn(@[ supported ]);
40 OCMStub([encoderFactoryMock createEncoder:[OCMArg any]]).andReturn(encoderMock);
41 return encoderFactoryMock;
42}
43
44id<RTCVideoEncoderFactory> CreateOKEncoderFactory() {
45 return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK);
46}
47
48id<RTCVideoEncoderFactory> CreateErrorEncoderFactory() {
49 return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR);
50}
51
Magnus Jedvert8b4e92d2018-04-13 13:36:4352std::unique_ptr<webrtc::VideoEncoder> GetObjCEncoder(id<RTCVideoEncoderFactory> factory) {
kthelgasonfb143122017-07-25 14:55:5853 webrtc::ObjCVideoEncoderFactory encoder_factory(factory);
Magnus Jedvert8b4e92d2018-04-13 13:36:4354 webrtc::SdpVideoFormat format("H264");
55 return encoder_factory.CreateVideoEncoder(format);
kthelgasonfb143122017-07-25 14:55:5856}
57
58#pragma mark -
59
60TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsOKOnSuccess) {
Magnus Jedvert8b4e92d2018-04-13 13:36:4361 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
kthelgasonfb143122017-07-25 14:55:5862
Mirko Bonadei17aff352018-07-26 10:20:4063 auto* settings = new webrtc::VideoCodec();
kthelgasonfb143122017-07-25 14:55:5864 EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_OK);
65}
66
67TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsErrorOnFail) {
Magnus Jedvert8b4e92d2018-04-13 13:36:4368 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
kthelgasonfb143122017-07-25 14:55:5869
Mirko Bonadei17aff352018-07-26 10:20:4070 auto* settings = new webrtc::VideoCodec();
kthelgasonfb143122017-07-25 14:55:5871 EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_ERROR);
72}
73
74TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsOKOnSuccess) {
Magnus Jedvert8b4e92d2018-04-13 13:36:4375 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
kthelgasonfb143122017-07-25 14:55:5876
77 CVPixelBufferRef pixel_buffer;
78 CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
79 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
80 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(
81 [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixel_buffer]);
Artem Titov1ebfb6a2019-01-03 22:49:3782 webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
83 .set_video_frame_buffer(buffer)
84 .set_rotation(webrtc::kVideoRotation_0)
85 .set_timestamp_us(0)
86 .build();
Niels Möller87e2d782019-03-07 09:18:2387 std::vector<webrtc::VideoFrameType> frame_types;
kthelgasonfb143122017-07-25 14:55:5888
Niels Möllerc8d2e732019-03-06 11:00:3389 EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_OK);
kthelgasonfb143122017-07-25 14:55:5890}
91
92TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsErrorOnFail) {
Magnus Jedvert8b4e92d2018-04-13 13:36:4393 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
kthelgasonfb143122017-07-25 14:55:5894
95 CVPixelBufferRef pixel_buffer;
96 CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
97 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
98 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(
99 [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixel_buffer]);
Artem Titov1ebfb6a2019-01-03 22:49:37100 webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
101 .set_video_frame_buffer(buffer)
102 .set_rotation(webrtc::kVideoRotation_0)
103 .set_timestamp_us(0)
104 .build();
Niels Möller87e2d782019-03-07 09:18:23105 std::vector<webrtc::VideoFrameType> frame_types;
kthelgasonfb143122017-07-25 14:55:58106
Niels Möllerc8d2e732019-03-06 11:00:33107 EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_ERROR);
kthelgasonfb143122017-07-25 14:55:58108}
109
110TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsOKOnSuccess) {
Magnus Jedvert8b4e92d2018-04-13 13:36:43111 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
kthelgasonfb143122017-07-25 14:55:58112
113 EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK);
114}
115
116TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsErrorOnFail) {
Magnus Jedvert8b4e92d2018-04-13 13:36:43117 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
kthelgasonfb143122017-07-25 14:55:58118
119 EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_ERROR);
120}
121
kthelgasonfb143122017-07-25 14:55:58122TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsOKOnSuccess) {
Magnus Jedvert8b4e92d2018-04-13 13:36:43123 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
kthelgasonfb143122017-07-25 14:55:58124
125 EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_OK);
126}
127
128TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsErrorOnFail) {
Magnus Jedvert8b4e92d2018-04-13 13:36:43129 std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
kthelgasonfb143122017-07-25 14:55:58130
131 EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_ERROR);
132}