Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg Resource temporarily unavailable

Tags:

c

ffmpeg

libav

opus

I'm trying to encode an audio frame using ffmpeg library and Opus codec but i faced with this error :

Resource temporarily unavailable

My source code :

void encode_audio(uint8_t *frame , int frame_size , void (*onPacket)(uint8_t *packet , int packet_size)){
if(audio_encoder_codec_context != NULL){
    memset(audio_encoder_frame_buffer , 0 , (size_t) audio_encoder_frame_size);
    swr_convert(
            s16_to_flt_resampler,
            &audio_encoder_frame_buffer,
            audio_encoder_frame_size,
            (const uint8_t **) &frame,
            frame_size
    );
    int result = avcodec_send_frame(audio_encoder_codec_context , audio_encoder_frame);
    while(result >= 0){
        result = avcodec_receive_packet(audio_encoder_codec_context , audio_encoder_packet);
        char *a = malloc(1024);
        av_strerror(result , a , 1024);
        printf("%s\n",a);
        if (result == AVERROR(EAGAIN) || result == AVERROR_EOF || result < 0){
            break;
        }
        onPacket(audio_encoder_packet->data , audio_encoder_packet->size);
        av_packet_unref(audio_encoder_packet);
    }
}
}
like image 697
KoLiBer Avatar asked Nov 08 '22 16:11

KoLiBer


1 Answers

This is coming from AVERROR(EAGAIN).

You should send more frames when AVERROR(EAGAIN) is returned.

Docs state this for avcodec_receive_packet

avcodec.h:

/**
 * Read encoded data from the encoder.
 *
 * @param avctx codec context
 * @param avpkt This will be set to a reference-counted packet allocated by the
 *              encoder. Note that the function will always call
 *              av_packet_unref(avpkt) before doing anything else.
 * @return 0 on success, otherwise negative error code:
 *      AVERROR(EAGAIN):   output is not available in the current state - user
 *                         must try to send input
 *      AVERROR_EOF:       the encoder has been fully flushed, and there will be
 *                         no more output packets
 *      AVERROR(EINVAL):   codec not opened, or it is a decoder
 *      other errors: legitimate encoding errors
 */
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);

Depending on the data, FFmpeg can require additional input/data before output can be produced.

More from the docs:

AVERROR(EAGAIN): output is not available in the current state - user must try to send input.

Keep giving it data until you get a return code of 0 (success).

like image 149
gavxn Avatar answered Nov 15 '22 11:11

gavxn