blob: e5718388fa8a685166dfa36978c57d7827b087ef [file] [log] [blame]
Eric Laurentb5323222017-05-31 15:01:56 -07001/*
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
19namespace android {
20
Ivan Lozano8cf3a072017-08-09 09:01:33 -070021using media::VolumeShaper;
22
Eric Laurentb5323222017-05-31 15:01:56 -070023//--------------------------------------------------------------------------------------------------
24TrackPlayerBase::TrackPlayerBase() : PlayerBase(),
25 mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f)
26{
27 ALOGD("TrackPlayerBase::TrackPlayerBase()");
28}
29
30
31TrackPlayerBase::~TrackPlayerBase() {
32 ALOGD("TrackPlayerBase::~TrackPlayerBase()");
33 doDestroy();
34}
35
36void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) {
37 PlayerBase::init(playerType, usage);
38 mAudioTrack = pat;
39}
40
41void TrackPlayerBase::destroy() {
42 doDestroy();
43 baseDestroy();
44}
45
46void 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
54void 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
65status_t TrackPlayerBase::playerStart() {
66 status_t status = NO_INIT;
67 if (mAudioTrack != 0) {
68 status = mAudioTrack->start();
69 }
70 return status;
71}
72
73status_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
83status_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
92status_t TrackPlayerBase::playerSetVolume() {
93 return doSetVolume();
94}
95
96status_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 Lozano8cf3a072017-08-09 09:01:33 -0700108binder::Status TrackPlayerBase::applyVolumeShaper(
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700109 const media::VolumeShaperConfiguration& configuration,
110 const media::VolumeShaperOperation& operation) {
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700111
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700112 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration();
113 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation();
114
115 status_t s = spConfiguration->readFromParcelable(configuration)
116 ?: spOperation->readFromParcelable(operation);
117 if (s != OK) {
118 return binder::Status::fromStatusT(s);
119 }
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700120
Eric Laurentb5323222017-05-31 15:01:56 -0700121 if (mAudioTrack != 0) {
122 ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700123 VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Eric Laurentb5323222017-05-31 15:01:56 -0700124 if (status < 0) { // a non-negative value is the volume shaper id.
125 ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
126 }
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700127 return binder::Status::fromStatusT(status);
Eric Laurentb5323222017-05-31 15:01:56 -0700128 } else {
129 ALOGD("TrackPlayerBase::applyVolumeShaper()"
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700130 " no AudioTrack for volume control from IPlayer");
131 return binder::Status::ok();
Eric Laurentb5323222017-05-31 15:01:56 -0700132 }
133}
134
135} // namespace android