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:

  1. Clone the OpenTok iOS sample apps repo on GitHub:
    git clone https://github.com/opentok/opentok-ios-sdk-samples.git
  2. In terminal, cd to the Archiving subdirectory of the project, and then run pod install.
  3. Open your project in Xcode by double-clicking the new Archiving.xcworkspace file in the Archiving directory.
  4. Open the Config.h file, and set the SAMPLE_SERVER_BASE_URL string to the base URL of the server that implements the learning-opentok-php project. This server provides OpenTok session ID and tokens to the iOS sample app. For more information, see Setting up the server.

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

The archiving sample builds upon the Basic-Video-Chat sample, and it adds the following functionality:

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;
    _archiveIndicatorImg.hidden = NO;
    if (SAMPLE_SERVER_BASE_URL) {
        _archiveControlBtn.hidden = NO;
        [_archiveControlBtn setTitle: @"Stop recording" forState:UIControlStateNormal];
        _archiveControlBtn.hidden = NO;
        [_archiveControlBtn removeTarget:self
                                  action:NULL
                        forControlEvents:UIControlEventTouchUpInside];
        [_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);
    _archiveIndicatorImg.hidden = YES;
    if (SAMPLE_SERVER_BASE_URL) {
        _archiveControlBtn.hidden = NO;
        [_archiveControlBtn setTitle: @"View recording" forState:UIControlStateNormal];
        [_archiveControlBtn removeTarget:self
                                  action:NULL
                        forControlEvents:UIControlEventTouchUpInside];
        [_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.

Notes:

For more information on archiving, see the OpenTok archiving developer guide.

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.