kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 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 | |
Anders Carlsson | 7bca8ca | 2018-08-30 07:30:29 | [diff] [blame] | 14 | #include "sdk/objc/native/src/objc_video_encoder_factory.h" |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 15 | |
Anders Carlsson | 7bca8ca | 2018-08-30 07:30:29 | [diff] [blame] | 16 | #import "base/RTCVideoEncoder.h" |
| 17 | #import "base/RTCVideoEncoderFactory.h" |
| 18 | #import "base/RTCVideoFrameBuffer.h" |
| 19 | #import "components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 20 | |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 21 | #include "api/video_codecs/sdp_video_format.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 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" |
Anders Carlsson | 7bca8ca | 2018-08-30 07:30:29 | [diff] [blame] | 26 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 27 | |
| 28 | id<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); |
magjed | 73c0eb5 | 2017-08-07 13:55:28 | [diff] [blame] | 35 | OCMStub([encoderMock setBitrate:0 framerate:0]).andReturn(return_code); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 36 | |
| 37 | id encoderFactoryMock = OCMProtocolMock(@protocol(RTCVideoEncoderFactory)); |
andersc | 81bc523 | 2017-08-18 13:34:09 | [diff] [blame] | 38 | RTCVideoCodecInfo *supported = [[RTCVideoCodecInfo alloc] initWithName:@"H264" parameters:nil]; |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 39 | OCMStub([encoderFactoryMock supportedCodecs]).andReturn(@[ supported ]); |
| 40 | OCMStub([encoderFactoryMock createEncoder:[OCMArg any]]).andReturn(encoderMock); |
| 41 | return encoderFactoryMock; |
| 42 | } |
| 43 | |
| 44 | id<RTCVideoEncoderFactory> CreateOKEncoderFactory() { |
| 45 | return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK); |
| 46 | } |
| 47 | |
| 48 | id<RTCVideoEncoderFactory> CreateErrorEncoderFactory() { |
| 49 | return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR); |
| 50 | } |
| 51 | |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 52 | std::unique_ptr<webrtc::VideoEncoder> GetObjCEncoder(id<RTCVideoEncoderFactory> factory) { |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 53 | webrtc::ObjCVideoEncoderFactory encoder_factory(factory); |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 54 | webrtc::SdpVideoFormat format("H264"); |
| 55 | return encoder_factory.CreateVideoEncoder(format); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | #pragma mark - |
| 59 | |
| 60 | TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsOKOnSuccess) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 61 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 62 | |
Mirko Bonadei | 17aff35 | 2018-07-26 10:20:40 | [diff] [blame] | 63 | auto* settings = new webrtc::VideoCodec(); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 64 | EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_OK); |
| 65 | } |
| 66 | |
| 67 | TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsErrorOnFail) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 68 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 69 | |
Mirko Bonadei | 17aff35 | 2018-07-26 10:20:40 | [diff] [blame] | 70 | auto* settings = new webrtc::VideoCodec(); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 71 | EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_ERROR); |
| 72 | } |
| 73 | |
| 74 | TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsOKOnSuccess) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 75 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 76 | |
| 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 Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 82 | 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öller | 87e2d78 | 2019-03-07 09:18:23 | [diff] [blame^] | 87 | std::vector<webrtc::VideoFrameType> frame_types; |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 88 | |
Niels Möller | c8d2e73 | 2019-03-06 11:00:33 | [diff] [blame] | 89 | EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_OK); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsErrorOnFail) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 93 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 94 | |
| 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 Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 100 | 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öller | 87e2d78 | 2019-03-07 09:18:23 | [diff] [blame^] | 105 | std::vector<webrtc::VideoFrameType> frame_types; |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 106 | |
Niels Möller | c8d2e73 | 2019-03-06 11:00:33 | [diff] [blame] | 107 | EXPECT_EQ(encoder->Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_ERROR); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsOKOnSuccess) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 111 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 112 | |
| 113 | EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 114 | } |
| 115 | |
| 116 | TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsErrorOnFail) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 117 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 118 | |
| 119 | EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_ERROR); |
| 120 | } |
| 121 | |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 122 | TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsOKOnSuccess) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 123 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 124 | |
| 125 | EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_OK); |
| 126 | } |
| 127 | |
| 128 | TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsErrorOnFail) { |
Magnus Jedvert | 8b4e92d | 2018-04-13 13:36:43 | [diff] [blame] | 129 | std::unique_ptr<webrtc::VideoEncoder> encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
kthelgason | fb14312 | 2017-07-25 14:55:58 | [diff] [blame] | 130 | |
| 131 | EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_ERROR); |
| 132 | } |