Created
December 15, 2014 21:44
-
-
Save stefanalund/a15c87acf85d922262d7 to your computer and use it in GitHub Desktop.
iOS native EventSource channel for demo.openwebrtc.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OWRDemoViewController.m | |
// OWRDemo | |
// | |
// Copyright (c) 2014, Ericsson AB. | |
// All rights reserved. | |
// | |
// Redistribution and use in source and binary forms, with or without modification, | |
// are permitted provided that the following conditions are met: | |
// | |
// 1. Redistributions of source code must retain the above copyright notice, this | |
// list of conditions and the following disclaimer. | |
// | |
// 2. Redistributions in binary form must reproduce the above copyright notice, this | |
// list of conditions and the following disclaimer in the documentation and/or other | |
// materials provided with the distribution. | |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY | |
// OF SUCH DAMAGE. | |
// | |
#import "SimpleNativeDemoViewController.h" | |
#import "TRVSEventSource.h" // From https://github.com/travisjeffery/TRVSEventSource | |
#include <owr.h> | |
#define kEventSourceURL @"http://localhost:8080/stoc/%d/%@" | |
@interface SimpleNativeDemoViewController () <TRVSEventSourceDelegate> | |
{ | |
IBOutlet UISlider *roomSlider; | |
IBOutlet UILabel *roomLabel; | |
IBOutlet UIBarButtonItem *joinButton; | |
IBOutlet UIBarButtonItem *callButton; | |
IBOutlet UIBarButtonItem *hangupButton; | |
// Video containers. | |
IBOutlet UIImageView *selfView; | |
IBOutlet UIImageView *remoteView; | |
} | |
@property (nonatomic, strong) TRVSEventSource *eventSource; | |
@end | |
@implementation OWRDemoViewController | |
- (void)viewDidLoad | |
{ | |
//owr_init(); | |
[self.navigationController setToolbarHidden:NO animated:NO]; | |
callButton.enabled = NO; | |
hangupButton.enabled = NO; | |
// TODO: Send selfView and remoteView ref to OpenWebRTC. | |
[super viewDidLoad]; | |
} | |
- (IBAction)joinButtonTapped:(id)sender | |
{ | |
float room = [roomSlider value]; | |
[self joinRoomWithNumber:room]; | |
} | |
- (IBAction)callButtonTapped:(id)sender | |
{ | |
} | |
- (IBAction)hangupButtonTapped:(id)sender | |
{ | |
} | |
- (IBAction)sliderValueChanged:(UISlider *)sender | |
{ | |
int sliderValue = (int)sender.value; | |
roomLabel.text = [NSString stringWithFormat:@"%d", sliderValue]; | |
} | |
- (void)joinRoomWithNumber:(int)nr | |
{ | |
NSString *deviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; | |
NSString *eventSourceURL = [NSString stringWithFormat:kEventSourceURL, nr, deviceID, nil]; | |
TRVSEventSource *eventSource = [[TRVSEventSource alloc] initWithURL:[NSURL URLWithString:eventSourceURL]]; | |
NSLog(@"Connecting to server at: %@", eventSourceURL); | |
/** | |
* Join a room. | |
*/ | |
[eventSource addListenerForEvent:@"join" usingEventHandler:^(TRVSServerSentEvent *event, NSError *error) { | |
//NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL]; | |
NSString *data = [NSString stringWithUTF8String:[event.data bytes]]; | |
NSLog(@"Received JOIN with data: %@", data); | |
if (!error) { | |
joinButton.enabled = NO; | |
callButton.enabled = YES; | |
} | |
NSString *peerUserID = [NSString stringWithFormat:@"user-%@", data]; | |
[self performSelector:@selector(joinEventSourceChannelWithPeerID:) withObject:peerUserID]; | |
}]; | |
/** | |
* Peer leaves room. | |
*/ | |
[eventSource addListenerForEvent:@"leave" usingEventHandler:^(TRVSServerSentEvent *event, NSError *error) { | |
//NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL]; | |
// TODO: owr close. | |
NSString *data = [NSString stringWithUTF8String:[event.data bytes]]; | |
NSLog(@"Received LEAVE with data: %@", data); | |
callButton.enabled = NO; | |
hangupButton.enabled = NO; | |
NSString *peerUserID = [NSString stringWithFormat:@"user-%@", data]; | |
[self performSelector:@selector(removeEventSourceListenerWithName:) withObject:peerUserID]; | |
}]; | |
/** | |
* Room is full. | |
*/ | |
[eventSource addListenerForEvent:@"sessionfull" usingEventHandler:^(TRVSServerSentEvent *event, NSError *error) { | |
//NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL]; | |
NSString *data = [NSString stringWithUTF8String:[event.data bytes]]; | |
NSLog(@"Received SESSIONFULL with data: %@", data); | |
joinButton.enabled = YES; | |
callButton.enabled = NO; | |
hangupButton.enabled = NO; | |
NSLog(@"Tell user to choose another room, this one is full"); | |
NSString *peerUserID = [NSString stringWithFormat:@"user-%@", data]; | |
[self performSelector:@selector(joinEventSourceChannelWithPeerID:) withObject:peerUserID]; | |
}]; | |
joinButton.enabled = NO; | |
self.eventSource = eventSource; | |
[self.eventSource setDelegate:self]; | |
[self.eventSource open]; | |
} | |
- (void)removeEventSourceListenerWithName:(NSString *)eventName | |
{ | |
NSLog(@"Removing listener for: %@", eventName); | |
[self.eventSource removeAllListenersForEvent:eventName]; | |
} | |
- (void)joinEventSourceChannelWithPeerID:(NSString *)peerID | |
{ | |
[self.eventSource addListenerForEvent:peerID usingEventHandler:^(TRVSServerSentEvent *event, NSError *error) { | |
//NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL]; | |
NSString *data = [NSString stringWithUTF8String:[event.data bytes]]; | |
NSLog(@"Received DATA frome peer: %@", data); | |
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL]; | |
NSLog(@"JSON:\n%@", JSON); | |
if ([@"OFFER" isEqualToString:JSON[@"messageType"]]) { | |
NSLog(@"OFFER received"); | |
} | |
}]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
NSLog(@"WARNING! didReceiveMemoryWarning"); | |
[super didReceiveMemoryWarning]; | |
} | |
#pragma mark - TRVSEventSourceDelegate methods | |
- (void)eventSourceDidOpen:(TRVSEventSource *)eventSource | |
{ | |
callButton.enabled = YES; | |
} | |
- (void)eventSource:(TRVSEventSource *)eventSource didFailWithError:(NSError *)error | |
{ | |
NSLog(@"An error occurred: %@", error); | |
callButton.enabled = NO; | |
joinButton.enabled = YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment