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), |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 33 | mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN) |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 34 | { |
| 35 | ALOGD("PlayerBase::PlayerBase()"); |
| 36 | // use checkService() to avoid blocking if audio service is not up yet |
| 37 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio")); |
| 38 | if (binder == 0) { |
| 39 | ALOGE("PlayerBase(): binding to audio service failed, service up?"); |
| 40 | } else { |
| 41 | mAudioManager = interface_cast<IAudioManager>(binder); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | PlayerBase::~PlayerBase() { |
| 47 | ALOGD("PlayerBase::~PlayerBase()"); |
| 48 | baseDestroy(); |
| 49 | } |
| 50 | |
Eric Laurent | 4193125 | 2021-01-29 20:40:35 +0100 | [diff] [blame] | 51 | 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] | 52 | if (mAudioManager == 0) { |
| 53 | ALOGE("AudioPlayer realize: no audio service, player will not be registered"); |
| 54 | } else { |
Eric Laurent | 4193125 | 2021-01-29 20:40:35 +0100 | [diff] [blame] | 55 | mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this, |
| 56 | sessionId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 60 | void PlayerBase::triggerPortIdUpdate(audio_port_handle_t portId) const { |
| 61 | if (mAudioManager == nullptr) { |
| 62 | ALOGE("%s: no audio service, player %d will not update portId %d", |
| 63 | __func__, |
| 64 | mPIId, |
| 65 | portId); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (mPIId != PLAYER_PIID_INVALID && portId != AUDIO_PORT_HANDLE_NONE) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 70 | mAudioManager->playerEvent(mPIId, android::PLAYER_UPDATE_PORT_ID, { portId }); |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 74 | void PlayerBase::baseDestroy() { |
| 75 | serviceReleasePlayer(); |
| 76 | if (mAudioManager != 0) { |
| 77 | mAudioManager.clear(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | //------------------------------------------------------------------------------ |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 82 | void PlayerBase::servicePlayerEvent(player_state_t event, const DeviceIdVector& deviceIds) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 83 | if (mAudioManager != 0) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 84 | bool changed = false; |
| 85 | { |
| 86 | Mutex::Autolock _l(mDeviceIdLock); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 87 | changed = !areDeviceIdsEqual(deviceIds, mLastReportedDeviceIds); |
| 88 | mLastReportedDeviceIds = deviceIds; |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | { |
| 92 | Mutex::Autolock _l(mPlayerStateLock); |
| 93 | // PLAYER_UPDATE_DEVICE_ID is not saved as an actual state, instead it is used to update |
| 94 | // device ID only. |
| 95 | if ((event != PLAYER_UPDATE_DEVICE_ID) && (event != mLastReportedEvent)) { |
| 96 | mLastReportedEvent = event; |
| 97 | changed = true; |
| 98 | } |
| 99 | } |
| 100 | if (changed && (mPIId != PLAYER_PIID_INVALID)) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 101 | mAudioManager->playerEvent(mPIId, event, deviceIds); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void PlayerBase::serviceReleasePlayer() { |
| 107 | if (mAudioManager != 0 |
| 108 | && mPIId != PLAYER_PIID_INVALID) { |
| 109 | mAudioManager->releasePlayer(mPIId); |
| 110 | } |
| 111 | } |
| 112 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 113 | //FIXME temporary method while some player state is outside of this class |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 114 | void PlayerBase::reportEvent(player_state_t event, const DeviceIdVector& deviceIds) { |
| 115 | servicePlayerEvent(event, deviceIds); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 118 | void PlayerBase::baseUpdateDeviceIds(const DeviceIdVector& deviceIds) { |
| 119 | servicePlayerEvent(PLAYER_UPDATE_DEVICE_ID, deviceIds); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 122 | status_t PlayerBase::startWithStatus(const DeviceIdVector& deviceIds) { |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 123 | status_t status = playerStart(); |
| 124 | if (status == NO_ERROR) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 125 | servicePlayerEvent(PLAYER_STATE_STARTED, deviceIds); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 126 | } else { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 127 | ALOGW("PlayerBase::start() error %d", status); |
| 128 | } |
| 129 | return status; |
| 130 | } |
| 131 | |
| 132 | status_t PlayerBase::pauseWithStatus() { |
| 133 | status_t status = playerPause(); |
| 134 | if (status == NO_ERROR) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 135 | servicePlayerEvent(PLAYER_STATE_PAUSED, {}); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 136 | } else { |
| 137 | ALOGW("PlayerBase::pause() error %d", status); |
| 138 | } |
| 139 | return status; |
| 140 | } |
| 141 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 142 | status_t PlayerBase::stopWithStatus() { |
| 143 | status_t status = playerStop(); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 144 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 145 | if (status == NO_ERROR) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 146 | servicePlayerEvent(PLAYER_STATE_STOPPED, {}); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 147 | } else { |
| 148 | ALOGW("PlayerBase::stop() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 149 | } |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 150 | return status; |
| 151 | } |
| 152 | |
| 153 | //------------------------------------------------------------------------------ |
| 154 | // Implementation of IPlayer |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 155 | binder::Status PlayerBase::start() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 156 | ALOGD("PlayerBase::start() from IPlayer"); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 157 | DeviceIdVector deviceIds; |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 158 | { |
| 159 | Mutex::Autolock _l(mDeviceIdLock); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 160 | deviceIds = mLastReportedDeviceIds; |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 161 | } |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 162 | (void)startWithStatus(deviceIds); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 163 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 166 | binder::Status PlayerBase::pause() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 167 | ALOGD("PlayerBase::pause() from IPlayer"); |
| 168 | (void)pauseWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 169 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 173 | binder::Status PlayerBase::stop() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 174 | ALOGD("PlayerBase::stop() from IPlayer"); |
| 175 | (void)stopWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 176 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 179 | binder::Status PlayerBase::setVolume(float vol) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 180 | ALOGD("PlayerBase::setVolume() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 181 | { |
| 182 | Mutex::Autolock _l(mSettingsLock); |
| 183 | mVolumeMultiplierL = vol; |
| 184 | mVolumeMultiplierR = vol; |
| 185 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 186 | status_t status = playerSetVolume(); |
| 187 | if (status != NO_ERROR) { |
| 188 | ALOGW("PlayerBase::setVolume() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 189 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 190 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 193 | binder::Status PlayerBase::setPan(float pan) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 194 | ALOGD("PlayerBase::setPan() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 195 | { |
| 196 | Mutex::Autolock _l(mSettingsLock); |
| 197 | pan = min(max(-1.0f, pan), 1.0f); |
| 198 | if (pan >= 0.0f) { |
| 199 | mPanMultiplierL = 1.0f - pan; |
| 200 | mPanMultiplierR = 1.0f; |
| 201 | } else { |
| 202 | mPanMultiplierL = 1.0f; |
| 203 | mPanMultiplierR = 1.0f + pan; |
| 204 | } |
| 205 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 206 | status_t status = playerSetVolume(); |
| 207 | if (status != NO_ERROR) { |
| 208 | ALOGW("PlayerBase::setPan() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 209 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 210 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 213 | binder::Status PlayerBase::setStartDelayMs(int32_t delayMs __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 214 | ALOGW("setStartDelay() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 215 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 218 | binder::Status PlayerBase::applyVolumeShaper( |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 219 | const VolumeShaperConfiguration& configuration __unused, |
| 220 | const VolumeShaperOperation& operation __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 221 | ALOGW("applyVolumeShaper() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 222 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace android |