summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLStreamingMediaLifecycleManagerSpec.m
blob: 93ec5eca717af08cba155d82f95f7044d4fb95db (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//
//  SDLStreamingMediaLifecycleManagerSpec.m
//  SmartDeviceLink-iOS
//

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

#import "SDLConnectionManagerType.h"
#import "SDLControlFramePayloadAudioStartServiceAck.h"
#import "SDLControlFramePayloadConstants.h"
#import "SDLControlFramePayloadNak.h"
#import "SDLControlFramePayloadVideoStartServiceAck.h"
#import "SDLDisplayCapabilities.h"
#import "SDLGenericResponse.h"
#import "SDLGetSystemCapability.h"
#import "SDLGetSystemCapabilityResponse.h"
#import "SDLGlobals.h"
#import "SDLHapticInterface.h"
#import "SDLHapticManager.h"
#import "SDLHMILevel.h"
#import "SDLImageResolution.h"
#import "SDLNotificationConstants.h"
#import "SDLOnHMIStatus.h"
#import "SDLProtocol.h"
#import "SDLRPCNotificationNotification.h"
#import "SDLRegisterAppInterfaceResponse.h"
#import "SDLRPCResponseNotification.h"
#import "SDLScreenParams.h"
#import "SDLStateMachine.h"
#import "SDLStreamingMediaConfiguration.h"
#import "SDLStreamingMediaLifecycleManager.h"
#import "SDLFakeStreamingManagerDataSource.h"
#import "SDLSystemCapability.h"
#import "SDLV2ProtocolHeader.h"
#import "SDLV2ProtocolMessage.h"
#import "SDLVideoStreamingCapability.h"
#import "SDLVideoStreamingCodec.h"
#import "SDLVideoStreamingFormat.h"
#import "SDLVideoStreamingProtocol.h"
#import "TestConnectionManager.h"

QuickSpecBegin(SDLStreamingMediaLifecycleManagerSpec)

describe(@"the streaming media manager", ^{
    __block SDLStreamingMediaLifecycleManager *streamingLifecycleManager = nil;
    __block SDLStreamingMediaConfiguration *testConfiguration = [SDLStreamingMediaConfiguration insecureConfiguration];
    __block UIWindow *testWindow = [[UIWindow alloc] init];
    __block SDLFakeStreamingManagerDataSource *testDataSource = [[SDLFakeStreamingManagerDataSource alloc] init];
    __block NSString *someBackgroundTitleString = nil;
    __block TestConnectionManager *testConnectionManager = nil;
    
    __block void (^sendNotificationForHMILevel)(SDLHMILevel hmiLevel) = ^(SDLHMILevel hmiLevel) {
        SDLOnHMIStatus *hmiStatus = [[SDLOnHMIStatus alloc] init];
        hmiStatus.hmiLevel = hmiLevel;
        SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidChangeHMIStatusNotification object:self rpcNotification:hmiStatus];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
        [NSThread sleepForTimeInterval:0.3];
    };
    
    beforeEach(^{
        testConfiguration.customVideoEncoderSettings = @{
                                     (__bridge NSString *)kVTCompressionPropertyKey_ExpectedFrameRate : @1
                                     };
        testConfiguration.dataSource = testDataSource;
        testConfiguration.window = testWindow;
        someBackgroundTitleString = @"Open Test App";
        testConnectionManager = [[TestConnectionManager alloc] init];
        streamingLifecycleManager = [[SDLStreamingMediaLifecycleManager alloc] initWithConnectionManager:testConnectionManager configuration:testConfiguration];
    });
    
    it(@"should initialize properties", ^{
        expect(streamingLifecycleManager.touchManager).toNot(beNil());
        expect(streamingLifecycleManager.hapticInterface).toNot(beNil());
        expect(@(streamingLifecycleManager.isStreamingSupported)).to(equal(@NO));
        expect(@(streamingLifecycleManager.isVideoConnected)).to(equal(@NO));
        expect(@(streamingLifecycleManager.isAudioConnected)).to(equal(@NO));
        expect(@(streamingLifecycleManager.isVideoEncrypted)).to(equal(@NO));
        expect(@(streamingLifecycleManager.isAudioEncrypted)).to(equal(@NO));
        expect(@(streamingLifecycleManager.isVideoStreamingPaused)).to(equal(@YES));
        expect(@(CGSizeEqualToSize(streamingLifecycleManager.screenSize, CGSizeZero))).to(equal(@YES));
        expect(@(streamingLifecycleManager.pixelBufferPool == NULL)).to(equal(@YES));
        expect(@(streamingLifecycleManager.requestedEncryptionType)).to(equal(@(SDLStreamingEncryptionFlagNone)));
        expect(streamingLifecycleManager.currentAppState).to(equal(SDLAppStateActive));
        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStopped));
        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
        expect(streamingLifecycleManager.videoFormat).to(beNil());
        expect(streamingLifecycleManager.dataSource).to(equal(testDataSource));
        expect(streamingLifecycleManager.supportedFormats).to(haveCount(2));
        expect(streamingLifecycleManager.preferredFormats).to(beNil());
        expect(streamingLifecycleManager.preferredResolutions).to(beNil());
        expect(streamingLifecycleManager.preferredFormatIndex).to(equal(0));
        expect(streamingLifecycleManager.preferredResolutionIndex).to(equal(0));
    });
    
    describe(@"when started", ^{
        __block BOOL readyHandlerSuccess = NO;
        __block NSError *readyHandlerError = nil;
        
        __block id protocolMock = OCMClassMock([SDLAbstractProtocol class]);
        
        beforeEach(^{
            readyHandlerSuccess = NO;
            readyHandlerError = nil;
            
            [streamingLifecycleManager startWithProtocol:protocolMock];
        });
        
        it(@"should be ready to stream", ^{
            expect(@(streamingLifecycleManager.isStreamingSupported)).to(equal(@NO));
            expect(@(streamingLifecycleManager.isVideoConnected)).to(equal(@NO));
            expect(@(streamingLifecycleManager.isAudioConnected)).to(equal(@NO));
            expect(@(streamingLifecycleManager.isVideoEncrypted)).to(equal(@NO));
            expect(@(streamingLifecycleManager.isAudioEncrypted)).to(equal(@NO));
            expect(@(streamingLifecycleManager.isVideoStreamingPaused)).to(equal(@YES));
            expect(@(CGSizeEqualToSize(streamingLifecycleManager.screenSize, CGSizeZero))).to(equal(@YES));
            expect(@(streamingLifecycleManager.pixelBufferPool == NULL)).to(equal(@YES));
            expect(streamingLifecycleManager.currentAppState).to(equal(SDLAppStateActive));
            expect(streamingLifecycleManager.currentAudioStreamState).to(match(SDLAudioStreamStateStopped));
            expect(streamingLifecycleManager.currentVideoStreamState).to(match(SDLVideoStreamStateStopped));
        });

        it(@"should send out a video capabilities request", ^{
            expect(testConnectionManager.receivedRequests.lastObject).to(beAnInstanceOf([SDLGetSystemCapability class]));

            SDLGetSystemCapability *getCapability = (SDLGetSystemCapability *)testConnectionManager.receivedRequests.lastObject;
            expect(getCapability.systemCapabilityType).to(equal(SDLSystemCapabilityTypeVideoStreaming));
        });
        
        describe(@"after receiving a register app interface notification", ^{
            __block SDLRegisterAppInterfaceResponse *someRegisterAppInterfaceResponse = nil;
            __block SDLDisplayCapabilities *someDisplayCapabilities = nil;
            __block SDLScreenParams *someScreenParams = nil;
            __block SDLImageResolution *someImageResolution = nil;
            
            beforeEach(^{
                someImageResolution = [[SDLImageResolution alloc] init];
                someImageResolution.resolutionWidth = @(600);
                someImageResolution.resolutionHeight = @(100);
                
                someScreenParams = [[SDLScreenParams alloc] init];
                someScreenParams.resolution = someImageResolution;
            });
            
            context(@"that does not support graphics", ^{
                beforeEach(^{
                    someDisplayCapabilities = [[SDLDisplayCapabilities alloc] init];
                    someDisplayCapabilities.graphicSupported = @NO;
                    
                    someDisplayCapabilities.screenParams = someScreenParams;
                    
                    someRegisterAppInterfaceResponse = [[SDLRegisterAppInterfaceResponse alloc] init];
                    someRegisterAppInterfaceResponse.displayCapabilities = someDisplayCapabilities;
                    SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceiveRegisterAppInterfaceResponse object:self rpcResponse:someRegisterAppInterfaceResponse];
                    
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                    [NSThread sleepForTimeInterval:0.1];
                });
                
                it(@"should not support streaming", ^{
                    expect(@(streamingLifecycleManager.isStreamingSupported)).to(equal(@NO));
                });
            });
            
            context(@"that supports graphics", ^{
                beforeEach(^{
                    someDisplayCapabilities = [[SDLDisplayCapabilities alloc] init];
                    someDisplayCapabilities.graphicSupported = @YES;
                    
                    someDisplayCapabilities.screenParams = someScreenParams;
                    
                    someRegisterAppInterfaceResponse = [[SDLRegisterAppInterfaceResponse alloc] init];
                    someRegisterAppInterfaceResponse.displayCapabilities = someDisplayCapabilities;
                    SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceiveRegisterAppInterfaceResponse object:self rpcResponse:someRegisterAppInterfaceResponse];
                    
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                    [NSThread sleepForTimeInterval:0.1];
                });
                
                it(@"should support streaming", ^{
                    expect(@(streamingLifecycleManager.isStreamingSupported)).to(equal(@YES));
                    expect(@(CGSizeEqualToSize(streamingLifecycleManager.screenSize, CGSizeMake(600, 100)))).to(equal(@YES));
                });
            });
        });

        describe(@"after sending GetSystemCapabilities", ^{
            context(@"and receiving an error response", ^{
                // This happens if the HU doesn't understand GetSystemCapabilities
                beforeEach(^{
                    SDLGenericResponse *genericResponse = [[SDLGenericResponse alloc] init];
                    genericResponse.resultCode = SDLResultInvalidData;

                    [testConnectionManager respondToLastRequestWithResponse:genericResponse];
                });

                it(@"should have correct format and resolution", ^{
                    expect(streamingLifecycleManager.preferredFormats).to(haveCount(1));
                    expect(streamingLifecycleManager.preferredFormats.firstObject.codec).to(equal(SDLVideoStreamingCodecH264));
                    expect(streamingLifecycleManager.preferredFormats.firstObject.protocol).to(equal(SDLVideoStreamingProtocolRAW));

                    expect(streamingLifecycleManager.preferredResolutions).to(haveCount(1));
                    expect(streamingLifecycleManager.preferredResolutions.firstObject.resolutionWidth).to(equal(0));
                    expect(streamingLifecycleManager.preferredResolutions.firstObject.resolutionHeight).to(equal(0));
                });
            });

            context(@"and receiving a response", ^{
                __block SDLImageResolution *resolution = nil;
                __block int32_t maxBitrate = 0;
                __block NSArray<SDLVideoStreamingFormat *> *testFormats = nil;
                __block BOOL testHapticsSupported = NO;

                beforeEach(^{
                    SDLGetSystemCapabilityResponse *response = [[SDLGetSystemCapabilityResponse alloc] init];
                    response.success = @YES;
                    response.systemCapability = [[SDLSystemCapability alloc] init];
                    response.systemCapability.systemCapabilityType = SDLSystemCapabilityTypeVideoStreaming;

                    resolution = [[SDLImageResolution alloc] initWithWidth:42 height:69];
                    maxBitrate = 12345;
                    testFormats = @[[[SDLVideoStreamingFormat alloc] initWithCodec:SDLVideoStreamingCodecH265 protocol:SDLVideoStreamingProtocolRTMP], [[SDLVideoStreamingFormat alloc] initWithCodec:SDLVideoStreamingCodecH264 protocol:SDLVideoStreamingProtocolRTP]];
                    testHapticsSupported = YES;
                    response.systemCapability.videoStreamingCapability = [[SDLVideoStreamingCapability alloc] initWithPreferredResolution:resolution maxBitrate:maxBitrate supportedFormats:testFormats hapticDataSupported:testHapticsSupported];
                    [testConnectionManager respondToLastRequestWithResponse:response];
                });

                it(@"should have set correct data", ^{
                    // Correct formats should be retrieved from the data source
                    expect(streamingLifecycleManager.preferredResolutions).to(haveCount(1));
                    expect(streamingLifecycleManager.preferredResolutions.firstObject.resolutionWidth).to(equal(resolution.resolutionWidth));
                    expect(streamingLifecycleManager.preferredResolutions.firstObject.resolutionHeight).to(equal(resolution.resolutionHeight));

                    expect(streamingLifecycleManager.preferredFormats).to(haveCount(streamingLifecycleManager.supportedFormats.count + 1));
                    expect(streamingLifecycleManager.preferredFormats.firstObject.codec).to(equal(testDataSource.extraFormat.codec));
                    expect(streamingLifecycleManager.preferredFormats.firstObject.protocol).to(equal(testDataSource.extraFormat.protocol));

                    // The haptic manager should be enabled
                    expect(streamingLifecycleManager.hapticInterface.enableHapticDataRequests).to(equal(YES));
                });

                describe(@"if the app state is active", ^{
                    __block id streamStub = nil;

                    beforeEach(^{
                        streamStub = OCMPartialMock(streamingLifecycleManager);

                        OCMStub([streamStub isStreamingSupported]).andReturn(YES);

                        [streamingLifecycleManager.appStateMachine setToState:SDLAppStateActive fromOldState:nil callEnterTransition:NO];
                    });

                    describe(@"and both streams are open", ^{
                        beforeEach(^{
                            [streamingLifecycleManager.audioStreamStateMachine setToState:SDLAudioStreamStateReady fromOldState:nil callEnterTransition:NO];
                            [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateReady fromOldState:nil callEnterTransition:NO];
                        });

                        describe(@"and the hmi state is limited", ^{
                            beforeEach(^{
                                streamingLifecycleManager.hmiLevel = SDLHMILevelLimited;
                            });

                            describe(@"and the hmi state changes to", ^{
                                context(@"none", ^{
                                    beforeEach(^{
                                        sendNotificationForHMILevel(SDLHMILevelNone);
                                    });

                                    it(@"should close only the video stream", ^{
                                        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateShuttingDown));
                                    });
                                });

                                context(@"background", ^{
                                    beforeEach(^{
                                        sendNotificationForHMILevel(SDLHMILevelBackground);
                                    });

                                    it(@"should close only the video stream", ^{
                                        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateShuttingDown));
                                    });
                                });

                                context(@"limited", ^{
                                    beforeEach(^{
                                        sendNotificationForHMILevel(SDLHMILevelLimited);
                                    });

                                    it(@"should not close either stream", ^{
                                        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                                    });
                                });

                                context(@"full", ^{
                                    beforeEach(^{
                                        sendNotificationForHMILevel(SDLHMILevelFull);
                                    });

                                    it(@"should not close either stream", ^{
                                        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                                    });
                                });
                            });

                            describe(@"and the app state changes to", ^{
                                context(@"inactive", ^{
                                    beforeEach(^{
                                        [streamingLifecycleManager.appStateMachine setToState:SDLAppStateInactive fromOldState:nil callEnterTransition:YES];
                                    });

                                    it(@"should flag to restart the video stream", ^{
                                        expect(@(streamingLifecycleManager.shouldRestartVideoStream)).to(equal(@YES));
                                        expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                        expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                                    });
                                });
                            });
                        });

                        describe(@"and the hmi state is full", ^{
                            beforeEach(^{
                                streamingLifecycleManager.hmiLevel = SDLHMILevelFull;
                            });

                            context(@"and hmi state changes to none", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelNone);
                                });

                                it(@"should close only the video stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateShuttingDown));
                                });
                            });

                            context(@"and hmi state changes to background", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelBackground);
                                });

                                it(@"should close only the video stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateShuttingDown));
                                });
                            });

                            context(@"and hmi state changes to limited", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelLimited);
                                });

                                it(@"should not close either stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                                });
                            });

                            context(@"and hmi state changes to full", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelFull);
                                });

                                it(@"should not close either stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                                });
                            });
                        });
                    });

                    describe(@"and both streams are closed", ^{
                        beforeEach(^{
                            [streamingLifecycleManager.audioStreamStateMachine setToState:SDLAudioStreamStateStopped fromOldState:nil callEnterTransition:NO];
                            [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateStopped fromOldState:nil callEnterTransition:NO];
                        });

                        describe(@"and the hmi state is none", ^{
                            beforeEach(^{
                                streamingLifecycleManager.hmiLevel = SDLHMILevelNone;
                            });

                            context(@"and hmi state changes to none", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelNone);
                                });

                                it(@"should only start the audio stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStarting));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
                                });
                            });

                            context(@"and hmi state changes to background", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelBackground);
                                });

                                it(@"should only start the audio stream", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStarting));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
                                });
                            });
                            
                            context(@"and hmi state changes to limited", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelLimited);
                                });
                                
                                it(@"should start both streams", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStarting));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStarting));
                                });
                            });
                            
                            context(@"and hmi state changes to full", ^{
                                beforeEach(^{
                                    sendNotificationForHMILevel(SDLHMILevelFull);
                                });
                                
                                it(@"should start both streams", ^{
                                    expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStarting));
                                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStarting));
                                });

                                it(@"should have decided upon the correct preferred format and resolution", ^{
                                    SDLVideoStreamingFormat *preferredFormat = streamingLifecycleManager.preferredFormats[streamingLifecycleManager.preferredFormatIndex];
                                    expect(preferredFormat.codec).to(equal(SDLVideoStreamingCodecH264));
                                    expect(preferredFormat.protocol).to(equal(SDLVideoStreamingProtocolRTP));

                                    SDLImageResolution *preferredResolution = streamingLifecycleManager.preferredResolutions[streamingLifecycleManager.preferredResolutionIndex];
                                    expect(preferredResolution.resolutionHeight).to(equal(@69));
                                    expect(preferredResolution.resolutionWidth).to(equal(@42));
                                });
                            });
                        });
                    });
                });
            });
        });

        describe(@"after receiving a Video Start ACK", ^{
            __block SDLProtocolHeader *testVideoHeader = nil;
            __block SDLProtocolMessage *testVideoMessage = nil;
            __block SDLControlFramePayloadVideoStartServiceAck *testVideoStartServicePayload = nil;
            __block int64_t testMTU = 789456;
            __block int32_t testVideoHeight = 42;
            __block int32_t testVideoWidth = 32;
            __block SDLVideoStreamingCodec testVideoCodec = SDLVideoStreamingCodecH264;
            __block SDLVideoStreamingProtocol testVideoProtocol = SDLVideoStreamingProtocolRTP;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testVideoHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testVideoHeader.frameType = SDLFrameTypeSingle;
                testVideoHeader.frameData = SDLFrameInfoStartServiceACK;
                testVideoHeader.encrypted = YES;
                testVideoHeader.serviceType = SDLServiceTypeVideo;
            });

            context(@"with data", ^{
                beforeEach(^{
                    testVideoStartServicePayload = [[SDLControlFramePayloadVideoStartServiceAck alloc] initWithMTU:testMTU height:testVideoHeight width:testVideoWidth protocol:testVideoProtocol codec:testVideoCodec];
                    testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:testVideoStartServicePayload.data];
                    [streamingLifecycleManager handleProtocolStartServiceACKMessage:testVideoMessage];
                });

                it(@"should have set all the right properties", ^{
                    expect([[SDLGlobals sharedGlobals] mtuSizeForServiceType:SDLServiceTypeVideo]).to(equal(testMTU));
                    expect(CGSizeEqualToSize(streamingLifecycleManager.screenSize, CGSizeMake(testVideoWidth, testVideoHeight))).to(equal(YES));
                    expect(streamingLifecycleManager.videoEncrypted).to(equal(YES));
                    expect(streamingLifecycleManager.videoFormat).to(equal([[SDLVideoStreamingFormat alloc] initWithCodec:testVideoCodec protocol:testVideoProtocol]));
                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                });
            });

            context(@"with missing data", ^{
                beforeEach(^{
                    testVideoStartServicePayload = [[SDLControlFramePayloadVideoStartServiceAck alloc] initWithMTU:testMTU height:testVideoHeight width:testVideoWidth protocol:nil codec:nil];
                    testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:testVideoStartServicePayload.data];
                    [streamingLifecycleManager handleProtocolStartServiceACKMessage:testVideoMessage];
                });

                it(@"should fall back correctly", ^{
                    expect(CGSizeEqualToSize(streamingLifecycleManager.screenSize, CGSizeMake(testVideoWidth, testVideoHeight))).to(equal(YES));
                    expect(streamingLifecycleManager.videoFormat).to(equal([[SDLVideoStreamingFormat alloc] initWithCodec:SDLVideoStreamingCodecH264 protocol:SDLVideoStreamingProtocolRAW]));
                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateReady));
                });
            });
        });

        describe(@"after receiving a Video Start NAK", ^{
            __block SDLProtocolHeader *testVideoHeader = nil;
            __block SDLProtocolMessage *testVideoMessage = nil;
            __block SDLControlFramePayloadNak *testVideoStartNakPayload = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testVideoHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testVideoHeader.frameType = SDLFrameTypeSingle;
                testVideoHeader.frameData = SDLFrameInfoStartServiceACK;
                testVideoHeader.encrypted = YES;
                testVideoHeader.serviceType = SDLServiceTypeVideo;
            });

            context(@"with data", ^{
                beforeEach(^{
                    testVideoStartNakPayload = [[SDLControlFramePayloadNak alloc] initWithRejectedParams:@[[NSString stringWithUTF8String:SDLControlFrameHeightKey], [NSString stringWithUTF8String:SDLControlFrameVideoCodecKey]]];
                    testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:testVideoStartNakPayload.data];
                    [streamingLifecycleManager handleProtocolStartServiceNAKMessage:testVideoMessage];
                });

                it(@"should have retried with new properties", ^{
                    expect(streamingLifecycleManager.preferredResolutionIndex).to(equal(1));
                    expect(streamingLifecycleManager.preferredFormatIndex).to(equal(1));
                });
            });

            context(@"with missing data", ^{
                beforeEach(^{
                    testVideoStartNakPayload = [[SDLControlFramePayloadNak alloc] initWithRejectedParams:nil];
                    testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:testVideoStartNakPayload.data];
                    [streamingLifecycleManager handleProtocolStartServiceNAKMessage:testVideoMessage];
                });

                it(@"should end the service", ^{
                    expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
                });
            });
        });

        describe(@"after receiving a video end ACK", ^{
            __block SDLProtocolHeader *testVideoHeader = nil;
            __block SDLProtocolMessage *testVideoMessage = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testVideoHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testVideoHeader.frameType = SDLFrameTypeSingle;
                testVideoHeader.frameData = SDLFrameInfoEndServiceACK;
                testVideoHeader.encrypted = NO;
                testVideoHeader.serviceType = SDLServiceTypeVideo;

                testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:nil];
                [streamingLifecycleManager handleProtocolEndServiceACKMessage:testVideoMessage];
            });

            it(@"should have set all the right properties", ^{
                expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
            });
        });

        describe(@"after receiving a video end NAK", ^{
            __block SDLProtocolHeader *testVideoHeader = nil;
            __block SDLProtocolMessage *testVideoMessage = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLVideoStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testVideoHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testVideoHeader.frameType = SDLFrameTypeSingle;
                testVideoHeader.frameData = SDLFrameInfoEndServiceNACK;
                testVideoHeader.encrypted = NO;
                testVideoHeader.serviceType = SDLServiceTypeVideo;

                testVideoMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testVideoHeader andPayload:nil];
                [streamingLifecycleManager handleProtocolEndServiceNAKMessage:testVideoMessage];
            });

            it(@"should have set all the right properties", ^{
                expect(streamingLifecycleManager.currentVideoStreamState).to(equal(SDLVideoStreamStateStopped));
            });
        });

        describe(@"after receiving an Audio Start ACK", ^{
            __block SDLProtocolHeader *testAudioHeader = nil;
            __block SDLProtocolMessage *testAudioMessage = nil;
            __block SDLControlFramePayloadAudioStartServiceAck *testAudioStartServicePayload = nil;
            __block int64_t testMTU = 786579;

            beforeEach(^{
                [streamingLifecycleManager.audioStreamStateMachine setToState:SDLAudioStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testAudioHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testAudioHeader.frameType = SDLFrameTypeSingle;
                testAudioHeader.frameData = SDLFrameInfoStartServiceACK;
                testAudioHeader.encrypted = YES;
                testAudioHeader.serviceType = SDLServiceTypeAudio;

                testAudioStartServicePayload = [[SDLControlFramePayloadAudioStartServiceAck alloc] initWithMTU:testMTU];
                testAudioMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testAudioHeader andPayload:testAudioStartServicePayload.data];
                [streamingLifecycleManager handleProtocolStartServiceACKMessage:testAudioMessage];
            });

            it(@"should have set all the right properties", ^{
                expect([[SDLGlobals sharedGlobals] mtuSizeForServiceType:SDLServiceTypeAudio]).to(equal(testMTU));
                expect(streamingLifecycleManager.audioEncrypted).to(equal(YES));
                expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateReady));
            });
        });

        describe(@"after receiving an Audio Start NAK", ^{
            __block SDLProtocolHeader *testAudioHeader = nil;
            __block SDLProtocolMessage *testAudioMessage = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLAudioStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testAudioHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testAudioHeader.frameType = SDLFrameTypeSingle;
                testAudioHeader.frameData = SDLFrameInfoStartServiceNACK;
                testAudioHeader.encrypted = NO;
                testAudioHeader.serviceType = SDLServiceTypeAudio;

                testAudioMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testAudioHeader andPayload:nil];
                [streamingLifecycleManager handleProtocolEndServiceACKMessage:testAudioMessage];
            });

            it(@"should have set all the right properties", ^{
                expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStopped));
            });
        });

        describe(@"after receiving a audio end ACK", ^{
            __block SDLProtocolHeader *testAudioHeader = nil;
            __block SDLProtocolMessage *testAudioMessage = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLAudioStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testAudioHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testAudioHeader.frameType = SDLFrameTypeSingle;
                testAudioHeader.frameData = SDLFrameInfoEndServiceACK;
                testAudioHeader.encrypted = NO;
                testAudioHeader.serviceType = SDLServiceTypeAudio;

                testAudioMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testAudioHeader andPayload:nil];
                [streamingLifecycleManager handleProtocolEndServiceACKMessage:testAudioMessage];
            });

            it(@"should have set all the right properties", ^{
                expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStopped));
            });
        });

        describe(@"after receiving a audio end NAK", ^{
            __block SDLProtocolHeader *testAudioHeader = nil;
            __block SDLProtocolMessage *testAudioMessage = nil;

            beforeEach(^{
                [streamingLifecycleManager.videoStreamStateMachine setToState:SDLAudioStreamStateStarting fromOldState:nil callEnterTransition:NO];

                testAudioHeader = [[SDLV2ProtocolHeader alloc] initWithVersion:5];
                testAudioHeader.frameType = SDLFrameTypeSingle;
                testAudioHeader.frameData = SDLFrameInfoEndServiceNACK;
                testAudioHeader.encrypted = NO;
                testAudioHeader.serviceType = SDLServiceTypeAudio;

                testAudioMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testAudioHeader andPayload:nil];
                [streamingLifecycleManager handleProtocolEndServiceNAKMessage:testAudioMessage];
            });

            it(@"should have set all the right properties", ^{
                expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamStateStopped));
            });
        });
    });
});

QuickSpecEnd