Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 1 | /* |
Aniket Kumar Lata | 25f2d52 | 2021-05-21 09:37:17 -0700 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved. |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
Ankit Mishra | 7e2b3bb | 2022-07-11 15:03:33 +0530 | [diff] [blame] | 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ankit Mishra | 7e2b3bb | 2022-07-11 15:03:33 +0530 | [diff] [blame] | 28 | * |
| 29 | * Changes from Qualcomm Innovation Center are provided under the following license: |
| 30 | * |
| 31 | * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. |
| 32 | * SPDX-License-Identifier: BSD-3-Clause-Clear |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #define LOG_TAG "StreamNonTunnel" |
| 36 | |
| 37 | #include "Session.h" |
| 38 | #include "kvh2xml.h" |
| 39 | #include "SessionGsl.h" |
| 40 | #include "ResourceManager.h" |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | static void handleSessionCallBack(uint64_t hdl, uint32_t event_id, void *data, |
Ganapathiraju Sarath Varma | cccd419 | 2022-06-22 16:39:19 +0530 | [diff] [blame] | 44 | uint32_t event_size, uint32_t miid __unused) |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 45 | { |
| 46 | Stream *s = NULL; |
| 47 | s = reinterpret_cast<Stream *>(hdl); |
| 48 | if (s->streamCb) |
| 49 | s->streamCb(reinterpret_cast<pal_stream_handle_t *>(s), event_id, (uint32_t *)data, |
| 50 | event_size, s->cookie); |
| 51 | } |
| 52 | |
| 53 | StreamNonTunnel::StreamNonTunnel(const struct pal_stream_attributes *sattr, struct pal_device *dattr __unused, |
| 54 | const uint32_t no_of_devices __unused, const struct modifier_kv *modifiers, |
| 55 | const uint32_t no_of_modifiers, const std::shared_ptr<ResourceManager> rm) |
| 56 | { |
| 57 | mStreamMutex.lock(); |
| 58 | uint32_t in_channels = 0, out_channels = 0; |
| 59 | uint32_t attribute_size = 0; |
| 60 | if (!sattr) { |
| 61 | PAL_ERR(LOG_TAG,"invalid arguments"); |
| 62 | mStreamMutex.unlock(); |
| 63 | throw std::runtime_error("invalid arguments"); |
| 64 | } |
| 65 | |
| 66 | if (rm->cardState == CARD_STATUS_OFFLINE) { |
| 67 | PAL_ERR(LOG_TAG, "Sound card offline, can not create stream"); |
| 68 | usleep(SSR_RECOVERY); |
| 69 | mStreamMutex.unlock(); |
| 70 | throw std::runtime_error("Sound card offline"); |
| 71 | } |
| 72 | |
| 73 | session = NULL; |
| 74 | mStreamAttr = (struct pal_stream_attributes *)nullptr; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 75 | inMaxMetadataSz = 0; |
| 76 | outMaxMetadataSz = 0; |
| 77 | mDevices.clear(); |
| 78 | currentState = STREAM_IDLE; |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 79 | ssrInNTMode = false; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 80 | //Modify cached values only at time of SSR down. |
| 81 | cachedState = STREAM_IDLE; |
| 82 | |
| 83 | PAL_DBG(LOG_TAG, "Enter"); |
| 84 | |
| 85 | //TBD handle modifiers later |
| 86 | mNoOfModifiers = 0; //no_of_modifiers; |
| 87 | mModifiers = (struct modifier_kv *) (NULL); |
| 88 | std::ignore = modifiers; |
| 89 | std::ignore = no_of_modifiers; |
| 90 | |
| 91 | |
| 92 | attribute_size = sizeof(struct pal_stream_attributes); |
| 93 | mStreamAttr = (struct pal_stream_attributes *) calloc(1, attribute_size); |
| 94 | if (!mStreamAttr) { |
| 95 | PAL_ERR(LOG_TAG, "malloc for stream attributes failed %s", strerror(errno)); |
| 96 | mStreamMutex.unlock(); |
| 97 | throw std::runtime_error("failed to malloc for stream attributes"); |
| 98 | } |
| 99 | |
| 100 | ar_mem_cpy(mStreamAttr, sizeof(pal_stream_attributes), sattr, sizeof(pal_stream_attributes)); |
| 101 | |
| 102 | if (mStreamAttr->in_media_config.ch_info.channels > PAL_MAX_CHANNELS_SUPPORTED) { |
| 103 | PAL_ERR(LOG_TAG,"in_channels is invalid %d", in_channels); |
| 104 | mStreamAttr->in_media_config.ch_info.channels = PAL_MAX_CHANNELS_SUPPORTED; |
| 105 | } |
| 106 | if (mStreamAttr->out_media_config.ch_info.channels > PAL_MAX_CHANNELS_SUPPORTED) { |
| 107 | PAL_ERR(LOG_TAG,"out_channels is invalid %d", out_channels); |
| 108 | mStreamAttr->out_media_config.ch_info.channels = PAL_MAX_CHANNELS_SUPPORTED; |
| 109 | } |
| 110 | |
| 111 | PAL_VERBOSE(LOG_TAG, "Create new Session"); |
| 112 | session = Session::makeSession(rm, sattr); |
| 113 | if (!session) { |
| 114 | PAL_ERR(LOG_TAG, "session creation failed"); |
| 115 | free(mStreamAttr); |
| 116 | mStreamMutex.unlock(); |
| 117 | throw std::runtime_error("failed to create session object"); |
| 118 | } |
| 119 | |
| 120 | session->registerCallBack(handleSessionCallBack, (uint64_t)this); |
| 121 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 122 | mStreamMutex.unlock(); |
Ritu Sharma | 3ca101a | 2021-05-31 13:21:30 +0530 | [diff] [blame] | 123 | rm->registerStream(this); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 124 | PAL_DBG(LOG_TAG, "Exit. state %d", currentState); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | StreamNonTunnel::~StreamNonTunnel() |
| 129 | { |
| 130 | rm->resetStreamInstanceID(this); |
Jaideep Sharma | 79937c5 | 2021-05-27 10:56:17 +0530 | [diff] [blame] | 131 | rm->deregisterStream(this); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 132 | if (mStreamAttr) { |
| 133 | free(mStreamAttr); |
| 134 | mStreamAttr = (struct pal_stream_attributes *)NULL; |
| 135 | } |
Jaideep Sharma | 79937c5 | 2021-05-27 10:56:17 +0530 | [diff] [blame] | 136 | delete session; |
| 137 | session = nullptr; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int32_t StreamNonTunnel::open() |
| 141 | { |
| 142 | int32_t status = 0; |
| 143 | |
| 144 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 145 | if (rm->cardState == CARD_STATUS_OFFLINE || ssrInNTMode == true) { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 146 | PAL_ERR(LOG_TAG, "Sound card offline, can not open stream"); |
| 147 | usleep(SSR_RECOVERY); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 148 | status = -ENETRESET; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 149 | goto exit; |
| 150 | } |
| 151 | |
| 152 | if (currentState == STREAM_IDLE) { |
| 153 | PAL_VERBOSE(LOG_TAG, "Enter. session handle - %pK", session); |
| 154 | status = session->open(this); |
| 155 | if (0 != status) { |
| 156 | PAL_ERR(LOG_TAG, "session open failed with status %d", status); |
| 157 | goto exit; |
| 158 | } |
| 159 | PAL_VERBOSE(LOG_TAG, "session open successful"); |
| 160 | |
| 161 | currentState = STREAM_INIT; |
| 162 | PAL_DBG(LOG_TAG, "Exit. streamLL opened. state %d", currentState); |
| 163 | } else if (currentState == STREAM_INIT) { |
| 164 | PAL_INFO(LOG_TAG, "Stream is already opened, state %d", currentState); |
| 165 | status = 0; |
| 166 | goto exit; |
| 167 | } else { |
| 168 | PAL_ERR(LOG_TAG, "Stream is not in correct state %d", currentState); |
| 169 | //TBD : which error code to return here. |
| 170 | status = -EINVAL; |
| 171 | goto exit; |
| 172 | } |
| 173 | exit: |
| 174 | mStreamMutex.unlock(); |
| 175 | return status; |
| 176 | } |
| 177 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 178 | int32_t StreamNonTunnel::close() |
| 179 | { |
| 180 | int32_t status = 0; |
| 181 | mStreamMutex.lock(); |
| 182 | |
| 183 | PAL_INFO(LOG_TAG, "Enter. session handle - %pK state %d", |
| 184 | session, currentState); |
| 185 | |
| 186 | if (currentState == STREAM_IDLE) { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 187 | PAL_VERBOSE(LOG_TAG, "closed the devices successfully"); |
| 188 | goto exit; |
| 189 | } else if (currentState == STREAM_STARTED || currentState == STREAM_PAUSED) { |
| 190 | status = stop(); |
| 191 | if (0 != status) |
| 192 | PAL_ERR(LOG_TAG, "stream stop failed. status %d", status); |
| 193 | } |
| 194 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 195 | status = session->close(this); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 196 | if (0 != status) { |
| 197 | PAL_ERR(LOG_TAG, "session close failed with status %d", status); |
| 198 | } |
| 199 | |
| 200 | exit: |
| 201 | currentState = STREAM_IDLE; |
| 202 | mStreamMutex.unlock(); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 203 | PAL_INFO(LOG_TAG, "Exit. closed the stream successfully %d status %d", |
| 204 | currentState, status); |
| 205 | return status; |
| 206 | } |
| 207 | |
| 208 | //TBD: move this to Stream, why duplicate code? |
| 209 | int32_t StreamNonTunnel::start() |
| 210 | { |
| 211 | int32_t status = 0; |
| 212 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 213 | if (rm->cardState == CARD_STATUS_OFFLINE || ssrInNTMode == true) { |
| 214 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 215 | currentState); |
| 216 | status = -ENETRESET; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 217 | goto exit; |
| 218 | } |
| 219 | |
| 220 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK mStreamAttr->direction - %d state %d", |
| 221 | session, mStreamAttr->direction, currentState); |
| 222 | |
Aniket Kumar Lata | 25f2d52 | 2021-05-21 09:37:17 -0700 | [diff] [blame] | 223 | if (currentState == STREAM_INIT || currentState == STREAM_STOPPED || |
| 224 | currentState == STREAM_SUSPENDED) { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 225 | status = session->prepare(this); |
| 226 | if (0 != status) { |
| 227 | PAL_ERR(LOG_TAG, "Rx session prepare is failed with status %d", |
| 228 | status); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 229 | goto exit; |
| 230 | } |
| 231 | PAL_VERBOSE(LOG_TAG, "session prepare successful"); |
| 232 | |
| 233 | status = session->start(this); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 234 | if (status == -ENETRESET && |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 235 | rm->cardState != CARD_STATUS_OFFLINE) { |
| 236 | PAL_ERR(LOG_TAG, "Sound card offline, informing RM"); |
| 237 | rm->ssrHandler(CARD_STATUS_OFFLINE); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 238 | goto exit; |
| 239 | } |
| 240 | if (0 != status) { |
| 241 | PAL_ERR(LOG_TAG, "Rx session start is failed with status %d", |
| 242 | status); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 243 | goto exit; |
| 244 | } |
| 245 | PAL_VERBOSE(LOG_TAG, "session start successful"); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 246 | currentState = STREAM_STARTED; |
| 247 | } else if (currentState == STREAM_STARTED) { |
| 248 | PAL_INFO(LOG_TAG, "Stream already started, state %d", currentState); |
| 249 | goto exit; |
| 250 | } else { |
| 251 | PAL_ERR(LOG_TAG, "Stream is not opened yet"); |
| 252 | status = -EINVAL; |
| 253 | goto exit; |
| 254 | } |
| 255 | PAL_DBG(LOG_TAG, "Exit. state %d", currentState); |
| 256 | |
| 257 | exit: |
| 258 | mStreamMutex.unlock(); |
| 259 | return status; |
| 260 | } |
| 261 | |
| 262 | //TBD: move this to Stream, why duplicate code? |
| 263 | int32_t StreamNonTunnel::stop() |
| 264 | { |
| 265 | int32_t status = 0; |
| 266 | |
| 267 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 268 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK mStreamAttr->direction - %d state %d", |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 269 | session, mStreamAttr->direction, currentState); |
| 270 | |
| 271 | if (currentState == STREAM_STARTED || currentState == STREAM_PAUSED) { |
| 272 | status = session->stop(this); |
| 273 | if (0 != status) { |
| 274 | PAL_ERR(LOG_TAG, "Rx session stop failed with status %d", status); |
| 275 | } |
| 276 | PAL_VERBOSE(LOG_TAG, "session stop successful"); |
| 277 | currentState = STREAM_STOPPED; |
| 278 | } else if (currentState == STREAM_STOPPED || currentState == STREAM_IDLE) { |
| 279 | PAL_INFO(LOG_TAG, "Stream is already in Stopped state %d", currentState); |
| 280 | goto exit; |
| 281 | } else { |
| 282 | PAL_ERR(LOG_TAG, "Stream should be in start/pause state, %d", currentState); |
| 283 | status = -EINVAL; |
| 284 | goto exit; |
| 285 | } |
| 286 | PAL_DBG(LOG_TAG, "Exit. status %d, state %d", status, currentState); |
| 287 | |
| 288 | exit: |
| 289 | mStreamMutex.unlock(); |
| 290 | return status; |
| 291 | } |
| 292 | |
| 293 | //TBD: move this to Stream, why duplicate code? |
| 294 | int32_t StreamNonTunnel::prepare() |
| 295 | { |
| 296 | int32_t status = 0; |
| 297 | |
| 298 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK", session); |
| 299 | |
| 300 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 301 | |
| 302 | if ((rm->cardState == CARD_STATUS_OFFLINE) |
| 303 | || ssrInNTMode == true) { |
| 304 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 305 | currentState); |
| 306 | mStreamMutex.unlock(); |
| 307 | return -ENETRESET; |
| 308 | } |
| 309 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 310 | status = session->prepare(this); |
| 311 | if (0 != status) |
| 312 | PAL_ERR(LOG_TAG, "session prepare failed with status = %d", status); |
| 313 | mStreamMutex.unlock(); |
| 314 | PAL_DBG(LOG_TAG, "Exit. status - %d", status); |
| 315 | |
| 316 | return status; |
| 317 | } |
| 318 | |
| 319 | int32_t StreamNonTunnel::read(struct pal_buffer* buf) |
| 320 | { |
| 321 | int32_t status = 0; |
| 322 | int32_t size; |
| 323 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK, state %d", |
| 324 | session, currentState); |
| 325 | |
Zhou Song | 88cbe03 | 2022-02-27 13:45:27 +0800 | [diff] [blame] | 326 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 327 | if ((rm->cardState == CARD_STATUS_OFFLINE) || ssrInNTMode == true) { |
| 328 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 329 | currentState); |
| 330 | status = -ENETRESET; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | if (currentState == STREAM_STARTED) { |
| 335 | status = session->read(this, SHMEM_ENDPOINT, buf, &size); |
| 336 | if (0 != status) { |
| 337 | PAL_ERR(LOG_TAG, "session read is failed with status %d", status); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 338 | if (status == -ENETRESET && |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 339 | rm->cardState != CARD_STATUS_OFFLINE) { |
| 340 | PAL_ERR(LOG_TAG, "Sound card offline, informing RM"); |
| 341 | rm->ssrHandler(CARD_STATUS_OFFLINE); |
| 342 | size = buf->size; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 343 | PAL_DBG(LOG_TAG, "dropped buffer size - %d", size); |
| 344 | goto exit; |
| 345 | } else if (rm->cardState == CARD_STATUS_OFFLINE) { |
| 346 | size = buf->size; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 347 | PAL_DBG(LOG_TAG, "dropped buffer size - %d", size); |
| 348 | goto exit; |
| 349 | } else { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 350 | goto exit; |
| 351 | } |
| 352 | } |
| 353 | } else { |
| 354 | PAL_ERR(LOG_TAG, "Stream not started yet, state %d", currentState); |
| 355 | status = -EINVAL; |
| 356 | goto exit; |
| 357 | } |
Zhou Song | 88cbe03 | 2022-02-27 13:45:27 +0800 | [diff] [blame] | 358 | mStreamMutex.unlock(); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 359 | PAL_DBG(LOG_TAG, "Exit. session read successful size - %d", size); |
| 360 | return size; |
| 361 | exit : |
Zhou Song | 88cbe03 | 2022-02-27 13:45:27 +0800 | [diff] [blame] | 362 | mStreamMutex.unlock(); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 363 | PAL_DBG(LOG_TAG, "session read failed status %d", status); |
| 364 | return status; |
| 365 | } |
| 366 | |
| 367 | int32_t StreamNonTunnel::write(struct pal_buffer* buf) |
| 368 | { |
| 369 | int32_t status = 0; |
| 370 | int32_t size = 0; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 371 | |
| 372 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK, state %d", |
| 373 | session, currentState); |
| 374 | |
| 375 | mStreamMutex.lock(); |
| 376 | |
| 377 | // If cached state is not STREAM_IDLE, we are still processing SSR up. |
| 378 | if ((rm->cardState == CARD_STATUS_OFFLINE) |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 379 | || ssrInNTMode == true) { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 380 | size = buf->size; |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 381 | PAL_DBG(LOG_TAG, "sound card offline dropped buffer size - %d", size); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 382 | mStreamMutex.unlock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 383 | return -ENETRESET; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 384 | } |
| 385 | mStreamMutex.unlock(); |
Bharath Tirunagaru | c6484d7 | 2021-07-14 21:56:20 -0700 | [diff] [blame] | 386 | //we should allow writes to go through in Start/Pause state as well. |
| 387 | if ( (currentState == STREAM_STARTED) || |
| 388 | (currentState == STREAM_PAUSED) ) { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 389 | status = session->write(this, SHMEM_ENDPOINT, buf, &size, 0); |
| 390 | if (0 != status) { |
| 391 | PAL_ERR(LOG_TAG, "session write is failed with status %d", status); |
| 392 | |
| 393 | /* ENETRESET is the error code returned by AGM during SSR */ |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 394 | if (status == -ENETRESET && |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 395 | rm->cardState != CARD_STATUS_OFFLINE) { |
| 396 | PAL_ERR(LOG_TAG, "Sound card offline, informing RM"); |
| 397 | rm->ssrHandler(CARD_STATUS_OFFLINE); |
| 398 | size = buf->size; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 399 | PAL_DBG(LOG_TAG, "dropped buffer size - %d", size); |
| 400 | goto exit; |
| 401 | } else if (rm->cardState == CARD_STATUS_OFFLINE) { |
| 402 | size = buf->size; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 403 | PAL_DBG(LOG_TAG, "dropped buffer size - %d", size); |
| 404 | goto exit; |
| 405 | } else { |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 406 | goto exit; |
| 407 | } |
| 408 | } |
| 409 | PAL_DBG(LOG_TAG, "Exit. session write successful size - %d", size); |
| 410 | return size; |
| 411 | } else { |
| 412 | PAL_ERR(LOG_TAG, "Stream not started yet, state %d", currentState); |
| 413 | if (currentState == STREAM_STOPPED) |
| 414 | status = -EIO; |
| 415 | else |
| 416 | status = -EINVAL; |
| 417 | goto exit; |
| 418 | } |
| 419 | |
| 420 | exit : |
| 421 | PAL_DBG(LOG_TAG, "session write failed status %d", status); |
| 422 | return status; |
| 423 | } |
| 424 | |
| 425 | int32_t StreamNonTunnel::registerCallBack(pal_stream_callback cb, uint64_t cookie) |
| 426 | { |
| 427 | streamCb = cb; |
| 428 | this->cookie = cookie; |
| 429 | return 0; return 0; |
| 430 | } |
| 431 | |
| 432 | int32_t StreamNonTunnel::getTagsWithModuleInfo(size_t *size, uint8_t *payload) |
| 433 | { |
| 434 | int32_t status = 0; |
| 435 | |
Ashish Jain | f5da4e1 | 2020-11-18 23:58:21 +0530 | [diff] [blame] | 436 | if (*size > 0 && !payload) |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 437 | { |
| 438 | status = -EINVAL; |
| 439 | PAL_ERR(LOG_TAG, "wrong params"); |
| 440 | goto exit; |
| 441 | } |
| 442 | |
| 443 | status = session->getTagsWithModuleInfo(this, size, payload); |
| 444 | exit: |
| 445 | return status; |
| 446 | } |
| 447 | |
| 448 | int32_t StreamNonTunnel::getCallBack(pal_stream_callback * /*cb*/) |
| 449 | { |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | int32_t StreamNonTunnel::getParameters(uint32_t /*param_id*/, void ** /*payload*/) |
| 454 | { |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | int32_t StreamNonTunnel::setParameters(uint32_t param_id, void *payload) |
| 459 | { |
| 460 | int32_t status = 0; |
| 461 | |
| 462 | if (!payload) |
| 463 | { |
| 464 | status = -EINVAL; |
| 465 | PAL_ERR(LOG_TAG, "wrong params"); |
| 466 | goto error; |
| 467 | } |
| 468 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 469 | mStreamMutex.lock(); |
Zhou Song | bb7f7b7 | 2021-12-13 16:04:38 +0800 | [diff] [blame] | 470 | if (currentState == STREAM_IDLE) { |
| 471 | PAL_ERR(LOG_TAG, "Invalid stream state: IDLE for param ID: %d", param_id); |
| 472 | mStreamMutex.unlock(); |
| 473 | return -EINVAL; |
| 474 | } |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 475 | if ((rm->cardState == CARD_STATUS_OFFLINE) || ssrInNTMode == true) { |
| 476 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 477 | currentState); |
| 478 | status = -ENETRESET; |
| 479 | goto error; |
| 480 | } |
| 481 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 482 | PAL_DBG(LOG_TAG, "start, set parameter %u, session handle - %p", param_id, session); |
| 483 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 484 | // Stream may not know about tags, so use setParameters instead of setConfig |
| 485 | switch (param_id) { |
| 486 | case PAL_PARAM_ID_MODULE_CONFIG: |
| 487 | status = session->setParameters(this, 0, param_id, payload); |
| 488 | break; |
| 489 | default: |
| 490 | PAL_ERR(LOG_TAG, "Unsupported param id %u", param_id); |
| 491 | status = -EINVAL; |
| 492 | break; |
| 493 | } |
| 494 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 495 | error: |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 496 | mStreamMutex.unlock(); |
| 497 | PAL_VERBOSE(LOG_TAG, "exit, session parameter %u set with status %d", param_id, status); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 498 | return status; |
| 499 | } |
| 500 | |
Ashish Jain | 89d0fc0 | 2021-01-18 23:37:59 +0530 | [diff] [blame] | 501 | int32_t StreamNonTunnel::drain(pal_drain_type_t type) |
| 502 | { |
| 503 | PAL_ERR(LOG_TAG, "drain"); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 504 | if ((rm->cardState == CARD_STATUS_OFFLINE) || ssrInNTMode == true) { |
| 505 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 506 | currentState); |
| 507 | return -ENETRESET; |
| 508 | } |
Ashish Jain | 89d0fc0 | 2021-01-18 23:37:59 +0530 | [diff] [blame] | 509 | return session->drain(type); |
| 510 | } |
| 511 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 512 | int32_t StreamNonTunnel::flush() |
| 513 | { |
| 514 | int32_t status = 0; |
| 515 | |
| 516 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 517 | if ((rm->cardState == CARD_STATUS_OFFLINE) || ssrInNTMode == true) { |
| 518 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 519 | currentState); |
| 520 | status = -ENETRESET; |
| 521 | goto exit; |
| 522 | } |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 523 | |
| 524 | status = session->flush(); |
Ashish Jain | 89d0fc0 | 2021-01-18 23:37:59 +0530 | [diff] [blame] | 525 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 526 | exit: |
| 527 | mStreamMutex.unlock(); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 528 | return status; |
| 529 | } |
| 530 | |
Aniket Kumar Lata | 25f2d52 | 2021-05-21 09:37:17 -0700 | [diff] [blame] | 531 | int32_t StreamNonTunnel::suspend() |
| 532 | { |
| 533 | int32_t status = 0; |
| 534 | |
| 535 | mStreamMutex.lock(); |
| 536 | if ((rm->cardState == CARD_STATUS_OFFLINE) || ssrInNTMode) { |
| 537 | PAL_ERR(LOG_TAG, "Sound card offline currentState %d", |
| 538 | currentState); |
| 539 | status = -ENETRESET; |
| 540 | goto exit; |
| 541 | } |
| 542 | |
| 543 | if (currentState == STREAM_STARTED) { |
Ankit Mishra | f2f8b73 | 2022-06-01 09:01:06 +0530 | [diff] [blame] | 544 | status = session->suspend(this); |
Aniket Kumar Lata | 25f2d52 | 2021-05-21 09:37:17 -0700 | [diff] [blame] | 545 | if (status) { |
| 546 | PAL_ERR(LOG_TAG, "Rx session suspend failed with status %d", status); |
| 547 | goto exit; |
| 548 | } |
| 549 | currentState = STREAM_SUSPENDED; |
| 550 | } |
| 551 | exit: |
| 552 | mStreamMutex.unlock(); |
| 553 | return status; |
| 554 | } |
| 555 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 556 | int32_t StreamNonTunnel::isSampleRateSupported(uint32_t sampleRate) |
| 557 | { |
| 558 | int32_t rc = 0; |
| 559 | PAL_DBG(LOG_TAG, "sampleRate %u", sampleRate); |
| 560 | switch(sampleRate) { |
| 561 | case SAMPLINGRATE_8K: |
| 562 | case SAMPLINGRATE_16K: |
| 563 | case SAMPLINGRATE_22K: |
| 564 | case SAMPLINGRATE_32K: |
| 565 | case SAMPLINGRATE_44K: |
| 566 | case SAMPLINGRATE_48K: |
| 567 | case SAMPLINGRATE_96K: |
| 568 | case SAMPLINGRATE_192K: |
| 569 | case SAMPLINGRATE_384K: |
| 570 | break; |
| 571 | default: |
| 572 | rc = 0; |
| 573 | PAL_VERBOSE(LOG_TAG, "sample rate received %d rc %d", sampleRate, rc); |
| 574 | break; |
| 575 | } |
| 576 | return rc; |
| 577 | } |
| 578 | |
| 579 | int32_t StreamNonTunnel::isChannelSupported(uint32_t numChannels) |
| 580 | { |
| 581 | int32_t rc = 0; |
| 582 | PAL_DBG(LOG_TAG, "numChannels %u", numChannels); |
| 583 | switch(numChannels) { |
| 584 | case CHANNELS_1: |
| 585 | case CHANNELS_2: |
| 586 | case CHANNELS_3: |
| 587 | case CHANNELS_4: |
| 588 | case CHANNELS_5: |
| 589 | case CHANNELS_5_1: |
| 590 | case CHANNELS_7: |
| 591 | case CHANNELS_8: |
| 592 | break; |
| 593 | default: |
| 594 | rc = -EINVAL; |
| 595 | PAL_ERR(LOG_TAG, "channels not supported %d rc %d", numChannels, rc); |
| 596 | break; |
| 597 | } |
| 598 | return rc; |
| 599 | } |
| 600 | |
| 601 | int32_t StreamNonTunnel::isBitWidthSupported(uint32_t bitWidth) |
| 602 | { |
| 603 | int32_t rc = 0; |
| 604 | PAL_DBG(LOG_TAG, "bitWidth %u", bitWidth); |
| 605 | switch(bitWidth) { |
| 606 | case BITWIDTH_16: |
| 607 | case BITWIDTH_24: |
| 608 | case BITWIDTH_32: |
| 609 | break; |
| 610 | default: |
| 611 | rc = -EINVAL; |
| 612 | PAL_ERR(LOG_TAG, "bit width not supported %d rc %d", bitWidth, rc); |
| 613 | break; |
| 614 | } |
| 615 | return rc; |
| 616 | } |
| 617 | |
| 618 | int32_t StreamNonTunnel::ssrDownHandler() |
| 619 | { |
Aditya Rathi | 0529a9e | 2022-11-07 21:13:45 -0800 | [diff] [blame] | 620 | int32_t status = 0; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 621 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 622 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 623 | mStreamMutex.lock(); |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 624 | /* In NonTunnelMode once SSR happens, that session is not reusuable |
| 625 | * Hence set the ssr to true and return all subsequent calls with |
| 626 | * -ENETRESET, untill the client sets up a new session. |
| 627 | * |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 628 | */ |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 629 | PAL_DBG(LOG_TAG, "Enter. session handle - %pK currentState State %d", |
| 630 | session, currentState); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 631 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 632 | ssrInNTMode = true; |
| 633 | if (streamCb) |
| 634 | streamCb(reinterpret_cast<pal_stream_handle_t *>(this), PAL_STREAM_CBK_EVENT_ERROR, NULL, 0, this->cookie); |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 635 | |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 636 | mStreamMutex.unlock(); |
| 637 | |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 638 | PAL_DBG(LOG_TAG, "Exit, status %d", status); |
| 639 | return status; |
| 640 | } |
| 641 | |
| 642 | int32_t StreamNonTunnel::ssrUpHandler() |
| 643 | { |
Ashish Jain | 35a20ca | 2021-02-12 07:01:54 +0530 | [diff] [blame] | 644 | return 0; |
Ashish Jain | a9aac35 | 2020-10-30 23:49:09 +0530 | [diff] [blame] | 645 | } |
| 646 | |