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 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 Video API account. If you don't have a Heroku account, you'll need to sign up (it's free).
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.
To follow this tutorial:
git clone https://github.com/opentok/opentok-ios-sdk-samples.git
cd
to the Archiving subdirectory of the project, and then run
pod install
.
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.
Note: The archiving sample builds upon the Basic-Video-Chat for Objective-C, but there is also a Basic-Video-Chat tutorial in Swift.
The archiving sample adds the following functionality:
Tap the Start recording button.
This starts recording the audio video streams on the OpenTok Media Server.
Tap the Stop recording button to stop the recording.
Tap 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;
_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:
In most applications, control of the archive recording would not be granted to each end-user.
You can have automatically archived sessions, which are recorded whenever a client starts publishing a stream.
You will want to set up an Amazon S3 or Microsoft Azure target for storage of your archive recordings.
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.
When you're finished here, continue building and enhancing your OpenTok application with these helpful resources: