Add your SDK license key
1 In the viewDidLoad override in PublishViewController.m, create the R5Configuration that will be used in a streaming session(s).
- Change the host attribute to point to the location of your Red5 Pro server.
- _Change the SDK license key to the one provided from your Red5 Pro Account._
- (void)viewDidLoad {
[super viewDidLoad];
config = [R5Configuration new];
config.host = @"localhost";
config.port = 8554;
config.contextName = @"live";
config.licenseKey = @"YOUR-SDK-LICENSE-KEY";
}
2 Create a new method named preview:
that will be responsible for setting up a stream to be shown and broadcast and access the audio and video devices that will be used in the stream
-(void)preview {
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *cameraDevice = [cameras lastObject];
R5Camera *camera = [[R5Camera alloc] initWithDevice:cameraDevice andBitRate:512];
AVCaptureDevice *audioDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
R5Microphone *microphone = [[R5Microphone new] initWithDevice:audioDevice];
}
3 Within the preview:
method, establish a new R5Connection with the R5Configuration and initialize a new R5Stream instance with the connection and device references
...
R5Connection *connection = [[R5Connection new] initWithConfig:config];
stream = [[R5Stream new] initWithConnection:connection];
[stream attachVideo:camera];
[stream attachAudio:microphone];
...
4 With an R5Stream instance created, attach as the stream for the PublishViewController and display as a preview
[stream setDelegate:self];
[self attachStream:stream];
[self showPreview:YES];
5 This will draw the camera stream into a context for preview before broadcasting to the Red5 Pro server. The full preview:
method should look like such:
-(void)preview {
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *cameraDevice = [cameras lastObject];
R5Camera *camera = [[R5Camera alloc] initWithDevice:cameraDevice andBitRate:512];
AVCaptureDevice *audioDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
R5Microphone *microphone = [[R5Microphone new] initWithDevice:audioDevice];
R5Connection *connection = [[R5Connection new] initWithConfig:config];
stream = [[R5Stream new] initWithConnection:connection];
[stream attachVideo:camera];
[stream attachAudio:microphone];
[stream setDelegate:self];
[self attachStream:stream];
[self showPreview:YES];
}
6 There are a few methods that are required in order to adhere to the interface contract of PublishViewController: start
and stop
-(void)start {
[self showPreview:NO];
[stream publish:@"red5prostream"];
}
-(void)stop {
[stream stop];
[stream setDelegate:nil];
[self preview];
}
7 Note the NSString passed into the stream:publish:
method. Change the publish stream name appropriately.
These are the methods we defined on the interface for PublishViewController previously and will be invoked externally by UI controls in the app.
8 Override the viewDidAppear:
and viewWillDisappear:
methods in order to control how the stream is maintained when changing views in our Tabbed Application
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self preview];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self stop];
}
9 To adhere to the contract of being an R5StreamDelegate add an onR5StreamStatus:withStatus:withMessage
method
-(void)onR5StreamStatus:(R5Stream *)stream withStatus:(int)statusCode withMessage:(NSString *)msg {
NSLog(@"Stream: %s - %@", r5_string_for_status(statusCode), msg);
}
That completes the PublishViewController implementation that will be used and interfaced with the FirstViewController.