msaaudio_player.cpp 14.5 KB
Newer Older
Andrea Gianarda's avatar
Andrea Gianarda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * aaudio_player.cpp - Android Media Player plugin for Linphone, based on AAudio APIs.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

20 21 22
#include <string.h>

#include "mediastreamer2/msjava.h"
Andrea Gianarda's avatar
Andrea Gianarda committed
23 24 25 26 27
#include <msaaudio/msaaudio.h>

static const int flowControlIntervalMs = 5000;
static const int flowControlThresholdMs = 40;

28 29
static void aaudio_player_callback_error(AAudioStream *stream, void *userData, aaudio_result_t error);

Andrea Gianarda's avatar
Andrea Gianarda committed
30 31 32 33 34 35 36 37 38
struct AAudioOutputContext {
	AAudioOutputContext(MSFilter *f) {
		mFilter = f;
		ms_flow_controlled_bufferizer_init(&buffer, f, DeviceFavoriteSampleRate, 1);
		ms_mutex_init(&mutex, NULL);
		ms_mutex_init(&stream_mutex, NULL);
		soundCard = NULL;
		usage = AAUDIO_USAGE_VOICE_COMMUNICATION;
		content_type = AAUDIO_CONTENT_TYPE_SPEECH;
39 40 41 42
		prevXRunCount = 0;
		bufferCapacity = 0;
		bufferSize = 0;
		framesPerBurst = 0;
Andrea Gianarda's avatar
Andrea Gianarda committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	}

	~AAudioOutputContext() {
		ms_flow_controlled_bufferizer_uninit(&buffer);
		ms_mutex_destroy(&mutex);
		ms_mutex_destroy(&stream_mutex);
	}
	
	void setContext(AAudioContext *context) {
		aaudio_context = context;
		ms_flow_controlled_bufferizer_set_samplerate(&buffer, aaudio_context->samplerate);
		ms_flow_controlled_bufferizer_set_nchannels(&buffer, aaudio_context->nchannels);
		ms_flow_controlled_bufferizer_set_max_size_ms(&buffer, flowControlThresholdMs);
		ms_flow_controlled_bufferizer_set_flow_control_interval_ms(&buffer, flowControlIntervalMs);
	}

	void updateStreamTypeFromMsSndCard() {
		MSSndCardStreamType type = ms_snd_card_get_stream_type(soundCard);
61 62 63
		switch (type) {
			case MS_SND_CARD_STREAM_RING:
				usage = AAUDIO_USAGE_NOTIFICATION_RINGTONE;
Sylvain Berfini's avatar
Sylvain Berfini committed
64
				content_type = AAUDIO_CONTENT_TYPE_MUSIC;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
				ms_message("[AAudio] Using RING mode");
				break;
			case MS_SND_CARD_STREAM_MEDIA:
				usage = AAUDIO_USAGE_MEDIA;
				content_type = AAUDIO_CONTENT_TYPE_MUSIC;
				ms_message("[AAudio] Using MEDIA mode");
				break;
			case MS_SND_CARD_STREAM_DTMF:
				usage = AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
				content_type =  AAUDIO_CONTENT_TYPE_SONIFICATION ;
				ms_message("[AAudio] Using DTMF mode");
				break;
			case MS_SND_CARD_STREAM_VOICE:
				usage = AAUDIO_USAGE_VOICE_COMMUNICATION;
				content_type = AAUDIO_CONTENT_TYPE_SPEECH;
				ms_message("[AAudio] Using COMMUNICATION mode");
				break;
			default:
				ms_error("[AAudio] Unknown stream type %0d", type);
				break;
Andrea Gianarda's avatar
Andrea Gianarda committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
		}
	}

	AAudioContext *aaudio_context;
	AAudioStream *stream;
	ms_mutex_t stream_mutex;

	MSSndCard *soundCard;
	MSFilter *mFilter;
	MSFlowControlledBufferizer buffer;
	int32_t samplesPerFrame;
	ms_mutex_t mutex;
	aaudio_usage_t usage;
	aaudio_content_type_t content_type;
99 100 101 102 103

