Ogg Demo#
Overview#
This sample demonstrates a real-time Opus-based audio loopback flow on K230, with Ogg muxing and demuxing inserted in the middle of the pipeline. The program captures audio from AI, encodes it with AENC as Opus, performs Ogg mux and demux entirely in memory, then sends the decoded stream to ADEC and finally plays it through AO.
Ogg muxing: package Opus frames output by
AENCinto Ogg pagesOgg demuxing: extract Opus frames from Ogg pages
Real-time loopback:
AI -> AENC -> Ogg mux -> Ogg demux -> ADEC -> AOExit method: the program keeps running until
Ctrl+C, then triggers signal handling and resource cleanup
Usage#
Code Location#
Sample directory:
/src/rtsmart/examples/mpp/sample_ogg
The current entry file is main.cpp, and the built executable is sample_ogg.elf.
Key Parameters#
Parameter |
Description |
Value |
|---|---|---|
|
audio sample rate |
|
|
audio frames per second |
|
|
intermediate Ogg/Opus buffer size |
|
codec format |
payload type used by |
|
codec channel count |
encode and decode channels |
|
sample precision |
I2S bit width |
|
AI/AO device ID |
default device used by the program |
|
AI/AO/AENC/ADEC channel ID |
default channel used by the program |
|
Run Program#
After startup, the program initializes VB, AI, AO, AENC, ADEC, the Ogg muxer, and the Ogg demuxer, then enters the real-time audio processing loop. Press Ctrl+C to exit.
./sample_ogg.elf
At startup, the program prints messages similar to:
Starting Ogg audio sample program. Press Ctrl+C to exit.
vb_set_config succeeded
Ogg stream muxer initialized successfully.
Code Flow Description#
Initialize VB resources
audio_sample_vb_init()callskd_mpi_vb_set_config()andkd_mpi_vb_init()to initialize the VB system.audio_data_vb_create_pool()creates a private VB pool for the intermediate audio stream used as ADEC input. The sample useskd_mpi_vb_get_block(),kd_mpi_vb_handle_to_phyaddr(), andkd_mpi_sys_mmap()to map the physical buffer into user space.Initialize the Ogg components
init_ogg_muxer()configureskd_ogg_muxer_paramswith16 kHz, mono, and streaming output mode.init_ogg_demuxer()creates the Ogg demuxer used to restore Ogg pages back into Opus frames.Configure audio input, output, and codecs In
audio_sample_ogg(), bothAIandAOare configured in I2S mode with the built-in codec.AIandAOboth use16 bit,16 kHz, andAUDIO_PERSEC_DIV_NUM = 25. The sample createsAENCandADECchannels with payload typeK_PT_OPUS.Build the MPP binding relationships
kd_mpi_sys_bind()binds theAIchannel toAENC.kd_mpi_sys_bind()binds theADECchannel toAO.Perform Ogg muxing and demuxing in memory The main loop gets the encoded Opus stream through
kd_mpi_aenc_get_stream().do_opus_stream()first callskd_ogg_write_frame_ex()to package the Opus frame into an Ogg page. It then callskd_ogg_demuxer_feed_page_ex()to extract the Opus frame back from that page. The extracted Opus data is copied into the preallocatedg_audio_streambuffer and sent to the decoder throughkd_mpi_adec_send_stream(). After processing, the sample callskd_mpi_aenc_release_stream()to release the encoded stream.Exit on signal and clean up resources
main()registersSIGINTandSIGTERM. Once a signal is received,g_runningis set tofalse. After the loop exits, the program unbindsAI/AENCandADEC/AO, then closesAI,AO,AENC, andADEC.cleanup_resources()destroys the Ogg muxer and demuxer, releases the VB block, destroys the VB pool, and callskd_mpi_vb_exit().
Notes#
This sample does not read or write
.oggfiles. All Ogg muxing and demuxing are performed in memory.The sample uses device ID
0and channel ID0by default. If you port it to a different audio path, update the code accordingly.If audio-device initialization fails, the program prints an error and immediately enters the cleanup path.
Tip
For Ogg processing and audio codec related interfaces, read sample_ogg/main.cpp together with the corresponding audio API documentation.
