ffmpeg + sdl -03 简单音频播放器实现
没办法,工作中遇到了问题。
目前NEC EMMA的架构如下:
从USB读入文件 -> 文件分析并提取Packet中的Payload Data -> NEC HANDLE AVTransfer -> NEC HANDLE WMV -> AUDIO OUTPUT
按照驱动的API写好代码后却怎么也没有声音,所有API返回值均OK。
郁闷开始了。继续绝望中寻找希望。
为了对比调试,参考
并做了一些ffmpeg版本升级修改。
修改前:
len = avcodec_decode_audio (pAudioCodecCtx,
(int16_t *)decompressed_audio_buf,
&decompressed_audio_buf_size, // it is the decompressed frame in BYTES 解码后的数据大小,字节为单位;
packet.data,
packet.size );
修改后:
decompressed_audio_buf_size = (AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2; // 不加这一行,执行时会出错。
len = avcodec_decode_audio3 (pCodecCtx,
(int16_t *)decompressed_audio_buf,
&decompressed_audio_buf_size, // it is the decompressed frame in BYTES
&packet);
遇到的问题:
/dev/dsp 设备不存在
解决办法:
modprobe snd_pcm_oss (需要su到root用户)
完整代码如下:(基本来自http://bbs.chinavideo.org/viewthread.php?tid=1247&extra=page%3D1)
#include <avcodec.h>
#include <avformat.h>
#include <avutil.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <sys/soundcard.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sched.h>
#define ALL_DEBUG
#ifdef ALL_DEBUG
#define AV_DEBUG
#define AUDIO_DEBUG
#endif
//------------------------------------------------------------------------------
// manipulations for file
int open_file (char *file_name, int mode)
{
// open file file_name and return the file descriptor;
int fd;
if ((fd = open (file_name, mode)) < 0)
{
fprintf (stderr, " Can't open %s!/n", file_name);
exit (-1);
}
return fd;
}
int set_audio (int fd, AVCodecContext * pCodecCtx)
{
// set the properties of audio device with pCodecCtx;
int i, err;
/* 设置适当的参数,使得声音设备工作正常 */
/* 详细情况请参考Linux关于声卡编程的文档 */
i = 0;
ioctl (fd, SNDCTL_DSP_RESET, &i);
i = 0;
ioctl (fd, SNDCTL_DSP_SYNC, &i);
i = 1;
ioctl (fd, SNDCTL_DSP_NONBLOCK, &i);
// set sample rate;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->sample_rate:%d/n", pCodecCtx->sample_rate);
#endif
i = pCodecCtx->sample_rate;
if (ioctl (fd, SNDCTL_DSP_SPEED, &i) == -1)
{
fprintf (stderr, "Set speed to %d failed:%s/n", i,
strerror (errno));
return (-1);
}
if (i != pCodecCtx->sample_rate)
{
fprintf (stderr, "do not support speed %d,supported is %d/n",
pCodecCtx->sample_rate, i);
return (-1);
}
// set channels;
i = pCodecCtx->channels;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->channels:%d/n", pCodecCtx->channels);
#endif
if ((ioctl (fd, SNDCTL_DSP_CHANNELS, &i)) == -1)
{
fprintf (stderr, "Set Audio Channels %d failed:%s/n", i,
strerror (errno));
return (-1);
}
if (i != pCodecCtx->channels)
{
fprintf (stderr, "do not support channel %d,supported %d/n",
pCodecCtx->channels, i);
return (-1);
}
// set bit format;
i = AFMT_S16_LE;
if (ioctl (fd, SNDCTL_DSP_SETFMT, &i) == -1)
{
fprintf (stderr, "Set fmt to bit %d failed:%s/n", i,
strerror (errno));
return (-1);
}
if (i != AFMT_S16_LE)
{
补充:综合编程 , 其他综合 ,