	int32_t bufferCapacity;
	int32_t prevXRunCount;
	int32_t bufferSize;
	int32_t framesPerBurst;
Andrea Gianarda's avatar
Andrea Gianarda committed
104 105 106 107 108 109 110 111
};

static void android_snd_write_init(MSFilter *obj){
	AAudioOutputContext *octx = new AAudioOutputContext(obj);
	obj->data = octx;
}

static void android_snd_write_uninit(MSFilter *obj){
112 113 114 115 116
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(obj->data);
	if (octx->soundCard) {
		ms_snd_card_unref(octx->soundCard);
		octx->soundCard = NULL;
	}
Andrea Gianarda's avatar
Andrea Gianarda committed
117 118 119 120 121 122 123 124 125
	delete octx;
}

static int android_snd_write_set_sample_rate(MSFilter *obj, void *data) {
	return -1; /*don't accept custom sample rates, use recommended rate always*/
}

static int android_snd_write_get_sample_rate(MSFilter *obj, void *data) {
	int *n = (int*)data;
126
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(obj->data);
Andrea Gianarda's avatar
Andrea Gianarda committed
127 128 129 130 131 132
	*n = octx->aaudio_context->samplerate;
	return 0;
}

static int android_snd_write_set_nchannels(MSFilter *obj, void *data) {
	int *n = (int*)data;
133
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(obj->data);
Andrea Gianarda's avatar
Andrea Gianarda committed
134 135 136 137 138 139 140
	octx->aaudio_context->nchannels = *n;
	ms_flow_controlled_bufferizer_set_nchannels(&octx->buffer, octx->aaudio_context->nchannels);
	return 0;
}

static int android_snd_write_get_nchannels(MSFilter *obj, void *data) {
	int *n = (int*)data;
141
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(obj->data);
Andrea Gianarda's avatar
Andrea Gianarda committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	*n = octx->aaudio_context->nchannels;
	return 0;
}

static aaudio_data_callback_result_t aaudio_player_callback(AAudioStream *stream, void *userData, void *audioData, int32_t numFrames) {
	AAudioOutputContext *octx = (AAudioOutputContext*)userData;
	
	if (numFrames <= 0) {
		ms_error("[AAudio] aaudio_player_callback has %i frames", numFrames);
	}

	ms_mutex_lock(&octx->mutex);
	int ask = sizeof(int16_t) * numFrames * octx->samplesPerFrame;
	int avail = ms_flow_controlled_bufferizer_get_avail(&octx->buffer);
	int bytes = MIN(ask, avail);
	
	if (bytes > 0) {
		ms_flow_controlled_bufferizer_read(&octx->buffer, (uint8_t *)audioData, bytes);
160 161
	}
	if (avail < ask) {
Andrea Gianarda's avatar
Andrea Gianarda committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		memset(static_cast<int16_t *>(audioData) + avail, 0, ask - avail);
	}
	ms_mutex_unlock(&octx->mutex);

	return AAUDIO_CALLBACK_RESULT_CONTINUE;	
}

