blob: c2dad940b345ffdc31da40b8c53025a1f943381d [file] [log] [blame]
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android/log.h>
#include <oboe/Oboe.h>
#include "OboeStream.h"
static const char * const TAG = "OboePlayer(native)";
using namespace oboe;
StreamBase::Result OboeStream::OboeErrorToMegaAudioError(oboe::Result oboeError) {
StreamBase::Result maErr = ERROR_UNKNOWN;
switch (oboeError) {
case oboe::Result::OK:
maErr = OK;
break;
case oboe::Result::ErrorInternal:
maErr = ERROR_UNKNOWN;
break;
case oboe::Result::ErrorClosed:
maErr = ERROR_INVALID_STATE;
break;
default:
maErr = ERROR_UNKNOWN;
break;
}
return maErr;
}
StreamBase::Result OboeStream::teardownStream() {
__android_log_print(ANDROID_LOG_INFO, TAG, "teardownStream()");
std::lock_guard<std::mutex> lock(mStreamLock);
return teardownStream_l();
}
StreamBase::Result OboeStream::teardownStream_l() {
// tear down the player
if (mAudioStream == nullptr) {
return ERROR_INVALID_STATE;
} else {
oboe::Result result = oboe::Result::OK;
result = mAudioStream->stop();
if (result == oboe::Result::OK) {
result = mAudioStream->close();
}
mAudioStream = nullptr;
return OboeErrorToMegaAudioError(result);
}
}
StreamBase::Result OboeStream::startStream() {
__android_log_print(ANDROID_LOG_INFO, TAG, "startStream()");
// Don't cover up (potential) bugs in AAudio
oboe::OboeGlobals::setWorkaroundsEnabled(false);
std::lock_guard<std::mutex> lock(mStreamLock);
oboe::Result result = oboe::Result::ErrorInternal;
if (mAudioStream != nullptr) {
result = mAudioStream->requestStart();
if (result != oboe::Result::OK){
__android_log_print(
ANDROID_LOG_ERROR,
TAG,
"requestStart failed. Error: %s", convertToText(result));
// clean up
teardownStream_l();
}
}
mStreamStarted = result == oboe::Result::OK;
return OboeErrorToMegaAudioError(result);
}
StreamBase::Result OboeStream::stopStream() {
std::lock_guard<std::mutex> lock(mStreamLock);
Result errCode = ERROR_UNKNOWN;
if (mAudioStream == nullptr) {
errCode = ERROR_INVALID_STATE;
} else {
oboe::Result result = mAudioStream->stop();
mStreamStarted = false;
errCode = OboeErrorToMegaAudioError(result);
}
mStreamStarted = false;
return errCode;
}