summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLControlFramePayloadVideoStartService.m
blob: d8dc841befcf6edb75f47bd71dd0a846bf63c245 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
//  SDLControlFramePayloadVideoStartService.m
//  SmartDeviceLink-iOS
//
//  Created by Joel Fischer on 7/24/17.
//  Copyright © 2017 smartdevicelink. All rights reserved.
//

#import "SDLControlFramePayloadVideoStartService.h"

#import "bson_object.h"
#import "SDLControlFramePayloadConstants.h"


NS_ASSUME_NONNULL_BEGIN

@interface SDLControlFramePayloadVideoStartService ()

@property (assign, nonatomic, readwrite) int32_t height;
@property (assign, nonatomic, readwrite) int32_t width;
@property (copy, nonatomic, readwrite, nullable) SDLVideoStreamingCodec videoCodec;
@property (copy, nonatomic, readwrite, nullable) SDLVideoStreamingProtocol videoProtocol;

@end

@implementation SDLControlFramePayloadVideoStartService

- (instancetype)initWithVideoHeight:(int32_t)height width:(int32_t)width protocol:(nullable SDLVideoStreamingProtocol)protocol codec:(nullable SDLVideoStreamingCodec)codec {
    self = [super init];
    if (!self) return nil;

    _height = height;
    _width = width;
    _videoProtocol = protocol;
    _videoCodec = codec;

    return self;
}

- (instancetype)initWithData:(nullable NSData *)data {
    self = [super init];
    if (!self) return nil;

    _height = SDLControlFrameInt32NotFound;
    _width = SDLControlFrameInt32NotFound;

    if (data.length > 0) {
        [self sdl_parse:data];
    }

    return self;
}

- (nullable NSData *)data {
    if (self.height == SDLControlFrameInt32NotFound
        && self.width == SDLControlFrameInt32NotFound
        && self.videoProtocol == nil
        && self.videoCodec == nil) {
        return nil;
    }

    BsonObject payloadObject;
    bson_object_initialize_default(&payloadObject);

    if (self.height != SDLControlFrameInt32NotFound) {
        bson_object_put_int32(&payloadObject, SDLControlFrameHeightKey, self.height);
    }

    if (self.width != SDLControlFrameInt32NotFound) {
        bson_object_put_int32(&payloadObject, SDLControlFrameWidthKey, self.width);
    }

    if (self.videoProtocol != nil) {
        bson_object_put_string(&payloadObject, SDLControlFrameVideoProtocolKey, (char *)self.videoProtocol.UTF8String);
    }

    if (self.videoCodec != nil) {
        bson_object_put_string(&payloadObject, SDLControlFrameVideoCodecKey, (char *)self.videoCodec.UTF8String);
    }

    BytePtr bsonData = bson_object_to_bytes(&payloadObject);
    NSUInteger length = bson_object_size(&payloadObject);

    bson_object_deinitialize(&payloadObject);

    return [[NSData alloc] initWithBytes:bsonData length:length];
}

- (void)sdl_parse:(NSData *)data {
    BsonObject payloadObject;
    size_t retval = bson_object_from_bytes_len(&payloadObject, (BytePtr)data.bytes, data.length);
    if (retval <= 0) {
        return;
    }

    self.height = bson_object_get_int32(&payloadObject, SDLControlFrameHeightKey);
    self.width = bson_object_get_int32(&payloadObject, SDLControlFrameWidthKey);

    char *utf8String = bson_object_get_string(&payloadObject, SDLControlFrameVideoProtocolKey);
    if (utf8String != NULL) {
        self.videoProtocol = [NSString stringWithUTF8String:utf8String];
    }

    utf8String = bson_object_get_string(&payloadObject, SDLControlFrameVideoCodecKey);
    if (utf8String != NULL) {
        self.videoCodec = [NSString stringWithUTF8String:utf8String];
    }

    bson_object_deinitialize(&payloadObject);
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@>: Width: %d, Height: %d, Protocol: %@, Codec: %@", NSStringFromClass(self.class), self.width, self.height, self.videoProtocol, self.videoCodec];
}

@end

NS_ASSUME_NONNULL_END