Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <binder/IServiceManager.h> |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 18 | #include <media/AidlConversionUtil.h> |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 19 | #include <media/PlayerBase.h> |
| 20 | |
| 21 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 22 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 23 | |
| 24 | namespace android { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 25 | using aidl_utils::binderStatusFromStatusT; |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 26 | using media::VolumeShaperConfiguration; |
| 27 | using media::VolumeShaperOperation; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 28 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 29 | //-------------------------------------------------------------------------------------------------- |
| 30 | PlayerBase::PlayerBase() : BnPlayer(), |
| 31 | mPanMultiplierL(1.0f), mPanMultiplierR(1.0f), |
| 32 | mVolumeMultiplierL(1.0f), mVolumeMultiplierR(1.0f), |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 33 | mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN), |
| 34 | mLastReportedDeviceId(AUDIO_PORT_HANDLE_NONE) |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 35 | { |
| 36 | ALOGD("PlayerBase::PlayerBase()"); |
| 37 | // use checkService() to avoid blocking if audio service is not up yet |
| 38 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio")); |
| 39 | if (binder == 0) { |
| 40 | ALOGE("PlayerBase(): binding to audio service failed, service up?"); |
| 41 | } else { |
| 42 | mAudioManager = interface_cast<IAudioManager>(binder); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | PlayerBase::~PlayerBase() { |
| 48 | ALOGD("PlayerBase::~PlayerBase()"); |
| 49 | baseDestroy(); |
| 50 | } |
| 51 | |
Eric Laurent | 4193125 | 2021-01-29 20:40:35 +0100 | [diff] [blame] | 52 | void PlayerBase::init(player_type_t playerType, audio_usage_t usage, audio_session_t sessionId) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 53 | if (mAudioManager == 0) { |
| 54 | ALOGE("AudioPlayer realize: no audio service, player will not be registered"); |
| 55 | } else { |
Eric Laurent | 4193125 | 2021-01-29 20:40:35 +0100 | [diff] [blame] | 56 | mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this, |
| 57 | sessionId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame^] | 61 | void PlayerBase::triggerPortIdUpdate(audio_port_handle_t portId) const { |
| 62 | if (mAudioManager == nullptr) { |
| 63 | ALOGE("%s: no audio service, player %d will not update portId %d", |
| 64 | __func__, |
| 65 | mPIId, |
| 66 | portId); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (mPIId != PLAYER_PIID_INVALID && portId != AUDIO_PORT_HANDLE_NONE) { |
| 71 | mAudioManager->playerEvent(mPIId, android::PLAYER_UPDATE_PORT_ID, portId); |
| 72 | } |
| 73 | } |
| 74 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 75 | void PlayerBase::baseDestroy() { |
| 76 | serviceReleasePlayer(); |
| 77 | if (mAudioManager != 0) { |
| 78 | mAudioManager.clear(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | //------------------------------------------------------------------------------ |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 83 | void PlayerBase::servicePlayerEvent(player_state_t event, audio_port_handle_t deviceId) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 84 | if (mAudioManager != 0) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 85 | bool changed = false; |
| 86 | { |
| 87 | Mutex::Autolock _l(mDeviceIdLock); |
| 88 | changed = mLastReportedDeviceId != deviceId; |
| 89 | mLastReportedDeviceId = deviceId; |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | Mutex::Autolock _l(mPlayerStateLock); |
| 94 | // PLAYER_UPDATE_DEVICE_ID is not saved as an actual state, instead it is used to update |
| 95 | // device ID only. |
| 96 | if ((event != PLAYER_UPDATE_DEVICE_ID) && (event != mLastReportedEvent)) { |
| 97 | mLastReportedEvent = event; |
| 98 | changed = true; |
| 99 | } |
| 100 | } |
| 101 | if (changed && (mPIId != PLAYER_PIID_INVALID)) { |
| 102 | mAudioManager->playerEvent(mPIId, event, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void PlayerBase::serviceReleasePlayer() { |
| 108 | if (mAudioManager != 0 |
| 109 | && mPIId != PLAYER_PIID_INVALID) { |
| 110 | mAudioManager->releasePlayer(mPIId); |
| 111 | } |
| 112 | } |
| 113 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 114 | //FIXME temporary method while some player state is outside of this class |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 115 | void PlayerBase::reportEvent(player_state_t event, audio_port_handle_t deviceId) { |
| 116 | servicePlayerEvent(event, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 119 | void PlayerBase::baseUpdateDeviceId(audio_port_handle_t deviceId) { |
| 120 | servicePlayerEvent(PLAYER_UPDATE_DEVICE_ID, deviceId); |
| 121 | } |
| 122 | |
| 123 | status_t PlayerBase::startWithStatus(audio_port_handle_t deviceId) { |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 124 | status_t status = playerStart(); |
| 125 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 126 | servicePlayerEvent(PLAYER_STATE_STARTED, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 127 | } else { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 128 | ALOGW("PlayerBase::start() error %d", status); |
| 129 | } |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | status_t PlayerBase::pauseWithStatus() { |
| 134 | status_t status = playerPause(); |
| 135 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 136 | servicePlayerEvent(PLAYER_STATE_PAUSED, AUDIO_PORT_HANDLE_NONE); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 137 | } else { |
| 138 | ALOGW("PlayerBase::pause() error %d", status); |
| 139 | } |
| 140 | return status; |
| 141 | } |
| 142 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 143 | status_t PlayerBase::stopWithStatus() { |
| 144 | status_t status = playerStop(); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 145 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 146 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 147 | servicePlayerEvent(PLAYER_STATE_STOPPED, AUDIO_PORT_HANDLE_NONE); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 148 | } else { |
| 149 | ALOGW("PlayerBase::stop() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 150 | } |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 151 | return status; |
| 152 | } |
| 153 | |
| 154 | //------------------------------------------------------------------------------ |
| 155 | // Implementation of IPlayer |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 156 | binder::Status PlayerBase::start() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 157 | ALOGD("PlayerBase::start() from IPlayer"); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 158 | audio_port_handle_t deviceId; |
| 159 | { |
| 160 | Mutex::Autolock _l(mDeviceIdLock); |
| 161 | deviceId = mLastReportedDeviceId; |
| 162 | } |
| 163 | (void)startWithStatus(deviceId); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 164 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 167 | binder::Status PlayerBase::pause() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 168 | ALOGD("PlayerBase::pause() from IPlayer"); |
| 169 | (void)pauseWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 170 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 174 | binder::Status PlayerBase::stop() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 175 | ALOGD("PlayerBase::stop() from IPlayer"); |
| 176 | (void)stopWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 177 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 180 | binder::Status PlayerBase::setVolume(float vol) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 181 | ALOGD("PlayerBase::setVolume() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 182 | { |
| 183 | Mutex::Autolock _l(mSettingsLock); |
| 184 | mVolumeMultiplierL = vol; |
| 185 | mVolumeMultiplierR = vol; |
| 186 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 187 | status_t status = playerSetVolume(); |
| 188 | if (status != NO_ERROR) { |
| 189 | ALOGW("PlayerBase::setVolume() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 190 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 191 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 194 | binder::Status PlayerBase::setPan(float pan) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 195 | ALOGD("PlayerBase::setPan() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 196 | { |
| 197 | Mutex::Autolock _l(mSettingsLock); |
| 198 | pan = min(max(-1.0f, pan), 1.0f); |
| 199 | if (pan >= 0.0f) { |
| 200 | mPanMultiplierL = 1.0f - pan; |
| 201 | mPanMultiplierR = 1.0f; |
| 202 | } else { |
| 203 | mPanMultiplierL = 1.0f; |
| 204 | mPanMultiplierR = 1.0f + pan; |
| 205 | } |
| 206 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 207 | status_t status = playerSetVolume(); |
| 208 | if (status != NO_ERROR) { |
| 209 | ALOGW("PlayerBase::setPan() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 210 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 211 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 214 | binder::Status PlayerBase::setStartDelayMs(int32_t delayMs __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 215 | ALOGW("setStartDelay() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 216 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 219 | binder::Status PlayerBase::applyVolumeShaper( |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 220 | const VolumeShaperConfiguration& configuration __unused, |
| 221 | const VolumeShaperOperation& operation __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 222 | ALOGW("applyVolumeShaper() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 223 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | } // namespace android |