how to play audio output to a device using ffmpeg's avdevice library?

Viewed 296

How can I use the ffmpeg's avdevices c library to output audio to an audio device (specifically alsa). All I could find is its doxygen and the only useful thing I was able to take out of it is, quote

"the (de)muxers in libavdevice are of the AVFMT_NOFILE type (they use their own I/O functions). The filename passed to avformat_open_input() often does not refer to an actually existing file, but has some special device-specific meaning - e.g. for xcbgrab it is the display name."

but I don't understand where do I specify AVFMT_NOFILE and where do I specify which device I want use.I see how I can get an 'AVOutputFormat' pointer but then what do I do with it?

update: so now i found the function 'avformat_alloc_output_context2' so my code looks like this:

AVPacket pkt = av_packet_alloc();
avformat_alloc_output_context2(&ofmt_ctx, NULL, "alsa", NULL);
avformat_new_stream(ofmt_ctx, NULL);

while(av_read_frame(fmt_ctx, pkt) == 0){
    av_write_frame(ofmt_ctx, pkt);
}

fmt_ctx is the input file's AVFormatContext.

but I am still getting an error '[alsa @ 0x555daf361140] Invalid packet stream index: 1' what am I missing?

1 Answers

The OP is almost two years ago... This is a rabbit hole because of the complexity of an open sourced C language based software released 14+ years ago with 1k+ contributors that supports multiple operating systems through layers of abstraction, one of those being the AVFMT_NOFILE type. There is a reason why audio and video on operating systems take time to get it right.

I'm looking into this now and my suggestion is to start here:

https://github.com/FFmpeg/FFmpeg/tree/master/doc/examples

These are demo examples on specific functionality. A quick search on AVFMT_NOFILE brought up this:

https://github.com/FFmpeg/FFmpeg/blob/b6aeee2d8bedcd8cfc6aa91cc124c904a78adb1e/doc/examples/remuxing.c#L132

For examples on finding devices depending on OS, check this link:

https://github.com/rbrisita/codio-sui/wiki/FFmpeg#list-devices

Share your findings if you can!

Godspeed.

Related