FFMPEG +SDL

yuanguojun123 2017-03-14 09:52:30
打开音频:int audio_stream_component_open(VideoState *is, int stream_index)
{
AVFormatContext *ic = is->ic;
AVCodecContext *codecCtx;
AVCodec *codec;
SDL_AudioSpec wanted_spec, spec;
int64_t wanted_channel_layout = 0;
int wanted_nb_channels;
const int next_nb_channels[] = { 0, 0, 1, 6, 2, 6, 4, 6 };
if (stream_index < 0 || stream_index >= ic->nb_streams) {
return -1;
}
codecCtx = ic->streams[stream_index]->codec;
wanted_nb_channels = codecCtx->channels;
if (!wanted_channel_layout
|| wanted_nb_channels
!= av_get_channel_layout_nb_channels(
wanted_channel_layout)) {
wanted_channel_layout = av_get_default_channel_layout(
wanted_nb_channels);
wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
}
wanted_spec.channels = av_get_channel_layout_nb_channels(
wanted_channel_layout);
wanted_spec.freq = codecCtx->sample_rate;
if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
return -1;
}
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.silence = 0;
wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = is;
if(SDL_OpenAudio(&wanted_spec, &spec) < 0)
{
return 0;
}
if (spec.format != AUDIO_S16SYS) {
return -1;
}
if (spec.channels != wanted_spec.channels) {
wanted_channel_layout = av_get_default_channel_layout(spec.channels);
if (!wanted_channel_layout) {
return -1;
}
}
is->audio_hw_buf_size = spec.size;
is->audio_src_fmt = is->audio_tgt_fmt = AV_SAMPLE_FMT_S16;
is->audio_src_freq = is->audio_tgt_freq = spec.freq;
is->audio_src_channel_layout = is->audio_tgt_channel_layout =
wanted_channel_layout;
is->audio_src_channels = is->audio_tgt_channels = spec.channels;
codec = avcodec_find_decoder(codecCtx->codec_id);
if (!codec || (avcodec_open2(codecCtx, codec, NULL) < 0)) {
fprintf(stderr,"Unsupported codec!\n");
return -1;
}
ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
switch (codecCtx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audio_st = ic->streams[stream_index];
is->audio_buf_size = 0;
is->audio_buf_index = 0;
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
packet_queue_init(&is->audioq);
SDL_PauseAudio(0);
break;
default:
break;
}
return 0;
}
回调函数:static void audio_callback(void *userdata, Uint8 *stream, int len) {
VideoState *is = (VideoState *) userdata;
int len1, audio_data_size;
double pts;
SDL_memset(stream, 0, len);
while (len > 0) {
if (is->audio_buf_index >= is->audio_buf_size) {
audio_data_size = audio_decode_frame(is, &pts);
if (audio_data_size < 0) {
is->audio_buf_size = 1024;
if (is->audio_buf == NULL) return;
memset(is->audio_buf, 0, is->audio_buf_size);
} else {
is->audio_buf_size = audio_data_size;
}
is->audio_buf_index = 0;
}
len1 = is->audio_buf_size - is->audio_buf_index;
if (len1 > len) {
len1 = len;
}
if (is->audio_buf == NULL) return;
memcpy(stream, (uint8_t *) is->audio_buf + is->audio_buf_index, len1);
len -= len1;
stream += len1;
is->audio_buf_index += len1;
}
}
解码:static int audio_decode_frame(VideoState *is, double *pts_ptr)
{
int len1, len2, decoded_data_size;
AVPacket *pkt = &is->audio_pkt;
int64_t dec_channel_layout;
int wanted_nb_samples, resampled_data_size, n;
double pts;
for (;;) {
while (is->audio_pkt_size > 0) {
if (!is->audio_frame) {
if (!(is->audio_frame = av_frame_alloc())) {
return AVERROR(ENOMEM);
}
}
int got_frame = 0;
len1 = avcodec_decode_audio4(is->audio_st->codec, is->audio_frame,
&got_frame, pkt);
is->audio_pkt_data += len1;
is->audio_pkt_size -= len1;
decoded_data_size = av_samples_get_buffer_size(NULL,
is->audio_frame->channels, is->audio_frame->nb_samples,
(AVSampleFormat)is->audio_frame->format, 1);
dec_channel_layout =
(is->audio_frame->channel_layout
&& is->audio_frame->channels
== av_get_channel_layout_nb_channels(
is->audio_frame->channel_layout)) ?
is->audio_frame->channel_layout :
av_get_default_channel_layout(
is->audio_frame->channels);
wanted_nb_samples = is->audio_frame->nb_samples;
if (is->audio_frame->format != is->audio_src_fmt
|| dec_channel_layout != is->audio_src_channel_layout
|| is->audio_frame->sample_rate != is->audio_src_freq
|| (wanted_nb_samples != is->audio_frame->nb_samples
&& !is->swr_ctx)) {
if (is->swr_ctx)
swr_free(&is->swr_ctx);
is->swr_ctx = swr_alloc_set_opts(NULL,
is->audio_tgt_channel_layout, (AVSampleFormat)is->audio_tgt_fmt,
is->audio_tgt_freq, dec_channel_layout,
(AVSampleFormat)is->audio_frame->format, is->audio_frame->sample_rate,
0, NULL);
if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
break;
}
is->audio_src_channel_layout = dec_channel_layout;
is->audio_src_channels = is->audio_st->codec->channels;
is->audio_src_freq = is->audio_st->codec->sample_rate;
is->audio_src_fmt = is->audio_st->codec->sample_fmt;
}
if (is->swr_ctx) {
const uint8_t **in =
(const uint8_t **) is->audio_frame->extended_data;
uint8_t *out[] = { is->audio_buf2 };
if (wanted_nb_samples != is->audio_frame->nb_samples) {
if (swr_set_compensation(is->swr_ctx,
(wanted_nb_samples - is->audio_frame->nb_samples)
* is->audio_tgt_freq
/ is->audio_frame->sample_rate,
wanted_nb_samples * is->audio_tgt_freq
/ is->audio_frame->sample_rate) < 0) {
break;
}
}
len2 = swr_convert(is->swr_ctx, out,
sizeof(is->audio_buf2) / is->audio_tgt_channels
/ av_get_bytes_per_sample(is->audio_tgt_fmt),
in, is->audio_frame->nb_samples);
if (len2 < 0) {
break;
}
if (len2
== sizeof(is->audio_buf2) / is->audio_tgt_channels
/ av_get_bytes_per_sample(is->audio_tgt_fmt)) {
swr_init(is->swr_ctx);
}
is->audio_buf = is->audio_buf2;
resampled_data_size = len2 * is->audio_tgt_channels
* av_get_bytes_per_sample(is->audio_tgt_fmt);
} else {
resampled_data_size = decoded_data_size;
memcpy(is->audio_buf, is->audio_frame->data[0], decoded_data_size);
}
pts = is->audio_clock;
*pts_ptr = pts;
n = 2 * is->audio_st->codec->channels;
is->audio_clock += (double) resampled_data_size
/ (double) (n * is->audio_st->codec->sample_rate);
if (is->seek_flag_audio)
{
if (is->audio_clock < is->seek_time)
{
break;
}
else
{
is->seek_flag_audio = 0;
}
}
return resampled_data_size;
}
if (pkt->data)
{
av_free_packet(pkt);
}
if (is->quit)
return -1;
if (packet_queue_get(&is->audioq, pkt, true) <= 0)
{
return -1;
}
if(strcmp((char*)pkt->data,FLUSH_DATA) == 0)
{
avcodec_flush_buffers(is->audio_st->codec);
av_free_packet(pkt);
continue;
}
is->audio_pkt_data = pkt->data;
is->audio_pkt_size = pkt->size;
if (pkt->pts != AV_NOPTS_VALUE) {
is->audio_clock = av_q2d(is->audio_st->time_base) * pkt->pts;
}
}
return 0;
}
获得数据:static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
AVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pkt1->pkt.size;
*pkt = pkt1->pkt;
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
SDL_CondWaitTimeout(q->cond, q->mutex,1);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
最后是关闭:stop(bool isWait)
{
SDL_CloseAudio();
return true;
}
问题:播放本地视频没问题。播放网络流的话,最后播放完了要关闭音频,但是执行到SDL_CloseAudio函数,就卡死了。大神们帮忙看看怎么回事。
...全文
413 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
rightorwrong 2017-03-16
  • 打赏
  • 举报
回复
SDL_CondWaitTimeout 是不是什么标志没有设置,死锁状态

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