来自ffmpeg的Http Streaming,如何获取有序数据包?

时间:2011-09-02 13:51:13

标签: http audio streaming ffmpeg

我正在尝试制作http流媒体节目 所以我在this处遵循此代码 但是,当我解码时,只解码一帧 我想我需要回电功能 你知道怎么做回电功能吗?
我知道'asf'包的回叫功能就像int read_data(void *opaque, char *buf, int buf_size)

但其他格式(mp3,ogg,aac,..)不起作用..

请帮帮我。

非常感谢任何建议或评论。

#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>

int main(int argc, char **argv)
{
        static AVInputFormat *file_iformat;
        static AVFormatContext *pFormatCtx;
        AVFormatParameters params;

        AVCodecContext *pCodecCtx;
        AVCodec *pCodec;

        const char url[] = "http://listen.radionomy.com/feelingfloyd";

        avcodec_register_all();
        avdevice_register_all();
        av_register_all();

        av_log_set_level(AV_LOG_VERBOSE);

        file_iformat = av_find_input_format("mp3"); /* mp3 demuxer */
        if (!file_iformat)
        {
                fprintf(stderr, "Unknown input format: %s\n", &url[0]);
                exit(1);
        }

        //file_iformat->flags |= AVFMT_NOFILE; /* ??? */
        params.prealloced_context = 0;

        if (av_open_input_file(&pFormatCtx, &url[0], file_iformat, 0, &params) != 0)
        {
                fprintf(stderr, "err 1\n");
                exit(2);
        }

        /* poulates AVFormatContex structure */
        if (av_find_stream_info(pFormatCtx) < 0)
        {
                fprintf(stderr, "err 2\n");
        }

        /* sanity check (1 stream) */
        if (pFormatCtx->nb_streams != 1 &&
                        pFormatCtx->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
        {
                fprintf(stderr, "err 3\n");
        }

        pCodecCtx = pFormatCtx->streams[0]->codec;

        /* find decoder for input audio stream */
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
        if (pCodec == NULL)
        {
                fprintf(stderr, "err 4: unsupported codec\n");
        }

        if (pCodec->capabilities & CODEC_CAP_TRUNCATED)
                pCodecCtx->flags |= CODEC_FLAG_TRUNCATED;

        if (avcodec_open(pCodecCtx, pCodec) < 0)
        {
                fprintf(stderr, "err 5\n");
        }

        {
                uint8_t *pAudioBuffer;
                AVPacket pkt;

                int ret;
                int data_size = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;

                av_init_packet(&pkt);
                //pkt.data=NULL;
                //pkt.size=0;
                //pkt.stream_index = 0;

                pAudioBuffer = av_malloc(data_size * sizeof(int16_t));

                while (av_read_frame(pFormatCtx, &pkt) == 0) {
                        //data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
                        ret = avcodec_decode_audio3(pFormatCtx->streams[pkt.stream_index]->codec,
                                        (int16_t *)pAudioBuffer, &data_size, &pkt);

                        /* got an error (-32) here */
                        if (ret < 0) {
                                av_strerror(ret, (char *)pAudioBuffer, data_size);
                                fprintf(stderr, "err 6 (%s)\n", pAudioBuffer);
                                break;
                        }

                        printf("size=%d, stream_index=%d |ret=%d data_size=%d\n",
                                        pkt.size, pkt.stream_index, ret, data_size);
                        av_free_packet(&pkt);
                }

                av_free(pAudioBuffer);
        }

        avcodec_close(pCodecCtx);
        av_close_input_file(pFormatCtx);

        return 0;
}

1 个答案:

答案 0 :(得分:0)

我使用av_open_input_file来解决这个问题 当我制作一个播放http音频流的iphone应用时,我遇到了这个问题。而上面的代码不起作用。只播放了一些音频帧。这也意味着有这么多的缓冲 但是使用了iphone音频回调功能和大音频缓冲后,工作正常 那些对最终代码感到好奇的人给我发了一条消息。

相关问题