1.环境需求
ffmpeg和alsa
csdn教程一大把。
2.实现方法
①通过Alsa框架进行录音,获取pcm数据;
②通过FFmpeg框架,把pcm数据重采样,编码进行推流。
3.关键代码介绍
①通过Alsa框架进行录音,获取pcm数据;
//ALSA头文件
#include
//双声道
#define CHANNELS 2
//每个采样点2bytes
#define FSIZE 2*CHANNELS
int main()
{
//要保存的pcm文件
int fd;
char *file=out_filename;
fd = open(file,O_WRONLY|O_CREAT,0777);
//pcm设备结构器
snd_pcm_t *handle;
//以录音模式打开设备
snd_pcm_open(&handle, "default",SND_PCM_STREAM_CAPTURE, 0);
//pcm参数结构体
snd_pcm_hw_params_t *params;
//params申请内存
snd_pcm_hw_params_malloc(¶ms);
//使用pcm设备初始化params
snd_pcm_hw_params_any(handle, params);
//设置多路数据在buffer中的存储方式
//SND_PCM_ACCESS_RW_INTERLEAVED每个周期(period)左右声道的数据交叉存放
snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED);
//设置16位采样格式,S16代表有符号16位,LE代表小端
snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);
//设置声道数
snd_pcm_hw_params_set_channels(handle, params, CHANNELS);
//采样率
unsigned int val=48000;
int dir;
//设置采样率,如果采样率不支持,会用硬件支持最接近的采样率
snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);
unsigned int buffer_time,period_time;
//获取最大的缓冲时间,buffer_time单位为us,500000us=0.5s
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
printf("max_buffer_time:%d\n",buffer_time);
if ( buffer_time >500000)
buffer_time = 500000;
//设置缓冲时间
snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);
//设置采样周期时间,计算方法38帧/秒,48000/38=1263点/帧
period_time = 26315;
snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);
//让这些参数设置到PCM设备
snd_pcm_hw_params(handle, params);
//这个frames并不是指帧率,而是1263采样点数/帧
snd_pcm_uframes_t frames;
snd_pcm_hw_params_get_period_size(params,&frames, &dir);
//缓冲区大小
/* 2 bytes/sample, 1 channels */
int size;
size = frames * FSIZE;
printf("size:%d\n",size);
//数据缓冲区
char *buffer;
buffer = (char *) malloc(size);
while (1)
{
//开始录音,收集一帧数据到缓冲区
snd_pcm_readi(handle, buffer, frames);
if (ret == -EPIPE) {
// EPIPE means overrun
fprintf(stderr, "overrun occurred\n");
ret=snd_pcm_prepare(handle);
if(ret <0){
printf("Failed to recover form overrun");
exit(1);
}
}
else if (ret < 0) {
fprintf(stderr,"error from read: %s\n",snd_strerror(ret));
exit(1);
}
else if (ret != (int)frames) {
fprintf(stderr, "short read, read %d frames\n", ret);
}
//保存录音
write(fd, buffer, size);
}
close(fd);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
}
②通过FFmpeg框架,把pcm数据重采样,编码进行推流。
#include
#include
#include
#include
#include
#include
int main()
{
//pframePCM用于保存每帧pcm数据
AVFrame *pframePCM;
//AVFrame初始化
pframePCM = av_frame_alloc();
pframePCM->format = AV_SAMPLE_FMT_S16; //S16格式
pframePCM->channel_layout = AV_CH_LAYOUT_STEREO; //双声道
pframePCM->sample_rate = 48000; //采样率
pframePCM->nb_samples = frames; //采样点/每帧
pframePCM->channels = CHANNELS;
//AVFrame应用设置
av_frame_get_buffer(pframePCM, 0);
//pframeAAC用于保存重采样数据
AVFrame *pframeAAC;
//AVFrame初始化
pframeAAC = av_frame_alloc();
pframeAAC->format = AV_SAMPLE_FMT_FLTP; //fltp格式
pframeAAC->channel_layout = AV_CH_LAYOUT_STEREO; //双声道
pframeAAC->sample_rate = 44100; //采样率
pframeAAC->nb_samples = 1024; //采样点/每帧
pframeAAC->channels = CHANNELS;
//AVFrame应用设置
av_frame_get_buffer(pframeAAC, 0);
//音频格式转换上下文
struct SwrContext *aac_convert_ctx = swr_alloc();
swr_alloc_set_opts(aac_convert_ctx,
AV_CH_LAYOUT_STEREO, //dst目标
AV_SAMPLE_FMT_FLTP,
44100,
AV_CH_LAYOUT_STEREO, //src原始
AV_SAMPLE_FMT_S16,
48000,
0,
NULL);
//编码器
AVCodec* encodec = NULL;
//设置AAC编码器
encodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
AVCodecContext* encodec_ctx = NULL;
//创建编码器上下文
encodec_ctx = avcodec_alloc_context3(encodec);
//设置编码器上下文参数
encodec_ctx->codec_id = encodec->id;
encodec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
encodec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
encodec_ctx->bit_rate = 64000;
encodec_ctx->sample_rate = 44100;
encodec_ctx->channel_layout = AV_CH_LAYOUT_STEREO ;
encodec_ctx->channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
//打开编码器上下文
avcodec_open2(encodec_ctx, encodec, NULL);
//RTMP地址
char *out_name="rtmp://127.0.0.1/live/stream";
//视频格式上下文
AVFormatContext* outfmt_ctx = NULL;
//创建输出封装器
avformat_alloc_output_context2(&outfmt_ctx, NULL, "flv",out_name);
//视频流
AVStream *out_stream = NULL;
//给AVFormatContext添加AVStream
out_stream = avformat_new_stream(outfmt_ctx,NULL);
//复制参数
avcodec_parameters_from_context(out_stream->codecpar, encodec_ctx);
//查看输出封装内容
av_dump_format(outfmt_ctx, 0, out_name, 1);
//打开rtmp的网络输出IO
avio_open(&outfmt_ctx->pb, out_name, AVIO_FLAG_WRITE);
//写入封装头
avformat_write_header(outfmt_ctx, NULL);
int got_picture;
int vpts=0;
//视频包裹
AVPacket enc_pkt;
//给视频包裹申请内存
memset(&enc_pkt, 0, sizeof(enc_pkt));
while(1)
{
//alsa获取数据
snd_pcm_readi(handle, buffer, frames);
//复制到pframePCM中
memcpy(pframePCM->data[0],buffer,size);
//格式转换
swr_convert(aac_convert_ctx,
pframeAAC->data, //dst
pframeAAC->nb_samples,
(const uint8_t **)pframePCM->data, //src
pframePCM->nb_samples);
//开始编码到视频包裹
avcodec_encode_audio2(encodec_ctx, &enc_pkt, pframeAAC, &got_picture);
//设置时间戳
enc_pkt.pts = av_rescale_q(enc_pkt.pts, encodec_ctx->time_base, out_stream->time_base);
enc_pkt.dts = av_rescale_q(enc_pkt.dts, encodec_ctx->time_base, out_stream->time_base);
enc_pkt.duration = av_rescale_q(enc_pkt.duration, encodec_ctx->time_base, out_stream->time_base);
//发送RMTP流
av_interleaved_write_frame(outfmt_ctx, &enc_pkt);
//清空包裹
av_free_packet(&enc_pkt);
pframeAAC->pts = vpts;
vpts+=pframeAAC->nb_samples;
}
}
3.完整代码
完整代码的注释没那么详细,复制黏贴后请从第二部分关键代码的介绍。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CHANNELS 2
#define FSIZE 2*CHANNELS
int volume_adjust(char *in_buf,float vol)
{
short buf=0;
buf=*in_buf+(*(in_buf+1)<<8);
if(buf>=-1&&buf<=1)
{
buf=0;
}
buf=buf*vol;
if(buf>=32767)
{
buf=0;
*in_buf=(char)buf;
*(in_buf+1)=buf>>8;
}
else if(buf<=-32768)
{
buf=0;
*in_buf=(char)buf;
*(in_buf+1)=buf>>8;
}
else
{
*in_buf=(char)buf;
*(in_buf+1)=buf>>8;
}
return 0;
}
int main()
{
int fd;
char *out_filename="output.raw";
char *file=out_filename;
fd = open(file,O_WRONLY|O_CREAT,0777);
if( fd ==-1)
{
printf("open file:%s fail.\n",out_filename);
exit(1);
}
int ret=0;
snd_pcm_t *handle;
//以录音模式打开设备
ret = snd_pcm_open(&handle, "default",SND_PCM_STREAM_CAPTURE, 0);
if (ret < 0)
{
printf("unable to open pcm device!\n");
exit(1);
}
//配置硬件参数结构体
snd_pcm_hw_params_t *params;
//params申请内存
snd_pcm_hw_params_malloc(¶ms);
//使用pcm设备初始化hwparams
ret=snd_pcm_hw_params_any(handle, params);
if (ret < 0)
{
printf("Can not configure this PCM device!\n");
exit(1);
}
//设置多路数据在buffer中的存储方式
//SND_PCM_ACCESS_RW_INTERLEAVED每个周期(period)左右声道的数据交叉存放
ret=snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED);
if (ret < 0)
{
printf("Failed to set PCM device to interleaved!\n");
exit(1);
}
//设置16位采样格式
ret=snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);
if (ret < 0)
{
printf("Failed to set PCM device to 16-bit signed PCM\n");
exit(1);
}
//设置声道数
ret=snd_pcm_hw_params_set_channels(handle, params, CHANNELS);
if (ret < 0)
{
printf("Failed to set PCM device CHANNELS\n");
exit(1);
}
unsigned int val=48000;
int dir;
//设置采样率,如果采样率不支持,会用硬件支持最接近的采样率
ret=snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);
if (ret < 0)
{
printf("Failed to set PCM device to sample rate\n");
exit(1);
}
unsigned int buffer_time,period_time;
//获取最大的缓冲时间,buffer_time单位为us,500000us=0.5s
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
//printf("buffer_time:%d\n",buffer_time);
if ( buffer_time >500000)
buffer_time = 500000;
//设置缓冲时间
ret = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);
if (ret < 0)
{
printf("Failed to set PCM device to sample rate\n");
exit(1);
}
//设置周期时间
period_time = 26315;
ret = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);
if (ret < 0)
{
printf("Failed to set PCM device to period time\n");
exit(1);
}
//让这些参数作用于PCM设备
ret = snd_pcm_hw_params(handle, params);
if (ret < 0)
{
printf("unable to set hw parameters\n");
exit(1);
}
snd_pcm_uframes_t frames;
snd_pcm_hw_params_get_period_size(params,&frames, &dir);
printf("period_size:%ld\n",frames);
int size;
// 1 frame = channels * sample_size.
size = frames * FSIZE; /* 2 bytes/sample, 1 channels */
printf("size:%d\n",size);
char *buffer;
buffer = (char *) malloc(size);
AVFrame *pframePCM;
pframePCM = av_frame_alloc();
pframePCM->format = AV_SAMPLE_FMT_S16;
pframePCM->channel_layout = AV_CH_LAYOUT_STEREO;
pframePCM->sample_rate = 48000;
pframePCM->nb_samples = frames;
pframePCM->channels = CHANNELS;
av_frame_get_buffer(pframePCM, 0);
AVFrame *pframeAAC;
pframeAAC = av_frame_alloc();
pframeAAC->format = AV_SAMPLE_FMT_FLTP;
pframeAAC->channel_layout = AV_CH_LAYOUT_STEREO;
pframeAAC->sample_rate = 44100;
pframeAAC->nb_samples = 1024;
pframeAAC->channels = CHANNELS;
av_frame_get_buffer(pframeAAC, 0);
struct SwrContext *aac_convert_ctx = swr_alloc();
if (!aac_convert_ctx)
{
fprintf(stderr, "Could not allocate resampler context\n");
return -1;
}
swr_alloc_set_opts(aac_convert_ctx,
AV_CH_LAYOUT_STEREO,
AV_SAMPLE_FMT_FLTP,
44100,
AV_CH_LAYOUT_STEREO,
AV_SAMPLE_FMT_S16,
48000,
0,
NULL);
if ((ret = swr_init(aac_convert_ctx)) < 0)
{
fprintf(stderr, "Failed to initialize the resampling context\n");
return -1;
}
AVCodec* encodec = NULL;
//找到编码器
encodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!encodec)
{
printf("not find encoder\n");
return -1;
}
AVCodecContext* encodec_ctx = NULL;
//创建编码器
encodec_ctx = avcodec_alloc_context3(encodec);
if (!encodec_ctx)
{
printf("not alloc context3\n\n");
return -1;
}
encodec_ctx->codec_id = encodec->id;
encodec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
encodec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
encodec_ctx->bit_rate = 64000;
encodec_ctx->sample_rate = 44100;
encodec_ctx->channel_layout = AV_CH_LAYOUT_STEREO ;
encodec_ctx->channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
//打开解码器
ret = avcodec_open2(encodec_ctx, encodec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
return -1;
}
char *out_name="rtmp://47.101.62.167/live/stream";
AVFormatContext* outfmt_ctx = NULL;
//创建输出封装器
ret=avformat_alloc_output_context2(&outfmt_ctx, NULL, "flv",out_name);
if (ret != 0)
{
printf("failed alloc output context\n");
return -1;;
}
AVStream *out_stream = NULL;
//添加视频流
out_stream = avformat_new_stream(outfmt_ctx,NULL);
if (!out_stream) {
printf("failed new stream\n");
return -1;
}
//复制参数
avcodec_parameters_from_context(out_stream->codecpar, encodec_ctx);
//查看输出封装内容
av_dump_format(outfmt_ctx, 0, out_name, 1);
//打开rtmp的网络输出IO
ret=avio_open(&outfmt_ctx->pb, out_name, AVIO_FLAG_WRITE);
if (ret!=0) {
printf("failed to open outfile\n");
return -1;
}
//写入封装头
ret=avformat_write_header(outfmt_ctx, NULL);
if (ret!=0) {
printf("failed to write header\n");
avio_close(outfmt_ctx->pb);
return -1;
}
AVPacket enc_pkt;
memset(&enc_pkt, 0, sizeof(enc_pkt));
int got_picture;
int i,vpts=0;
char *p;
struct timeval start, end;
gettimeofday( &start, NULL );
while (1)
{
ret = snd_pcm_readi(handle, buffer, frames);
if (ret == -EPIPE) {
// EPIPE means overrun
fprintf(stderr, "overrun occurred\n");
ret=snd_pcm_prepare(handle);
if(ret <0){
printf("Failed to recover form overrun");
exit(1);
}
}
else if (ret < 0) {
fprintf(stderr,"error from read: %s\n",snd_strerror(ret));
exit(1);
}
else if (ret != (int)frames) {
fprintf(stderr, "short read, read %d frames\n", ret);
}
memcpy(pframePCM->data[0],buffer,size);
ret=swr_convert(aac_convert_ctx,pframeAAC->data, pframeAAC->nb_samples,(const uint8_t **)pframePCM->data, pframePCM->nb_samples);
avcodec_encode_audio2(encodec_ctx, &enc_pkt, pframeAAC, &got_picture);
if(!got_picture)
{
printf("123\n");
continue;
}
//推流
enc_pkt.pts = av_rescale_q(enc_pkt.pts, encodec_ctx->time_base, out_stream->time_base);
enc_pkt.dts = av_rescale_q(enc_pkt.dts, encodec_ctx->time_base, out_stream->time_base);
enc_pkt.duration = av_rescale_q(enc_pkt.duration, encodec_ctx->time_base, out_stream->time_base);
ret = av_interleaved_write_frame(outfmt_ctx, &enc_pkt);
if (ret < 0) {
fprintf(stderr, "Error muxing packet\n");
break;
}
av_free_packet(&enc_pkt);
pframeAAC->pts = vpts;
vpts+=pframeAAC->nb_samples;
ret = write(fd, buffer, size);
if (ret <0){
perror("fail to write to audio file\n");
}
gettimeofday( &end, NULL );
printf("%ld",end.tv_sec-start.tv_sec);
printf("\r\033[k");
fflush(stdout);
}
close(fd);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
不知不觉就搞定了FFmpeg框架下实现的视频编解码和音频编解码,过几天我把他们整合到一起,成为完整的音视频RTMP流。
还是要感谢雷大佬的帮助。