OLD | NEW |
(Empty) | |
| 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 "webrtc/sdk/objc/Framework/Classes/VideoToolbox/objc_video_encoder_fact
ory.h" |
| 15 |
| 16 #import "WebRTC/RTCVideoCodec.h" |
| 17 #import "WebRTC/RTCVideoCodecFactory.h" |
| 18 #import "WebRTC/RTCVideoFrameBuffer.h" |
| 19 #include "webrtc/modules/include/module_common_types.h" |
| 20 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 21 #include "webrtc/modules/video_coding/include/video_error_codes.h" |
| 22 #include "webrtc/rtc_base/gunit.h" |
| 23 #include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h" |
| 24 |
| 25 id<RTCVideoEncoderFactory> CreateEncoderFactoryReturning(int return_code) { |
| 26 id encoderMock = OCMProtocolMock(@protocol(RTCVideoEncoder)); |
| 27 OCMStub([encoderMock startEncodeWithSettings:[OCMArg any] numberOfCores:1]) |
| 28 .andReturn(return_code); |
| 29 OCMStub([encoderMock encode:[OCMArg any] codecSpecificInfo:[OCMArg any] frameT
ypes:[OCMArg any]]) |
| 30 .andReturn(return_code); |
| 31 OCMStub([encoderMock releaseEncoder]).andReturn(return_code); |
| 32 OCMStub([encoderMock setBitrate:0 framerate:0]).andReturn(return_code == WEBRT
C_VIDEO_CODEC_OK); |
| 33 |
| 34 id encoderFactoryMock = OCMProtocolMock(@protocol(RTCVideoEncoderFactory)); |
| 35 RTCVideoCodecInfo *supported = |
| 36 [[RTCVideoCodecInfo alloc] initWithPayload:0 name:@"H264" parameters:@{}]; |
| 37 OCMStub([encoderFactoryMock supportedCodecs]).andReturn(@[ supported ]); |
| 38 OCMStub([encoderFactoryMock createEncoder:[OCMArg any]]).andReturn(encoderMock
); |
| 39 return encoderFactoryMock; |
| 40 } |
| 41 |
| 42 id<RTCVideoEncoderFactory> CreateOKEncoderFactory() { |
| 43 return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_OK); |
| 44 } |
| 45 |
| 46 id<RTCVideoEncoderFactory> CreateErrorEncoderFactory() { |
| 47 return CreateEncoderFactoryReturning(WEBRTC_VIDEO_CODEC_ERROR); |
| 48 } |
| 49 |
| 50 webrtc::VideoEncoder *GetObjCEncoder(id<RTCVideoEncoderFactory> factory) { |
| 51 webrtc::ObjCVideoEncoderFactory encoder_factory(factory); |
| 52 cricket::VideoCodec codec("H264"); |
| 53 return encoder_factory.CreateVideoEncoder(codec); |
| 54 } |
| 55 |
| 56 #pragma mark - |
| 57 |
| 58 TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsOKOnSuccess) { |
| 59 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
| 60 |
| 61 auto settings = new webrtc::VideoCodec(); |
| 62 EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_OK); |
| 63 } |
| 64 |
| 65 TEST(ObjCVideoEncoderFactoryTest, InitEncodeReturnsErrorOnFail) { |
| 66 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
| 67 |
| 68 auto settings = new webrtc::VideoCodec(); |
| 69 EXPECT_EQ(encoder->InitEncode(settings, 1, 0), WEBRTC_VIDEO_CODEC_ERROR); |
| 70 } |
| 71 |
| 72 TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsOKOnSuccess) { |
| 73 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
| 74 |
| 75 CVPixelBufferRef pixel_buffer; |
| 76 CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB,
nil, &pixel_buffer); |
| 77 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 78 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>( |
| 79 [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixel_buffer]); |
| 80 webrtc::VideoFrame frame(buffer, webrtc::kVideoRotation_0, 0); |
| 81 webrtc::CodecSpecificInfo info; |
| 82 info.codecType = webrtc::kVideoCodecH264; |
| 83 info.codec_name = "H264"; |
| 84 std::vector<webrtc::FrameType> frame_types; |
| 85 |
| 86 EXPECT_EQ(encoder->Encode(frame, &info, &frame_types), WEBRTC_VIDEO_CODEC_OK); |
| 87 } |
| 88 |
| 89 TEST(ObjCVideoEncoderFactoryTest, EncodeReturnsErrorOnFail) { |
| 90 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
| 91 |
| 92 CVPixelBufferRef pixel_buffer; |
| 93 CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_32ARGB,
nil, &pixel_buffer); |
| 94 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 95 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>( |
| 96 [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixel_buffer]); |
| 97 webrtc::VideoFrame frame(buffer, webrtc::kVideoRotation_0, 0); |
| 98 webrtc::CodecSpecificInfo info; |
| 99 info.codecType = webrtc::kVideoCodecH264; |
| 100 info.codec_name = "H264"; |
| 101 std::vector<webrtc::FrameType> frame_types; |
| 102 |
| 103 EXPECT_EQ(encoder->Encode(frame, &info, &frame_types), WEBRTC_VIDEO_CODEC_ERRO
R); |
| 104 } |
| 105 |
| 106 TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsOKOnSuccess) { |
| 107 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
| 108 |
| 109 EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 110 } |
| 111 |
| 112 TEST(ObjCVideoEncoderFactoryTest, ReleaseEncodeReturnsErrorOnFail) { |
| 113 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
| 114 |
| 115 EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_ERROR); |
| 116 } |
| 117 |
| 118 TEST(ObjCVideoEncoderFactoryTest, SetChannelParametersAlwaysReturnsOK) { |
| 119 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
| 120 |
| 121 EXPECT_EQ(encoder->SetChannelParameters(1, 1), WEBRTC_VIDEO_CODEC_OK); |
| 122 } |
| 123 |
| 124 TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsOKOnSuccess) { |
| 125 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateOKEncoderFactory()); |
| 126 |
| 127 EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_OK); |
| 128 } |
| 129 |
| 130 TEST(ObjCVideoEncoderFactoryTest, SetRatesReturnsErrorOnFail) { |
| 131 webrtc::VideoEncoder *encoder = GetObjCEncoder(CreateErrorEncoderFactory()); |
| 132 |
| 133 EXPECT_EQ(encoder->SetRates(0, 0), WEBRTC_VIDEO_CODEC_ERROR); |
| 134 } |
OLD | NEW |