Search code examples
iosobjective-cvideocore

How do I create an encoding session with VideoCore?


I'm integrating the VideoCore RTMP encoder into my app, but I'm having trouble starting a new encoding session.  I see the VCSimpleSession class, but how do I start the encoder and see the output?

VCSimpleSession *mySession = [[VCSimpleSession alloc] init];

I don't see the preview view.  What am I doing wrong?


Solution

  • VideoCore uses simple and complex graphs. VCSimpleSession is the easiest way to setup the encoder.

    There are a few different initializers for VCSimpleSession:

    - (instancetype) initWithVideoSize:(CGSize)videoSize
                             frameRate:(int)fps
                               bitrate:(int)bps;
    
    // -----------------------------------------------------------------------------
    - (instancetype) initWithVideoSize:(CGSize)videoSize
                             frameRate:(int)fps
                               bitrate:(int)bps
               useInterfaceOrientation:(BOOL)useInterfaceOrientation;
    
    // -----------------------------------------------------------------------------
    - (instancetype) initWithVideoSize:(CGSize)videoSize
                             frameRate:(int)fps
                               bitrate:(int)bps
               useInterfaceOrientation:(BOOL)useInterfaceOrientation
                           cameraState:(VCCameraState) cameraState;
    
    // -----------------------------------------------------------------------------
    - (instancetype) initWithVideoSize:(CGSize)videoSize
                             frameRate:(int)fps
                               bitrate:(int)bps
               useInterfaceOrientation:(BOOL)useInterfaceOrientation
                           cameraState:(VCCameraState) cameraState
                            aspectMode:(VCAspectMode) aspectMode;
    

    videoSize is the desired resolution of your encoded video.

    fps is the video frame rate. Depending on your server settings, you will probably want to make this 30, maybe even 60 if your server supports it.

    bps is the video bitrate, in bits per second.

    useInterfaceOrientation is used to notify the encoder of device orientation changes.. Passing in YES will tell the encoder to rotate the video when you rotate the device.

    cameraState is used to start the encoder with your desired camera source. The VCCameraState enum has two values: VCCameraStateFront and VCCameraStateBack.

    aspectMode is used to set the aspect mode of your preview view. VCAspectMode has two values: VCAspectModeFit (your sessions video preview should "aspect fit" into its parent view, and VCAscpectModeFill (scale your video preview to fill its parent view).

    Use one of these initializers to create a new session. Make sure you retain the session after initialization as a property or ivar. After initialization, add the sessions's previewView property as a subview to some UIView on your view controller.

    To connect to a server, use the startRtmpSessionWithURL: method on your session. To stop your encoder, call endRtmpSession.

    Your full setup will probably look something like this:

    @property (weak, nonatomic) IBOutlet UIView *previewView; // This is a UIView on your view controller
    @property (weak, nonatomic) VCSimpleSession* session;
    
    // ...
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.session = [[VCSimpleSession alloc] initWithVideoSize:CGSizeMake(1280, 720) frameRate:30 bitrate:1000000 useInterfaceOrientation:NO];
    
        [self.previewView addSubview:_session.previewView];
        [self.previewView bringSubviewToFront:self.session.previewView]
        [_session startRtmpSessionWithURL:@"rtmp://192.168.1.151/live" andStreamKey:@"myStream"];
    }
    

    When you're done:

    [self.session endRtmpSession];
    

    I've done some work on the VideoCore sample app, you should check it out the sample view controller.