MultiMedia_Recorder

Overview

Defines the Recorder class and provides functions for audio and video recording.

Since:

1.0

Version:

1.0

Summary

Files

File Name

Description

recorder.h

Declares the Recorder class for audio and video recording.

Data Structures

Data Structure Name

Description

OHOS::Media::RecorderCallback

Provides listeners for recording errors and information events.

OHOS::Media::Recorder

Provides functions for audio and video recording.

Enumerations

Enumeration Name

Description

OHOS::Media::VideoSourceType : int32_t { OHOS::Media::VIDEO_SOURCE_SURFACE_YUV = 0, OHOS::Media::VIDEO_SOURCE_SURFACE_RGB, OHOS::Media::VIDEO_SOURCE_SURFACE_ES, OHOS::Media::VIDEO_SOURCE_BUTT }

Enumerates video source types.

OHOS::Media::DataSourceType : int32_t { OHOS::Media::IMAGE = 0 }

Enumerates data source types.

OHOS::Media::OutputFormatType : int32_t { OHOS::Media::FORMAT_DEFAULT = 0, OHOS::Media::FORMAT_MPEG_4, OHOS::Media::FORMAT_TS }

Enumerates output file formats.

OHOS::Media::FileSplitType : int32_t { OHOS::Media::FILE_SPLIT_POST = 0, OHOS::Media::FILE_SPLIT_PRE, OHOS::Media::FILE_SPLIT_NORMAL }

Enumerates file split types.

OHOS::Media::RecorderCallback::RecorderInfoType : int32_t {   OHOS::Media::RecorderCallback::RECORDER_INFO_MAX_DURATION_APPROACHING = 0, OHOS::Media::RecorderCallback::RECORDER_INFO_MAX_FILESIZE_APPROACHING, OHOS::Media::RecorderCallback::RECORDER_INFO_MAX_DURATION_REACHED, OHOS::Media::RecorderCallback::RECORDER_INFO_MAX_FILESIZE_REACHED,   OHOS::Media::RecorderCallback::RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED, OHOS::Media::RecorderCallback::RECORDER_INFO_FILE_SPLIT_FINISHED, OHOS::Media::RecorderCallback::RECORDER_INFO_FILE_START_TIME_MS }

Enumerates recording information types.

OHOS::Media::RecorderCallback::RecorderErrorType : int32_t { OHOS::Media::RecorderCallback::RECORDER_ERROR_UNKNOWN = 0 }

Enumerates recording error types.

Functions

Function Name

Description

OHOS::Media::RecorderCallback::OnError (int32_t errorType, int32_t errorCode)=0

virtual void 

Called when an error occurs during recording. This callback is used to report recording errors.

OHOS::Media::RecorderCallback::OnInfo (int32_t type, int32_t extra)=0

virtual void 

Called when an information event occurs during recording. This callback is used to report recording information.

OHOS::Media::Recorder::SetVideoSource (VideoSourceType source, int32_t &sourceId)

int32_t 

Sets a video source for recording.

OHOS::Media::Recorder::SetVideoEncoder (int32_t sourceId, VideoCodecFormat encoder)

int32_t 

Sets a video encoder for recording.

OHOS::Media::Recorder::SetVideoSize (int32_t sourceId, int32_t width, int32_t height)

int32_t 

Sets the width and height of the video to record.

OHOS::Media::Recorder::SetVideoFrameRate (int32_t sourceId, int32_t frameRate)

int32_t 

Sets the frame rate of the video to record.

OHOS::Media::Recorder::SetVideoEncodingBitRate (int32_t sourceId, int32_t rate)

int32_t 

Sets the encoding bit rate of the video to record.

OHOS::Media::Recorder::SetCaptureRate (int32_t sourceId, double fps)

int32_t 

Sets the video capture rate.

OHOS::Media::Recorder::GetSurface (int32_t sourceId)

std::shared_ptr< OHOS::Surface

Obtains the surface of the video source.

OHOS::Media::Recorder::SetAudioSource (AudioSourceType source, int32_t &sourceId)

int32_t 

Sets the audio source for recording.

OHOS::Media::Recorder::SetAudioEncoder (int32_t sourceId, AudioCodecFormat encoder)

int32_t 

Sets an audio encoder for recording.

OHOS::Media::Recorder::SetAudioSampleRate (int32_t sourceId, int32_t rate)

int32_t 

Sets the audio sampling rate for recording.

