1. 程式人生 > 其它 >聊天平臺原始碼,聊天平臺如何獲取到音訊流

聊天平臺原始碼,聊天平臺如何獲取到音訊流

聊天平臺原始碼,聊天平臺如何獲取到音訊流的相關程式碼

我們重取樣的引數要和SDL的引數是一致的,還要考慮到雖然沒有AVPacket了,但是解碼器中還有未解碼的資料,我們要用avcodec_send_packet(aCodecCtx, NULL)告訴解碼器沒有資料了,重新整理解碼器。

下面這段程式碼的邏輯是如果沒有資料要解碼了,就跳轉到獲取解碼資料的邏輯。

int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)
{

static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = 0;
static AVFrame frame;
av_frame_get_buffer(&frame, 0);
int len1, data_size = 0;
int ret = 0;
int is_no_pkt = 0;

for (;;)
{

if (pkt.data)
av_packet_unref(&pkt);

if (quit)
{
return -1;
}

if (packet_queue_get(&audioq, &pkt, 1) < 0)
{
ret = avcodec_send_packet(aCodecCtx, NULL);
if (ret < 0)
{
return -1;
}
is_no_pkt = 1;
goto __RECEIVE;
}

ret = avcodec_send_packet(aCodecCtx, &pkt);
if (ret < 0)
{
ret = -1;
printf("decode error");
av_packet_unref(&pkt);
return -1;
}
__RECEIVE:
if (is_no_pkt)
{
return -1;
}
else if (pkt.data)
{
av_packet_unref(&pkt);
}
ret = avcodec_receive_frame(aCodecCtx, &frame);
if (ret < 0)
{
continue;
}

data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;

swr_convert(audio_convert_ctx,
&audio_buf,
SDL_AUDIO_BUFFER_SIZE,
(const uint8_t **)frame.data,
frame.nb_samples);
return data_size;
}
}

轉碼後的資料大小=取樣大小 * 單通道取樣個數 * 通道數

data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;

下面是完整的程式碼

#include <stdio.h>
#include <assert.h>

#include <SDL2/SDL.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/samplefmt.h>

#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIO_FRAME_SIZE 192000
typedef struct PacketQueue
{
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;

PacketQueue audioq;
int quit = 0;
struct SwrContext *audio_convert_ctx = NULL;
static Uint8 out_channel = 2;
static SDL_AudioFormat out_format = AV_SAMPLE_FMT_S16;
static int out_nb_samples = 0;
static int sample_rate = 48000;


void audio_callback(void *userdata, Uint8 *stream, int len);
void packet_queue_init(PacketQueue *q);
int packet_queue_put(PacketQueue *q, AVPacket *pkt);
int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block);
int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size);

int main(int argc, char *argv[])
{
int ret = 0;
AVFormatContext *fmt_ctx = NULL;
char *in_file;
int video_stream_index = -1;
int audio_stream_index = -1;
AVCodec *video_codec = NULL, *audio_codec = NULL;
AVCodecParameters *audio_codecpar = NULL, *video_codecpar = NULL;
AVCodecContext *audio_codec_ctx = NULL, *video_codec_ctx = NULL;
SDL_AudioSpec wanted_spec, src_spec;
int64_t in_channel_layout;
int64_t out_channel_layout;
AVPacket packet;
SDL_Event event;
av_log_set_level(AV_LOG_INFO);

if (argc < 2)
{
av_log(NULL, AV_LOG_ERROR, "need infile\n");
return -1;
}
in_file = argv[1];
//初始化SDL
ret = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER);
if (ret != 0)
{
av_log(NULL, AV_LOG_ERROR, "sdl init error\n");
return ret;
}

//獲取輸入檔案上下文
ret = avformat_open_input(&fmt_ctx,
in_file,
NULL,
NULL);
if (!fmt_ctx)
{
av_log(NULL, AV_LOG_ERROR, "open input file fail\n");
goto __FAIL;
}

//獲取視訊流索引和音訊流索引
ret = avformat_find_stream_info(fmt_ctx, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "find stream fail\n");
goto __FAIL;
}
av_dump_format(fmt_ctx, 0, in_file, 0);
video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, -1);
audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_codec, -1);
if (video_stream_index < 0 || audio_stream_index < 0)
{
av_log(NULL, AV_LOG_ERROR, "find audio or video fail\n");
goto __FAIL;
}
if (!audio_codec || !video_codec)
{
ret = -1;
av_log(NULL, AV_LOG_ERROR, "find audio codec or video codec fail");
goto __FAIL;
}
//獲取解碼器上下文並開啟解碼器
audio_codecpar = fmt_ctx->streams[audio_stream_index]->codecpar;
out_nb_samples = audio_codecpar->frame_size;

video_codecpar = fmt_ctx->streams[video_stream_index]->codecpar;

audio_codec_ctx = avcodec_alloc_context3(audio_codec);
video_codec_ctx = avcodec_alloc_context3(video_codec);

avcodec_parameters_to_context(audio_codec_ctx, audio_codecpar);
avcodec_parameters_to_context(video_codec_ctx, video_codecpar);

ret = avcodec_open2(audio_codec_ctx, audio_codec, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "open audio codec fail\n");
goto __FAIL;
}

ret = avcodec_open2(video_codec_ctx, video_codec, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "open video codec fail\n");
goto __FAIL;
}

