rename parameter

dev
xufuji456 3 years ago
parent 7ccecadd06
commit 436fd27021
  1. 100
      app/src/main/cpp/audio_player.cpp

@ -28,101 +28,101 @@ extern "C" {
#define SLEEP_TIME 1000 * 16 #define SLEEP_TIME 1000 * 16
#define MAX_AUDIO_FRAME_SIZE 48000 * 4 #define MAX_AUDIO_FRAME_SIZE 48000 * 4
int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink, int init_volume_filter(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink,
uint64_t channel_layout, AVSampleFormat inputFormat, int sample_rate) { uint64_t channel_layout, AVSampleFormat inputFormat, int sample_rate) {
AVFilterGraph *filter_graph; AVFilterGraph *filter_graph;
AVFilterContext *abuffer_ctx; AVFilterContext *buffer_ctx;
const AVFilter *abuffer; const AVFilter *buffer;
AVFilterContext *volume_ctx; AVFilterContext *volume_ctx;
const AVFilter *volume; const AVFilter *volume;
AVFilterContext *abuffersink_ctx; AVFilterContext *buffersink_ctx;
const AVFilter *abuffersink; const AVFilter *buffersink;
AVDictionary *options_dict = NULL; AVDictionary *options_dict = NULL;
uint8_t ch_layout[64]; uint8_t ch_layout[64];
int err; int ret;
/* Create a new filter graph, which will contain all the filters. */ /* Create a new filter graph, which will contain all the filters. */
filter_graph = avfilter_graph_alloc(); filter_graph = avfilter_graph_alloc();
if (!filter_graph) { if (!filter_graph) {
LOGE(TAG, "Unable to create filter graph:%d", stderr); LOGE(TAG, "Unable to create filter graph...");
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
/* Create the abuffer filter: feed data into the graph. */ /* Create the abuffer filter: feed data into the graph. */
abuffer = avfilter_get_by_name("abuffer"); buffer = avfilter_get_by_name("abuffer");
if (!abuffer) { if (!buffer) {
LOGE(TAG, "Could not find the abuffer filter:%d", stderr); LOGE(TAG, "Could not find the buffer filter...");
return AVERROR_FILTER_NOT_FOUND; return AVERROR_FILTER_NOT_FOUND;
} }
abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src"); buffer_ctx = avfilter_graph_alloc_filter(filter_graph, buffer, "src");
if (!abuffer_ctx) { if (!buffer_ctx) {
LOGE(TAG, "Could not allocate the abuffer instance:%d", stderr); LOGE(TAG, "Could not allocate the buffer instance...");
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
/* Set the filter options through the AVOptions API. */ /* Set the filter options through the AVOptions API. */
av_get_channel_layout_string(reinterpret_cast<char *>(ch_layout), sizeof(ch_layout), 0, channel_layout); av_get_channel_layout_string(reinterpret_cast<char *>(ch_layout), sizeof(ch_layout), 0, channel_layout);
av_opt_set (abuffer_ctx, "channel_layout", reinterpret_cast<char *>(ch_layout), AV_OPT_SEARCH_CHILDREN); av_opt_set (buffer_ctx, "channel_layout", reinterpret_cast<char *>(ch_layout), AV_OPT_SEARCH_CHILDREN);
av_opt_set (abuffer_ctx, "sample_fmt", av_get_sample_fmt_name(inputFormat), AV_OPT_SEARCH_CHILDREN); av_opt_set (buffer_ctx, "sample_fmt", av_get_sample_fmt_name(inputFormat), AV_OPT_SEARCH_CHILDREN);
av_opt_set_q (abuffer_ctx, "time_base", (AVRational){ 1, sample_rate }, AV_OPT_SEARCH_CHILDREN); av_opt_set_q (buffer_ctx, "time_base", (AVRational){ 1, sample_rate }, AV_OPT_SEARCH_CHILDREN);
av_opt_set_int(abuffer_ctx, "sample_rate", sample_rate, AV_OPT_SEARCH_CHILDREN); av_opt_set_int(buffer_ctx, "sample_rate", sample_rate, AV_OPT_SEARCH_CHILDREN);
err = avfilter_init_str(abuffer_ctx, NULL); ret = avfilter_init_str(buffer_ctx, NULL);
if (err < 0) { if (ret < 0) {
LOGE(TAG, "Could not initialize the abuffer filter:%d", stderr); LOGE(TAG, "Could not initialize the buffer filter:%d", ret);
return err; return ret;
} }
/* Create volume filter. */ /* Create volume filter. */
volume = avfilter_get_by_name("volume"); volume = avfilter_get_by_name("volume");
if (!volume) { if (!volume) {
LOGE(TAG, "Could not find the volume filter:%d", stderr); LOGE(TAG, "Could not find the volume filter...");
return AVERROR_FILTER_NOT_FOUND; return AVERROR_FILTER_NOT_FOUND;
} }
volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume"); volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
if (!volume_ctx) { if (!volume_ctx) {
LOGE(TAG, "Could not allocate the volume instance:%d", stderr); LOGE(TAG, "Could not allocate the volume instance...");
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
/* Passing the options is as key/value pairs in a dictionary. */ /* Passing the options is as key/value pairs in a dictionary. */
av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0); av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
err = avfilter_init_dict(volume_ctx, &options_dict); ret = avfilter_init_dict(volume_ctx, &options_dict);
av_dict_free(&options_dict); av_dict_free(&options_dict);
if (err < 0) { if (ret < 0) {
LOGE(TAG, "Could not initialize the volume filter:%d", stderr); LOGE(TAG, "Could not initialize the volume filter:%d", ret);
return err; return ret;
} }
/* Create the abuffersink filter: get the filtered data from graph. */ /* Create the abuffersink filter: get the filtered data from graph. */
abuffersink = avfilter_get_by_name("abuffersink"); buffersink = avfilter_get_by_name("abuffersink");
if (!abuffersink) { if (!buffersink) {
LOGE(TAG, "Could not find the abuffersink filter:%d", stderr); LOGE(TAG, "Could not find the abuffersink filter...");
return AVERROR_FILTER_NOT_FOUND; return AVERROR_FILTER_NOT_FOUND;
} }
abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink"); buffersink_ctx = avfilter_graph_alloc_filter(filter_graph, buffersink, "sink");
if (!abuffersink_ctx) { if (!buffersink_ctx) {
LOGE(TAG, "Could not allocate the abuffersink instance:%d", stderr); LOGE(TAG, "Could not allocate the abuffersink instance...");
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
err = avfilter_init_str(abuffersink_ctx, NULL); ret = avfilter_init_str(buffersink_ctx, NULL);
if (err < 0) { if (ret < 0) {
LOGE(TAG, "Could not initialize the abuffersink instance:%d", stderr); LOGE(TAG, "Could not initialize the abuffersink instance:%d", ret);
return err; return ret;
} }
/* Connect the filters */ /* Connect the filters */
err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0); ret = avfilter_link(buffer_ctx, 0, volume_ctx, 0);
if (err >= 0) if (ret >= 0)
err = avfilter_link(volume_ctx, 0, abuffersink_ctx, 0); ret = avfilter_link(volume_ctx, 0, buffersink_ctx, 0);
if (err < 0) { if (ret < 0) {
LOGE(TAG, "Error connecting filters:%d", stderr); LOGE(TAG, "Error connecting filters:%d", ret);
return err; return ret;
} }
/* Configure the graph. */ /* Configure the graph. */
err = avfilter_graph_config(filter_graph, NULL); ret = avfilter_graph_config(filter_graph, NULL);
if (err < 0) { if (ret < 0) {
LOGE(TAG, "Error configuring the filter graph:%d", err); LOGE(TAG, "Error configuring the filter graph:%d", ret);
return err; return ret;
} }
*graph = filter_graph; *graph = filter_graph;
*src = abuffer_ctx; *src = buffer_ctx;
*sink = abuffersink_ctx; *sink = buffersink_ctx;
return 0; return 0;
} }
@ -203,7 +203,7 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr) {
/* Set up the filter graph. */ /* Set up the filter graph. */
AVFrame *filter_frame = av_frame_alloc(); AVFrame *filter_frame = av_frame_alloc();
ret = init_filter_graph(&audioFilterGraph, &audioSrcContext, &audioSinkContext, ret = init_volume_filter(&audioFilterGraph, &audioSrcContext, &audioSinkContext,
in_ch_layout, in_sample_fmt, in_sample_rate); in_ch_layout, in_sample_fmt, in_sample_rate);
if (ret < 0) { if (ret < 0) {
LOGE(TAG, "Unable to init filter graph:%d", stderr); LOGE(TAG, "Unable to init filter graph:%d", stderr);

Loading…
Cancel
Save