FFmpeg音频解码播放

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


音频解码流程.png

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

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

#include "YBFFmpeg.h"
#include "android_log.h"
#include "PlayerContan.h"
#include <pthread.h>

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);
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容