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> |
| 18 | #include <media/PlayerBase.h> |
| 19 | |
| 20 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 21 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 22 | |
| 23 | namespace android { |
| 24 | |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 25 | using media::VolumeShaperConfiguration; |
| 26 | using media::VolumeShaperOperation; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 27 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 28 | //-------------------------------------------------------------------------------------------------- |
| 29 | PlayerBase::PlayerBase() : BnPlayer(), |
| 30 | mPanMultiplierL(1.0f), mPanMultiplierR(1.0f), |
| 31 | mVolumeMultiplierL(1.0f), mVolumeMultiplierR(1.0f), |
| 32 | mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN) |
| 33 | { |
| 34 | ALOGD("PlayerBase::PlayerBase()"); |
| 35 | // use checkService() to avoid blocking if audio service is not up yet |
| 36 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio")); |
| 37 | if (binder == 0) { |
| 38 | ALOGE("PlayerBase(): binding to audio service failed, service up?"); |
| 39 | } else { |
| 40 | mAudioManager = interface_cast<IAudioManager>(binder); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | PlayerBase::~PlayerBase() { |
| 46 | ALOGD("PlayerBase::~PlayerBase()"); |
| 47 | baseDestroy(); |
| 48 | } |
| 49 | |
| 50 | void PlayerBase::init(player_type_t playerType, audio_usage_t usage) { |
| 51 | if (mAudioManager == 0) { |
| 52 | ALOGE("AudioPlayer realize: no audio service, player will not be registered"); |
| 53 | } else { |
| 54 | mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void PlayerBase::baseDestroy() { |
| 59 | serviceReleasePlayer(); |
| 60 | if (mAudioManager != 0) { |
| 61 | mAudioManager.clear(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | //------------------------------------------------------------------------------ |
| 66 | void PlayerBase::servicePlayerEvent(player_state_t event) { |
| 67 | if (mAudioManager != 0) { |
| 68 | // only report state change |
| 69 | Mutex::Autolock _l(mPlayerStateLock); |
| 70 | if (event != mLastReportedEvent |
| 71 | && mPIId != PLAYER_PIID_INVALID) { |
| 72 | mLastReportedEvent = event; |
| 73 | mAudioManager->playerEvent(mPIId, event); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void PlayerBase::serviceReleasePlayer() { |
| 79 | if (mAudioManager != 0 |
| 80 | && mPIId != PLAYER_PIID_INVALID) { |
| 81 | mAudioManager->releasePlayer(mPIId); |
| 82 | } |
| 83 | } |
| 84 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 85 | //FIXME temporary method while some player state is outside of this class |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 86 | void PlayerBase::reportEvent(player_state_t event) { |
| 87 | servicePlayerEvent(event); |
| 88 | } |
| 89 | |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 90 | status_t PlayerBase::startWithStatus() { |
| 91 | status_t status = playerStart(); |
| 92 | if (status == NO_ERROR) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 93 | servicePlayerEvent(PLAYER_STATE_STARTED); |
| 94 | } else { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 95 | ALOGW("PlayerBase::start() error %d", status); |
| 96 | } |
| 97 | return status; |
| 98 | } |
| 99 | |
| 100 | status_t PlayerBase::pauseWithStatus() { |
| 101 | status_t status = playerPause(); |
| 102 | if (status == NO_ERROR) { |
| 103 | servicePlayerEvent(PLAYER_STATE_PAUSED); |
| 104 | } else { |
| 105 | ALOGW("PlayerBase::pause() error %d", status); |
| 106 | } |
| 107 | return status; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | status_t PlayerBase::stopWithStatus() { |
| 112 | status_t status = playerStop(); |
| 113 | if (status == NO_ERROR) { |
| 114 | servicePlayerEvent(PLAYER_STATE_STOPPED); |
| 115 | } else { |
| 116 | ALOGW("PlayerBase::stop() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 117 | } |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 118 | return status; |
| 119 | } |
| 120 | |
| 121 | //------------------------------------------------------------------------------ |
| 122 | // Implementation of IPlayer |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 123 | binder::Status PlayerBase::start() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 124 | ALOGD("PlayerBase::start() from IPlayer"); |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 125 | (void)startWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 126 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 129 | binder::Status PlayerBase::pause() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 130 | ALOGD("PlayerBase::pause() from IPlayer"); |
| 131 | (void)pauseWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 132 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 136 | binder::Status PlayerBase::stop() { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 137 | ALOGD("PlayerBase::stop() from IPlayer"); |
| 138 | (void)stopWithStatus(); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 139 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 142 | binder::Status PlayerBase::setVolume(float vol) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 143 | ALOGD("PlayerBase::setVolume() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 144 | { |
| 145 | Mutex::Autolock _l(mSettingsLock); |
| 146 | mVolumeMultiplierL = vol; |
| 147 | mVolumeMultiplierR = vol; |
| 148 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 149 | status_t status = playerSetVolume(); |
| 150 | if (status != NO_ERROR) { |
| 151 | ALOGW("PlayerBase::setVolume() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 152 | } |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 153 | return binder::Status::fromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 156 | binder::Status PlayerBase::setPan(float pan) { |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 157 | ALOGD("PlayerBase::setPan() from IPlayer"); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 158 | { |
| 159 | Mutex::Autolock _l(mSettingsLock); |
| 160 | pan = min(max(-1.0f, pan), 1.0f); |
| 161 | if (pan >= 0.0f) { |
| 162 | mPanMultiplierL = 1.0f - pan; |
| 163 | mPanMultiplierR = 1.0f; |
| 164 | } else { |
| 165 | mPanMultiplierL = 1.0f; |
| 166 | mPanMultiplierR = 1.0f + pan; |
| 167 | } |
| 168 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 169 | status_t status = playerSetVolume(); |
| 170 | if (status != NO_ERROR) { |
| 171 | ALOGW("PlayerBase::setPan() error %d", status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 172 | } |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 173 | return binder::Status::fromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 176 | binder::Status PlayerBase::setStartDelayMs(int32_t delayMs __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 177 | ALOGW("setStartDelay() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 178 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 181 | binder::Status PlayerBase::applyVolumeShaper( |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 182 | const VolumeShaperConfiguration& configuration __unused, |
| 183 | const VolumeShaperOperation& operation __unused) { |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 184 | ALOGW("applyVolumeShaper() is not supported"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 185 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace android |