2014-10-24 16:02:10 -04:00
2014-08-31 13:13:46 -04:00
// MainFrameViewController . m
// Limelight - iOS
//
// Created by Diego Waxemberg on 1 / 17 / 14.
// Copyright ( c ) 2014 Diego Waxemberg . All rights reserved .
//
# import "MainFrameViewController.h"
2014-10-14 19:21:48 -04:00
# import "Computer.h"
2014-10-16 21:20:27 -04:00
# import "CryptoManager.h"
2014-10-18 21:13:48 -04:00
# import "HttpManager.h"
2014-10-20 11:31:40 -04:00
# import "Connection.h"
# import "VideoDecoderRenderer.h"
2014-10-20 13:35:42 -04:00
# import "StreamManager.h"
2014-10-20 20:54:03 -04:00
# import "Utils.h"
2014-10-24 16:02:10 -04:00
# import "UIComputerView.h"
# import "UIAppView.h"
# import "App.h"
2014-08-31 13:13:46 -04:00
2014-10-20 11:31:40 -04:00
@ implementation MainFrameViewController {
NSOperationQueue * _opQueue ;
MDNSManager * _mDNSManager ;
Computer * _selectedHost ;
2014-10-20 13:35:42 -04:00
UIAlertView * _pairAlert ;
2014-10-20 11:31:40 -04:00
}
2014-10-20 20:54:03 -04:00
static StreamConfiguration * streamConfig ;
2014-08-31 13:13:46 -04:00
2014-10-20 20:54:03 -04:00
+ ( StreamConfiguration * ) getStreamConfiguration {
return streamConfig ;
2014-08-31 13:13:46 -04:00
}
2014-10-24 16:02:10 -04:00
// TODO : no more pair button
/ *
2014-08-31 13:13:46 -04:00
- ( void ) PairButton : ( UIButton * ) sender
{
NSLog ( @ "Pair Button Pressed!" ) ;
2014-10-20 20:08:12 -04:00
if ( [ self . hostTextField . text length ] > 0 ) {
_selectedHost = [ [ Computer alloc ] initWithIp : self . hostTextField . text ] ;
NSLog ( @ "Using custom host: %@" , self . hostTextField . text ) ;
}
2014-10-24 16:02:10 -04:00
2014-10-21 13:46:05 -04:00
if ( ! [ self validatePcSelected ] ) {
NSLog ( @ "No valid PC selected" ) ;
return ;
}
2014-10-20 02:59:16 -04:00
[ CryptoManager generateKeyPairUsingSSl ] ;
NSString * uniqueId = [ CryptoManager getUniqueID ] ;
NSData * cert = [ CryptoManager readCertFromFile ] ;
2014-10-21 14:31:03 -04:00
if ( [ Utils resolveHost : _selectedHost . hostName ] = = 0 ) {
[ self displayDnsFailedDialog ] ;
return ;
}
2014-10-20 11:31:40 -04:00
HttpManager * hMan = [ [ HttpManager alloc ] initWithHost : _selectedHost . hostName uniqueId : uniqueId deviceName : @ "roth" cert : cert ] ;
2014-10-20 13:35:42 -04:00
PairManager * pMan = [ [ PairManager alloc ] initWithManager : hMan andCert : cert callback : self ] ;
2014-10-20 11:31:40 -04:00
[ _opQueue addOperation : pMan ] ;
2014-08-31 13:13:46 -04:00
}
2014-10-24 16:02:10 -04:00
* /
2014-08-31 13:13:46 -04:00
2014-10-20 13:35:42 -04:00
- ( void ) showPIN : ( NSString * ) PIN {
dispatch_sync ( dispatch_get _main _queue ( ) , ^ {
_pairAlert = [ [ UIAlertView alloc ] initWithTitle : @ "Pairing" message : [ NSString stringWithFormat : @ "Enter the following PIN on the host machine: %@" , PIN ] delegate : self cancelButtonTitle : @ "Ok" otherButtonTitles : nil , nil ] ;
[ _pairAlert show ] ;
} ) ;
}
- ( void ) pairFailed : ( NSString * ) message {
dispatch_sync ( dispatch_get _main _queue ( ) , ^ {
[ _pairAlert dismissWithClickedButtonIndex : 0 animated : NO ] ;
_pairAlert = [ [ UIAlertView alloc ] initWithTitle : @ "Pairing Failed" message : message delegate : self cancelButtonTitle : @ "Ok" otherButtonTitles : nil , nil ] ;
[ _pairAlert show ] ;
} ) ;
}
- ( void ) pairSuccessful {
dispatch_sync ( dispatch_get _main _queue ( ) , ^ {
[ _pairAlert dismissWithClickedButtonIndex : 0 animated : NO ] ;
_pairAlert = [ [ UIAlertView alloc ] initWithTitle : @ "Pairing Succesful" message : @ "Successfully paired to host" delegate : self cancelButtonTitle : @ "Ok" otherButtonTitles : nil , nil ] ;
[ _pairAlert show ] ;
} ) ;
}
2014-10-21 14:31:03 -04:00
- ( void ) displayDnsFailedDialog {
UIAlertController * alert = [ UIAlertController alertControllerWithTitle : @ "Network Error"
message : @ "Failed to resolve host."
preferredStyle : UIAlertControllerStyleAlert ] ;
[ alert addAction : [ UIAlertAction actionWithTitle : @ "Ok" style : UIAlertActionStyleDestructive handler : nil ] ] ;
[ self presentViewController : alert animated : YES completion : nil ] ;
}
2014-10-24 16:02:10 -04:00
// TODO : No more stream button
/ *
2014-08-31 13:13:46 -04:00
- ( void ) StreamButton : ( UIButton * ) sender
{
NSLog ( @ "Stream Button Pressed!" ) ;
2014-10-24 16:02:10 -04:00
2014-10-20 20:08:12 -04:00
if ( [ self . hostTextField . text length ] > 0 ) {
_selectedHost = [ [ Computer alloc ] initWithIp : self . hostTextField . text ] ;
NSLog ( @ "Using custom host: %@" , self . hostTextField . text ) ;
}
2014-10-21 13:46:05 -04:00
if ( ! [ self validatePcSelected ] ) {
NSLog ( @ "No valid PC selected" ) ;
return ;
}
2014-10-20 20:54:03 -04:00
streamConfig = [ [ StreamConfiguration alloc ] init ] ;
streamConfig . host = _selectedHost . hostName ;
streamConfig . hostAddr = [ Utils resolveHost : _selectedHost . hostName ] ;
2014-10-21 14:31:03 -04:00
if ( streamConfig . hostAddr = = 0 ) {
[ self displayDnsFailedDialog ] ;
return ;
}
2014-10-20 20:54:03 -04:00
2014-10-21 03:15:29 -04:00
unsigned long selectedConf = [ self . StreamConfigs selectedRowInComponent : 0 ] ;
NSLog ( @ "selectedConf: %ld" , selectedConf ) ;
2014-10-20 20:54:03 -04:00
switch ( selectedConf ) {
case 0 :
streamConfig . width = 1280 ;
streamConfig . height = 720 ;
streamConfig . frameRate = 30 ;
2014-10-22 00:04:58 -04:00
streamConfig . bitRate = 5000 ;
2014-10-20 20:54:03 -04:00
break ;
2014-10-22 00:04:58 -04:00
default :
2014-10-20 20:54:03 -04:00
case 1 :
streamConfig . width = 1280 ;
streamConfig . height = 720 ;
streamConfig . frameRate = 60 ;
2014-10-22 00:04:58 -04:00
streamConfig . bitRate = 10000 ;
2014-10-20 20:54:03 -04:00
break ;
case 2 :
streamConfig . width = 1920 ;
2014-10-21 04:35:06 -04:00
streamConfig . height = 1080 ;
2014-10-20 20:54:03 -04:00
streamConfig . frameRate = 30 ;
2014-10-22 00:04:58 -04:00
streamConfig . bitRate = 10000 ;
2014-10-20 20:54:03 -04:00
break ;
case 3 :
streamConfig . width = 1920 ;
streamConfig . height = 1080 ;
streamConfig . frameRate = 60 ;
2014-10-22 00:04:58 -04:00
streamConfig . bitRate = 20000 ;
2014-10-20 20:54:03 -04:00
break ;
}
2014-10-22 00:04:58 -04:00
NSLog ( @ "StreamConfig: %@, %d, %dx%dx%d at %d Mbps" , streamConfig . host , streamConfig . hostAddr , streamConfig . width , streamConfig . height , streamConfig . frameRate , streamConfig . bitRate ) ;
2014-08-31 13:13:46 -04:00
[ self performSegueWithIdentifier : @ "createStreamFrame" sender : self ] ;
}
2014-10-24 16:02:10 -04:00
* /
2014-08-31 13:13:46 -04:00
2014-10-24 16:02:10 -04:00
/ *
2014-10-21 13:39:14 -04:00
- ( void ) setSelectedHost : ( NSInteger ) selectedIndex
{
_selectedHost = ( Computer * ) ( [ self . hostPickerVals objectAtIndex : selectedIndex ] ) ;
if ( _selectedHost . hostName = = NULL ) {
// This must be the placeholder computer
_selectedHost = NULL ;
}
}
2014-10-24 16:02:10 -04:00
* /
2014-10-21 13:39:14 -04:00
2014-08-31 13:13:46 -04:00
- ( void ) viewDidLoad
{
[ super viewDidLoad ] ;
2014-10-14 19:21:48 -04:00
2014-10-24 16:02:10 -04:00
NSArray * streamConfigVals = [ [ NSArray alloc ] initWithObjects : @ "1280x720 (30Hz)" , @ "1280x720 (60Hz)" , @ "1920x1080 (30Hz)" , @ "1920x1080 (60Hz)" , nil ] ;
2014-10-19 04:19:54 -04:00
2014-10-21 05:11:51 -04:00
_opQueue = [ [ NSOperationQueue alloc ] init ] ;
2014-10-21 13:39:14 -04:00
// Initialize the host picker list
2014-10-24 16:02:10 -04:00
// [ self updateHosts : [ [ NSArray alloc ] init ] ] ;
Computer * test = [ [ Computer alloc ] initWithIp : @ "CEMENT-TRUCK" ] ;
UIScrollView * hostScrollView = [ [ UIScrollView alloc ] init ] ;
hostScrollView . frame = CGRectMake ( 0 , 0 , self . view . frame . size . width , self . view . frame . size . height / 2 ) ;
[ hostScrollView setShowsHorizontalScrollIndicator : NO ] ;
UIComputerView * compView ;
for ( int i = 0 ; i < 5 ; i + + ) {
compView = [ [ UIComputerView alloc ] initWithComputer : test ] ;
[ hostScrollView addSubview : compView ] ;
[ compView sizeToFit ] ;
compView . center = CGPointMake ( ( compView . frame . size . width + 20 ) * i + compView . frame . size . width , hostScrollView . frame . size . height / 2 ) ;
}
[ hostScrollView setContentSize : CGSizeMake ( compView . frame . size . width * 5 + compView . frame . size . width , hostScrollView . frame . size . height ) ] ;
UIScrollView * appScrollView = [ [ UIScrollView alloc ] init ] ;
appScrollView . frame = CGRectMake ( 0 , hostScrollView . frame . size . height , self . view . frame . size . width , self . view . frame . size . height / 2 ) ;
[ appScrollView setShowsHorizontalScrollIndicator : NO ] ;
App * testApp = [ [ App alloc ] init ] ;
testApp . displayName = @ "Left 4 Dead 2" ;
UIAppView * appView ;
for ( int i = 0 ; i < 5 ; i + + ) {
appView = [ [ UIAppView alloc ] initWithApp : testApp ] ;
[ appScrollView addSubview : appView ] ;
[ appView sizeToFit ] ;
appView . center = CGPointMake ( ( appView . frame . size . width + 20 ) * i + compView . frame . size . width , appScrollView . frame . size . height / 2 ) ;
}
[ appScrollView setContentSize : CGSizeMake ( appView . frame . size . width * 5 + appView . frame . size . width , appScrollView . frame . size . height ) ] ;
[ self . view addSubview : hostScrollView ] ;
[ self . view addSubview : appScrollView ] ;
2014-10-21 05:11:51 -04:00
}
- ( void ) viewDidAppear : ( BOOL ) animated
{
2014-10-24 16:02:10 -04:00
[ super viewDidDisappear : animated ] ;
// _mDNSManager = [ [ MDNSManager alloc ] initWithCallback : self ] ;
// [ _mDNSManager searchForHosts ] ;
2014-10-21 05:11:51 -04:00
}
- ( void ) viewDidDisappear : ( BOOL ) animated
{
2014-10-24 16:02:10 -04:00
[ super viewDidDisappear : animated ] ;
// [ _mDNSManager stopSearching ] ;
2014-10-14 19:21:48 -04:00
}
- ( void ) updateHosts : ( NSArray * ) hosts {
2014-10-21 13:39:14 -04:00
NSMutableArray * hostPickerValues = [ [ NSMutableArray alloc ] initWithArray : hosts ] ;
if ( [ hostPickerValues count ] = = 0 ) {
[ hostPickerValues addObject : [ [ Computer alloc ] initPlaceholder ] ] ;
}
2014-08-31 13:13:46 -04:00
}
2014-10-21 13:46:05 -04:00
- ( BOOL ) validatePcSelected {
if ( _selectedHost = = NULL ) {
UIAlertController * alert = [ UIAlertController alertControllerWithTitle : @ "No PC Selected"
message : @ "You must select a PC to continue."
preferredStyle : UIAlertControllerStyleAlert ] ;
[ alert addAction : [ UIAlertAction actionWithTitle : @ "Ok" style : UIAlertActionStyleDestructive handler : nil ] ] ;
[ self presentViewController : alert animated : YES completion : nil ] ;
return FALSE ;
}
return TRUE ;
}
2014-08-31 13:13:46 -04:00
- ( void ) didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ] ;
// Dispose of any resources that can be recreated .
}
2014-10-20 21:39:40 -04:00
2014-10-20 21:45:37 -04:00
- ( void ) touchesBegan : ( NSSet * ) touches withEvent : ( UIEvent * ) event {
[ self . view endEditing : YES ] ;
}
- ( BOOL ) textFieldShouldReturn : ( UITextField * ) textField {
[ textField resignFirstResponder ] ;
return YES ;
}
2014-10-20 21:39:40 -04:00
- ( BOOL ) shouldAutorotate {
return YES ;
}
2014-08-31 13:13:46 -04:00
@ end