summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLVideoEncoderSpec.m
blob: c621f6a8483ee5c34c04b606cfab3130c7fff0d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
//  SDLVideoEncoderSpec.m
//  SmartDeviceLink-iOS
//
//  Created by Muller, Alexander (A.) on 2/7/17.
//  Copyright © 2017 smartdevicelink. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import "SDLVideoEncoder.h"
#import <AVFoundation/AVFoundation.h>

QuickSpecBegin(SDLVideoEncoderSpec)

describe(@"a video encoder", ^{
    __block SDLVideoEncoder *testVideoEncoder = nil;
    __block CGSize testSize = CGSizeMake(100, 200);
    __block id videoEncoderDelegateMock = OCMProtocolMock(@protocol(SDLVideoEncoderDelegate));
    __block NSError *testError = nil;
    
    context(@"if using default video encoder settings", ^{
        
        beforeEach(^{
            testVideoEncoder = [[SDLVideoEncoder alloc] initWithDimensions:testSize properties:SDLVideoEncoder.defaultVideoEncoderSettings delegate:videoEncoderDelegateMock error:&testError];
        });
        
        it(@"should initialize properties", ^{
            expect(testVideoEncoder).toNot(beNil());
            expect(testVideoEncoder.videoEncoderSettings).to(equal(SDLVideoEncoder.defaultVideoEncoderSettings));
            expect(@(testVideoEncoder.pixelBufferPool == NULL)).to(equal(@NO));
            expect(testError).to(beNil());
            
            NSDictionary *pixelBufferProperties = (__bridge NSDictionary*)CVPixelBufferPoolGetPixelBufferAttributes(testVideoEncoder.pixelBufferPool);
            expect(pixelBufferProperties[(__bridge NSString*)kCVPixelBufferWidthKey]).to(equal(@100));
            expect(pixelBufferProperties[(__bridge NSString*)kCVPixelBufferHeightKey]).to(equal(@200));
        });
        
        context(@"when stopping", ^{
            beforeEach(^{
                [testVideoEncoder stop];
            });
            
            it(@"should have a nil pixel buffer pool", ^{
                expect(@(testVideoEncoder.pixelBufferPool == NULL)).to(equal(@YES));
            });
        });
    });
    
    context(@"is using custom video encoder settings", ^{
        __block NSDictionary *testSettings = nil;
        
        context(@"that is a valid setting", ^{
            beforeEach(^{
                testSettings = @{
                                 (__bridge NSString *)kVTCompressionPropertyKey_ExpectedFrameRate : @1
                                 };
                testVideoEncoder = [[SDLVideoEncoder alloc] initWithDimensions:testSize properties:testSettings delegate:videoEncoderDelegateMock error:&testError];
            });
            
            it(@"should initialize properties", ^{
                expect(testVideoEncoder).toNot(beNil());
                expect(testVideoEncoder.videoEncoderSettings).to(equal(testSettings));
                expect(@(testVideoEncoder.pixelBufferPool == NULL)).to(equal(@NO));
                expect(testError).to(beNil());
                
                NSDictionary *pixelBufferProperties = (__bridge NSDictionary*)CVPixelBufferPoolGetPixelBufferAttributes(testVideoEncoder.pixelBufferPool);
                expect(pixelBufferProperties[(__bridge NSString*)kCVPixelBufferWidthKey]).to(equal(@100));
                expect(pixelBufferProperties[(__bridge NSString*)kCVPixelBufferHeightKey]).to(equal(@200));
            });
        });
        
        context(@"that is not a valid setting", ^{
            beforeEach(^{
                testSettings = @{
                                 @"Bad" : @"Property"
                                 };
                testVideoEncoder = [[SDLVideoEncoder alloc] initWithDimensions:testSize properties:testSettings delegate:videoEncoderDelegateMock error:&testError];
            });
            
            it(@"should not be initialized", ^{
                expect(testVideoEncoder).to(beNil());
                expect(testVideoEncoder.videoEncoderSettings).to(beNil());
                expect(@(testVideoEncoder.pixelBufferPool == NULL)).to(equal(@YES));
                expect(testError).to(equal([NSError errorWithDomain:SDLErrorDomainVideoEncoder code:SDLVideoEncoderErrorConfigurationCompressionSessionSetPropertyFailure userInfo:@{ NSLocalizedDescriptionKey : @"\"Bad\" is not a supported key." }]));
            });
        });
    });
});

QuickSpecEnd