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 | |
| 61 | void PlayerBase::baseDestroy() { |
| 62 | serviceReleasePlayer(); |
| 63 | if (mAudioManager != 0) { |
| 64 | mAudioManager.clear(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | //------------------------------------------------------------------------------ |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 69 | void PlayerBase::servicePlayerEvent(player_state_t event, audio_port_handle_t deviceId) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 70 | if (mAudioManager != 0) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 71 | bool changed = false; |
| 72 | { |
| 73 | Mutex::Autolock _l(mDeviceIdLock); |
| 74 | changed = mLastReportedDeviceId != deviceId; |
| 75 | mLastReportedDeviceId = deviceId; |
| 76 | } |
| 77 | |
| 78 | { |
| 79 | Mutex::Autolock _l(mPlayerStateLock); |
| 80 | // PLAYER_UPDATE_DEVICE_ID is not saved as an actual state, instead it is used to update |
| 81 | // device ID only. |
| 82 | if ((event != PLAYER_UPDATE_DEVICE_ID) && (event != mLastReportedEvent)) { |
| 83 | mLastReportedEvent = event; |
| 84 | changed = true; |
| 85 | } |
| 86 | } |
| 87 | if (changed && (mPIId != PLAYER_PIID_INVALID)) { |
| 88 | mAudioManager->playerEvent(mPIId, event, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void PlayerBase::serviceReleasePlayer() { |
| 94 | if (mAudioManager != 0 |
| 95 | && mPIId != PLAYER_PIID_INVALID) { |
| 96 | mAudioManager->releasePlayer(mPIId); |
| 97 | } |
| 98 | } |
| 99 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 100 | //FIXME temporary method while some player state is outside of this class |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 101 | void PlayerBase::reportEvent(player_state_t event, audio_port_handle_t deviceId) { |
| 102 | servicePlayerEvent(event, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 105 | void PlayerBase::baseUpdateDeviceId(audio_port_handle_t deviceId) { |
| 106 | servicePlayerEvent(PLAYER_UPDATE_DEVICE_ID, deviceId); |
| 107 | } |
| 108 | |
| 109 | status_t PlayerBase::startWithStatus(audio_port_handle_t deviceId) { |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 110 | status_t status = playerStart(); |
| 111 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 112 | servicePlayerEvent(PLAYER_STATE_STARTED, deviceId); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 113 | } else { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 114 | ALOGW("PlayerBase::start() error %d", status); |
| 115 | } |
| 116 | return status; |
| 117 | } |
| 118 | |
| 119 | status_t PlayerBase::pauseWithStatus() { |
| 120 | status_t status = playerPause(); |
| 121 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 122 | servicePlayerEvent(PLAYER_STATE_PAUSED, AUDIO_PORT_HANDLE_NONE); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 123 | } else { |
| 124 | ALOGW("PlayerBase::pause() error %d", status); |
| 125 | } |
| 126 | return status; |
| 127 | } |
| 128 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 129 | status_t PlayerBase::stopWithStatus() { |
| 130 | status_t status = playerStop(); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 131 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 132 | if (status == NO_ERROR) { |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 133 | servicePlayerEvent(PLAYER_STATE_STOPPED, AUDIO_PORT_HANDLE_NONE); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 134 | } else { |
| 135 | ALOGW("PlayerBase::stop() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 136 | } |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 137 | return status; |
| 138 | } |
| 139 | |
| 140 | //------------------------------------------------------------------------------ |
| 141 | // Implementation of IPlayer |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 142 | binder::Status PlayerBase::start() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 143 | ALOGD("PlayerBase::start() from IPlayer"); |
Oscar Azucena | e0414ec | 2020-12-22 20:40:07 +0000 | [diff] [blame] | 144 | audio_port_handle_t deviceId; |
| 145 | { |
| 146 | Mutex::Autolock _l(mDeviceIdLock); |
| 147 | deviceId = mLastReportedDeviceId; |
| 148 | } |
| 149 | (void)startWithStatus(deviceId); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 150 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 153 | binder::Status PlayerBase::pause() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 154 | ALOGD("PlayerBase::pause() from IPlayer"); |
| 155 | (void)pauseWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 156 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 160 | binder::Status PlayerBase::stop() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 161 | ALOGD("PlayerBase::stop() from IPlayer"); |
| 162 | (void)stopWithStatus(); |
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::setVolume(float vol) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 167 | ALOGD("PlayerBase::setVolume() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 168 | { |
| 169 | Mutex::Autolock _l(mSettingsLock); |
| 170 | mVolumeMultiplierL = vol; |
| 171 | mVolumeMultiplierR = vol; |
| 172 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 173 | status_t status = playerSetVolume(); |
| 174 | if (status != NO_ERROR) { |
| 175 | ALOGW("PlayerBase::setVolume() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 176 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 177 | return binderStatusFromStatusT(status); |
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::setPan(float pan) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 181 | ALOGD("PlayerBase::setPan() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 182 | { |
| 183 | Mutex::Autolock _l(mSettingsLock); |
| 184 | pan = min(max(-1.0f, pan), 1.0f); |
| 185 | if (pan >= 0.0f) { |
| 186 | mPanMultiplierL = 1.0f - pan; |
| 187 | mPanMultiplierR = 1.0f; |
| 188 | } else { |
| 189 | mPanMultiplierL = 1.0f; |
| 190 | mPanMultiplierR = 1.0f + pan; |
| 191 | } |
| 192 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 193 | status_t status = playerSetVolume(); |
| 194 | if (status != NO_ERROR) { |
| 195 | ALOGW("PlayerBase::setPan() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 196 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 197 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 200 | binder::Status PlayerBase::setStartDelayMs(int32_t delayMs __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 201 | ALOGW("setStartDelay() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 202 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 205 | binder::Status PlayerBase::applyVolumeShaper( |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 206 | const VolumeShaperConfiguration& configuration __unused, |
| 207 | const VolumeShaperOperation& operation __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 208 | ALOGW("applyVolumeShaper() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 209 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | } // namespace android |