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-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
}
- ( 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-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-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-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-08-31 13:13:46 -04:00
- ( void ) StreamButton : ( UIButton * ) sender
{
NSLog ( @ "Stream 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-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 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 ;
break ;
case 1 :
streamConfig . width = 1280 ;
streamConfig . height = 720 ;
streamConfig . frameRate = 60 ;
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 ;
break ;
case 3 :
streamConfig . width = 1920 ;
streamConfig . height = 1080 ;
streamConfig . frameRate = 60 ;
break ;
default :
streamConfig . width = 1280 ;
streamConfig . height = 720 ;
streamConfig . frameRate = 60 ;
break ;
}
NSLog ( @ "StreamConfig: %@, %d, %dx%dx%d" , streamConfig . host , streamConfig . hostAddr , streamConfig . width , streamConfig . height , streamConfig . frameRate ) ;
2014-08-31 13:13:46 -04:00
[ self performSegueWithIdentifier : @ "createStreamFrame" sender : self ] ;
}
- ( NSString * ) pickerView : ( UIPickerView * ) pickerView titleForRow : ( NSInteger ) row forComponent : ( NSInteger ) component
{
2014-10-14 19:21:48 -04:00
if ( pickerView = = self . StreamConfigs ) {
return [ self . streamConfigVals objectAtIndex : row ] ;
} else if ( pickerView = = self . HostPicker ) {
return ( ( Computer * ) ( [ self . hostPickerVals objectAtIndex : row ] ) ) . displayName ;
} else {
return nil ;
}
2014-08-31 13:13:46 -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-08-31 13:13:46 -04:00
- ( void ) pickerView : ( UIPickerView * ) pickerView didSelectRow : ( NSInteger ) row inComponent : ( NSInteger ) component
{
2014-10-14 19:21:48 -04:00
if ( pickerView = = self . HostPicker ) {
2014-10-21 13:39:14 -04:00
[ self setSelectedHost : [ self . HostPicker selectedRowInComponent : 0 ] ] ;
2014-10-14 19:21:48 -04:00
}
2014-08-31 13:13:46 -04:00
// TODO : figure out how to save this info ! !
}
// returns the number of ' columns ' to display .
- ( NSInteger ) numberOfComponentsInPickerView : ( UIPickerView * ) pickerView
{
return 1 ;
}
// returns the # of rows in each component . .
- ( NSInteger ) pickerView : ( UIPickerView * ) pickerView numberOfRowsInComponent : ( NSInteger ) component
{
2014-10-14 19:21:48 -04:00
if ( pickerView = = self . StreamConfigs ) {
return self . streamConfigVals . count ;
} else if ( pickerView = = self . HostPicker ) {
return self . hostPickerVals . count ;
} else {
return 0 ;
}
2014-08-31 13:13:46 -04:00
}
- ( void ) viewDidLoad
{
[ super viewDidLoad ] ;
2014-10-14 19:21:48 -04:00
self . streamConfigVals = [ [ NSArray alloc ] initWithObjects : @ "1280x720 (30Hz)" , @ "1280x720 (60Hz)" , @ "1920x1080 (30Hz)" , @ "1920x1080 (60Hz)" , nil ] ;
2014-10-21 01:51:15 -04:00
[ self . StreamConfigs selectRow : 1 inComponent : 0 animated : NO ] ;
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
[ self updateHosts : [ [ NSArray alloc ] init ] ] ;
2014-10-21 05:11:51 -04:00
}
- ( void ) viewDidAppear : ( BOOL ) animated
{
2014-10-20 11:31:40 -04:00
_mDNSManager = [ [ MDNSManager alloc ] initWithCallback : self ] ;
[ _mDNSManager searchForHosts ] ;
2014-10-21 05:11:51 -04:00
}
- ( void ) viewDidDisappear : ( BOOL ) 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 ] ] ;
}
self . hostPickerVals = hostPickerValues ;
2014-10-14 19:21:48 -04:00
[ self . HostPicker reloadAllComponents ] ;
2014-10-21 13:39:14 -04:00
[ self setSelectedHost : [ self . HostPicker selectedRowInComponent : 0 ] ] ;
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 ;
}
- ( NSUInteger ) supportedInterfaceOrientations {
NSString * deviceType = [ UIDevice currentDevice ] . model ;
if ( [ deviceType containsString : @ "iPhone" ] || [ deviceType containsString : @ "iPod" ] ) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown ;
} else {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight ;
}
}
- ( UIInterfaceOrientation ) preferredInterfaceOrientationForPresentation {
NSString * deviceType = [ UIDevice currentDevice ] . model ;
if ( [ deviceType containsString : @ "iPhone" ] || [ deviceType containsString : @ "iPod" ] ) {
return UIInterfaceOrientationPortrait ;
} else {
2014-10-21 03:50:18 -04:00
return UIInterfaceOrientationLandscapeRight ;
2014-10-20 21:39:40 -04:00
}
}
2014-08-31 13:13:46 -04:00
@ end