• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_encoder_factory.h"
16
17#include "api/video_codecs/sdp_video_format.h"
18#include "api/video_codecs/video_encoder.h"
19#import "base/RTCVideoEncoder.h"
20#import "base/RTCVideoEncoderFactory.h"
21#import "base/RTCVideoFrameBuffer.h"
22#import "components/video_frame_buffer/RTCCVPixelBuffer.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"
26#include "sdk/objc/native/src/objc_frame_buffer.h"
27
28id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)> CreateEncoderFactoryReturning(int return_code) {
29  id encoderMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(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);
35  OCMStub([encoderMock setBitrate:0 framerate:0]).andReturn(return_code);
36
37  id encoderFactoryMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoEncoderFactory)));
38  RTC_OBJC_TYPE(RTCVideoCodecInfo)* supported =
39      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:@"H264" parameters:nil];
40  OCMStub([encoderFactoryMock supportedCodecs]).andReturn(@[ supported ]);
41  OCMStub([encoderFactoryMock implementations]).andReturn(@[ supported ]);
42  OCMStub([encoderFactoryMock createEncoder:[OCMArg any]]).andReturn(encoderMock);
43  return encoderFactoryMock;
44}
45
46id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)> CreateOKEncoderFactory() {
47  return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK);
48}
49
50id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)> CreateErrorEncoderFactory() {
51  return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR);
52}
53
54std::unique_ptr<webrtc::VideoEncoder> GetObjCEncoder(
55    id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)> factory) {
56  webrtc::ObjCVideoEncoderFactory encoder_factory(factory);
57  webrtc::SdpVideoFormat format("H264");
58  return encoder_factory.CreateVideoEncoder(format);
59}
60
61#pragma mark -
62
63@interface ObjCVideoEncoderFactoryTests : XCTestCase
64@end
65
66@implementation ObjCVideoEncoderFactoryTests
67
68- (void)testInitEncodeReturnsOKOnSuccess {
69  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
70
71  auto* settings = new webrtc::VideoCodec();
72  const webrtc::VideoEncoder::Capabilities kCapabilities(false);
73  EXPECT_EQ(encoder->InitEncode(settings, webrtc::VideoEncoder::Settings(kCapabilities, 1, 0)),
74            WEBRTC_VIDEO_CODEC_OK);
75}
76
77- (void)testInitEncodeReturnsErrorOnFail {
78  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
79
80  auto* settings = new webrtc::VideoCodec();
81  const webrtc::VideoEncoder::Capabilities kCapabilities(false);
82  EXPECT_EQ(encoder->InitEncode(settings, webrtc::VideoEncoder::Settings(kCapabilities, 1, 0)),
83            WEBRTC_VIDEO_CODEC_ERROR);
84}
85
86- (void)testEncodeReturnsOKOnSuccess {
87  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
88
89  CVPixelBufferRef pixel_buffer;
90  CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
91  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
92      rtc::make_ref_counted<webrtc::ObjCFrameBuffer>(
93          [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixel_buffer]);
94  webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
95                                 .set_video_frame_buffer(buffer)
96                                 .set_rotation(webrtc::kVideoRotation_0)
97                                 .set_timestamp_us(0)
98                                 .build();
99  std::vector<webrtc::VideoFrameType> frame_types;
100
101  EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_OK);
102}
103
104- (void)testEncodeReturnsErrorOnFail {
105  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
106
107  CVPixelBufferRef pixel_buffer;
108  CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
109  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
110      rtc::make_ref_counted<webrtc::ObjCFrameBuffer>(
111          [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixel_buffer]);
112  webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
113                                 .set_video_frame_buffer(buffer)
114                                 .set_rotation(webrtc::kVideoRotation_0)
115                                 .set_timestamp_us(0)
116                                 .build();
117  std::vector<webrtc::VideoFrameType> frame_types;
118
119  EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_ERROR);
120}
121
122- (void)testReleaseEncodeReturnsOKOnSuccess {
123  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
124
125  EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK);
126}
127
128- (void)testReleaseEncodeReturnsErrorOnFail {
129  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
130
131  EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_ERROR);
132}
133
134- (void)testGetSupportedFormats {
135  webrtc::ObjCVideoEncoderFactory encoder_factory(CreateOKEncoderFactory());
136  std::vector<webrtc::SdpVideoFormat> supportedFormats = encoder_factory.GetSupportedFormats();
137  EXPECT_EQ(supportedFormats.size(), 1u);
138  EXPECT_EQ(supportedFormats[0].name, "H264");
139}
140
141- (void)testGetImplementations {
142  webrtc::ObjCVideoEncoderFactory encoder_factory(CreateOKEncoderFactory());
143  std::vector<webrtc::SdpVideoFormat> supportedFormats = encoder_factory.GetImplementations();
144  EXPECT_EQ(supportedFormats.size(), 1u);
145  EXPECT_EQ(supportedFormats[0].name, "H264");
146}
147
148@end
149