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