static void aaudio_player_init(AAudioOutputContext *octx) {
	AAudioStreamBuilder *builder;
	aaudio_result_t result = AAudio_createStreamBuilder(&builder);
	if (result != AAUDIO_OK && !builder) {
		ms_error("[AAudio] Couldn't create stream builder for player: %i / %s", result, AAudio_convertResultToText(result));
	}

	octx->updateStreamTypeFromMsSndCard();
177
	AAudioStreamBuilder_setDeviceId(builder, octx->soundCard->internal_id);
Sylvain Berfini's avatar
Sylvain Berfini committed
178
	ms_message("[AAudio] Using device ID: %s (%i)", octx->soundCard->id, octx->soundCard->internal_id);
Andrea Gianarda's avatar
Andrea Gianarda committed
179 180 181 182 183 184 185 186 187 188
	AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT);
	AAudioStreamBuilder_setSampleRate(builder, octx->aaudio_context->samplerate);
	AAudioStreamBuilder_setDataCallback(builder, aaudio_player_callback, octx);
	AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_I16);
	AAudioStreamBuilder_setChannelCount(builder, octx->aaudio_context->nchannels);
	AAudioStreamBuilder_setSharingMode(builder, AAUDIO_SHARING_MODE_EXCLUSIVE); // If EXCLUSIVE mode isn't available the builder will fall back to SHARED mode.
	AAudioStreamBuilder_setPerformanceMode(builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
	AAudioStreamBuilder_setErrorCallback(builder, aaudio_player_callback_error, octx);
	AAudioStreamBuilder_setUsage(builder, octx->usage); // Requires NDK build target of 28 instead of 26 !
	AAudioStreamBuilder_setContentType(builder, octx->content_type); // Requires NDK build target of 28 instead of 26 !
189

Andrea Gianarda's avatar
Andrea Gianarda committed
190 191 192 193 194 195
	ms_message("[AAudio] Player stream configured with samplerate %i and %i channels", octx->aaudio_context->samplerate, octx->aaudio_context->nchannels);
	
	result = AAudioStreamBuilder_openStream(builder, &(octx->stream));
	if (result != AAUDIO_OK && !octx->stream) {
		ms_error("[AAudio] Open stream for player failed: %i / %s", result, AAudio_convertResultToText(result));
		AAudioStreamBuilder_delete(builder);
196
		octx->stream = NULL;
Andrea Gianarda's avatar
Andrea Gianarda committed
197 198 199 200
		return;
	} else {
		ms_message("[AAudio] Player stream opened");
	}
201

202 203 204 205 206
	aaudio_content_type_t contentType = AAudioStream_getContentType(octx->stream);
	aaudio_usage_t usage = AAudioStream_getUsage(octx->stream);
	ms_message("[AAudio] Expected content type %i, got %i", octx->content_type, contentType);
	ms_message("[AAudio] Expected usage %i, got %i", octx->usage, usage);

207
	octx->framesPerBurst = AAudioStream_getFramesPerBurst(octx->stream);
Andrea Gianarda's avatar
Andrea Gianarda committed
208
	// Set the buffer size to the burst size - this will give us the minimum possible latency
209
	AAudioStream_setBufferSizeInFrames(octx->stream, octx->framesPerBurst * octx->aaudio_context->nchannels * 2);
Andrea Gianarda's avatar
Andrea Gianarda committed
210 211
	octx->samplesPerFrame = AAudioStream_getSamplesPerFrame(octx->stream);

212 213 214 215
	// Current settings
	octx->bufferCapacity = AAudioStream_getBufferCapacityInFrames(octx->stream);
	octx->bufferSize = AAudioStream_getBufferSizeInFrames(octx->stream);

Andrea Gianarda's avatar
Andrea Gianarda committed
216
	result = AAudioStream_requestStart(octx->stream);
Andrea Gianarda's avatar
Andrea Gianarda committed
217

Andrea Gianarda's avatar
Andrea Gianarda committed
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
	if (result != AAUDIO_OK) {
		ms_error("[AAudio] Start stream for player failed: %i / %s", result, AAudio_convertResultToText(result));
		result = AAudioStream_close(octx->stream);
		if (result != AAUDIO_OK) {
			ms_error("[AAudio] Player stream close failed: %i / %s", result, AAudio_convertResultToText(result));
		} else {
			ms_message("[AAudio] Player stream closed");
		}
		octx->stream = NULL;
	} else {
		ms_message("[AAudio] Player stream started");
	}

	AAudioStreamBuilder_delete(builder);
}

