Media Stream Recording
PUBLISHED
You can record audio and video from a stream, and control the recording process through various settings. With the stream recorder, live audio and video can be kept on your target.
The main features of the Tizen.Multimedia.StreamRecorder
class include:
- Creating a media packet
You must create a media packet for the stream recording using raw data from the source. The media packet must be created for each buffer captured from the source and passed to the
PushBuffer()
method of theTizen.Multimedia.StreamRecorder
class. - Recording audio and video, and controlling the recording
You can record a stream, pause, stop, and cancel the recording, and push the buffer.
- Managing recording options
You can manage various recording details, such as the bitrate, codec, maximum recording length, and filename:
- You can encode files in various formats:
- Video: MP4 and 3GP
- Audio: AMR, AAC, and WAV
The supported file formats are defined in the Tizen.Multimedia.RecorderFileFormat [1] enumeration.
- You can use various video and audio encoders.
The available video and audio codecs are defined in the Tizen.Multimedia.RecorderVideoCodec [2] and Tizen.Multimedia.RecorderAudioCodec [3] enumerations.
- You can encode files in various formats:
Valid input sources consist of external sources, such as a live buffer passed by the application. Most operations of the stream recorder work synchronously.
The following figure illustrates general stream recorder state changes. Use the stream recorder methods according to pre and post conditions, by following the state changes.
Figure: Stream recorder state changes
Prerequisites
To use the methods and properties of the Tizen.Multimedia.StreamRecorder [4] class, include the Tizen.Multimedia [5] namespace in your application:
using Tizen.Multimedia;
Managing Recording Options
To define recording options for video recording:
-
Create an instance of the Tizen.Multimedia.StreamRecorderOptions [6] class with the output path and file format for the recorded media stream:
var options = new StreamRecorderOptions(SavePath, RecorderFileFormat.Mp4);
You can check which file formats the device supports using the
GetSupportedFileFormats()
method of theTizen.Multimedia.StreamRecorder
class. - Define the video recording options in the
Video
property of theTizen.Multimedia.StreamRecorderOptions
class, using an instance of the Tizen.Multimedia.StreamRecorderVideoOptions [7] class:
options.Video = new StreamRecorderVideoOptions(codec: RecorderVideoCodec.H263, resolution: new Size(1920, 1080), sourceFormat: StreamRecorderVideoFormat.Nv12, frameRate: 30, bitRate: 288000);
To get a list of video codecs the device supports, use the
GetSupportedVideoCodecs()
method of the Tizen.Multimedia.StreamRecorder [4] class. The following example retrieves the first supported codec found:var streamRecorder = new StreamRecorder(); var videoCodec = streamRecorder.GetSupportedVideoCodecs().First();
Note Even if a higher bitrate is set, the recording bitrate is limited by that of the stream buffer pushed.
Similarly, to record an audio stream, define the audio recording options in the Audio
property of the StreamRecorderOptions
class instance, using an instance of the Tizen.Multimedia.StreamRecorderAudioOptions [8] class.
Creating a Media Packet
When the stream recorder is configured, create the media packet using the raw data from the source:
byte[] rawbuffer; /// Data to be passed for recording var videoFormat = new VideoMediaFormat(MediaFormatVideoMimeType.Mpeg4SP, new Size(640, 480)); var mediaPacket = MediaPacket.Create(videoFormat); mediaPacket.Buffer.CopyFrom(rawbuffer, 0, rawbuffer.length);
The media packet must be created for each buffer captured from the source and passed to the PushBuffer()
method of the Tizen.Multimedia.StreamRecorder [4] class when the stream recorder is prepared to record.
Recording a Stream
To record a stream:
- Call the
Prepare()
method of the Tizen.Multimedia.StreamRecorder [4] class with a Tizen.Multimedia.StreamRecorderOptions [6] instance:streamRecorder.Prepare(options);
The stream recorder state changes to
Ready
. - Start recording by calling the
Start()
method:
streamRecorder.Start();
Once the recording starts, if you set the file path to an existing file, the file is removed automatically and replaced with a new one.
You can only call the
Start()
method in theReady
orPaused
state. After starting, the state changes toRecording
.Push the media packet to record audio or video, using the
PushBuffer()
method:streamRecorder.PushBuffer(mediaPacket);
- During the recording, you can pause or stop it:
- To pause recording, use the
Pause()
method:streamRecorder.Pause();
The stream recorder state changes from
Recording
toPaused
.To resume recording, use the
Start()
method.Alternatively, you can stop pushing the stream buffers. In this case, the stream recorder remains in the
Recording
state, and continues waiting for buffers. It creates the same effect as a pause in the recording. - To stop recording and save the result, use the
Commit()
method. The recording result is saved to the file path defined in theTizen.Multimedia.StreamRecorderOptions
instance.
streamRecorder.Commit();
You can only call the
Start()
method in theRecording
orPaused
state. After committing, the state changes toReady
. - To stop recording without saving the result, use the
Cancel()
method.The only difference between this method and the
Commit()
method is that the recording data is not written to the file.streamRecorder.Cancel();
- To pause recording, use the
- When you have finished recording, use the
Unprepare()
method to reset the stream recorder:
streamRecorder.Unprepare();
The stream recorder state changes from
Ready
toIdle
.