blob: ecfde157b8e3ff41f376e8cd1502d0a5dbfe06ba [file] [log] [blame]
/*
* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "PAL: API"
#include <set>
#include <unistd.h>
#include <stdlib.h>
#include <PalApi.h>
#include "Stream.h"
#include "Device.h"
#include "ResourceManager.h"
#include "PalCommon.h"
class Stream;
/*
* enable_gcov - Enable gcov for pal
*
* Prerequisites
* Should be call from CATF
*/
/*void enable_gcov()
{
__gcov_flush();
}*/
static void notify_concurrent_stream(pal_stream_type_t type,
pal_stream_direction_t dir,
bool active)
{
std::shared_ptr<ResourceManager> rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG,"Resource manager unavailable");
return;
}
PAL_DBG(LOG_TAG, "Notify concurrent stream type %d, direction %d, active %d",
type, dir, active);
rm->ConcurrentStreamStatus(type, dir, active);
}
/*
* pal_init - Initialize PAL
*
* Return 0 on success or error code otherwise
*
* Prerequisites
* None.
*/
int32_t pal_init(void)
{
PAL_DBG(LOG_TAG, "Enter.");
int32_t ret = 0;
std::shared_ptr<ResourceManager> ri = NULL;
try {
ri = ResourceManager::getInstance();
} catch (const std::exception& e) {
PAL_ERR(LOG_TAG, "pal init failed: %s", e.what());
ret = -EINVAL;
goto exit;
}
ret = ri->initSndMonitor();
if (ret != 0) {
PAL_ERR(LOG_TAG, "snd monitor init failed");
goto exit;
}
ri->init();
ret = ri->initContextManager();
if (ret != 0) {
PAL_ERR(LOG_TAG, "ContextManager init failed, error:%d", ret);
goto exit;
}
exit:
PAL_DBG(LOG_TAG, "Exit. exit status : %d ", ret);
return ret;
}
/*
* pal_deinit - De-initialize PAL
*
* Prerequisites
* PAL must be initialized.
*/
void pal_deinit(void)
{
PAL_DBG(LOG_TAG, "Enter.");
std::shared_ptr<ResourceManager> ri = NULL;
try {
ri = ResourceManager::getInstance();
} catch (const std::exception& e) {
PAL_ERR(LOG_TAG, "ResourceManager::getInstance() failed: %s", e.what());
}
ri->deInitContextManager();
ResourceManager::deinit();
PAL_DBG(LOG_TAG, "Exit.");
return;
}
int32_t pal_stream_open(struct pal_stream_attributes *attributes,
uint32_t no_of_devices, struct pal_device *devices,
uint32_t no_of_modifiers, struct modifier_kv *modifiers,
pal_stream_callback cb, uint64_t cookie,
pal_stream_handle_t **stream_handle)
{
uint64_t *stream = NULL;
Stream *s = NULL;
int status;
struct pal_stream_attributes sAttr;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
if (!attributes) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter, stream type:%d", attributes->type);
try {
s = Stream::create(attributes, devices, no_of_devices, modifiers,
no_of_modifiers);
} catch (const std::exception& e) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Stream create failed: %s", e.what());
Stream::handleStreamException(attributes, cb, cookie);
goto exit;
}
if (!s) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "stream creation failed status %d", status);
goto exit;
}
status = s->open();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_open failed with status %d", status);
if (s->close() != 0) {
PAL_ERR(LOG_TAG, "stream closed failed.");
}
delete s;
goto exit;
}
s->getStreamAttributes(&sAttr);
notify_concurrent_stream(sAttr.type, sAttr.direction, true);
if (cb)
s->registerCallBack(cb, cookie);
rm->initStreamUserCounter(s);
stream = reinterpret_cast<uint64_t *>(s);
*stream_handle = stream;
exit:
PAL_INFO(LOG_TAG, "Exit. Value of stream_handle %pK, status %d", stream, status);
return status;
}
int32_t pal_stream_close(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
struct pal_stream_attributes sAttr;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
status = -EINVAL;
rm->unlockValidStreamMutex();
return status;
}
rm->unlockValidStreamMutex();
s = reinterpret_cast<Stream *>(stream_handle);
s->setCachedState(STREAM_IDLE);
status = s->close();
if (rm->deactivateStreamUserCounter(s)) {
PAL_ERR(LOG_TAG, "stream is being closed by another client");
return 0;
}
if (0 != status) {
PAL_ERR(LOG_TAG, "stream closed failed. status %d", status);
goto exit;
}
exit:
s->getStreamAttributes(&sAttr);
notify_concurrent_stream(sAttr.type, sAttr.direction, false);
delete s;
rm->eraseStreamUserCounter(s);
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_start(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle %pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockValidStreamMutex();
status = s->start();
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "stream start failed. status %d", status);
goto exit;
}
exit:
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_stop(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockValidStreamMutex();
s->setCachedState(STREAM_STOPPED);
status = s->stop();
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "stream stop failed. status : %d", status);
goto exit;
}
exit:
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
ssize_t pal_stream_write(pal_stream_handle_t *stream_handle, struct pal_buffer *buf)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle) || !buf) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->write(buf);
if (status < 0) {
PAL_ERR(LOG_TAG, "stream write failed status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_VERBOSE(LOG_TAG, "Exit. status %d", status);
return status;
}
ssize_t pal_stream_read(pal_stream_handle_t *stream_handle, struct pal_buffer *buf)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle) || !buf) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->read(buf);
if (status < 0) {
PAL_ERR(LOG_TAG, "stream read failed status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_VERBOSE(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_get_param(pal_stream_handle_t *stream_handle,
uint32_t param_id, pal_param_payload **param_payload)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->getParameters(param_id, (void **)param_payload);
if (0 != status) {
PAL_ERR(LOG_TAG, "get parameters failed status %d param_id %u", status, param_id);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_param(pal_stream_handle_t *stream_handle, uint32_t param_id,
pal_param_payload *param_payload)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle, status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK param_id %d", stream_handle,
param_id);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->setParameters(param_id, (void *)param_payload);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "set parameters failed status %d param_id %u", status, param_id);
return status;
}
if (param_id == PAL_PARAM_ID_STOP_BUFFERING) {
PAL_DBG(LOG_TAG, "Buffering stopped, handle deferred LPI<->NLPI switch");
rm->handleDeferredSwitch();
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_volume(pal_stream_handle_t *stream_handle,
struct pal_volume_data *volume)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
if (!stream_handle || !volume || volume->no_of_volpair > PAL_MAX_CHANNELS_SUPPORTED) {
status = -EINVAL;
PAL_ERR(LOG_TAG,"Invalid input parameters status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
return status;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->setVolume(volume);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "setVolume failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_mute(pal_stream_handle_t *stream_handle, bool state)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status = 0;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockValidStreamMutex();
status = s->mute(state);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "mute failed with status %d", status);
return status;
}
exit:
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_pause(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->pause();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_pause failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_resume(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->resume();
if (0 != status) {
PAL_ERR(LOG_TAG, "resume failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_drain(pal_stream_handle_t *stream_handle, pal_drain_type_t type)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockValidStreamMutex();
status = s->drain(type);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "drain failed with status %d", status);
return status;
}
exit:
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_flush(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->flush();
if (0 != status) {
PAL_ERR(LOG_TAG, "flush failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_suspend(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->suspend();
if (0 != status) {
PAL_ERR(LOG_TAG, "suspend failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_buffer_size (pal_stream_handle_t *stream_handle,
pal_buffer_config *in_buffer_cfg,
pal_buffer_config *out_buffer_cfg)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->setBufInfo(in_buffer_cfg, out_buffer_cfg);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_set_buffer_size failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_get_timestamp(pal_stream_handle_t *stream_handle,
struct pal_session_time *stime)
{
Stream *s = NULL;
int status = -EINVAL;
std::shared_ptr<ResourceManager> rm = NULL;
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK\n", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->getTimestamp(stime);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_get_timestamp failed with status %d\n", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "stime->session_time.value_lsw = %u, stime->session_time.value_msw = %u \n", stime->session_time.value_lsw, stime->session_time.value_msw);
PAL_VERBOSE(LOG_TAG, "stime->absolute_time.value_lsw = %u, stime->absolute_time.value_msw = %u \n", stime->absolute_time.value_lsw, stime->absolute_time.value_msw);
PAL_VERBOSE(LOG_TAG, "stime->timestamp.value_lsw = %u, stime->timestamp.value_msw = %u \n", stime->timestamp.value_lsw, stime->timestamp.value_msw);
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_add_remove_effect(pal_stream_handle_t *stream_handle,
pal_audio_effect_t effect, bool enable)
{
Stream *s = NULL;
int status = 0;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->addRemoveEffect(effect, enable);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_add_effect failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_device(pal_stream_handle_t *stream_handle,
uint32_t no_of_devices, struct pal_device *devices)
{
int status = -EINVAL;
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
struct pal_stream_attributes sattr;
struct pal_device_info devinfo = {};
struct pal_device *pDevices = NULL;
std::vector <std::shared_ptr<Device>> aDevices;
std::vector <struct pal_device> palDevices;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
if (no_of_devices == 0 || !devices) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid device status %d", status);
return status;
}
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
return status;
}
/* Choose best device config for this stream */
/* TODO: Decide whether to update device config or not based on flag */
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
s->getStreamAttributes(&sattr);
// device switch will be handled in global param setting for SVA
if (sattr.type == PAL_STREAM_VOICE_UI) {
PAL_DBG(LOG_TAG,
"Device switch handles in global param set, skip here");
goto exit;
}
if (sattr.type == PAL_STREAM_VOICE_CALL_RECORD ||
sattr.type == PAL_STREAM_VOICE_CALL_MUSIC) {
PAL_DBG(LOG_TAG,
"Device switch skipped for Incall record/music stream");
status = 0;
goto exit;
}
s->getAssociatedPalDevices(palDevices);
s->getAssociatedDevices(aDevices);
if (!palDevices.empty() && !aDevices.empty()) {
std::set<pal_device_id_t> activeDevices, curPalDevices;
std::set<pal_device_id_t> newDevices;
bool force_switch = s->isA2dpMuted();
for (auto &dev : aDevices)
activeDevices.insert((pal_device_id_t)dev->getSndDeviceId());
for (auto palDev: palDevices) {
curPalDevices.insert(palDev.id);
// check if custom key matches for stream associated pal device
for (int i = 0; i < no_of_devices; i++) {
if (palDev.id == devices[i].id) {
if (strcmp(devices[i].custom_config.custom_key,
palDev.custom_config.custom_key) != 0) {
PAL_DBG(LOG_TAG, "diff custom key found, force device switch");
force_switch = true;
}
break;
}
}
if (force_switch)
break;
}
if (!force_switch) {
for (int i = 0; i < no_of_devices; i++) {
newDevices.insert(devices[i].id);
if ((devices[i].id == PAL_DEVICE_OUT_BLUETOOTH_A2DP) ||
(devices[i].id == PAL_DEVICE_OUT_BLUETOOTH_SCO) ||
(devices[i].id == PAL_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
PAL_DBG(LOG_TAG, "always switch device for bt device");
force_switch = true;
break;
}
}
}
if (!force_switch && (activeDevices == newDevices) &&
(curPalDevices == newDevices)) {
status = 0;
PAL_DBG(LOG_TAG, "devices are same, no need to switch");
goto exit;
}
}
pDevices = (struct pal_device *) calloc(no_of_devices, sizeof(struct pal_device));
if (!pDevices) {
status = -ENOMEM;
PAL_ERR(LOG_TAG, "Memory alloc failed");
goto exit;
}
ar_mem_cpy(pDevices, no_of_devices * sizeof(struct pal_device),
devices, no_of_devices * sizeof(struct pal_device));
for (int i = 0; i < no_of_devices; i++) {
if (strlen(pDevices[i].custom_config.custom_key)) {
PAL_DBG(LOG_TAG, "Device has custom key %s",
pDevices[i].custom_config.custom_key);
} else {
PAL_DBG(LOG_TAG, "Device has no custom key");
strlcpy(pDevices[i].custom_config.custom_key, "", PAL_MAX_CUSTOM_KEY_SIZE);
}
status = rm->getDeviceConfig((struct pal_device *)&pDevices[i], &sattr);
if (status) {
PAL_ERR(LOG_TAG, "Failed to get Device config, err: %d", status);
goto exit;
}
}
// TODO: Check with RM if the same device is being used by other stream with different
// configuration then update corresponding stream device configuration also based on priority.
PAL_DBG(LOG_TAG, "Stream handle :%pK no_of_devices %d first_device id %d",
stream_handle, no_of_devices, pDevices[0].id);
status = s->switchDevice(s, no_of_devices, pDevices);
if (0 != status) {
PAL_ERR(LOG_TAG, "failed with status %d", status);
goto exit;
}
exit:
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
if (pDevices)
free(pDevices);
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_get_tags_with_module_info(pal_stream_handle_t *stream_handle,
size_t *size, uint8_t *payload)
{
int status = 0;
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->getTagsWithModuleInfo(size, payload);
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. Stream handle: %pK, status %d", stream_handle, status);
return status;
}
int32_t pal_set_param(uint32_t param_id, void *param_payload,
size_t payload_size)
{
PAL_DBG(LOG_TAG, "Enter: param id %d", param_id);
int status = 0;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (rm) {
status = rm->setParameter(param_id, param_payload, payload_size);
if (0 != status) {
PAL_ERR(LOG_TAG, "Failed to set global parameter %u, status %d",
param_id, status);
}
} else {
PAL_ERR(LOG_TAG, "Pal has not been initialized yet");
status = -EINVAL;
}
PAL_DBG(LOG_TAG, "Exit, status %d", status);
return status;
}
int32_t pal_get_param(uint32_t param_id, void **param_payload,
size_t *payload_size, void *query)
{
int status = 0;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
PAL_DBG(LOG_TAG, "Enter:");
if (rm) {
status = rm->getParameter(param_id, param_payload, payload_size, query);
if (0 != status) {
PAL_ERR(LOG_TAG, "Failed to get global parameter %u, status %d",
param_id, status);
}
} else {
PAL_ERR(LOG_TAG, "Pal has not been initialized yet");
status = -EINVAL;
}
PAL_DBG(LOG_TAG, "Exit, status %d", status);
return status;
}
int32_t pal_stream_get_mmap_position(pal_stream_handle_t *stream_handle,
struct pal_mmap_position *position)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->GetMmapPosition(position);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_get_mmap_position failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_create_mmap_buffer(pal_stream_handle_t *stream_handle,
int32_t min_size_frames,
struct pal_mmap_buffer *info)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockValidStreamMutex();
if (!stream_handle || !rm->isActiveStream(stream_handle)) {
rm->unlockValidStreamMutex();
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockValidStreamMutex();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockValidStreamMutex();
status = s->createMmapBuffer(min_size_frames, info);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_create_mmap_buffer failed with status %d", status);
}
rm->lockValidStreamMutex();
rm->decreaseStreamUserCounter(s);
rm->unlockValidStreamMutex();
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_register_global_callback(pal_global_callback cb, uint64_t cookie)
{
std::shared_ptr<ResourceManager> rm = NULL;
PAL_DBG(LOG_TAG, "Enter. global callback %pK", cb);
rm = ResourceManager::getInstance();
if (cb != NULL) {
rm->globalCb = cb;
rm->cookie = cookie;
}
PAL_DBG(LOG_TAG, "Exit");
return 0;
}
int32_t pal_gef_rw_param(uint32_t param_id, void *param_payload,
size_t payload_size, pal_device_id_t pal_device_id,
pal_stream_type_t pal_stream_type, unsigned int dir)
{
int status = 0;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
PAL_DBG(LOG_TAG, "Enter.");
if (rm) {
if (GEF_PARAM_WRITE == dir) {
status = rm->setParameter(param_id, param_payload, payload_size,
pal_device_id, pal_stream_type);
if (0 != status) {
PAL_ERR(LOG_TAG, "Failed to set global parameter %u, status %d",
param_id, status);
}
} else {
status = rm->getParameter(param_id, param_payload, payload_size,
pal_device_id, pal_stream_type);
if (0 != status) {
PAL_ERR(LOG_TAG, "Failed to set global parameter %u, status %d",
param_id, status);
}
}
} else {
PAL_ERR(LOG_TAG, "Pal has not been initialized yet");
status = -EINVAL;
}
PAL_DBG(LOG_TAG, "Exit:");
return status;
}
int32_t pal_gef_rw_param_acdb(uint32_t param_id __unused, void *param_payload,
size_t payload_size __unused, pal_device_id_t pal_device_id,
pal_stream_type_t pal_stream_type, uint32_t sample_rate,
uint32_t instance_id, uint32_t dir, bool is_play )
{
int status = 0;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
PAL_DBG(LOG_TAG, "Enter.");
if (rm) {
status = rm->rwParameterACDB(param_id, param_payload, payload_size,
pal_device_id, pal_stream_type,
sample_rate, instance_id, dir, is_play);
if (0 != status) {
PAL_ERR(LOG_TAG, "Failed to rw global parameter %u, status %d",
param_id, status);
}
} else {
PAL_ERR(LOG_TAG, "Pal has not been initialized yet");
status = -EINVAL;
}
PAL_DBG(LOG_TAG, "Exit, status %d", status);
return status;
}