• 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
14#include "sdk/objc/native/src/objc_video_encoder_factory.h"
15
16#include "api/video_codecs/sdp_video_format.h"
17#include "api/video_codecs/video_encoder.h"
18#import "base/RTCVideoEncoder.h"
19#import "base/RTCVideoEncoderFactory.h"
20#import "base/RTCVideoFrameBuffer.h"
21#import "components/video_frame_buffer/RTCCVPixelBuffer.h"
22#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"
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
63TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsOKOnSuccess) {
64  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
65
66  auto* settings = new webrtc::VideoCodec();
67  const webrtc::VideoEncoder::Capabilities kCapabilities(false);
68  EXPECT_EQ(encoder->InitEncode(settings, webrtc::VideoEncoder::Settings(kCapabilities, 1, 0)),
69            WEBRTC_VIDEO_CODEC_OK);
70}
71
72TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsErrorOnFail) {
73  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
74
75  auto* settings = new webrtc::VideoCodec();
76  const webrtc::VideoEncoder::Capabilities kCapabilities(false);
77  EXPECT_EQ(encoder->InitEncode(settings, webrtc::VideoEncoder::Settings(kCapabilities, 1, 0)),
78            WEBRTC_VIDEO_CODEC_ERROR);
79}
80
81TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsOKOnSuccess) {
82  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
83
84  CVPixelBufferRef pixel_buffer;
85  CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
86  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
87      new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(
88          [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixel_buffer]);
89  webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
90                                 .set_video_frame_buffer(buffer)
91                                 .set_rotation(webrtc::kVideoRotation_0)
92                                 .set_timestamp_us(0)
93                                 .build();
94  std::vector<webrtc::VideoFrameType> frame_types;
95
96  EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_OK);
97}
98
99TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsErrorOnFail) {
100  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
101
102  CVPixelBufferRef pixel_buffer;
103  CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB, nil, &pixel_buffer);
104  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
105      new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(
106          [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixel_buffer]);
107  webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
108                                 .set_video_frame_buffer(buffer)
109                                 .set_rotation(webrtc::kVideoRotation_0)
110                                 .set_timestamp_us(0)
111                                 .build();
112  std::vector<webrtc::VideoFrameType> frame_types;
113
114  EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_ERROR);
115}
116
117TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsOKOnSuccess) {
118  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory());
119
120  EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK);
121}
122
123TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsErrorOnFail) {
124  std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory());
125
126  EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_ERROR);
127}
128
129TEST(ObjCVideoEncoderFactoryTest, GetSupportedFormats) {
130  webrtc::ObjCVideoEncoderFactory encoder_factory(CreateOKEncoderFactory());
131  std::vector<webrtc::SdpVideoFormat> supportedFormats = encoder_factory.GetSupportedFormats();
132  EXPECT_EQ(supportedFormats.size(), 1u);
133  EXPECT_EQ(supportedFormats[0].name, "H264");
134}
135
136TEST(ObjCVideoEncoderFactoryTest, GetImplementations) {
137  webrtc::ObjCVideoEncoderFactory encoder_factory(CreateOKEncoderFactory());
138  std::vector<webrtc::SdpVideoFormat> supportedFormats = encoder_factory.GetImplementations();
139  EXPECT_EQ(supportedFormats.size(), 1u);
140  EXPECT_EQ(supportedFormats[0].name, "H264");
141}
142