blob: 5c7375623c13a5543444a37949b9fb7250adf634 [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 <media/TrackPlayerBase.h>
18
19namespace android {
Andy Hung1131b6e2020-12-08 20:47:45 -080020using aidl_utils::binderStatusFromStatusT;
Ivan Lozano8cf3a072017-08-09 09:01:33 -070021using media::VolumeShaper;
22
Eric Laurentb5323222017-05-31 15:01:56 -070023//--------------------------------------------------------------------------------------------------
24TrackPlayerBase::TrackPlayerBase() : PlayerBase(),
25 mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f)
26{
27 ALOGD("TrackPlayerBase::TrackPlayerBase()");
28}
29
30
31TrackPlayerBase::~TrackPlayerBase() {
32 ALOGD("TrackPlayerBase::~TrackPlayerBase()");
33 doDestroy();
34}
35
36void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) {
37 PlayerBase::init(playerType, usage);
38 mAudioTrack = pat;
Oscar Azucena6f183192020-11-30 11:20:33 -080039 if (mAudioTrack != 0) {
40 mSelfAudioDeviceCallback = new SelfAudioDeviceCallback(*this);
41 mAudioTrack->addAudioDeviceCallback(mSelfAudioDeviceCallback);
42 }
Eric Laurentb5323222017-05-31 15:01:56 -070043}
44
45void TrackPlayerBase::destroy() {
46 doDestroy();
47 baseDestroy();
48}
49
Oscar Azucena6f183192020-11-30 11:20:33 -080050TrackPlayerBase::SelfAudioDeviceCallback::SelfAudioDeviceCallback(PlayerBase& self) :
51 AudioSystem::AudioDeviceCallback(), mSelf(self) {
52}
53
54TrackPlayerBase::SelfAudioDeviceCallback::~SelfAudioDeviceCallback() {
55}
56
57void TrackPlayerBase::SelfAudioDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t __unused,
58 audio_port_handle_t deviceId) {
59 mSelf.baseUpdateDeviceId(deviceId);
60}
61
Eric Laurentb5323222017-05-31 15:01:56 -070062void TrackPlayerBase::doDestroy() {
63 if (mAudioTrack != 0) {
64 mAudioTrack->stop();
Oscar Azucena6f183192020-11-30 11:20:33 -080065 mAudioTrack->removeAudioDeviceCallback(mSelfAudioDeviceCallback);
66 mSelfAudioDeviceCallback.clear();
Eric Laurentb5323222017-05-31 15:01:56 -070067 // Note that there may still be another reference in post-unlock phase of SetPlayState
68 mAudioTrack.clear();
69 }
70}
71
72void TrackPlayerBase::setPlayerVolume(float vl, float vr) {
73 {
74 Mutex::Autolock _l(mSettingsLock);
75 mPlayerVolumeL = vl;
76 mPlayerVolumeR = vr;
77 }
78 doSetVolume();
79}
80
81//------------------------------------------------------------------------------
82// Implementation of IPlayer
83status_t TrackPlayerBase::playerStart() {
84 status_t status = NO_INIT;
85 if (mAudioTrack != 0) {
86 status = mAudioTrack->start();
87 }
88 return status;
89}
90
91status_t TrackPlayerBase::playerPause() {
92 status_t status = NO_INIT;
93 if (mAudioTrack != 0) {
94 mAudioTrack->pause();
95 status = NO_ERROR;
96 }
97 return status;
98}
99
100
101status_t TrackPlayerBase::playerStop() {
102 status_t status = NO_INIT;
103 if (mAudioTrack != 0) {
104 mAudioTrack->stop();
105 status = NO_ERROR;
106 }
107 return status;
108}
109
110status_t TrackPlayerBase::playerSetVolume() {
111 return doSetVolume();
112}
113
114status_t TrackPlayerBase::doSetVolume() {
115 status_t status = NO_INIT;
116 if (mAudioTrack != 0) {
117 float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL;
118 float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR;
119 mAudioTrack->setVolume(tl, tr);
120 status = NO_ERROR;
121 }
122 return status;
123}
124
125
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700126binder::Status TrackPlayerBase::applyVolumeShaper(
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700127 const media::VolumeShaperConfiguration& configuration,
128 const media::VolumeShaperOperation& operation) {
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700129
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700130 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration();
131 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation();
132
133 status_t s = spConfiguration->readFromParcelable(configuration)
134 ?: spOperation->readFromParcelable(operation);
135 if (s != OK) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800136 return binderStatusFromStatusT(s);
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700137 }
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700138
Eric Laurentb5323222017-05-31 15:01:56 -0700139 if (mAudioTrack != 0) {
140 ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700141 VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Eric Laurentb5323222017-05-31 15:01:56 -0700142 if (status < 0) { // a non-negative value is the volume shaper id.
143 ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
144 }
Andy Hung1131b6e2020-12-08 20:47:45 -0800145 return binderStatusFromStatusT(status);
Eric Laurentb5323222017-05-31 15:01:56 -0700146 } else {
147 ALOGD("TrackPlayerBase::applyVolumeShaper()"
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700148 " no AudioTrack for volume control from IPlayer");
149 return binder::Status::ok();
Eric Laurentb5323222017-05-31 15:01:56 -0700150 }
151}
152
153} // namespace android