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