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 <binder/IServiceManager.h>
18 #include <media/PlayerBase.h>
19
20 #define max(a, b) ((a) > (b) ? (a) : (b))
21 #define min(a, b) ((a) < (b) ? (a) : (b))
22
23 namespace android {
24
25 //--------------------------------------------------------------------------------------------------
PlayerBase()26 PlayerBase::PlayerBase() : BnPlayer(),
27 mPanMultiplierL(1.0f), mPanMultiplierR(1.0f),
28 mVolumeMultiplierL(1.0f), mVolumeMultiplierR(1.0f),
29 mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN)
30 {
31 ALOGD("PlayerBase::PlayerBase()");
32 // use checkService() to avoid blocking if audio service is not up yet
33 sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio"));
34 if (binder == 0) {
35 ALOGE("PlayerBase(): binding to audio service failed, service up?");
36 } else {
37 mAudioManager = interface_cast<IAudioManager>(binder);
38 }
39 }
40
41
~PlayerBase()42 PlayerBase::~PlayerBase() {
43 ALOGD("PlayerBase::~PlayerBase()");
44 baseDestroy();
45 }
46
init(player_type_t playerType,audio_usage_t usage)47 void PlayerBase::init(player_type_t playerType, audio_usage_t usage) {
48 if (mAudioManager == 0) {
49 ALOGE("AudioPlayer realize: no audio service, player will not be registered");
50 } else {
51 mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this);
52 }
53 }
54
baseDestroy()55 void PlayerBase::baseDestroy() {
56 serviceReleasePlayer();
57 if (mAudioManager != 0) {
58 mAudioManager.clear();
59 }
60 }
61
62 //------------------------------------------------------------------------------
servicePlayerEvent(player_state_t event)63 void PlayerBase::servicePlayerEvent(player_state_t event) {
64 if (mAudioManager != 0) {
65 // only report state change
66 Mutex::Autolock _l(mPlayerStateLock);
67 if (event != mLastReportedEvent
68 && mPIId != PLAYER_PIID_INVALID) {
69 mLastReportedEvent = event;
70 mAudioManager->playerEvent(mPIId, event);
71 }
72 }
73 }
74
serviceReleasePlayer()75 void PlayerBase::serviceReleasePlayer() {
76 if (mAudioManager != 0
77 && mPIId != PLAYER_PIID_INVALID) {
78 mAudioManager->releasePlayer(mPIId);
79 }
80 }
81
82 //FIXME temporary method while some player state is outside of this class
reportEvent(player_state_t event)83 void PlayerBase::reportEvent(player_state_t event) {
84 servicePlayerEvent(event);
85 }
86
startWithStatus()87 status_t PlayerBase::startWithStatus() {
88 status_t status = playerStart();
89 if (status == NO_ERROR) {
90 servicePlayerEvent(PLAYER_STATE_STARTED);
91 } else {
92 ALOGW("PlayerBase::start() error %d", status);
93 }
94 return status;
95 }
96
pauseWithStatus()97 status_t PlayerBase::pauseWithStatus() {
98 status_t status = playerPause();
99 if (status == NO_ERROR) {
100 servicePlayerEvent(PLAYER_STATE_PAUSED);
101 } else {
102 ALOGW("PlayerBase::pause() error %d", status);
103 }
104 return status;
105 }
106
107
stopWithStatus()108 status_t PlayerBase::stopWithStatus() {
109 status_t status = playerStop();
110 if (status == NO_ERROR) {
111 servicePlayerEvent(PLAYER_STATE_STOPPED);
112 } else {
113 ALOGW("PlayerBase::stop() error %d", status);
114 }
115 return status;
116 }
117
118 //------------------------------------------------------------------------------
119 // Implementation of IPlayer
start()120 void PlayerBase::start() {
121 ALOGD("PlayerBase::start() from IPlayer");
122 (void)startWithStatus();
123 }
124
pause()125 void PlayerBase::pause() {
126 ALOGD("PlayerBase::pause() from IPlayer");
127 (void)pauseWithStatus();
128 }
129
130
stop()131 void PlayerBase::stop() {
132 ALOGD("PlayerBase::stop() from IPlayer");
133 (void)stopWithStatus();
134 }
135
setVolume(float vol)136 void PlayerBase::setVolume(float vol) {
137 ALOGD("PlayerBase::setVolume() from IPlayer");
138 {
139 Mutex::Autolock _l(mSettingsLock);
140 mVolumeMultiplierL = vol;
141 mVolumeMultiplierR = vol;
142 }
143 status_t status = playerSetVolume();
144 if (status != NO_ERROR) {
145 ALOGW("PlayerBase::setVolume() error %d", status);
146 }
147 }
148
setPan(float pan)149 void PlayerBase::setPan(float pan) {
150 ALOGD("PlayerBase::setPan() from IPlayer");
151 {
152 Mutex::Autolock _l(mSettingsLock);
153 pan = min(max(-1.0f, pan), 1.0f);
154 if (pan >= 0.0f) {
155 mPanMultiplierL = 1.0f - pan;
156 mPanMultiplierR = 1.0f;
157 } else {
158 mPanMultiplierL = 1.0f;
159 mPanMultiplierR = 1.0f + pan;
160 }
161 }
162 status_t status = playerSetVolume();
163 if (status != NO_ERROR) {
164 ALOGW("PlayerBase::setPan() error %d", status);
165 }
166 }
167
setStartDelayMs(int32_t delayMs __unused)168 void PlayerBase::setStartDelayMs(int32_t delayMs __unused) {
169 ALOGW("setStartDelay() is not supported");
170 }
171
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration __unused,const sp<VolumeShaper::Operation> & operation __unused)172 void PlayerBase::applyVolumeShaper(
173 const sp<VolumeShaper::Configuration>& configuration __unused,
174 const sp<VolumeShaper::Operation>& operation __unused) {
175 ALOGW("applyVolumeShaper() is not supported");
176 }
177
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)178 status_t PlayerBase::onTransact(
179 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
180 {
181 return BnPlayer::onTransact(code, data, reply, flags);
182 }
183
184 } // namespace android
185