CM OPUS Encode

CM OPUS Encode

The cmOpusEncode is used to encode the audio/x-raw(PCM) data to audio/x-opus(OPUS) data by the OPUS library.


API Instructions

create/destroy

gpointer cm_opus_encode_create(); void cm_opus_encode_destroy(gpointer hd);

start/stop

void cm_opus_encode_start(gpointer hd); void cm_opus_encode_stop(gpointer hd);

parameters

/* @type Supports 2049(generic), 2048(voice), 2051(low delay), default is 2048 */ void cm_opus_encode_set_audio_type(gpointer hd, int type); /* @br:4000-65000, default 64000 */ void cm_opus_encode_set_bitrate(gpointer hd, int br); /* @type Supports 0(cbr), 1(vbr), 2(constrained br), default is 1 */ void cm_opus_encode_set_bitrate_type(gpointer hd, int type); /* @size Duration per frames in ms, supports 2(2.5), 5, 10, 20, 40, 60, default is 10 */ void cm_opus_encode_set_frame_size(gpointer hd, int size);

data

The encoder needs to know the actual format of the input before encoding, so must ensure the source is ready to be linked(the media info is ready).

/* @src An audio/x-raw source(cmAlsaSrc, ...) */ void cm_opus_encode_link_to_source(gpointer hd, gpointer src); void cm_opus_encode_unlink(gpointer hd); /* @ cb data callback to get data */ void cm_opus_encode_set_data_callback(gpointerhd, cm_data_cb_ptrcb, gpointeruser_data); void cm_opus_encode_remove_data_callback(gpointerhd, cm_data_cb_ptrcb, gpointeruser_data);

others

/* @return Encode output format, "audio/x-opus,..." */ const char* cm_opus_encode_get_caps_str(gpointer hd); GstElement* cm_opus_encode_get_bin(gpointer hd);

Demo

Opus PV to PB Demo

Structure

Get raw data and encode to opus, then decode them and send to speaker to play.

image-20240520-071811.png

Main codes

//The encode data will be send to here, push it to the decode static GstFlowReturn _out_data_cb (GstBuffer *buffer, gpointer user_data) { if(opus_dec){ //Push opus data to decode cm_opus_decode_push_buffer(opus_dec, buffer); } return GST_FLOW_OK; } static gboolean _main_loop(gpointer arg) { if(!alsa_src) { //Create source and start alsa_src = cm_audio_alsa_source_create("hw:0,0"); cm_audio_alsa_source_start(alsa_src); } //Wait source media info ready if(!opus_enc && cm_audio_alsa_source_is_ready(alsa_src)){ //Create opus dec opus_enc = cm_opus_encode_create(); //Link opus encode to alsa source cm_opus_encode_link_to_source(opus_enc, alsa_src); //Set callback to get opus data cm_opus_encode_set_data_callback(opus_enc, _out_data_cb, opus_enc); //Start encode cm_opus_encode_start(opus_enc); } if(!opus_dec){ //Create opus decode and start opus_dec = cm_opus_decode_create(); cm_opus_decode_start(opus_dec); } //Wait opus decode prase media info done if(!alsa_sink && cm_opus_decode_is_ready(opus_dec)){ //Create sink alsa_sink = cm_audio_alsa_sink_create("hw:0,0"); //Link alsa sink to opus decode cm_audio_alsa_sink_link_to_source(alsa_sink, opus_dec); cm_audio_alsa_source_start(alsa_sink); }

For more details please refer to the demo file.

Test result

The speaker can hear the sound from the microphone.

image-20240520-071916.png