/

Develop The Objective-C Project


Watch a demonstration of the current section
  1. Right-Click the Publisher > Publisher folder
  2. Choose New File...
  3. From the iOS tab, select Cocoa Touch Class
  4. Click Next
  5. For the Class: field, enter PublisherViewController and leave the Subclass of field set to R5VideoViewController
  6. Click Next and choose the default folder
  7. 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
  8. 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
  9. Open Publisher > Publisher > Main.storyboard
  10. From the Object Library pull out a new controller
  11. From the inspector, give it a class name of PublisherViewController
  12. Also give it the Storyboard ID of publishView
  13. Place a button component with the label Start in the original ViewController
  14. Open the Assistant Editor and drag the Publish Button over to the ViewController.h and give it the name publishButton
  15. 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
  16. Open the Assistant Editor and click on the Start button
  17. On the Connections Inspector drag the touch up inside over the onPublishToggle method declared in ViewController.m
  18. Select the top level project Publisher
  19. Select Build Settings and filter by "bitcode"
  20. Make sure Enable Bitcode is set to No
  21. Select the Info tab
  22. From the Custom iOS Target Properties section and add a Privacy - Camera Usage Description and a Privacy - Microphone Usage Description
  23. Restart Red5 Pro per previous steps
  24. Make sure your device is selected and unlocked, then click Run
  25. When the application loads on your device, you can then Click Start
  26. From a browser window, navigate to http://localhost:5080/live/subscribe.jsp
  27. Scroll down and select your stream in order to view the stream over WebRTC