Skip to end of metadata
Go to start of metadata

Example of iOS application for stream recording

This streamer can be used to publish and record WebRTC video stream on Web Call Server.

On the screenshot below (from left to right)

  • a stream is being published
  • stream publication is stopped and connection to server is closed

In the URL specified in the input field

  • is the address of the WCS server
  • testStream is the stream name

Above the input field video from the camera is displayed.
When publication is stopped, download link for the recording of the published stream is displayed.

Work with code of the example

To analyze the code, let's take StreamRecording example, which can be downloaded with build 2.5.2.

View class for the main view of the application: ViewController (header file ViewController.h; implementation file ViewController.m).

1. Import of API. code

#import <FPWCSApi2/FPWCSApi2.h>

2. Session creation and connection to server.

FPWCSApi2 createSession, FPWCSApi2Session connect code

The options include:

  • URL of WCS server
  • appKey of internal server-side application (defaultApp)
- (FPWCSApi2Session *)connect {
    FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
    url =[[NSURL alloc] initWithString:_connectUrl.text];
    options.urlServer = [NSString stringWithFormat:@"%@://%@:%@", url.scheme,, url.port];
    streamName = [url.path.stringByDeletingPathExtension stringByReplacingOccurrencesOfString: @"/" withString:@""];
    options.appKey = @"defaultApp";
    NSError *error;
    FPWCSApi2Session *session = [FPWCSApi2 createSession:options error:&error];
    [session connect];
    return session;

3. Receiving the event confirming successful connection.

ViewController onConnected code

On this event, ViewController publishStream method is called to publish the stream

- (void)onConnected:(FPWCSApi2Session *)session {
    [self publishStream];

4. Stream publishing.

FPWCSApi2Session createStream, FPWCSApi2Stream publish code

Object with next stream options is passed to createStream method:

  • stream name
  • view to display video
  • 'true' for parameter 'record' to enable stream recording
  • video constraints for iPad

- (FPWCSApi2Stream *)publishStream {
    FPWCSApi2Session *session = [FPWCSApi2 getSessions][0];
    FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init]; = streamName;
    options.display = _remoteDisplay;
    options.record = true;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
        options.constraints = [[FPWCSApi2MediaConstraints alloc] initWithAudio:YES videoWidth:640 videoHeight:480 videoFps:15];
    NSError *error;
    FPWCSApi2Stream *stream = [session createStream:options error:&error];
    if(![stream publish:&error]) {
        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Failed to publish"
        UIAlertAction* okButton = [UIAlertAction
                                   handler:^(UIAlertAction * action) {
                                       [session disconnect];
        [alert addAction:okButton];
        [self presentViewController:alert animated:YES completion:nil];
    return stream;

5. Receiving the event confirming successful stream publishing.

ViewController onPublishing code

When the stream is published, FPWCSApi2Stream method getRecordName is used to get the filename of the stream recording

- (void)onPublishing:(FPWCSApi2Stream *)stream {
    [_startButton setTitle:@"STOP" forState:UIControlStateNormal];
    [self changeViewState:_startButton enabled:YES];
    recordName = [stream getRecordName];

6. Disconnection.

FPWCSApi2Session disconnect code

- (void)startButton:(UIButton *)button {
    [self changeViewState:button enabled:NO];
    if ([button.titleLabel.text isEqualToString:@"STOP"]) {
        if ([FPWCSApi2 getSessions].count) {
            FPWCSApi2Session *session = [FPWCSApi2 getSessions][0];
            NSLog(@"Disconnect session with server %@", [session getServerUrl]);
            [session disconnect];
        } else {
            NSLog(@"Nothing to disconnect");
            [self onDisconnected];
    } else {
        [self changeViewState:_connectUrl enabled:NO];
        [self connect];

7. Receiving the event confirming successful disconnection.

ViewController onDisconnected code

On this event, the record file download link is formed, and ViewController playVideo is called to play the record

- (void)onDisconnected {
    [self changeViewState:_connectUrl enabled:YES];
    [self onUnpublished];
    if (url && recordName) {
//        NSString *urlString = @"";
        NSString *urlString = [NSString stringWithFormat:@"http://%@:9091/client/records/%@",, recordName];
        _recordLink.text = urlString;
        [self playVideo: urlString];

8. Record playback

AVPlayer play code

- (void)playVideo:(NSString *)urlString {
    NSURL *url = [NSURL URLWithString:urlString];
    AVURLAsset *movieAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    [movieAsset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    _player = [AVPlayer playerWithPlayerItem:playerItem];
    _playerViewController.player = _player;
    [_player play];
  • No labels