static void aaudio_player_close(AAudioOutputContext *octx) {
	ms_mutex_lock(&octx->stream_mutex);
	if (octx->stream) {
		aaudio_result_t result = AAudioStream_requestStop(octx->stream);
		if (result != AAUDIO_OK) {
			ms_error("[AAudio] Player stream stop failed: %i / %s", result, AAudio_convertResultToText(result));
		} else {
			ms_message("[AAudio] Player stream stopped");
		}

		result = AAudioStream_close(octx->stream);
		if (result != AAUDIO_OK) {
			ms_error("[AAudio] Player stream close failed: %i / %s", result, AAudio_convertResultToText(result));
		} else {
			ms_message("[AAudio] Player stream closed");
		}
		octx->stream = NULL;
	}
	ms_mutex_unlock(&octx->stream_mutex);
}

static void aaudio_player_callback_error(AAudioStream *stream, void *userData, aaudio_result_t result) {
	AAudioOutputContext *octx = (AAudioOutputContext *)userData;
	ms_error("[AAudio] aaudio_player_callback_error has result: %i / %s", result, AAudio_convertResultToText(result));
}

static void android_snd_write_preprocess(MSFilter *obj) {
	AAudioOutputContext *octx = (AAudioOutputContext*)obj->data;
	aaudio_player_init(octx);
263 264

	JNIEnv *env = ms_get_jni_env();
265 266
	ms_android_set_bt_enable(env, (ms_snd_card_get_device_type(octx->soundCard) == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_BLUETOOTH));
	ms_android_hack_volume(env);
Andrea Gianarda's avatar
Andrea Gianarda committed
267 268
}

269
static void android_snd_adjust_buffer_size(AAudioOutputContext *octx) {
270 271 272
	// Ensure that stream has been created before adjusting buffer size
	if (octx->stream) {
		int32_t xRunCount = AAudioStream_getXRunCount(octx->stream);
273

274 275
		// If underrunning is getting worse
		if (xRunCount > octx->prevXRunCount) {
276

277 278
			// New buffer size
			int32_t newBufferSize = octx->bufferSize + octx->framesPerBurst;
279

280 281 282 283 284 285
			// Buffer size cannot be bigger than the buffer capacity and it must be larger than 0
			if (octx->bufferCapacity < newBufferSize) {
				newBufferSize = octx->bufferCapacity;
			} else if (newBufferSize <= 0) {
				newBufferSize = 1;
			}
286

287 288
			ms_message("[AAudio] xRunCount %0d - Changing buffer size from %0d to %0d frames (maximum capacity %0d frames)", xRunCount, octx->bufferSize, newBufferSize, octx->bufferCapacity);
			AAudioStream_setBufferSizeInFrames(octx->stream, newBufferSize);
289

290 291 292
			octx->bufferSize = newBufferSize;
			octx->prevXRunCount = xRunCount;
		}
293 294 295
	}
}

Andrea Gianarda's avatar
Andrea Gianarda committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
static void android_snd_write_process(MSFilter *obj) {
	AAudioOutputContext *octx = (AAudioOutputContext*)obj->data;

	ms_mutex_lock(&octx->stream_mutex);
	if (!octx->stream) {
		aaudio_player_init(octx);
	} else {
		aaudio_stream_state_t streamState = AAudioStream_getState(octx->stream);
		if (streamState == AAUDIO_STREAM_STATE_DISCONNECTED) {
			ms_warning("[AAudio] Player stream has disconnected");
			if (octx->stream) {
				AAudioStream_close(octx->stream);
				octx->stream = NULL;
			}
		}
	}
312 313 314

	android_snd_adjust_buffer_size(octx);

Andrea Gianarda's avatar
Andrea Gianarda committed
315 316 317 318 319 320 321 322 323
	ms_mutex_unlock(&octx->stream_mutex);

	ms_mutex_lock(&octx->mutex);
	ms_flow_controlled_bufferizer_put_from_queue(&octx->buffer, obj->inputs[0]);
	ms_mutex_unlock(&octx->mutex);
}