//設定SDL_音訊引數
wanted_spec.freq = sample_rate; //取樣率
wanted_spec.format = AUDIO_S16LSB; //取樣大小
wanted_spec.channels = out_channel; //聲道數
wanted_spec.silence = 0;
wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; //取樣個數
wanted_spec.callback = audio_callback;
wanted_spec.userdata = audio_codec_ctx;

ret = SDL_OpenAudio(&wanted_spec, &src_spec);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "open audio device fail\n");
goto __FAIL;
}

//設定重取樣
in_channel_layout = audio_codecpar->channel_layout;
out_channel_layout = av_get_default_channel_layout(out_channel);
audio_convert_ctx = swr_alloc();
if (!audio_convert_ctx)
{
ret = -1;
av_log(NULL, AV_LOG_ERROR, "alloc swr fail\n");
goto __FAIL;
}

swr_alloc_set_opts(audio_convert_ctx,
out_channel_layout,
out_format,
sample_rate,
in_channel_layout,
audio_codecpar->format,
audio_codecpar->sample_rate,
0,
NULL);

swr_init(audio_convert_ctx);
SDL_PauseAudio(0);

packet_queue_init(&audioq);
while (av_read_frame(fmt_ctx, &packet) >= 0)
{
if (packet.stream_index == audio_stream_index)
{
packet_queue_put(&audioq, &packet);
}
else
{
av_packet_unref(&packet);
}
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = 1;
goto __FAIL;
break;
default:
break;
}
}
while (1)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = 1;
goto __FAIL;
break;
default:
break;
}
}

__FAIL:
SDL_CloseAudio();
SDL_Quit();
if (audio_codecpar)
{
avcodec_parameters_free(&audio_codecpar);
}

if (video_codecpar)
{
avcodec_parameters_free(&video_codecpar);
}

if (video_codec_ctx)
{
avcodec_close(video_codec_ctx);
avcodec_free_context(&video_codec_ctx);
}

if (audio_codec_ctx)
{
avcodec_close(audio_codec_ctx);
avcodec_free_context(&audio_codec_ctx);
}
if (fmt_ctx)
{
avformat_close_input(&fmt_ctx);
avformat_free_context(fmt_ctx);
}

return ret;
}

void audio_callback(void *userdata, Uint8 *stream, int len)
{

AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
int len1, audio_size;

static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
static unsigned int audio_buf_size = 0;
static unsigned int audio_buf_index = 0;

while (len > 0)
{
if (audio_buf_index >= audio_buf_size)
{
/* We have already sent all our data; get more */
audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));
if (audio_size < 0)
{
/* If error, output silence */
audio_buf_size = 1024; // arbitrary?
memset(audio_buf, 0, audio_buf_size);
}
else
{
audio_buf_size = audio_size;
}
audio_buf_index = 0;
}
len1 = audio_buf_size - audio_buf_index;
if (len1 > len)
len1 = len;
fprintf(stderr, "index=%d, len1=%d, len=%d\n",
audio_buf_index,
len,
len1);
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);
len -= len1;
stream += len1;
audio_buf_index += len1;
}
}

void packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}

int packet_queue_put(PacketQueue *q, AVPacket *pkt)
{

AVPacketList *pkt1;
AVPacket dst_pkt;
av_init_packet(&dst_pkt);
if (av_packet_ref(&dst_pkt, pkt) < 0)
{
return -1;
}
pkt1 = av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = dst_pkt;
pkt1->next = NULL;

SDL_LockMutex(q->mutex);

if (!q->last_pkt)
{
q->first_pkt = pkt1;
}
else
{
q->last_pkt->next = pkt1;
}

q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size;
SDL_CondSignal(q->cond);

SDL_UnlockMutex(q->mutex);
return 0;
}

int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
AVPacketList *pkt1;
int ret;

SDL_LockMutex(q->mutex);

for (;;)
{

if (quit)
{
ret = -1;
break;
}

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_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}

int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)
{

static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = 0;
static AVFrame frame;
av_frame_get_buffer(&frame, 0);
int len1, data_size = 0;
int ret = 0;
int is_no_pkt = 0;

for (;;)
{

if (pkt.data)
av_packet_unref(&pkt);

if (quit)
{
return -1;
}

if (packet_queue_get(&audioq, &pkt, 1) < 0)
{
ret = avcodec_send_packet(aCodecCtx, NULL);
if (ret < 0)
{
return -1;
}
is_no_pkt = 1;
goto __RECEIVE;
}

ret = avcodec_send_packet(aCodecCtx, &pkt);
if (ret < 0)
{
ret = -1;
printf("decode error");
av_packet_unref(&pkt);
return -1;
}
__RECEIVE:
if (is_no_pkt)
{
return -1;
}
else if (pkt.data)
{
av_packet_unref(&pkt);
}
ret = avcodec_receive_frame(aCodecCtx, &frame);
if (ret < 0)
{
continue;
}

data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;

swr_convert(audio_convert_ctx,
&audio_buf,
SDL_AUDIO_BUFFER_SIZE,
(const uint8_t **)frame.data,
frame.nb_samples);
return data_size;
}
}

以上就是 聊天平臺原始碼,聊天平臺如何獲取到音訊流的相關程式碼,更多內容歡迎關注之後的文章