[ English | 简体中文 ]
Media Recorder API
Audio/video recording functionality, supporting file recording and buffer mode.
Header: #include <media_recorder.h>
openvela Implementation Notes
- Synchronous/Asynchronous dual model:
media_recorder_*(synchronous) andmedia_uv_recorder_*(asynchronous, based on libuv) - Output methods: Two destination types are supported
- Local file: Specify the path via
prepare(url) - Byte stream buffer: Read via
read_data, or obtain the underlying socket viaget_socket
- Local file: Specify the path via
- Photo capture: In addition to audio/video recording, provides
take_picture/start_picture/finish_pictureimage capture interfaces - Event callback: Register an event listener via
set_event_callback
Synchronous Interface - Lifecycle
media_recorder_open
void* media_recorder_open(const char* params);
Opens a recorder with the specified source type.
Parameters:
paramsSource type constant, typicallyMEDIA_SOURCE_MIC.
Returns:
Returns the recorder handle on success; returns NULL on failure.
media_recorder_close
int media_recorder_close(void* handle);
Closes the recorder.
Parameters:
handleRecorder handle.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_set_event_callback
int media_recorder_set_event_callback(void* handle, void* cookie, media_event_callback event_cb);
Sets the recorder event callback, which is invoked on state changes or other events of interest.
Parameters:
handleRecorder handle.cookieUser data passed back whenevent_cbis triggered.event_cbEvent callback function.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_prepare
int media_recorder_prepare(void* handle, const char* url, const char* options);
Prepares the recorder.
Parameters:
handleRecorder handle.urlResource path, supporting two modes: 1. URL mode:urlis a local file path; the framework opens and records to that path. 2. BUFFER mode:urlisNULL; the caller must continuously receive data viamedia_recorder_read_data()ormedia_recorder_get_socket()+read().optionsAdditional configuration parameters, including: format (container format, e.g. opus/wav), sample_rate (sample rate), ch_layout (channel layout), b (bitrate, e.g."23900"), vbr (0=constant bitrate, 1=variable bitrate), level (encoding complexity, 0-10, default 10). Example:"format=opusraw:sample_rate=16000:ch_layout=mono:b=32000:vbr=0:level=1".
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_reset
int media_recorder_reset(void* handle);
Resets the recorder.
Parameters:
handleRecorder handle.
Returns:
Returns 0 on success, or a negative errno on failure.
Synchronous Interface - Data Stream
media_recorder_read_data
ssize_t media_recorder_read_data(void* handle, void* data, size_t len);
Reads recorded data from the recorder.
Parameters:
handleRecorder handle.dataBuffer address.lenBuffer length.
Returns:
Returns the number of bytes read on success, or a negative errno on failure.
media_recorder_get_sockaddr
int media_recorder_get_sockaddr(void* handle, struct sockaddr_storage* addr);
Gets the socket address information for buffer mode.
Parameters:
handleRecorder handle.addrSocket address information output.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_get_socket
int media_recorder_get_socket(void* handle);
Gets the socket file descriptor for reading.
Parameters:
handleRecorder handle.
Returns:
Returns the socket file descriptor on success, or a negative errno on failure.
media_recorder_close_socket
void media_recorder_close_socket(void* handle);
Closes the socket file descriptor when the recorder finishes receiving data.
Parameters:
handleRecorder handle.
Synchronous Interface - Recording Control
media_recorder_start
int media_recorder_start(void* handle);
Starts or resumes recording from the audio source.
Parameters:
handleRecorder handle.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_pause
int media_recorder_pause(void* handle);
Pauses the recorder after capture has started.
Parameters:
handleRecorder handle.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_stop
int media_recorder_stop(void* handle);
Stops recording.
Parameters:
handleRecorder handle.
Returns:
Returns 0 on success, or a negative errno on failure.
Synchronous Interface - Properties
media_recorder_set_property
int media_recorder_set_property(void* handle, const char* target, const char* key, const char* value);
Sets a property on the recorder path.
Parameters:
handleRecorder handle.targetTarget filter name.keyProperty key to set.valueProperty value to set.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_get_property
int media_recorder_get_property(void* handle, const char* target, const char* key, char* value, int value_len);
Gets a property from the recorder path.
Parameters:
handleRecorder handle.targetTarget filter name.keyProperty key to query.valueOutput buffer.value_lenLength of the output buffer.
Returns:
Returns 0 on success, or a negative errno on failure.
Synchronous Interface - Image Capture
media_recorder_take_picture
int media_recorder_take_picture(char* params, char* filename, size_t number);
Takes a picture from the camera.
Parameters:
paramsCamera open path parameters.filenameStorage path for the new image.numberNumber of pictures to take.
Returns:
Returns 0 on success, or a negative errno on failure.
media_recorder_start_picture
void* media_recorder_start_picture(char* params, char* filename, size_t number, media_event_callback event_cb, void* cookie);
Starts taking a picture, including open, set_event_callback, prepare, and start operations.
Parameters:
paramsOpen parameters.filenameStorage path for the new image.numberNumber of pictures to take.event_cbCallback function for handling status feedback.cookieUser private data.
Returns:
Returns a valid handle on success, or NULL on failure.
media_recorder_finish_picture
int media_recorder_finish_picture(void* handle);
Closes the recorder when picture taking is finished.
Parameters:
handleRecorder handle returned bymedia_recorder_start_picture().
Returns:
Returns 0 on success, or a negative errno on failure.
Asynchronous Interface (libuv-based)
The following interfaces are only available when CONFIG_LIBUV is enabled.
media_uv_recorder_open
void* media_uv_recorder_open(void* loop, const char* source, media_uv_callback on_open, void* cookie);
Opens an asynchronous recorder.
Parameters:
loopTheuv_loop_t*event loop handle of the current thread.sourceSource type.on_openCallback function triggered after open completes.cookieCallback context shared byon_open,on_event, andon_close.
Returns:
Returns the recorder handle on success.
media_uv_recorder_listen
int media_uv_recorder_listen(void* handle, media_event_callback on_event);
Listens for status change events by setting a callback.
Parameters:
handleAsynchronous recorder handle.on_eventEvent callback function, invoked upon notification.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_close
int media_uv_recorder_close(void* handle, media_uv_callback on_close);
Closes the asynchronous recorder.
Parameters:
handleAsynchronous recorder handle.on_closeCallback function triggered after resource release completes.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_prepare
int media_uv_recorder_prepare(void* handle, const char* url, const char* options, media_uv_object_callback on_connection, media_uv_callback on_prepare, void* cookie);
Prepares the destination file.
Parameters:
handleAsynchronous recorder handle.urlDestination path.optionsDestination configuration parameters; seemedia_recorder_preparefor details.on_connectionConnection callback; in BUFFER mode, carries a writableuv_pipe_t.on_prepareResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_start_auto
int media_uv_recorder_start_auto(void* handle, const char* stream, media_uv_callback on_start, void* cookie);
Starts or resumes capturing with an automatic focus request.
Parameters:
handleAsynchronous recorder handle.streamScenario constant defined inmedia_defs.h.on_startResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_start
int media_uv_recorder_start(void* handle, media_uv_callback on_start, void* cookie);
Starts or resumes capturing.
Parameters:
handleAsynchronous recorder handle.on_startResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_pause
int media_uv_recorder_pause(void* handle, media_uv_callback on_pause, void* cookie);
Pauses capturing.
Parameters:
handleAsynchronous recorder handle.on_pauseResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_stop
int media_uv_recorder_stop(void* handle, media_uv_callback on_stop, void* cookie);
Stops capturing and finishes the destination file.
Parameters:
handleAsynchronous recorder handle.on_stopResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_set_property
int media_uv_recorder_set_property(void* handle, const char* target, const char* key, const char* value, media_uv_callback cb, void* cookie);
Sets a property on the recorder.
Parameters:
handleAsynchronous recorder handle.targetTarget filter name.keyProperty key.valueProperty value.cbResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_get_property
int media_uv_recorder_get_property(void* handle, const char* target, const char* key, media_uv_string_callback cb, void* cookie);
Gets a property from the recorder.
Parameters:
handleAsynchronous recorder handle.targetTarget filter name.keyProperty key.cbResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_reset
int media_uv_recorder_reset(void* handle, media_uv_callback on_reset, void* cookie);
Resets the recorder, clearing the current recording to start a new one.
Parameters:
handleAsynchronous recorder handle.on_resetResult callback function.cookieOne-time callback context.
Returns:
Returns 0 on success, or a negative errno on failure.
media_uv_recorder_take_picture
int media_uv_recorder_take_picture(void* loop, char* params, char* filename, size_t number, media_uv_callback on_complete, void* cookie);
Takes a picture from the camera.
Parameters:
loopTheuv_loop_t*event loop handle of the current thread.paramsCamera open path parameters.filenameStorage path for the new image.numberNumber of pictures to take.on_completeResult callback function.cookieUser private data.
Returns:
Returns 0 on success, or a negative errno on failure.