Suggestions

close search

Back to Tutorials

Archiving Tutorial (iOS)

Overview

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.

Setting up the server

In order to archive OpenTok sessions, you need to have a server set up. There are many ways to implement archiving with a server, but for this tutorial we'll be quick-launching a simple PHP server.

To launch the server, simply click the Heroku button below, at which point you'll be sent to Heroku's website and prompted for your OpenTok API Key and API Secret — you can get these values on your project page in your TokBox Account. If you don't have a Heroku account, you'll need to sign up (it's free).

Deploy

Want to explore the code? The button above launches server code from the learning-opentok-php GitHub repo. Visit the repo to review the code as well as additional documentation — you can even fork the repo and make changes before deploying.

Setting up your project

To follow this tutorial, clone OpenTok's iOS sample apps repo on GitHub:

git clone https://github.com/opentok/opentok-ios-sdk-samples.git

Then open the Archiving in XCode.

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.

Exploring the code

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.

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.

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.

Congratulations! You've finished the Archiving Tutorial for iOS.
You can continue to play with and adjust the code you've developed here, or check out the Next Steps below. For more information on archiving, see the OpenTok archiving developer guide.