static void android_snd_write_postprocess(MSFilter *obj) {
	AAudioOutputContext *octx = (AAudioOutputContext*)obj->data;
324
	aaudio_player_close(octx);
325 326
	// At the end of a call, postprocess is called therefore here the bluetooth device is disabled
	JNIEnv *env = ms_get_jni_env();
327
	ms_android_set_bt_enable(env, FALSE);
Andrea Gianarda's avatar
Andrea Gianarda committed
328 329
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static int android_snd_write_set_device_id(MSFilter *obj, void *data) {
	MSSndCard *card = (MSSndCard*)data;
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(obj->data);
	ms_message("[AAudio] Requesting to output card. Current %s (device ID %0d) and requested %s (device ID %0d)", ms_snd_card_get_string_id(octx->soundCard), octx->soundCard->internal_id, ms_snd_card_get_string_id(card), card->internal_id);
	// Change device ID only if the new value is different from the previous one
	if (octx->soundCard->internal_id != card->internal_id) {
		ms_mutex_lock(&octx->stream_mutex);
		if (octx->soundCard) {
			ms_snd_card_unref(octx->soundCard);
			octx->soundCard = NULL;
		}
		octx->soundCard = ms_snd_card_ref(card);

		if (octx->stream) {
			AAudioStream_close(octx->stream);
			octx->stream = NULL;
		}

		aaudio_player_init(octx);
		JNIEnv *env = ms_get_jni_env();
350 351
		ms_android_set_bt_enable(env, (ms_snd_card_get_device_type(octx->soundCard) == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_BLUETOOTH));
		ms_android_hack_volume(env);
352 353 354 355 356 357 358 359 360 361 362 363 364

		ms_mutex_unlock(&octx->stream_mutex);
	}
	return 0;
}

static int android_snd_write_get_device_id(MSFilter *obj, void *data) {
	int *n = (int*)data;
	AAudioOutputContext *octx = (AAudioOutputContext*)obj->data;
	*n = octx->soundCard->internal_id;
	return 0;
}

Andrea Gianarda's avatar
Andrea Gianarda committed
365 366 367 368 369
static MSFilterMethod android_snd_write_methods[] = {
	{MS_FILTER_SET_SAMPLE_RATE, android_snd_write_set_sample_rate},
	{MS_FILTER_GET_SAMPLE_RATE, android_snd_write_get_sample_rate},
	{MS_FILTER_SET_NCHANNELS, android_snd_write_set_nchannels},
	{MS_FILTER_GET_NCHANNELS, android_snd_write_get_nchannels},
370 371
	{MS_AUDIO_PLAYBACK_SET_INTERNAL_ID, android_snd_write_set_device_id},
	{MS_AUDIO_PLAYBACK_GET_INTERNAL_ID, android_snd_write_get_device_id},
Andrea Gianarda's avatar
Andrea Gianarda committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	{0,NULL}
};

MSFilterDesc android_snd_aaudio_player_desc = {
	MS_FILTER_PLUGIN_ID,
	"MSAAudioPlayer",
	"android sound output",
	MS_FILTER_OTHER,
	NULL,
	1,
	0,
	android_snd_write_init,
	android_snd_write_preprocess,
	android_snd_write_process,
	android_snd_write_postprocess,
	android_snd_write_uninit,
	android_snd_write_methods
};

// Register aaudio player to the factory
void register_aaudio_player(MSFactory* factory) {
	ms_factory_register_filter(factory, &android_snd_aaudio_player_desc);
}

static MSFilter* ms_android_snd_write_new(MSFactory* factory) {
	MSFilter *f = ms_factory_create_filter_from_desc(factory, &android_snd_aaudio_player_desc);
	return f;
}

MSFilter *android_snd_card_create_writer(MSSndCard *card) {
	MSFilter *f = ms_android_snd_write_new(ms_snd_card_get_factory(card));
	AAudioOutputContext *octx = static_cast<AAudioOutputContext*>(f->data);
404
	octx->soundCard = ms_snd_card_ref(card);
Andrea Gianarda's avatar
Andrea Gianarda committed
405 406 407
	octx->setContext((AAudioContext*)card->data);
	return f;
}