记录记录记录记录记录记录

#ifndef HELMET_AUDIOVIDEO_H
#define HELMET_AUDIOVIDEO_H

#include <jni.h>
#include <android/log.h>

extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavutil/imgutils.h"
#include "libavfilter/buffersrc.h"
}

class AudioVideo {

public:

    int audioInit(JNIEnv *env, jstring pushPath);

    void audioEncoder(JNIEnv *env, jbyte *byte, int byteLength);
};


#endif //HELMET_AUDIOVIDEO_H

#include "AudioVideo.h"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "PushFlow",__VA_ARGS__)

//音频输出格式上下文
AVFormatContext *audioFormatContext;

//音频编码器对象
AVCodec *audioCodec;

//音频编码器上下文
AVCodecContext *audioCodecCtx;

//音频流通道
AVStream *audioStream;

//音频缓冲区大小
int audioBufferSize;

//音频储存原始PCM数据AVFrame
AVFrame *audioFrame;

//音频储存编码后AAC数据AVPacket
AVPacket *audioPacket;

//音频缓冲区Buffer
uint8_t *audioBuffer;

//推流地址
const char *pushPath;

int AudioVideo::audioInit(JNIEnv *env, jstring path) {
    //设置推流地址
    pushPath = env->GetStringUTFChars(path, 0);
    //注册组件
    av_register_all();
    //创建输出上下文
    avformat_alloc_output_context2(&audioFormatContext, NULL, "flv", pushPath);
    //打开输出文件
    if (avio_open(&audioFormatContext->pb, pushPath, AVIO_FLAG_READ_WRITE) < 0) return -1;
    //查找编码器
    audioCodec = avcodec_find_encoder_by_name("libfdk_aac");
    if (audioCodec == NULL) return -1;
    //创建编码器上下文
    audioCodecCtx = avcodec_alloc_context3(audioCodec);
    //设置参数
    audioCodecCtx->codec_id = audioCodec->id;//设置id
    audioCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;//设置类型为音频
    audioCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;//设置格式为16bit
    audioCodecCtx->sample_rate = 8000;//设置采样率
    audioCodecCtx->channel_layout = AV_CH_LAYOUT_MONO;//设置通道类型和下面通道数量对应
    audioCodecCtx->channels = 1;//单通道
    audioCodecCtx->bit_rate = 128000;//码率
    //创建流通道
    audioStream = avformat_new_stream(audioFormatContext, audioCodec);
    if (audioStream == NULL) return -1;
    //设置编码器参数
    if (avcodec_parameters_from_context(audioStream->codecpar, audioCodecCtx) < 0) return -1;
    //打开编码器
    if (avcodec_open2(audioCodecCtx, audioCodec, NULL) < 0) return -1;
    //写入头文件
    avformat_write_header(audioFormatContext, NULL);
    //初始化编码前(原始数据)数据储存结构体,也就是创建数据缓冲区
    audioFrame = av_frame_alloc();
    //设置参数
    audioFrame->nb_samples = audioCodecCtx->frame_size;
    audioFrame->format = audioCodecCtx->sample_fmt;
    //获取缓冲区大小
    audioBufferSize = av_samples_get_buffer_size(NULL,
                                                 audioCodecCtx->channels,
                                                 audioCodecCtx->frame_size,
                                                 audioCodecCtx->sample_fmt,
                                                 1);
    //创建缓冲区
    audioBuffer = (uint8_t *) av_malloc(audioBufferSize);
    avcodec_fill_audio_frame(audioFrame,
                             audioCodecCtx->channels,
                             audioCodecCtx->sample_fmt,
                             (const uint8_t *) audioBuffer,
                             audioBufferSize,
                             1);
    //初始化编码后数据储存结构体
    audioPacket = (AVPacket *) av_malloc(audioBufferSize);
    return 0;
}

void AudioVideo::audioEncoder(JNIEnv *env, jbyte *byte, int byteLength) {
    int ret;
    memcpy(audioFrame->data[0], byte, byteLength);
    ret = avcodec_send_frame(audioCodecCtx, audioFrame);
    if (ret < 0) return;
    ret = avcodec_receive_packet(audioCodecCtx, audioPacket);
    if (ret < 0) return;
    ret = av_interleaved_write_frame(audioFormatContext, audioPacket);
    if (ret < 0) return;
    LOGE("音频编码写入成功");
    av_packet_unref(audioPacket);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值