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 { |
| 20 | |
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; |
| 39 | } |
| 40 | |
| 41 | void TrackPlayerBase::destroy() { |
| 42 | doDestroy(); |
| 43 | baseDestroy(); |
| 44 | } |
| 45 | |
| 46 | void TrackPlayerBase::doDestroy() { |
| 47 | if (mAudioTrack != 0) { |
| 48 | mAudioTrack->stop(); |
| 49 | // Note that there may still be another reference in post-unlock phase of SetPlayState |
| 50 | mAudioTrack.clear(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void TrackPlayerBase::setPlayerVolume(float vl, float vr) { |
| 55 | { |
| 56 | Mutex::Autolock _l(mSettingsLock); |
| 57 | mPlayerVolumeL = vl; |
| 58 | mPlayerVolumeR = vr; |
| 59 | } |
| 60 | doSetVolume(); |
| 61 | } |
| 62 | |
| 63 | //------------------------------------------------------------------------------ |
| 64 | // Implementation of IPlayer |
| 65 | status_t TrackPlayerBase::playerStart() { |
| 66 | status_t status = NO_INIT; |
| 67 | if (mAudioTrack != 0) { |
| 68 | status = mAudioTrack->start(); |
| 69 | } |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | status_t TrackPlayerBase::playerPause() { |
| 74 | status_t status = NO_INIT; |
| 75 | if (mAudioTrack != 0) { |
| 76 | mAudioTrack->pause(); |
| 77 | status = NO_ERROR; |
| 78 | } |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | status_t TrackPlayerBase::playerStop() { |
| 84 | status_t status = NO_INIT; |
| 85 | if (mAudioTrack != 0) { |
| 86 | mAudioTrack->stop(); |
| 87 | status = NO_ERROR; |
| 88 | } |
| 89 | return status; |
| 90 | } |
| 91 | |
| 92 | status_t TrackPlayerBase::playerSetVolume() { |
| 93 | return doSetVolume(); |
| 94 | } |
| 95 | |
| 96 | status_t TrackPlayerBase::doSetVolume() { |
| 97 | status_t status = NO_INIT; |
| 98 | if (mAudioTrack != 0) { |
| 99 | float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL; |
| 100 | float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR; |
| 101 | mAudioTrack->setVolume(tl, tr); |
| 102 | status = NO_ERROR; |
| 103 | } |
| 104 | return status; |
| 105 | } |
| 106 | |
| 107 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 108 | binder::Status TrackPlayerBase::applyVolumeShaper( |
| 109 | const VolumeShaper::Configuration& configuration, |
| 110 | const VolumeShaper::Operation& operation) { |
| 111 | |
| 112 | sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration); |
| 113 | sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation); |
| 114 | |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 115 | if (mAudioTrack != 0) { |
| 116 | ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer"); |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 117 | VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 118 | if (status < 0) { // a non-negative value is the volume shaper id. |
| 119 | ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status); |
| 120 | } |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 121 | return binder::Status::fromStatusT(status); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 122 | } else { |
| 123 | ALOGD("TrackPlayerBase::applyVolumeShaper()" |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 124 | " no AudioTrack for volume control from IPlayer"); |
| 125 | return binder::Status::ok(); |
Eric Laurent | b532322 | 2017-05-31 15:01:56 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | } // namespace android |