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 <media/TrackPlayerBase.h> |
| 18 | |
| 19 | namespace android { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 20 | using aidl_utils::binderStatusFromStatusT; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 21 | using media::VolumeShaper; |
| 22 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 23 | //-------------------------------------------------------------------------------------------------- |
| 24 | TrackPlayerBase::TrackPlayerBase() : PlayerBase(), |
| 25 | mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f) |
| 26 | { |
| 27 | ALOGD("TrackPlayerBase::TrackPlayerBase()"); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | TrackPlayerBase::~TrackPlayerBase() { |
| 32 | ALOGD("TrackPlayerBase::~TrackPlayerBase()"); |
| 33 | doDestroy(); |
| 34 | } |
| 35 | |
| 36 | void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) { |
| 37 | PlayerBase::init(playerType, usage); |
| 38 | mAudioTrack = pat; |
Oscar Azucena | 6f18319 | 2020-11-30 11:20:33 -0800 | [diff] [blame^] | 39 | if (mAudioTrack != 0) { |
| 40 | mSelfAudioDeviceCallback = new SelfAudioDeviceCallback(*this); |
| 41 | mAudioTrack->addAudioDeviceCallback(mSelfAudioDeviceCallback); |
| 42 | } |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void TrackPlayerBase::destroy() { |
| 46 | doDestroy(); |
| 47 | baseDestroy(); |
| 48 | } |
| 49 | |
Oscar Azucena | 6f18319 | 2020-11-30 11:20:33 -0800 | [diff] [blame^] | 50 | TrackPlayerBase::SelfAudioDeviceCallback::SelfAudioDeviceCallback(PlayerBase& self) : |
| 51 | AudioSystem::AudioDeviceCallback(), mSelf(self) { |
| 52 | } |
| 53 | |
| 54 | TrackPlayerBase::SelfAudioDeviceCallback::~SelfAudioDeviceCallback() { |
| 55 | } |
| 56 | |
| 57 | void TrackPlayerBase::SelfAudioDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t __unused, |
| 58 | audio_port_handle_t deviceId) { |
| 59 | mSelf.baseUpdateDeviceId(deviceId); |
| 60 | } |
| 61 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 62 | void TrackPlayerBase::doDestroy() { |
| 63 | if (mAudioTrack != 0) { |
| 64 | mAudioTrack->stop(); |
Oscar Azucena | 6f18319 | 2020-11-30 11:20:33 -0800 | [diff] [blame^] | 65 | mAudioTrack->removeAudioDeviceCallback(mSelfAudioDeviceCallback); |
| 66 | mSelfAudioDeviceCallback.clear(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 67 | // Note that there may still be another reference in post-unlock phase of SetPlayState |
| 68 | mAudioTrack.clear(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void TrackPlayerBase::setPlayerVolume(float vl, float vr) { |
| 73 | { |
| 74 | Mutex::Autolock _l(mSettingsLock); |
| 75 | mPlayerVolumeL = vl; |
| 76 | mPlayerVolumeR = vr; |
| 77 | } |
| 78 | doSetVolume(); |
| 79 | } |
| 80 | |
| 81 | //------------------------------------------------------------------------------ |
| 82 | // Implementation of IPlayer |
| 83 | status_t TrackPlayerBase::playerStart() { |
| 84 | status_t status = NO_INIT; |
| 85 | if (mAudioTrack != 0) { |
| 86 | status = mAudioTrack->start(); |
| 87 | } |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | status_t TrackPlayerBase::playerPause() { |
| 92 | status_t status = NO_INIT; |
| 93 | if (mAudioTrack != 0) { |
| 94 | mAudioTrack->pause(); |
| 95 | status = NO_ERROR; |
| 96 | } |
| 97 | return status; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | status_t TrackPlayerBase::playerStop() { |
| 102 | status_t status = NO_INIT; |
| 103 | if (mAudioTrack != 0) { |
| 104 | mAudioTrack->stop(); |
| 105 | status = NO_ERROR; |
| 106 | } |
| 107 | return status; |
| 108 | } |
| 109 | |
| 110 | status_t TrackPlayerBase::playerSetVolume() { |
| 111 | return doSetVolume(); |
| 112 | } |
| 113 | |
| 114 | status_t TrackPlayerBase::doSetVolume() { |
| 115 | status_t status = NO_INIT; |
| 116 | if (mAudioTrack != 0) { |
| 117 | float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL; |
| 118 | float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR; |
| 119 | mAudioTrack->setVolume(tl, tr); |
| 120 | status = NO_ERROR; |
| 121 | } |
| 122 | return status; |
| 123 | } |
| 124 | |
| 125 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 126 | binder::Status TrackPlayerBase::applyVolumeShaper( |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 127 | const media::VolumeShaperConfiguration& configuration, |
| 128 | const media::VolumeShaperOperation& operation) { |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 129 | |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 130 | sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(); |
| 131 | sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(); |
| 132 | |
| 133 | status_t s = spConfiguration->readFromParcelable(configuration) |
| 134 | ?: spOperation->readFromParcelable(operation); |
| 135 | if (s != OK) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 136 | return binderStatusFromStatusT(s); |
Ytai Ben-Tsvi | f0658f4 | 2020-10-26 11:51:14 -0700 | [diff] [blame] | 137 | } |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 138 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 139 | if (mAudioTrack != 0) { |
| 140 | ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 141 | VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 142 | if (status < 0) { // a non-negative value is the volume shaper id. |
| 143 | ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status); |
| 144 | } |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 145 | return binderStatusFromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 146 | } else { |
| 147 | ALOGD("TrackPlayerBase::applyVolumeShaper()" |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 148 | " no AudioTrack for volume control from IPlayer"); |
| 149 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | } // namespace android |