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 
21 using media::VolumeShaper;
22 
23 //--------------------------------------------------------------------------------------------------
TrackPlayerBase()24 TrackPlayerBase::TrackPlayerBase() : PlayerBase(),
25         mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f)
26 {
27     ALOGD("TrackPlayerBase::TrackPlayerBase()");
28 }
29 
30 
~TrackPlayerBase()31 TrackPlayerBase::~TrackPlayerBase() {
32     ALOGD("TrackPlayerBase::~TrackPlayerBase()");
33     doDestroy();
34 }
35 
init(AudioTrack * pat,player_type_t playerType,audio_usage_t usage)36 void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) {
37     PlayerBase::init(playerType, usage);
38     mAudioTrack = pat;
39 }
40 
destroy()41 void TrackPlayerBase::destroy() {
42     doDestroy();
43     baseDestroy();
44 }
45 
doDestroy()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 
setPlayerVolume(float vl,float vr)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
playerStart()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 
playerPause()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 
playerStop()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 
playerSetVolume()92 status_t TrackPlayerBase::playerSetVolume() {
93     return doSetVolume();
94 }
95 
doSetVolume()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 
applyVolumeShaper(const VolumeShaper::Configuration & configuration,const VolumeShaper::Operation & operation)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 
115     if (mAudioTrack != 0) {
116         ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
117         VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
118         if (status < 0) { // a non-negative value is the volume shaper id.
119             ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
120         }
121         return binder::Status::fromStatusT(status);
122     } else {
123         ALOGD("TrackPlayerBase::applyVolumeShaper()"
124               " no AudioTrack for volume control from IPlayer");
125         return binder::Status::ok();
126     }
127 }
128 
129 } // namespace android
130