From 34d871403c2b56bfae5b7b0d50786ab866458f49 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 21 Oct 2014 01:54:31 -0400 Subject: [PATCH] Properly stop the stream when exiting or switching apps --- Limelight/Connection.h | 1 + Limelight/Connection.m | 12 +++++++++++- Limelight/StreamFrameViewController.m | 18 +++++++++++++++--- Limelight/StreamManager.h | 2 ++ Limelight/StreamManager.m | 10 ++++++++-- limelight-common-c | 2 +- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Limelight/Connection.h b/Limelight/Connection.h index d897186..e4a9735 100644 --- a/Limelight/Connection.h +++ b/Limelight/Connection.h @@ -19,6 +19,7 @@ @interface Connection : NSOperation -(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id)callback; +-(void) terminate; -(void) main; @end diff --git a/Limelight/Connection.m b/Limelight/Connection.m index 5b22960..0272ef2 100644 --- a/Limelight/Connection.m +++ b/Limelight/Connection.m @@ -182,8 +182,8 @@ void ClConnectionStarted(void) void ClConnectionTerminated(long errorCode) { NSLog(@"ConnectionTerminated: %ld", errorCode); + [_callback connectionTerminated]; - NSLog(@"Tried calling callback"); } void ClDisplayMessage(char* message) @@ -196,6 +196,16 @@ void ClDisplayTransientMessage(char* message) NSLog(@"DisplayTransientMessage: %s", message); } +-(void) terminate +{ + // We dispatch this async to get out because this can be invoked + // on a thread inside common and we don't want to deadlock + dispatch_async(dispatch_get_main_queue(), ^{ + // This is safe to call even before LiStartConnection + LiStopConnection(); + }); +} + -(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id)callback { self = [super init]; diff --git a/Limelight/StreamFrameViewController.m b/Limelight/StreamFrameViewController.m index 26ae694..0079983 100644 --- a/Limelight/StreamFrameViewController.m +++ b/Limelight/StreamFrameViewController.m @@ -18,6 +18,7 @@ @implementation StreamFrameViewController { ControllerSupport *_controllerSupport; + StreamManager *_streamMan; } - (void)viewDidLoad @@ -28,18 +29,29 @@ _controllerSupport = [[ControllerSupport alloc] init]; - StreamManager* streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration] renderView:self.view connectionTerminatedCallback:self]; + _streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration] renderView:self.view connectionTerminatedCallback:self]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; - [opQueue addOperation:streamMan]; + [opQueue addOperation:_streamMan]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationWillResignActive:) + name:UIApplicationWillResignActiveNotification + object:nil]; +} + +- (void)applicationWillResignActive:(NSNotification *)notification { + [_streamMan stopStream]; + [self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; } - (void)connectionTerminated { - NSLog(@"StreamFrame - Connection Terminated"); UIAlertController* conTermAlert = [UIAlertController alertControllerWithTitle:@"Connection Terminated" message:@"The connection terminated unexpectedly" preferredStyle:UIAlertControllerStyleAlert]; [conTermAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){ [self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; }]]; [self presentViewController:conTermAlert animated:YES completion:nil]; + + [_streamMan stopStream]; } - (void)didReceiveMemoryWarning diff --git a/Limelight/StreamManager.h b/Limelight/StreamManager.h index 1a3565f..4f865f2 100644 --- a/Limelight/StreamManager.h +++ b/Limelight/StreamManager.h @@ -13,4 +13,6 @@ @interface StreamManager : NSOperation - (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id)callback; +- (void) stopStream; + @end diff --git a/Limelight/StreamManager.m b/Limelight/StreamManager.m index be9aad5..21e4cb7 100644 --- a/Limelight/StreamManager.m +++ b/Limelight/StreamManager.m @@ -15,6 +15,7 @@ StreamConfiguration* _config; UIView* _renderView; id _callback; + Connection* _connection; } - (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id)callback { @@ -49,9 +50,14 @@ } VideoDecoderRenderer* renderer = [[VideoDecoderRenderer alloc]initWithView:_renderView]; - Connection* conn = [[Connection alloc] initWithConfig:_config renderer:renderer connectionTerminatedCallback:_callback]; + _connection = [[Connection alloc] initWithConfig:_config renderer:renderer connectionTerminatedCallback:_callback]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; - [opQueue addOperation:conn]; + [opQueue addOperation:_connection]; +} + +- (void) stopStream +{ + [_connection terminate]; } - (void) launchApp:(HttpManager*)hMan { diff --git a/limelight-common-c b/limelight-common-c index 25faaa9..88d8c8e 160000 --- a/limelight-common-c +++ b/limelight-common-c @@ -1 +1 @@ -Subproject commit 25faaa9e6bd04ed35bb94d66c38f6229c5a1c30a +Subproject commit 88d8c8ede77915f980f38d07728233bfb19a328b