Suggestions

close search

Back to Archiving Overview

Archiving: Recording your session to an archive

The code for this section is in the archiving branch of the learning-opentok-ios repo:

git checkout archiving

This page shows the difference between this branch and the basics branch which it was built upon.

Important: You can only archive sessions that use the OpenTok Media Router (sessions with the media mode set to routed). The default learning-opentok-php code used by this tutorial app uses routed sessions.

In the archiving branch adds the following functionality:

  1. Tap the Start recording button.

    This starts recording the audio video streams on the OpenTok Media Server.

  2. Click the Stop recording button to stop the recording.

  3. Click the View recording button to view the recording in the web browser.

The OpenTok archiving API lets you record audio-video streams in a session to MP4 files. You use server-side code to start and stop archive recordings. In the Config.h file, you set the following constant to the base URL of the web service the app calls to start archive recording, stop recording, and play back the recorded video:

#define SAMPLE_SERVER_BASE_URL

If you do not set this string, the Start Recording, Stop Recording, and View Archive buttons will not be available in the app.

When the user clicks the Start Recording and Stop Recording buttons, the app calls the [self startArchive:] and [self stopArchive:] methods. These call web services that call server-side code start and stop archive recordings. (See Setting up the test web service.)

When archive recording starts, the implementation of the [OTSessionDelegate session:archiveStartedWithId:name:] method is called:

- (void)     session:(OTSession*)session
archiveStartedWithId:(NSString *)archiveId
                name:(NSString *)name
{
    NSLog(@"session archiving started with id:%@ name:%@", archiveId, name);
    _archiveId = archiveId;
    _archivingIndicatorImg.hidden = NO;
    [_archiveControlBtn setTitle: @"Stop recording" forState:UIControlStateNormal];
    _archiveControlBtn.hidden = NO;
    [_archiveControlBtn addTarget:self
                           action:@selector(stopArchive)
                 forControlEvents:UIControlEventTouchUpInside];
}

This causes the _archivingIndicatorImg image (defined in the main storyboard) to be displayed. The method stores the archive ID (identifying the archive) to an archiveId property. The method also changes the archiving control button text to change to "Stop recording".

When the user clicks the Stop Recording button, the app passes the archive ID along to the web service that stops the archive recording.

When archive recording stops, the implementation of the [OTSessionDelegate session:archiveStoppedWithId:] method is called:

- (void)     session:(OTSession*)session
archiveStoppedWithId:(NSString *)archiveId
{
    NSLog(@"session archiving stopped with id:%@", archiveId);
    _archivingIndicatorImg.hidden = YES;
    [_archiveControlBtn setTitle: @"View archive" forState:UIControlStateNormal];
    _archiveControlBtn.hidden = NO;
    [_archiveControlBtn addTarget:self
                           action:@selector(loadArchivePlaybackInBrowser)
                 forControlEvents:UIControlEventTouchUpInside];
}

This causes the _archivingIndicatorImg image (defined in the main storyboard) to be displayed. It also changes the archiving control button text to change to "View archive". When the user clicks this button, the [self loadArchivePlaybackInBrowser:] method opens a web page (in Safari) that displays the archive recording.