moonlight-ios/Limelight/Stream/StreamManager.m

133 lines
4.6 KiB
Mathematica
Raw Normal View History

2014-10-20 13:35:42 -04:00
//
// StreamManager.m
// Moonlight
2014-10-20 13:35:42 -04:00
//
// Created by Diego Waxemberg on 10/20/14.
// Copyright (c) 2014 Moonlight Stream. All rights reserved.
2014-10-20 13:35:42 -04:00
//
#import "StreamManager.h"
#import "CryptoManager.h"
#import "HttpManager.h"
#import "Utils.h"
#import "StreamView.h"
#import "ServerInfoResponse.h"
#import "HttpResponse.h"
#import "HttpRequest.h"
#import "IdManager.h"
2014-10-20 13:35:42 -04:00
@implementation StreamManager {
StreamConfiguration* _config;
OSView* _renderView;
id<ConnectionCallbacks> _callbacks;
Connection* _connection;
2014-10-20 13:35:42 -04:00
}
- (id) initWithConfig:(StreamConfiguration*)config renderView:(OSView*)view connectionCallbacks:(id<ConnectionCallbacks>)callbacks {
self = [super init];
_config = config;
_renderView = view;
_callbacks = callbacks;
_config.riKey = [Utils randomBytes:16];
_config.riKeyId = arc4random();
return self;
}
2014-10-20 13:35:42 -04:00
- (void)main {
2018-08-27 00:34:34 -07:00
[CryptoManager generateKeyPairUsingSSL];
NSString* uniqueId = [IdManager getUniqueId];
2014-10-20 13:35:42 -04:00
2014-10-20 21:26:08 -04:00
HttpManager* hMan = [[HttpManager alloc] initWithHost:_config.host
uniqueId:uniqueId
2018-12-22 00:05:48 -08:00
serverCert:_config.serverCert];
2014-10-20 13:35:42 -04:00
ServerInfoResponse* serverInfoResp = [[ServerInfoResponse alloc] init];
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResp withUrlRequest:[hMan newServerInfoRequest:false]
2015-07-08 23:03:23 -07:00
fallbackError:401 fallbackRequest:[hMan newHttpServerInfoRequest]]];
NSString* pairStatus = [serverInfoResp getStringTag:@"PairStatus"];
2015-02-01 20:31:57 -05:00
NSString* appversion = [serverInfoResp getStringTag:@"appversion"];
2016-10-05 17:32:30 -07:00
NSString* gfeVersion = [serverInfoResp getStringTag:@"GfeVersion"];
2015-11-11 18:24:36 -08:00
NSString* serverState = [serverInfoResp getStringTag:@"state"];
if (![serverInfoResp isStatusOk]) {
[_callbacks launchFailed:serverInfoResp.statusMessage];
return;
}
else if (pairStatus == NULL || appversion == NULL || serverState == NULL) {
[_callbacks launchFailed:@"Failed to connect to PC"];
return;
}
if (![pairStatus isEqualToString:@"1"]) {
// Not paired
[_callbacks launchFailed:@"Device not paired to PC"];
return;
}
// resumeApp and launchApp handle calling launchFailed
2015-11-11 18:24:36 -08:00
if ([serverState hasSuffix:@"_SERVER_BUSY"]) {
2014-10-20 21:26:08 -04:00
// App already running, resume it
if (![self resumeApp:hMan]) {
return;
}
2014-10-20 21:26:08 -04:00
} else {
// Start app
if (![self launchApp:hMan]) {
return;
}
2014-10-20 21:26:08 -04:00
}
2016-10-05 17:32:30 -07:00
// Populate the config's version fields from serverinfo
_config.appVersion = appversion;
_config.gfeVersion = gfeVersion;
2015-02-01 20:31:57 -05:00
// Initializing the renderer must be done on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
VideoDecoderRenderer* renderer = [[VideoDecoderRenderer alloc] initWithView:self->_renderView];
self->_connection = [[Connection alloc] initWithConfig:self->_config renderer:renderer connectionCallbacks:self->_callbacks];
NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:self->_connection];
});
}
- (void) stopStream
{
[_connection terminate];
2014-10-20 13:35:42 -04:00
}
- (BOOL) launchApp:(HttpManager*)hMan {
HttpResponse* launchResp = [[HttpResponse alloc] init];
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:launchResp withUrlRequest:[hMan newLaunchRequest:_config]]];
NSString *gameSession = [launchResp getStringTag:@"gamesession"];
if (![launchResp isStatusOk]) {
[_callbacks launchFailed:launchResp.statusMessage];
2015-02-10 21:26:55 -05:00
Log(LOG_E, @"Failed Launch Response: %@", launchResp.statusMessage);
return FALSE;
} else if (gameSession == NULL || [gameSession isEqualToString:@"0"]) {
[_callbacks launchFailed:@"Failed to launch app"];
Log(LOG_E, @"Failed to parse game session");
return FALSE;
}
return TRUE;
2014-10-20 21:26:08 -04:00
}
- (BOOL) resumeApp:(HttpManager*)hMan {
HttpResponse* resumeResp = [[HttpResponse alloc] init];
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:resumeResp withUrlRequest:[hMan newResumeRequest:_config]]];
NSString* resume = [resumeResp getStringTag:@"resume"];
if (![resumeResp isStatusOk]) {
[_callbacks launchFailed:resumeResp.statusMessage];
2015-02-10 21:26:55 -05:00
Log(LOG_E, @"Failed Resume Response: %@", resumeResp.statusMessage);
return FALSE;
} else if (resume == NULL || [resume isEqualToString:@"0"]) {
[_callbacks launchFailed:@"Failed to resume app"];
Log(LOG_E, @"Failed to parse resume response");
return FALSE;
}
return TRUE;
2014-10-20 21:26:08 -04:00
}
2014-10-20 13:35:42 -04:00
@end