FFmpeg音频解码播放

音频的播放Android提供了像MediaPlayer,SoundPool,AudioTrack(需自己解码音频)等。这些都只是单纯的播放一个声音,支持的音频文件格式也存在有限。比如我们想开发一款像QQ音乐这样的音乐播放器,一款好的音乐器并不是简单的播放歌曲,里面会包含有很多设置,能够对音效的操作编辑。FFmpeg作为音视频操作的库,解码出音频文件PCM数据,PCM作为音频的原始数据我们可以对其进行编辑等等,FFmpeg可对音频添加滤镜。
不管是视频图像解码还是音频解码FFmpeg都有标准的步骤。
看看FFmpeg音频解码的过程:


FFmpeg音频解码播放_第1张图片
音频解码流程.png

下面是ffmpeg解码音频流程的代码

//
// Created by Administrator on 2019/8/21.
//

#include "YBFFmpeg.h"
#include "android_log.h"
#include "PlayerContan.h"
#include 

YBFFmpeg::YBFFmpeg(PlayerJNICall *playerJNICall, const char *url) {
    pPlayerJNICall = playerJNICall;
    char *copyUrl = (char *) malloc(strlen(url) + 1);
    memcpy(copyUrl, url, strlen(url) + 1);
    this->url = url;
}

YBFFmpeg::~YBFFmpeg() {
    relese();
}


void *thread_play(void *arg) {
    YBFFmpeg *ybfFmpeg = (YBFFmpeg *) arg;
    int res;
    int audioStramIndex = -1;
    int index = 0;

    //注册所有组件
    av_register_all();

    //打开文件
    res = avformat_open_input(&(ybfFmpeg->pFormatContext), ybfFmpeg->url, NULL, NULL);
    if (res != 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    //查找流信息
    res = avformat_find_stream_info(ybfFmpeg->pFormatContext, NULL);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    //查找对应流index 找的是AVMEDIA_TYPE_AUDIO(音频流)
    audioStramIndex = av_find_best_stream(ybfFmpeg->pFormatContext, AVMEDIA_TYPE_AUDIO,
                                          -1, -1, NULL,
                                          0);
    if (audioStramIndex < 0) {
        LOGE("%s", "no find audio stream");
        ybfFmpeg->callPlayerJniError(PLAYER_FIND_AUDIO_STREAM_ERRO, "no find audio stream");
        return (void*)-1;
    }

    AVCodecParameters *pCodecParameters = ybfFmpeg->pFormatContext->streams[audioStramIndex]->codecpar;
    //查找解码器
    AVCodec *pCodec = avcodec_find_decoder(
            pCodecParameters->codec_id);
    if (pCodec == NULL) {
        LOGE("%s", "no find Codec fail");
        ybfFmpeg->callPlayerJniError(PLAYER_FIND_CODEC_ERRO, "PLAYER_FIND_CODEC_ERRO");
        return (void*)-1;
    }


    //开辟pCodecContext
    ybfFmpeg->pCodecContext = avcodec_alloc_context3(pCodec);
    if (ybfFmpeg->pCodecContext == NULL) {
        LOGE("%s", "avcodec alloc context fail");
        ybfFmpeg->callPlayerJniError(PLAYER_ALLOC_CODECCONTEXT_ERRO,
                                     "PLAYER_ALLOC_CODECCONTEXT_ERRO");
        return (void*)-1;
    }

    res = avcodec_parameters_to_context(ybfFmpeg->pCodecContext, pCodecParameters);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }
    //打开解码器
    res = avcodec_open2(ybfFmpeg->pCodecContext, pCodec, NULL);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    int64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int out_sample_rate = 44100;
    int64_t in_ch_layout = pCodecParameters->channel_layout;
    enum AVSampleFormat in_sample_fmt = ybfFmpeg->pCodecContext->sample_fmt;
    int in_sample_rate = ybfFmpeg->pCodecContext->sample_rate;

    ybfFmpeg->pSwrContext = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt,
                                               out_sample_rate, in_ch_layout,
                                               in_sample_fmt, in_sample_rate, 0, NULL);


    int errocode = swr_init(ybfFmpeg->pSwrContext);

   
    // size 是播放指定的大小,是最终输出的大小
    int outChannels = av_get_channel_layout_nb_channels(out_ch_layout);


    int dataSize = av_samples_get_buffer_size(NULL, outChannels,
                                              pCodecParameters->frame_size,
                                              out_sample_fmt, 0);

    int out_sample_fmt_track;
    if (out_sample_fmt == AV_SAMPLE_FMT_U8) {
        out_sample_fmt_track = 3;
    } else {
        out_sample_fmt_track = 2;
    }

    uint8_t *resampleOutBuffer = (uint8_t *) malloc(dataSize);

    JNIEnv* tehread_Env;
    ybfFmpeg->pPlayerJNICall->javaVM->AttachCurrentThread(&tehread_Env,NULL);
    ybfFmpeg->pPlayerJNICall->initCrateAudioTrack(tehread_Env,outChannels, out_sample_fmt_track);
    jbyteArray jPcmByteArray =tehread_Env->NewByteArray(dataSize);
    jbyte *jPcmData = tehread_Env->GetByteArrayElements(jPcmByteArray, NULL);

    AVPacket *pkt = av_packet_alloc();
    AVFrame *pFrame = av_frame_alloc();

    //开始解码流
    while (av_read_frame(ybfFmpeg->pFormatContext, pkt) == 0) {

        if (audioStramIndex == pkt->stream_index) {
            //音频流
            if (avcodec_send_packet(ybfFmpeg->pCodecContext, pkt) == 0) {

                if (avcodec_receive_frame(ybfFmpeg->pCodecContext, pFrame) == 0) {
                    //解码数据
                    index++;
                    LOGE("解码音频%d帧", index);

                    swr_convert(ybfFmpeg->pSwrContext, &resampleOutBuffer, pFrame->nb_samples,
                                (const uint8_t **) (pFrame->data),
                                pFrame->nb_samples);


                    memcpy(jPcmData, resampleOutBuffer, dataSize);
                    // 0 把 c 的数组的数据同步到 jbyteArray , 然后释放native数组
                    tehread_Env->ReleaseByteArrayElements(jPcmByteArray,
                                                                               jPcmData,
                                                                               JNI_COMMIT);
                    ybfFmpeg->pPlayerJNICall->callAudioTrackWrite(tehread_Env,jPcmByteArray, 0, dataSize);

                }

            }
        }

        av_packet_unref(pkt);
        av_frame_unref(pFrame);
    }

    av_packet_free(&pkt);
    av_frame_free(&pFrame);
    tehread_Env->DeleteLocalRef(jPcmByteArray);
    ybfFmpeg->pPlayerJNICall->javaVM->DetachCurrentThread()
    free(resampleOutBuffer);

}


