Skip to content

Commit

Permalink
针对AAC-HE,暂时性的提供了个解决方案
Browse files Browse the repository at this point in the history
  • Loading branch information
yetote committed Sep 18, 2019
1 parent 7036e76 commit fc405c3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 24 deletions.
20 changes: 11 additions & 9 deletions app/src/main/cpp/decode/Decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by ether on 2019/8/6.
//

#include <unistd.h>

#include "Decode.h"

// @formatter:off
Expand All @@ -22,17 +22,19 @@ Decode::Decode(const Callback &callback1, PlayStates &playStates1) : callback(ca
}

void Decode::prepare(const std::string &path) {
// if (hardwareDecode->checkSupport(path)) {
// LOGE(Decode_TAG, "%s:支持硬解", __func__);
// callback.callPrepare(Callback::MAIN_THREAD, true, audioPlayer->totalTime);
// LOGE(Decode_TAG, "%s:总时长=%d", __func__, audioPlayer->totalTime);
// } else
if (ffmpegDecode->prepare(path)) {
playStates.setHardware(false);
callback.callPrepare(Callback::MAIN_THREAD, true, audioPlayer->totalTime);
if (ffmpegDecode->prepare(path)) {
if (hardwareDecode->checkSupport(path)) {
LOGE(Decode_TAG, "%s:支持硬解", __func__);
callback.callPrepare(Callback::MAIN_THREAD, true, audioPlayer->totalTime);
LOGE(Decode_TAG, "%s:总时长=%d", __func__, audioPlayer->totalTime);
} else {
playStates.setHardware(false);
callback.callPrepare(Callback::MAIN_THREAD, true, audioPlayer->totalTime);
}
} else {
callback.callPrepare(Callback::MAIN_THREAD, false, 0);
}

}

void Decode::playAudio() {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/cpp/decode/Decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "FFmpegDecode.h"
#include <thread>
#include <android/native_window.h>

#include <unistd.h>

#define Decode_TAG "Decode"
#define LOGE(LOG_TAG, ...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
Expand All @@ -25,7 +25,7 @@ class Decode {

Decode(const Callback &callback, PlayStates &playStates);

void prepare(const std::string& path);
void prepare(const std::string &path);

void playAudio();

Expand Down Expand Up @@ -53,6 +53,8 @@ class Decode {
FFmpegDecode *ffmpegDecode = nullptr;
Callback callback;
PlayStates &playStates;
int32_t sampleRate = 0;
int32_t channelCount = 0;
};


Expand Down
11 changes: 3 additions & 8 deletions app/src/main/cpp/decode/FFmpegDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,16 @@ bool FFmpegDecode::prepare(const std::string path) {
audioPlayer->totalTime = pFmtCtx->duration / AV_TIME_BASE;
LOGE(FFmpegDecode_TAG, "%s:总时长%d", __func__, audioPlayer->totalTime);
audioPlayer->timeBase = pAudioStream->time_base;
if (audioIndex != -1 && audioPlayer != nullptr) {
rst = findCodec(pAudioStream, &audioPlayer->pCodecCtx, &pAudioCodec);
}
return true;
}


void FFmpegDecode::playAudio() {
LOGE(FFmpegDecode_TAG, "%s:fmfpeg", __func__);
int rst = 0;
if (audioIndex != -1 && audioPlayer != nullptr) {
rst = findCodec(pAudioStream, &audioPlayer->pCodecCtx, &pAudioCodec);
}
if (rst < 0) {
isFinish = true;
stop();
return;
}
audioPlayer->init();
audioPlayer->play();
decode();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/decode/FFmpegDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class FFmpegDecode {
std::string vertexCode, std::string fragCode);

void pause();

void resume();

virtual ~FFmpegDecode();


Expand Down
12 changes: 9 additions & 3 deletions app/src/main/cpp/decode/HardwareDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ bool HardwareDecode::checkSupport(std::string _path) {
}
AMediaExtractor_selectTrack(videoInfo->extractor, i);
videoInfo->codec = AMediaCodec_createDecoderByType(mime);
//todo pwindow这时无法获取
// AMediaCodec_configure(videoInfo->codec, pFmt, nullptr, nullptr, 0);
videoInfo->pFmt = AMediaExtractor_getTrackFormat(videoInfo->extractor, i);
videoInfo->isSuccess = true;
Expand All @@ -77,16 +76,23 @@ bool HardwareDecode::checkSupport(std::string _path) {
int64_t totalTime = 0;
AMediaFormat_getInt64(pFmt, "durationUs", &totalTime);
audioPlay->totalTime = totalTime / 1000000;
auto srst = AMediaFormat_getInt32(pFmt, "sample-rate", &sampleRate);
//todo 这里由于无论硬解软解,pcodecCtx都会被初始化,所以需要考虑删除这里设置aaudio的采样率和声道布局
AMediaFormat_setInt32(pFmt, AMEDIAFORMAT_KEY_SAMPLE_RATE,
audioPlay->pCodecCtx->sample_rate);
auto srst = AMediaFormat_getInt32(pFmt, AMEDIAFORMAT_KEY_SAMPLE_RATE, &sampleRate);
if (!srst) {
LOGE(HardwareDecode_TAG, "%s:获取采样率失败", __func__);
sampleRate = 0;
}
auto crst = AMediaFormat_getInt32(pFmt, "channel-count", &channelCount);
AMediaFormat_setInt32(pFmt, AMEDIAFORMAT_KEY_CHANNEL_COUNT,
audioPlay->pCodecCtx->channels);
auto crst = AMediaFormat_getInt32(pFmt, AMEDIAFORMAT_KEY_CHANNEL_COUNT, &channelCount);

if (!crst) {
LOGE(HardwareDecode_TAG, "%s:获取音频通道数失败", __func__);
channelCount = 0;
}
// AMediaFormat_setInt32(pFmt, "aac-profile", AMEDIAFORMAT_KEY_AAC_SBR_MODE);
if (audioInfo) {
audioInfo->extractor = AMediaExtractor_new();
rst = AMediaExtractor_setDataSource(audioInfo->extractor, _path.c_str());
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/yetote/bamboomusic/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ private void initView() {
// musicList.add("http://fs.open.kugou.com/36b4d9283ccf764fa5a2d60eefd01433/5d80d309/G009/M00/08/11/SQ0DAFUMq2WASS7-ADfoAWywlaw669.mp3");
// musicList.add(getExternalFilesDir(null).getPath() + "/test.aac");
// musicList.add(getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath() + "/track1.mp3");
musicList.add(getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath() + "/track1.mp3");
musicList.add(getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath() + "/1.m4a");
musicList.add(getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath() + "/2.mp3");
musicList.add(getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath() + "/3.mp3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ public class MineFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mine, null);
return v;

}
}
5 changes: 4 additions & 1 deletion 问题记录.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
- 这是因为环形数组在判断是否可以写入时没有进行writePos==readPos单纯的认为是读取的速度大于了写入的速度,导致继续写入,覆盖了之前未读取的数据
- [x] 硬解的时候无法播放音频
- 因为MediaExtractor只能同时选择一条轨道,而一般的MP4封装格式获取时,音轨在视轨上面,所以MediaExtractor选择的其实是最后的视轨,所以导致数据不正确,无法播放音频
- [ ] 编码格式为HEAAC的话,硬解获取到的采样率为原采样率的一半
- [x] 编码格式为HEAAC的话,硬解获取到的采样率为原采样率的一半
- 未找到原因,提供下解决方案,需要先用ffmpeg获取争取的sampleRate和channelCount,然后在进行硬解的判断
- [ ] 切换下一首时,不定期会闪退
- [ ] 播放完成之后,无法切换下一首

0 comments on commit fc405c3

Please sign in to comment.