Develop The Objective-C Project
Watch a demonstration of the current section
- Right-Click the Publisher > Publisher folder
- Choose New File…
- From the iOS tab, select Cocoa Touch Class
- Click Next
- For the Class: field, enter PublisherViewController and leave the Subclass of field set to R5VideoViewController
- Click Next and choose the default folder
-
Open PublishViewController.h and setup the class so that it reflects the following:
#import <UIKit/UIKit.h> #import <R5Streaming/R5Streaming.h> @interface PublisherViewController : R5VideoViewController<R5StreamDelegate> - (void) start; - (void) stop; @end
-
Open PublishViewController.m and setup the class so that it reflects the following:
#import "PublisherViewController.h" #import <R5Streaming/R5Streaming.h> @interface PublisherViewController() { R5Configuration *config; R5Stream *stream; } @end @implementation PublisherViewController - (void)viewDidLoad { [super viewDidLoad]; config = [R5Configuration new]; config.host = @"192.168.0.8"; config.port = 8554; config.contextName = @"live"; config.licenseKey = @""; } -(void)preview { NSString *cameraID = nil; NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera]; AVCaptureDeviceDiscoverySession *captureDevice = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:captureDeviceType mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified]; cameraID = [captureDevice.devices.lastObject localizedName]; // AVCaptureDevice *defaultCamera = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInDualCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront]; R5Camera *camera = [[R5Camera alloc] initWithDevice:captureDevice.devices.lastObject 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]; } -(void)start { [self showPreview:YES]; [self showDebugInfo:true]; [stream publish:@"stream1" type:R5RecordTypeLive]; } -(void)stop { [stream stop]; [stream setDelegate:nil]; [self preview]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self preview]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self stop]; } -(void)onR5StreamStatus:(R5Stream *)stream withStatus:(int)statusCode withMessage:(NSString *)msg { NSLog(@"Stream: %s - %@", r5_string_for_status(statusCode), msg); } @end
- Open Publisher > Publisher > Main.storyboard
- From the Object Library pull out a new controller
- From the inspector, give it a class name of PublisherViewController
- Also give it the Storyboard ID of publishView
- Place a button component with the label Start in the original ViewController
- Open the Assistant Editor and drag the Publish Button over to the ViewController.h and give it the name publishButton
-
Open the _ViewController.m and have the code reflect the following:
#import "ViewController.h" #import "PublisherViewController.h" @interface ViewController () { PublisherViewController *publisher; BOOL isPublishing; } @end @implementation ViewController - (IBAction)onPublishToggle:(id)sender { if(isPublishing) { [publisher stop]; } else { [publisher start]; } isPublishing = !isPublishing; [[self publishButton] setTitle:isPublishing ? @"STOP" : @"START" forState:UIControlStateNormal]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"publishView"]; CGRect frameSize = self.view.bounds; publisher.view.layer.frame = frameSize; publisher = (PublisherViewController *)controller; [self.view addSubview:publisher.view]; [self.view sendSubviewToBack:publisher.view]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[self publishButton] setTitle:@"START" forState:UIControlStateNormal]; } -(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; isPublishing = NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
- Open the Assistant Editor and click on the Start button
- On the Connections Inspector drag the touch up inside over the onPublishToggle method declared in ViewController.m
- Select the top level project Publisher
- Select Build Settings and filter by “bitcode”
- Make sure Enable Bitcode is set to No
- Select the Info tab
- From the Custom iOS Target Properties section and add a Privacy – Camera Usage Description and a Privacy – Microphone Usage Description
- Restart Red5 Pro per previous steps
- Make sure your device is selected and unlocked, then click Run
- When the application loads on your device, you can then Click Start
- From a browser window, navigate to http://localhost:5080/live/subscribe.jsp
- Scroll down and select your stream in order to view the stream over WebRTC