void YBFFmpeg::play() {

    pthread_t play_thread;
    pthread_create(&play_thread, NULL, thread_play, this);
    pthread_join(play_thread, NULL);

}


void YBFFmpeg::callPlayerJniError(int code, char *msg) {
    relese();
    callPlayerJniError(code, msg);
}

void YBFFmpeg::relese() {
    if (pSwrContext != NULL) {
        swr_close(pSwrContext);
        swr_free(&pSwrContext);
        pSwrContext = NULL;
    }
    if (pCodecContext != NULL) {
        avcodec_free_context(&pCodecContext);
        pCodecContext = NULL;
    }

    if (pFormatContext != NULL) {
        avformat_close_input(&pFormatContext);
        avformat_free_context(pFormatContext);
        pFormatContext = NULL;
    }
}


上述代码包含媒体文件的读取(IO),解码模块,音频渲染。简单的展示FFmpeg的解码音频的流程。制作播放器,要考虑东西比较多,比如网络的抖动,解码抖动,秒开等优化。

avformat_open_input()主要是连接网络或者本地资源以及码流头部信息的拉取,
avformat_find_stream_info()媒体信息的探测与分析,做完这步操作一些媒体的基本信息都被填入上下文。看看AVFormatContext 有哪些重要的信息

typedef struct AVFormatContext {
  struct AVInputFormat *iformat;
  struct AVOutputFormat *oformat;
  unsigned int nb_streams;
  AVStream **streams;
  char filename[1024];
  int64_t start_time;
  int64_t duration;
  int64_t bit_rate;
  unsigned int packet_size;
  int max_delay;
  enum AVCodecID video_codec_id;
  AVDictionary *metadata;
  AVCodec *video_codec;
......
}AVFormatContext 

