blob: 0ec69db9e124e33a74848be85b31582d11e2ac70 [file] [log] [blame]
Eric Laurentb5323222017-05-31 15:01:56 -07001/*
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
23namespace android {
24
25//--------------------------------------------------------------------------------------------------
26PlayerBase::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
42PlayerBase::~PlayerBase() {
43 ALOGD("PlayerBase::~PlayerBase()");
44 baseDestroy();
45}
46
47void 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
55void PlayerBase::baseDestroy() {
56 serviceReleasePlayer();
57 if (mAudioManager != 0) {
58 mAudioManager.clear();
59 }
60}
61
62//------------------------------------------------------------------------------
63void 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
75void PlayerBase::serviceReleasePlayer() {
76 if (mAudioManager != 0
77 && mPIId != PLAYER_PIID_INVALID) {
78 mAudioManager->releasePlayer(mPIId);
79 }
80}
81
82//FIXME temporary method while some AudioTrack state is outside of this class
83void PlayerBase::reportEvent(player_state_t event) {
84 servicePlayerEvent(event);
85}
86
87//------------------------------------------------------------------------------
88// Implementation of IPlayer
89void PlayerBase::start() {
90 if (playerStart() == NO_ERROR) {
91 ALOGD("PlayerBase::start() from IPlayer");
92 servicePlayerEvent(PLAYER_STATE_STARTED);
93 } else {
94 ALOGD("PlayerBase::start() no AudioTrack to start from IPlayer");
95 }
96}
97
98void PlayerBase::pause() {
99 if (playerPause() == NO_ERROR) {
100 ALOGD("PlayerBase::pause() from IPlayer");
101 servicePlayerEvent(PLAYER_STATE_PAUSED);
102 } else {
103 ALOGD("PlayerBase::pause() no AudioTrack to pause from IPlayer");
104 }
105}
106
107
108void PlayerBase::stop() {
109 if (playerStop() == NO_ERROR) {
110 ALOGD("PlayerBase::stop() from IPlayer");
111 servicePlayerEvent(PLAYER_STATE_STOPPED);
112 } else {
113 ALOGD("PlayerBase::stop() no AudioTrack to stop from IPlayer");
114 }
115}
116
117void PlayerBase::setVolume(float vol) {
118 {
119 Mutex::Autolock _l(mSettingsLock);
120 mVolumeMultiplierL = vol;
121 mVolumeMultiplierR = vol;
122 }
123 if (playerSetVolume() == NO_ERROR) {
124 ALOGD("PlayerBase::setVolume() from IPlayer");
125 } else {
126 ALOGD("PlayerBase::setVolume() no AudioTrack for volume control from IPlayer");
127 }
128}
129
130void PlayerBase::setPan(float pan) {
131 {
132 Mutex::Autolock _l(mSettingsLock);
133 pan = min(max(-1.0f, pan), 1.0f);
134 if (pan >= 0.0f) {
135 mPanMultiplierL = 1.0f - pan;
136 mPanMultiplierR = 1.0f;
137 } else {
138 mPanMultiplierL = 1.0f;
139 mPanMultiplierR = 1.0f + pan;
140 }
141 }
142 if (playerSetVolume() == NO_ERROR) {
143 ALOGD("PlayerBase::setPan() from IPlayer");
144 } else {
145 ALOGD("PlayerBase::setPan() no AudioTrack for volume control from IPlayer");
146 }
147}
148
149void PlayerBase::setStartDelayMs(int32_t delayMs __unused) {
150 ALOGW("setStartDelay() is not supported");
151}
152
153void PlayerBase::applyVolumeShaper(
154 const sp<VolumeShaper::Configuration>& configuration __unused,
155 const sp<VolumeShaper::Operation>& operation __unused) {
156 ALOGW("applyVolumeShaper() is not supported");
157}
158
159status_t PlayerBase::onTransact(
160 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
161{
162 return BnPlayer::onTransact(code, data, reply, flags);
163}
164
165} // namespace android