OHOS::Media::Recorder::SetAudioChannels (int32_t sourceId, int32_t num)

int32_t 

Sets the number of audio channels to record.

OHOS::Media::Recorder::SetAudioEncodingBitRate (int32_t sourceId, int32_t bitRate)

int32_t 

Sets the encoding bit rate of the audio to record.

OHOS::Media::Recorder::SetMaxDuration (int32_t duration)

int32_t 

Sets the maximum duration of a recorded file, in seconds.

OHOS::Media::Recorder::SetOutputFormat (OutputFormatType format)

int32_t 

Sets the output file format.

OHOS::Media::Recorder::SetOutputPath (const string &path)

int32_t 

Sets the output file path.

OHOS::Media::Recorder::SetOutputFile (int32_t fd)

int32_t 

Sets the file descriptor (FD) of the output file.

OHOS::Media::Recorder::SetNextOutputFile (int32_t fd)

int32_t 

Sets the FD of the next output file.

OHOS::Media::Recorder::SetMaxFileSize (int64_t size)

int32_t 

Sets the maximum size of a recorded file, in bytes.

OHOS::Media::Recorder::SetRecorderCallback (const std::shared_ptr< RecorderCallback > &callback)

int32_t 

Registers a recording listener.

OHOS::Media::Recorder::Prepare ()

int32_t 

Prepares for recording.

OHOS::Media::Recorder::Start ()

int32_t 

Starts recording.

OHOS::Media::Recorder::Pause ()

int32_t 

Pauses recording.

OHOS::Media::Recorder::Resume ()

int32_t 

Resumes recording.

OHOS::Media::Recorder::Stop (bool block)

int32_t 

Stops recording.

OHOS::Media::Recorder::Reset ()

int32_t 

Resets the recording.

OHOS::Media::Recorder::Release ()

int32_t 

Releases recording resources.

OHOS::Media::Recorder::SetFileSplitDuration (FileSplitType type, int64_t timestamp, uint32_t duration)

int32_t 

Manually splits a video.

OHOS::Media::Recorder::SetParameter (int32_t sourceId, const Format &format)

int32_t 

Sets an extended parameter for recording, for example, RCORDER_PRE_CACHE_DURATION.

Details

Enumeration Type Documentation

