• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //--------------------------------------------------------------------------------------------------
TrackPlayerBase()22 TrackPlayerBase::TrackPlayerBase() : PlayerBase(),
23         mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f)
24 {
25     ALOGD("TrackPlayerBase::TrackPlayerBase()");
26 }
27 
28 
~TrackPlayerBase()29 TrackPlayerBase::~TrackPlayerBase() {
30     ALOGD("TrackPlayerBase::~TrackPlayerBase()");
31     doDestroy();
32 }
33 
init(AudioTrack * pat,player_type_t playerType,audio_usage_t usage)34 void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) {
35     PlayerBase::init(playerType, usage);
36     mAudioTrack = pat;
37 }
38 
destroy()39 void TrackPlayerBase::destroy() {
40     doDestroy();
41     baseDestroy();
42 }
43 
doDestroy()44 void TrackPlayerBase::doDestroy() {
45     if (mAudioTrack != 0) {
46         mAudioTrack->stop();
47         // Note that there may still be another reference in post-unlock phase of SetPlayState
48         mAudioTrack.clear();
49     }
50 }
51 
setPlayerVolume(float vl,float vr)52 void TrackPlayerBase::setPlayerVolume(float vl, float vr) {
53     {
54         Mutex::Autolock _l(mSettingsLock);
55         mPlayerVolumeL = vl;
56         mPlayerVolumeR = vr;
57     }
58     doSetVolume();
59 }
60 
61 //------------------------------------------------------------------------------
62 // Implementation of IPlayer
playerStart()63 status_t TrackPlayerBase::playerStart() {
64     status_t status = NO_INIT;
65     if (mAudioTrack != 0) {
66         status = mAudioTrack->start();
67     }
68     return status;
69 }
70 
playerPause()71 status_t TrackPlayerBase::playerPause() {
72     status_t status = NO_INIT;
73     if (mAudioTrack != 0) {
74         mAudioTrack->pause();
75         status = NO_ERROR;
76     }
77     return status;
78 }
79 
80 
playerStop()81 status_t TrackPlayerBase::playerStop() {
82     status_t status = NO_INIT;
83     if (mAudioTrack != 0) {
84         mAudioTrack->stop();
85         status = NO_ERROR;
86     }
87     return status;
88 }
89 
playerSetVolume()90 status_t TrackPlayerBase::playerSetVolume() {
91     return doSetVolume();
92 }
93 
doSetVolume()94 status_t TrackPlayerBase::doSetVolume() {
95     status_t status = NO_INIT;
96     if (mAudioTrack != 0) {
97         float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL;
98         float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR;
99         mAudioTrack->setVolume(tl, tr);
100         status = NO_ERROR;
101     }
102     return status;
103 }
104 
105 
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration,const sp<VolumeShaper::Operation> & operation)106 void TrackPlayerBase::applyVolumeShaper(
107         const sp<VolumeShaper::Configuration>& configuration,
108         const sp<VolumeShaper::Operation>& operation) {
109     if (mAudioTrack != 0) {
110         ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
111         VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(configuration, operation);
112         if (status < 0) { // a non-negative value is the volume shaper id.
113             ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
114         }
115     } else {
116         ALOGD("TrackPlayerBase::applyVolumeShaper()"
117                 " no AudioTrack for volume control from IPlayer");
118     }
119 }
120 
121 } // namespace android
122