forked from xufuji456/FFmpegAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff_audio_player.cpp
More file actions
282 lines (253 loc) · 9.56 KB
/
ff_audio_player.cpp
File metadata and controls
282 lines (253 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// Created by xu fulong on 2022/9/4.
//
#include "ff_audio_player.h"
#define AUDIO_TAG "AudioPlayer"
#define BUFFER_SIZE (48000 * 10)
const char *FILTER_DESC = "superequalizer=6b=4:8b=5:10b=5";
int initFilter(const char *filterDesc, AVCodecContext *codecCtx, AVFilterGraph **graph,
AVFilterContext **src, AVFilterContext **sink) {
int ret;
char args[512];
AVFilterContext *buffersrc_ctx;
AVFilterContext *buffersink_ctx;
AVRational time_base = codecCtx->time_base;
AVFilterInOut *inputs = avfilter_inout_alloc();
AVFilterInOut *outputs = avfilter_inout_alloc();
const AVFilter *buffersrc = avfilter_get_by_name("abuffer");
const AVFilter *buffersink = avfilter_get_by_name("abuffersink");
AVFilterGraph *filter_graph = avfilter_graph_alloc();
if (!outputs || !inputs || !filter_graph) {
ret = AVERROR(ENOMEM);
goto end;
}
/* buffer audio source: the decoded frames from the decoder will be inserted here. */
if (!codecCtx->channel_layout)
codecCtx->channel_layout = static_cast<uint64_t>(av_get_default_channel_layout(
codecCtx->channels));
snprintf(args, sizeof(args),
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%" PRIx64 "",
time_base.num, time_base.den, codecCtx->sample_rate,
av_get_sample_fmt_name(codecCtx->sample_fmt), codecCtx->channel_layout);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
args, nullptr, filter_graph);
if (ret < 0) {
LOGE(AUDIO_TAG, "Cannot create buffer source:%d", ret);
goto end;
}
/* buffer audio sink: to terminate the filter chain. */
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
nullptr, nullptr, filter_graph);
if (ret < 0) {
LOGE(AUDIO_TAG, "Cannot create buffer sink:%d", ret);
goto end;
}
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = nullptr;
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = nullptr;
if ((ret = avfilter_graph_parse_ptr(filter_graph, filterDesc,
&inputs, &outputs, nullptr)) < 0) {
LOGE(AUDIO_TAG, "avfilter_graph_parse_ptr error:%d", ret);
goto end;
}
if ((ret = avfilter_graph_config(filter_graph, nullptr)) < 0) {
LOGE(AUDIO_TAG, "avfilter_graph_config error:%d", ret);
goto end;
}
*graph = filter_graph;
*src = buffersrc_ctx;
*sink = buffersink_ctx;
end:
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
FFAudioPlayer::FFAudioPlayer() {
m_state = new AudioPlayerState();
}
FFAudioPlayer::~FFAudioPlayer() {
delete m_state;
}
int FFAudioPlayer::open(const char *path) {
if (!path)
return -1;
int ret;
const AVCodec *codec;
m_state->inputFrame = av_frame_alloc();
m_state->packet = av_packet_alloc();
m_state->outBuffer = new uint8_t [BUFFER_SIZE];
// open input stream
ret = avformat_open_input(&m_state->formatContext, path, nullptr, nullptr);
if (ret < 0) {
LOGE(AUDIO_TAG, "avformat_open_input error=%s", av_err2str(ret));
return ret;
}
// (if need)find info: width、height、sample_rate、duration
avformat_find_stream_info(m_state->formatContext, nullptr);
// find audio index
for (int i=0; i<m_state->formatContext->nb_streams; i++) {
if (AVMEDIA_TYPE_AUDIO == m_state->formatContext->streams[i]->codecpar->codec_type) {
m_state->audioIndex = i;
break;
}
}
if (m_state->audioIndex == -1) {
return -1;
}
// find audio decoder
codec = avcodec_find_decoder(m_state->formatContext->streams[m_state->audioIndex]->codecpar->codec_id);
m_state->codecContext = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(m_state->codecContext, m_state->formatContext->streams[m_state->audioIndex]->codecpar);
// open decoder
ret = avcodec_open2(m_state->codecContext, codec, nullptr);
if (ret < 0) {
LOGE(AUDIO_TAG, "avcodec_open2 error=%s", av_err2str(ret));
return ret;
}
// input and output params
int in_sample_rate = m_state->codecContext->sample_rate;
auto in_sample_fmt = m_state->codecContext->sample_fmt;
int in_ch_layout = (int)m_state->codecContext->channel_layout;
m_state->out_sample_rate = in_sample_rate;
m_state->out_sample_fmt = AV_SAMPLE_FMT_S16;
m_state->out_ch_layout = AV_CH_LAYOUT_STEREO;
m_state->out_channel = m_state->codecContext->channels;
// init resample context
m_state->swrContext = swr_alloc();
swr_alloc_set_opts(m_state->swrContext, m_state->out_ch_layout, m_state->out_sample_fmt, m_state->out_sample_rate,
in_ch_layout, in_sample_fmt, in_sample_rate, 0, nullptr);
swr_init(m_state->swrContext);
// init filter graph
m_state->filterFrame = av_frame_alloc();
initFilter(FILTER_DESC, m_state->codecContext, &m_state->audioFilterGraph,
&m_state->audioSrcContext, &m_state->audioSinkContext);
return 0;
}
int FFAudioPlayer::getChannel() const {
return m_state->out_channel;
}
int FFAudioPlayer::getSampleRate() const {
return m_state->out_sample_rate;
}
int FFAudioPlayer::decodeAudio() {
int ret;
std::unique_lock<std::mutex> lock(m_state->m_playMutex);
bool exit = m_state->exitPlaying;
lock.unlock();
if (exit) {
return -1;
}
// demux: read a frame(should be demux thread)
ret = av_read_frame(m_state->formatContext, m_state->packet);
if (ret < 0) {
return ret;
}
// see if audio packet
if (m_state->packet->stream_index != m_state->audioIndex) {
return 0;
}
// decode audio frame(should be decode thread)
ret = avcodec_send_packet(m_state->codecContext, m_state->packet);
if (ret < 0) {
LOGE(AUDIO_TAG, "avcodec_send_packet=%s", av_err2str(ret));
}
ret = avcodec_receive_frame(m_state->codecContext, m_state->inputFrame);
if (ret < 0) {
if (ret == AVERROR(EAGAIN)) {
return 0;
} else {
return ret;
}
}
// change filter
if (m_state->filterAgain) {
m_state->filterAgain = false;
avfilter_graph_free(&m_state->audioFilterGraph);
if ((ret = initFilter(m_state->filterDesc, m_state->codecContext, &m_state->audioFilterGraph,
&m_state->audioSrcContext, &m_state->audioSinkContext)) < 0) {
LOGE(AUDIO_TAG, "init_filter error, ret=%d\n", ret);
return ret;
}
}
// put into filter
ret = av_buffersrc_add_frame(m_state->audioSrcContext, m_state->inputFrame);
if (ret < 0) {
LOGE(AUDIO_TAG, "av_buffersrc_add_frame error=%s", av_err2str(ret));
}
// drain from filter
ret = av_buffersink_get_frame(m_state->audioSinkContext, m_state->filterFrame);
if (ret == AVERROR(EAGAIN)) {
return 0;
} else if (ret == AVERROR_EOF) {
LOGE(AUDIO_TAG, "enf of stream...");
return ret;
} else if (ret < 0) {
LOGE(AUDIO_TAG, "av_buffersink_get_frame error:%s", av_err2str(ret));
return ret;
}
// convert audio format and sample_rate
swr_convert(m_state->swrContext, &m_state->outBuffer, BUFFER_SIZE,
(const uint8_t **)(m_state->filterFrame->data), m_state->filterFrame->nb_samples);
// get buffer size after converting
int buffer_size = av_samples_get_buffer_size(nullptr, m_state->out_channel,
m_state->filterFrame->nb_samples, m_state->out_sample_fmt, 1);
// update position
AVRational time_base = m_state->formatContext->streams[m_state->audioIndex]->time_base;
m_state->m_position = (int64_t)(m_state->filterFrame->pts * av_q2d(time_base) * 1000);
av_frame_unref(m_state->inputFrame);
av_frame_unref(m_state->filterFrame);
av_packet_unref(m_state->packet);
return buffer_size;
}
uint8_t *FFAudioPlayer::getDecodeFrame() const {
return m_state->outBuffer;
}
void FFAudioPlayer::setFilterAgain(bool again) {
m_state->filterAgain = again;
}
void FFAudioPlayer::setFilterDesc(const char *filterDescription) {
m_state->filterDesc = filterDescription;
}
void FFAudioPlayer::setExit(bool exit) {
std::unique_lock<std::mutex> lock(m_state->m_playMutex);
m_state->exitPlaying = exit;
lock.unlock();
}
int64_t FFAudioPlayer::getCurrentPosition() {
return m_state->m_position;
}
int64_t FFAudioPlayer::getDuration() {
if (!m_state || !m_state->formatContext)
return 0;
return m_state->formatContext->duration / AV_TIME_BASE * 1000;
}
void FFAudioPlayer::close() {
if (!m_state)
return;
if (m_state->formatContext) {
avformat_close_input(&m_state->formatContext);
}
if (m_state->codecContext) {
avcodec_free_context(&m_state->codecContext);
}
if (m_state->packet) {
av_packet_free(&m_state->packet);
}
if (m_state->inputFrame) {
av_frame_free(&m_state->inputFrame);
}
if (m_state->swrContext) {
swr_close(m_state->swrContext);
}
avfilter_free(m_state->audioSrcContext);
avfilter_free(m_state->audioSinkContext);
if (m_state->audioFilterGraph) {
avfilter_graph_free(&m_state->audioFilterGraph);
}
delete[] m_state->outBuffer;
}