从AVFormatContext 的结构体中可以看出包含的码流数量,码流,文件名,时长,解码器等等,如果想要知道码流的详细数据(比如音频流的采样率,采样点个数,通道等详细信息),通过av_find_best_stream()可以找到我们关注的码流,AVStream结构体中包含了对应码流的详细信息

typedef struct AVStream {
  int index;    /**< stream index in AVFormatContext */
  AVCodecContext *codec;//解码上下文
   int64_t start_time; 第一帧数据显示时间
   int64_t nb_frames;  多少帧数据
   AVCodecParameters *codecpar;  //解码参数
}AVStream 

AVCodecParameters 解码的一些信息参数

typedef struct AVCodecParameters {
    enum AVCodecID   codec_id;
    int64_t bit_rate;
    int width;
    int height;
    uint64_t channel_layout;
    int      channels;
    int      sample_rate;
    int      block_align;
    int      frame_size;
}AVCodecParameters 

经过层层的解析媒体文件信息几乎都可以取到。有些信息也可能没有比如AVCodecParameters 中的width,height信息码流中可能并不含盖,要等具体解码来覆盖该值。

创建JNI层创建AudioTrack 上篇文章讲到了子线程与主线程调用Java的区别,这里把播放放到了子线程,JNIEnv包括对象都是线程私有的,主要是通过JavaVM来获取JNIEnv,包括创建对象的全局引用

//
// Created by Administrator on 2019/8/21.
//

#include "PlayerJNICall.h"
#include "android_log.h"

PlayerJNICall::PlayerJNICall(JNIEnv *env, JavaVM *javaVM, jobject jPlayerObj) {
    this->javaVM = javaVM;
    this->JniEnv = env;
    this->jPlayerObj = JniEnv->NewGlobalRef(jPlayerObj);
    this->instance_clazz = (jclass) (JniEnv->NewGlobalRef(
            JniEnv->GetObjectClass(this->jPlayerObj)));


    jErroMid = JniEnv->GetMethodID(instance_clazz, "onErro", "(ILjava/lang/String;)V");
}

PlayerJNICall::~PlayerJNICall() {
    if (audioTrackInstance != NULL) {
        JniEnv->DeleteLocalRef(audioTrackInstance);
        audioTrackInstance = NULL;
    }
    if (jPlayerObj != NULL) {
        JniEnv->DeleteGlobalRef(jPlayerObj);
        jPlayerObj = NULL;
    }
    if (instance_clazz != NULL) {
        JniEnv->DeleteGlobalRef(instance_clazz);
        instance_clazz = NULL;

    }

}

/**
* 创建AudioTrack
* @param pEnv
*
*    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
           int bufferSizeInBytes, int mode)
*/

//对象也是线程私有的

void PlayerJNICall::initCrateAudioTrack(JNIEnv *env, int channelConfig, int audioFormat) {
    jmethodID jm_id = env->GetMethodID(instance_clazz, "initAudioTrack",
                                       "(II)Landroid/media/AudioTrack;");


    audioTrackInstance = env->CallObjectMethod(jPlayerObj, jm_id, channelConfig,
                                               audioFormat);


    jclass audioTrackClazz = env->GetObjectClass(audioTrackInstance);


    jmethodID play_mID = env->GetMethodID(audioTrackClazz, "play", "()V");


    jWriteMid = env->GetMethodID(audioTrackClazz, "write", "([BII)I");

    env->CallVoidMethod(audioTrackInstance, play_mID);
}


void PlayerJNICall::callAudioTrackWrite(JNIEnv *env, jbyteArray audioData, int offsetInBytes,
                                        int sizeInBytes) {
    env->CallIntMethod(audioTrackInstance, jWriteMid, audioData, offsetInBytes, sizeInBytes);
}

void PlayerJNICall::callErro(int code, char *msg) {
    jstring jMsg = JniEnv->NewStringUTF(msg);
    JniEnv->CallVoidMethod(audioTrackInstance, jErroMid, code, jMsg);
    JniEnv->ReleaseStringUTFChars(jMsg, msg);
}

你可能感兴趣的:(FFmpeg音频解码播放)