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