DataSourceType

  1. enum [OHOS::Media::DataSourceType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#gaccc05bb178cecd760369ea096dceae4c) : int32_t

Description:

Enumerates data source types.

Enumerator

Description

IMAGE 

Image data source

FileSplitType

  1. enum [OHOS::Media::FileSplitType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga8759c7e5a74964a584a716f4ec0b7edb) : int32_t

Description:

Enumerates file split types.

Enumerator

Description

FILE_SPLIT_POST 

Delayed/Backward split

FILE_SPLIT_PRE 

Advanced/Forward split

FILE_SPLIT_NORMAL 

Normal split

OutputFormatType

  1. enum [OHOS::Media::OutputFormatType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga3dfe2e61369bf1c081ce569e235354df) : int32_t

Description:

Enumerates output file formats.

Enumerator

Description

FORMAT_DEFAULT 

Default format

FORMAT_MPEG_4 

MPEG4 format

FORMAT_TS 

TS format

RecorderErrorType

  1. enum [OHOS::Media::RecorderCallback::RecorderErrorType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga5132172c298fc1497d12040b6bd511cf) : int32_t

Description:

Enumerates recording error types.

Enumerator

Description

RECORDER_ERROR_UNKNOWN 

Unknown error

RecorderInfoType

  1. enum [OHOS::Media::RecorderCallback::RecorderInfoType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga0db5cf9cc68d4b468e921a563248ffe0) : int32_t

Description:

Enumerates recording information types.

Enumerator

Description

RECORDER_INFO_MAX_DURATION_APPROACHING 

The recording duration is reaching the threshold specified by SetMaxDuration. This type of information is reported when only one second or 10% is left to reach the allowed duration.

RECORDER_INFO_MAX_FILESIZE_APPROACHING 

The recorded file size is reaching the threshold specified by SetMaxFileSize. This type of information is reported when only 100 KB or 10% is left to reach the allowed size.

RECORDER_INFO_MAX_DURATION_REACHED 

The threshold specified by SetMaxDuration is reached, and the recording ends. Before calling SetOutputFile, you must close the file.

RECORDER_INFO_MAX_FILESIZE_REACHED 

The threshold specified by SetMaxFileSize is reached, and the recording ends. Before calling SetOutputFile, you must close the file.

RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED 

Recording started for the next output file.

RECORDER_INFO_FILE_SPLIT_FINISHED 

Manual file split completed.

RECORDER_INFO_FILE_START_TIME_MS 

The start time position of the recording file is not supported.

VideoSourceType

  1. enum [OHOS::Media::VideoSourceType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#gad252d27f9ce4b6ae0756bfeaa5f34490) : int32_t

Description:

Enumerates video source types.

Enumerator

Description

VIDEO_SOURCE_SURFACE_YUV 

YUV video data provided through Surface

VIDEO_SOURCE_SURFACE_RGB 

RGB video data provided through Surface

VIDEO_SOURCE_SURFACE_ES 

Raw encoded data provided through Surface

VIDEO_SOURCE_BUTT 

Invalid value

Function Documentation

GetSurface()

  1. std::shared_ptr<[OHOS::Surface]($api-api-SmartVision-Devices-OHOS-Surface.md)> OHOS::Media::Recorder::GetSurface (int32_t sourceId)

Description:

Obtains the surface of the video source.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.

Returns:

Returns the pointer to the surface.

OnError()

  1. virtual void OHOS::Media::RecorderCallback::OnError (int32_t errorType, int32_t errorCode )

Description:

Called when an error occurs during recording. This callback is used to report recording errors.

Parameters:

Name

Description

errorType Indicates the error type. For details, see RecorderErrorType.
errorCode Indicates the error code.

OnInfo()

  1. virtual void OHOS::Media::RecorderCallback::OnInfo (int32_t type, int32_t extra )

Description:

Called when an information event occurs during recording. This callback is used to report recording information.

Parameters:

Name

Description

type Indicates the information type. For details, see RecorderInfoType.
extra Indicates other information, for example, the start time position of a recording file.

Pause()

  1. int32_t OHOS::Media::Recorder::Pause ()

Description:

Pauses recording.

After Start is called, you can call this function to pause recording. The audio and video source streams are not paused, and source data is discarded.

Returns:

Returns SUCCESS if the recording is paused; returns an error code defined in media_errors.h otherwise.

Prepare()

  1. int32_t OHOS::Media::Recorder::Prepare ()

Description:

Prepares for recording.

This function must be called before Start.

Returns:

Returns SUCCESS if the preparation is successful; returns an error code defined in media_errors.h otherwise.

Release()

  1. int32_t OHOS::Media::Recorder::Release ()

Description:

Releases recording resources.

Returns:

Returns SUCCESS if recording resources are released; returns an error code defined in media_errors.h otherwise.

Reset()

  1. int32_t OHOS::Media::Recorder::Reset ()

Description:

Resets the recording.

After the function is called, add a recording source by calling SetVideoSource or SetAudioSource, set related parameters, and call Start to start recording again after Prepare is called.

Returns:

Returns SUCCESS if the recording is reset; returns an error code defined in media_errors.h otherwise.

Resume()

  1. int32_t OHOS::Media::Recorder::Resume ()

Description:

Resumes recording.

You can call this function to resume recording after Pause is called.

Returns:

Returns SUCCESS if the recording is resumed; returns an error code defined in media_errors.h otherwise.

SetAudioChannels()

  1. int32_t OHOS::Media::Recorder::SetAudioChannels (int32_t sourceId, int32_t num )

Description:

Sets the number of audio channels to record.

This function must be called after SetAudioSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the audio source ID, which can be obtained from SetAudioSource.
num Indicates the number of audio channels to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetAudioEncoder()

  1. int32_t OHOS::Media::Recorder::SetAudioEncoder (int32_t sourceId, [AudioCodecFormat]($api-api-SmartVision-Devices-MultiMedia_MediaCommon.md#gaa4ea6f314644ed287e0704be26c768b7) encoder )

Description:

Sets an audio encoder for recording.

If this function is not called, the output file does not contain the audio track. This function must be called after SetAudioSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the audio source ID, which can be obtained from SetAudioSource.
encoder Indicates the audio encoder to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetAudioEncodingBitRate()

  1. int32_t OHOS::Media::Recorder::SetAudioEncodingBitRate (int32_t sourceId, int32_t bitRate )

Description:

Sets the encoding bit rate of the audio to record.

This function must be called after SetAudioSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the audio source ID, which can be obtained from SetAudioSource.
bitRate Indicates the audio encoding bit rate, in bit/s.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetAudioSampleRate()

  1. int32_t OHOS::Media::Recorder::SetAudioSampleRate (int32_t sourceId, int32_t rate )

Description:

Sets the audio sampling rate for recording.

This function must be called after SetAudioSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the audio source ID, which can be obtained from SetAudioSource.
rate Indicates the sampling rate of the audio per second.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetAudioSource()

  1. int32_t OHOS::Media::Recorder::SetAudioSource ([AudioSourceType]($api-api-SmartVision-Devices-MultiMedia_MediaCommon.md#gadc3158e093b995ca7b9b6aa32388ccdd) source, int32_t & sourceId )

Description:

Sets the audio source for recording.

If this function is not called, the output file does not contain the audio track.

Parameters:

Name

Description

source Indicates the audio source type. For details, see AudioSourceType.
sourceId Indicates the audio source ID. The value -1 indicates an invalid ID and the setting fails.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetCaptureRate()

  1. int32_t OHOS::Media::Recorder::SetCaptureRate (int32_t sourceId, double fps )

Description:

Sets the video capture rate.

This function must be called after SetVideoSource but before Prepare. It is valid when the video source is YUV or RGB.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.
fps Indicates the rate at which frames are captured per second.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetFileSplitDuration()

  1. int32_t OHOS::Media::Recorder::SetFileSplitDuration ([FileSplitType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga8759c7e5a74964a584a716f4ec0b7edb) type, int64_t timestamp, uint32_t duration )

Description:

Manually splits a video.

This function must be called after Start. After this function is called, the file is split based on the manual split type. After the manual split is complete, the initial split type is used. This function can be called again only after RECORDER_INFO_FILE_SPLIT_FINISHED is reported.

Parameters:

Name

Description

type Indicates the file split type. For details, see FileSplitType.
timestamp Indicates the file split timestamp. This parameter is not supported currently and can be set to -1. The recording module splits a file based on the call time.
duration Indicates the duration for splitting the file.

Returns:

Returns SUCCESS if the video is manually split; returns an error code defined in media_errors.h otherwise.

SetMaxDuration()

  1. int32_t OHOS::Media::Recorder::SetMaxDuration (int32_t duration)

Description:

Sets the maximum duration of a recorded file, in seconds.

This method must be called before Prepare. If the setting is valid, RECORDER_INFO_MAX_DURATION_APPROACHING is reported through OnInfo in the RecorderCallback class when only one second or 10% is left to reach the allowed duration. If the recording output file is set by calling SetOutputFile, call SetNextOutputFile to set the next output file. Otherwise, the current file will be overwritten when the allowed duration is reached.

Parameters:

Name

Description

duration Indicates the maximum recording duration to set. If the value is 0 or a negative number, a failure message is returned. The default duration is 60s.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetMaxFileSize()

  1. int32_t OHOS::Media::Recorder::SetMaxFileSize (int64_t size)

Description:

Sets the maximum size of a recorded file, in bytes.

This function must be called before Prepare. If the setting is valid, RECORDER_INFO_MAX_DURATION_APPROACHING is reported through OnInfo in the RecorderCallback class when only 100 KB or 10% is left to reach the allowed size. If the recording output file is set by calling SetOutputFile, call SetNextOutputFile to set the next output file. Otherwise, when the allowed size is reached, the current file will be overwritten. If MaxDuration is also set by calling SetMaxDuration, MaxDuration or MaxFileSize prevails depending on which of them is first satisfied.

Parameters:

Name

Description

size Indicates the maximum file size to set. If the value is 0 or a negative number, a failure message is returned. By default, the maximum size of a single file supported by the current file system is used as the limit.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetNextOutputFile()

  1. int32_t OHOS::Media::Recorder::SetNextOutputFile (int32_t fd)

Description:

Sets the FD of the next output file.

If SetOutputFile is successful, call this function to set the FD of the next output file after RECORDER_INFO_MAX_DURATION_APPROACHING or RECORDER_INFO_MAX_FILESIZE_APPROACHING is received.

Parameters:

Name

Description

fd Indicates the FD of the next output file.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetOutputFile()

  1. int32_t OHOS::Media::Recorder::SetOutputFile (int32_t fd)

Description:

Sets the file descriptor (FD) of the output file.

This function must be called before Prepare.

Parameters:

Name

Description

fd Indicates the FD of the file.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetOutputFormat()

  1. int32_t OHOS::Media::Recorder::SetOutputFormat ([OutputFormatType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#ga3dfe2e61369bf1c081ce569e235354df) format)

Description:

Sets the output file format.

This function must be called before Prepare.

Parameters:

Name

Description

format Indicates the output file format. For details, see OutputFormatType.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetOutputPath()

  1. int32_t OHOS::Media::Recorder::SetOutputPath (const string & path)

Description:

Sets the output file path.

This function must be called before Prepare and One of them SetOutputFile must be set.

Parameters:

Name

Description

path Indicates the output file path.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetParameter()

  1. int32_t OHOS::Media::Recorder::SetParameter (int32_t sourceId, const [Format]($api-api-SmartVision-Devices-OHOS-Media-Format.md) & format )

Description:

Sets an extended parameter for recording, for example, RCORDER_PRE_CACHE_DURATION.

Parameters:

Name

Description

sourceId Indicates the data source ID. The value -1 indicates all sources.
format Indicates the string key and value. For details, see Format and RCORDER_PRE_CACHE_DURATION.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetRecorderCallback()

  1. int32_t OHOS::Media::Recorder::SetRecorderCallback (const std::shared_ptr< [RecorderCallback]($api-api-SmartVision-Devices-OHOS-Media-RecorderCallback.md) > & callback)

Description:

Registers a recording listener.

This function must be called before Prepare.

Parameters:

Name

Description

callback Indicates the recording listener to register. For details, see RecorderCallback.

Returns:

Returns SUCCESS if the listener is registered; returns an error code defined in media_errors.h otherwise.

SetVideoEncoder()

  1. int32_t OHOS::Media::Recorder::SetVideoEncoder (int32_t sourceId, [VideoCodecFormat]($api-api-SmartVision-Devices-MultiMedia_MediaCommon.md#ga797e6c5e38e23e730eff5bcc41427d7e) encoder )

Description:

Sets a video encoder for recording.

If this function is not called, the output file does not contain the video track. This function must be called after SetVideoSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.
encoder Indicates the video encoder to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetVideoEncodingBitRate()

  1. int32_t OHOS::Media::Recorder::SetVideoEncodingBitRate (int32_t sourceId, int32_t rate )

Description:

Sets the encoding bit rate of the video to record.

This function must be called after SetVideoSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.
rate Indicates the encoding bit rate to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetVideoFrameRate()

  1. int32_t OHOS::Media::Recorder::SetVideoFrameRate (int32_t sourceId, int32_t frameRate )

Description:

Sets the frame rate of the video to record.

This function must be called after SetVideoSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.
frameRate Indicates the frame rate to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetVideoSize()

  1. int32_t OHOS::Media::Recorder::SetVideoSize (int32_t sourceId, int32_t width, int32_t height )

Description:

Sets the width and height of the video to record.

This function must be called after SetVideoSource but before Prepare.

Parameters:

Name

Description

sourceId Indicates the video source ID, which can be obtained from SetVideoSource.
width Indicates the video width to set.
height Indicates the video height to set.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

SetVideoSource()

  1. int32_t OHOS::Media::Recorder::SetVideoSource ([VideoSourceType]($api-api-SmartVision-Devices-MultiMedia_Recorder.md#gad252d27f9ce4b6ae0756bfeaa5f34490) source, int32_t & sourceId )

Description:

Sets a video source for recording.

If this function is not called, the output file does not contain the video track.

Parameters:

Name

Description

source Indicates the video source type. For details, see VideoSourceType.
sourceId Indicates the video source ID. The value -1 indicates an invalid ID and the setting fails.

Returns:

Returns SUCCESS if the setting is successful; returns an error code defined in media_errors.h otherwise.

Start()

  1. int32_t OHOS::Media::Recorder::Start ()

Description:

Starts recording.

This function must be called after Prepare.

Returns:

Returns SUCCESS if the recording is started; returns an error code defined in media_errors.h otherwise.

Stop()

  1. int32_t OHOS::Media::Recorder::Stop (bool block)

Description:

Stops recording.

Parameters:

Name

Description

block Indicates the stop mode. The value true indicates that the processing stops after all caches are processed, and false indicates that the processing stops immediately and all caches are discarded.

Returns:

Returns SUCCESS if the recording is stopped; returns an error code defined in media_errors.h otherwise.

Variable Documentation

RCORDER_PRE_CACHE_DURATION

  1. const string OHOS::Media::RCORDER_PRE_CACHE_DURATION = "pre-cache-duration"

Description:

This constant can be used as the value of Format in SetParameter to configure the duration for storing recorded data in the cache.