• 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 using aidl_utils::binderStatusFromStatusT;
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(const sp<AudioTrack> & pat,const sp<AudioTrack::IAudioTrackCallback> & callback,player_type_t playerType,audio_usage_t usage,audio_session_t sessionId)36 void TrackPlayerBase::init(const sp<AudioTrack>& pat,
37                            const sp<AudioTrack::IAudioTrackCallback>& callback,
38                            player_type_t playerType, audio_usage_t usage,
39                            audio_session_t sessionId) {
40     PlayerBase::init(playerType, usage, sessionId);
41     mAudioTrack = pat;
42     if (mAudioTrack != 0) {
43         mCallbackHandle = callback;
44         mSelfAudioDeviceCallback = new SelfAudioDeviceCallback(*this);
45         mAudioTrack->addAudioDeviceCallback(mSelfAudioDeviceCallback);
46         mAudioTrack->setPlayerIId(mPIId); // set in PlayerBase::init().
47     }
48 }
49 
destroy()50 void TrackPlayerBase::destroy() {
51     doDestroy();
52     baseDestroy();
53 }
54 
SelfAudioDeviceCallback(PlayerBase & self)55 TrackPlayerBase::SelfAudioDeviceCallback::SelfAudioDeviceCallback(PlayerBase& self) :
56     AudioSystem::AudioDeviceCallback(), mSelf(self) {
57 }
58 
~SelfAudioDeviceCallback()59 TrackPlayerBase::SelfAudioDeviceCallback::~SelfAudioDeviceCallback() {
60 }
61 
onAudioDeviceUpdate(audio_io_handle_t __unused,audio_port_handle_t deviceId)62 void TrackPlayerBase::SelfAudioDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t __unused,
63                                                                    audio_port_handle_t deviceId) {
64     mSelf.baseUpdateDeviceId(deviceId);
65 }
66 
doDestroy()67 void TrackPlayerBase::doDestroy() {
68     if (mAudioTrack != 0) {
69         mAudioTrack->stop();
70         mAudioTrack->removeAudioDeviceCallback(mSelfAudioDeviceCallback);
71         mSelfAudioDeviceCallback.clear();
72         // Note that there may still be another reference in post-unlock phase of SetPlayState
73         mAudioTrack.clear();
74     }
75 }
76 
setPlayerVolume(float vl,float vr)77 void TrackPlayerBase::setPlayerVolume(float vl, float vr) {
78     {
79         Mutex::Autolock _l(mSettingsLock);
80         mPlayerVolumeL = vl;
81         mPlayerVolumeR = vr;
82     }
83     doSetVolume();
84 }
85 
86 //------------------------------------------------------------------------------
87 // Implementation of IPlayer
playerStart()88 status_t TrackPlayerBase::playerStart() {
89     status_t status = NO_INIT;
90     if (mAudioTrack != 0) {
91         status = mAudioTrack->start();
92     }
93     return status;
94 }
95 
playerPause()96 status_t TrackPlayerBase::playerPause() {
97     status_t status = NO_INIT;
98     if (mAudioTrack != 0) {
99         mAudioTrack->pause();
100         status = NO_ERROR;
101     }
102     return status;
103 }
104 
105 
playerStop()106 status_t TrackPlayerBase::playerStop() {
107     status_t status = NO_INIT;
108     if (mAudioTrack != 0) {
109         mAudioTrack->stop();
110         status = NO_ERROR;
111     }
112     return status;
113 }
114 
playerSetVolume()115 status_t TrackPlayerBase::playerSetVolume() {
116     return doSetVolume();
117 }
118 
doSetVolume()119 status_t TrackPlayerBase::doSetVolume() {
120     status_t status = NO_INIT;
121     if (mAudioTrack != 0) {
122         float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL;
123         float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR;
124         mAudioTrack->setVolume(tl, tr);
125         status = NO_ERROR;
126     }
127     return status;
128 }
129 
130 
applyVolumeShaper(const media::VolumeShaperConfiguration & configuration,const media::VolumeShaperOperation & operation)131 binder::Status TrackPlayerBase::applyVolumeShaper(
132         const media::VolumeShaperConfiguration& configuration,
133         const media::VolumeShaperOperation& operation) {
134 
135     sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration();
136     sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation();
137 
138     status_t s = spConfiguration->readFromParcelable(configuration)
139             ?: spOperation->readFromParcelable(operation);
140     if (s != OK) {
141         return binderStatusFromStatusT(s);
142     }
143 
144     if (mAudioTrack != 0) {
145         ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
146         VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
147         if (status < 0) { // a non-negative value is the volume shaper id.
148             ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
149         }
150         return binderStatusFromStatusT(status);
151     } else {
152         ALOGD("TrackPlayerBase::applyVolumeShaper()"
153               " no AudioTrack for volume control from IPlayer");
154         return binder::Status::ok();
155     }
156 }
157 
158 } // namespace android
159