Skip to content

Instantly share code, notes, and snippets.

@bondydaa
Created March 25, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bondydaa/2db355ed45b7e50e4071 to your computer and use it in GitHub Desktop.
Save bondydaa/2db355ed45b7e50e4071 to your computer and use it in GitHub Desktop.
OpenTok Sample Project ViewController.m
//
// ViewController.h
// Getting Started
//
// Created by Jeff Swartz on 11/19/14.
// Copyright (c) 2014 TokBox, Inc. All rights reserved.
#import "ViewController.h"
#import <OpenTok/OpenTok.h>
@interface ViewController ()
<OTSessionDelegate, OTSubscriberKitDelegate, OTPublisherDelegate, UITextViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *controlsView;
@property (weak, nonatomic) IBOutlet UIView *videoContainerView;
@property (weak, nonatomic) IBOutlet UIView *subscriberView;
@property (weak, nonatomic) IBOutlet UIView *publisherView;
@property (weak, nonatomic) IBOutlet UIButton *swapCameraBtn;
@property (weak, nonatomic) IBOutlet UIButton *publisherAudioBtn;
@property (weak, nonatomic) IBOutlet UIButton *subscriberAudioBtn;
@end
@implementation ViewController {
OTSession* _session;
OTPublisher* _publisher;
OTSubscriber* _subscriber;
NSString* _archiveId;
NSString* _apiKey;
NSString* _sessionId;
NSString* _token;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self getSessionCredentials];
}
- (void)getSessionCredentials
{
NSString* urlPath = SAMPLE_SERVER_BASE_URL;
urlPath = [urlPath stringByAppendingString:@"/session"];
NSURL *url = [NSURL URLWithString: urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[request setHTTPMethod: @"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (error){
NSLog(@"Error,%@, URL: %@", [error localizedDescription],urlPath);
}
else{
NSDictionary *roomInfo = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
_apiKey = [roomInfo objectForKey:@"apiKey"];
_token = [roomInfo objectForKey:@"token"];
_sessionId = [roomInfo objectForKey:@"sessionId"];
if(!_apiKey || !_token || !_sessionId) {
NSLog(@"Error invalid response from server, URL: %@",urlPath);
} else {
[self doConnect];
}
}
}];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (UIUserInterfaceIdiomPhone == [[UIDevice currentDevice]
userInterfaceIdiom])
{
return NO;
} else {
return YES;
}
}
#pragma mark - OpenTok methods
- (void)doConnect
{
// Initialize a new instance of OTSession and begin the connection process.
_session = [[OTSession alloc] initWithApiKey:_apiKey
sessionId:_sessionId
delegate:self];
OTError *error = nil;
[_session connectWithToken:_token error:&error];
if (error)
{
NSLog(@"Unable to connect to session (%@)",
error.localizedDescription);
}
}
- (void)doPublish
{
_publisher = [[OTPublisher alloc]
initWithDelegate:self];
OTError *error = nil;
[_session publish:_publisher error:&error];
if (error)
{
NSLog(@"Unable to publish (%@)",
error.localizedDescription);
}
[_publisher.view setFrame:CGRectMake(0, 0, _publisherView.bounds.size.width,
_publisherView.bounds.size.height)];
[_publisherView addSubview:_publisher.view];
_publisherAudioBtn.hidden = NO;
[_publisherAudioBtn addTarget:self
action:@selector(togglePublisherMic)
forControlEvents:UIControlEventTouchUpInside];
_swapCameraBtn.hidden = NO;
[_swapCameraBtn addTarget:self
action:@selector(swapCamera)
forControlEvents:UIControlEventTouchUpInside];
}
-(void)togglePublisherMic
{
_publisher.publishAudio = !_publisher.publishAudio;
UIImage *buttonImage;
if (_publisher.publishAudio) {
buttonImage = [UIImage imageNamed: @"mic-24.png"];
} else {
buttonImage = [UIImage imageNamed: @"mic_muted-24.png"];
}
[_publisherAudioBtn setImage:buttonImage forState:UIControlStateNormal];
}
-(void)toggleSubscriberAudio
{
_subscriber.subscribeToAudio = !_subscriber.subscribeToAudio;
UIImage *buttonImage;
if (_subscriber.subscribeToAudio) {
buttonImage = [UIImage imageNamed: @"Subscriber-Speaker-35.png"];
} else {
buttonImage = [UIImage imageNamed: @"Subscriber-Speaker-Mute-35.png"];
}
[_subscriberAudioBtn setImage:buttonImage forState:UIControlStateNormal];
}
-(void)swapCamera
{
if (_publisher.cameraPosition == AVCaptureDevicePositionFront) {
_publisher.cameraPosition = AVCaptureDevicePositionBack;
} else {
_publisher.cameraPosition = AVCaptureDevicePositionFront;
}
}
- (void)cleanupPublisher {
[_publisher.view removeFromSuperview];
_publisher = nil;
}
- (void)doSubscribe:(OTStream*)stream
{
_subscriber = [[OTSubscriber alloc] initWithStream:stream
delegate:self];
OTError *error = nil;
[_session subscribe:_subscriber error:&error];
if (error)
{
NSLog(@"Unable to publish (%@)",
error.localizedDescription);
}
}
- (void)cleanupSubscriber
{
[_subscriber.view removeFromSuperview];
_subscriber = nil;
}
# pragma mark - OTSession delegate callbacks
- (void)sessionDidConnect:(OTSession*)session
{
[self doPublish];
}
- (void)sessionDidDisconnect:(OTSession*)session
{
NSString* alertMessage =
[NSString stringWithFormat:@"Session disconnected: (%@)",
session.sessionId];
NSLog(@"sessionDidDisconnect (%@)", alertMessage);
}
- (void)session:(OTSession*)session
streamCreated:(OTStream *)stream
{
NSLog(@"session streamCreated (%@)", stream.streamId);
if (nil == _subscriber)
{
[self doSubscribe:stream];
}
}
- (void)session:(OTSession*)session
streamDestroyed:(OTStream *)stream
{
NSLog(@"session streamDestroyed (%@)", stream.streamId);
if ([_subscriber.stream.streamId isEqualToString:stream.streamId])
{
[self cleanupSubscriber];
}
}
- (void) session:(OTSession *)session
connectionCreated:(OTConnection *)connection
{
NSLog(@"session connectionCreated (%@)", connection.connectionId);
}
- (void) session:(OTSession *)session
connectionDestroyed:(OTConnection *)connection
{
NSLog(@"session connectionDestroyed (%@)", connection.connectionId);
}
- (void) session:(OTSession*)session
didFailWithError:(OTError*)error
{
NSLog(@"didFailWithError: (%@)", error);
}
# pragma mark - OTPublisher delegate callbacks
- (void)publisher:(OTPublisherKit *)publisher
streamCreated:(OTStream *)stream
{
NSLog(@"Now publishing.");
}
- (void)publisher:(OTPublisherKit*)publisher
streamDestroyed:(OTStream *)stream
{
[self cleanupPublisher];
}
- (void)publisher:(OTPublisherKit*)publisher
didFailWithError:(OTError*) error
{
NSLog(@"publisher didFailWithError %@", error);
[self cleanupPublisher];
}
# pragma mark - OTSubscriber delegate callbacks
- (void)subscriberDidConnectToStream:(OTSubscriberKit*)subscriber
{
NSLog(@"subscriberDidConnectToStream (%@)",
subscriber.stream.connection.connectionId);
[_subscriber.view setFrame:CGRectMake(0, 0, _subscriberView.bounds.size.width,
_subscriberView.bounds.size.height)];
[_subscriberView addSubview:_subscriber.view];
_subscriberAudioBtn.hidden = NO;
[_subscriberAudioBtn addTarget:self
action:@selector(toggleSubscriberAudio)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)subscriber:(OTSubscriberKit*)subscriber
didFailWithError:(OTError*)error
{
NSLog(@"subscriber %@ didFailWithError %@",
subscriber.stream.streamId,
error);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment