blob: 536e9fb15b722c2165a54feb06141f8d41d67605 [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
Eric Laurent41931252021-01-29 20:40:35 +010036void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage,
37 audio_session_t sessionId) {
38 PlayerBase::init(playerType, usage, sessionId);
Eric Laurentb5323222017-05-31 15:01:56 -070039 mAudioTrack = pat;
Oscar Azucenae0414ec2020-12-22 20:40:07 +000040 if (mAudioTrack != 0) {
41 mSelfAudioDeviceCallback = new SelfAudioDeviceCallback(*this);
42 mAudioTrack->addAudioDeviceCallback(mSelfAudioDeviceCallback);
43 }
Eric Laurentb5323222017-05-31 15:01:56 -070044}
45
46void TrackPlayerBase::destroy() {
47 doDestroy();
48 baseDestroy();
49}
50
Oscar Azucenae0414ec2020-12-22 20:40:07 +000051TrackPlayerBase::SelfAudioDeviceCallback::SelfAudioDeviceCallback(PlayerBase& self) :
52 AudioSystem::AudioDeviceCallback(), mSelf(self) {
53}
54
55TrackPlayerBase::SelfAudioDeviceCallback::~SelfAudioDeviceCallback() {
56}
57
58void TrackPlayerBase::SelfAudioDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t __unused,
59 audio_port_handle_t deviceId) {
60 mSelf.baseUpdateDeviceId(deviceId);
61}
62
Eric Laurentb5323222017-05-31 15:01:56 -070063void TrackPlayerBase::doDestroy() {
64 if (mAudioTrack != 0) {
65 mAudioTrack->stop();
Oscar Azucenae0414ec2020-12-22 20:40:07 +000066 mAudioTrack->removeAudioDeviceCallback(mSelfAudioDeviceCallback);
67 mSelfAudioDeviceCallback.clear();
Eric Laurentb5323222017-05-31 15:01:56 -070068 // Note that there may still be another reference in post-unlock phase of SetPlayState
69 mAudioTrack.clear();
70 }
71}
72
73void TrackPlayerBase::setPlayerVolume(float vl, float vr) {
74 {
75 Mutex::Autolock _l(mSettingsLock);
76 mPlayerVolumeL = vl;
77 mPlayerVolumeR = vr;
78 }
79 doSetVolume();
80}
81
82//------------------------------------------------------------------------------
83// Implementation of IPlayer
84status_t TrackPlayerBase::playerStart() {
85 status_t status = NO_INIT;
86 if (mAudioTrack != 0) {
87 status = mAudioTrack->start();
88 }
89 return status;
90}
91
92status_t TrackPlayerBase::playerPause() {
93 status_t status = NO_INIT;
94 if (mAudioTrack != 0) {
95 mAudioTrack->pause();
96 status = NO_ERROR;
97 }
98 return status;
99}
100
101
102status_t TrackPlayerBase::playerStop() {
103 status_t status = NO_INIT;
104 if (mAudioTrack != 0) {
105 mAudioTrack->stop();
106 status = NO_ERROR;
107 }
108 return status;
109}
110
111status_t TrackPlayerBase::playerSetVolume() {
112 return doSetVolume();
113}
114
115status_t TrackPlayerBase::doSetVolume() {
116 status_t status = NO_INIT;
117 if (mAudioTrack != 0) {
118 float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL;
119 float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR;
120 mAudioTrack->setVolume(tl, tr);
121 status = NO_ERROR;
122 }
123 return status;
124}
125
126
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700127binder::Status TrackPlayerBase::applyVolumeShaper(
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700128 const media::VolumeShaperConfiguration& configuration,
129 const media::VolumeShaperOperation& operation) {
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700130
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700131 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration();
132 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation();
133
134 status_t s = spConfiguration->readFromParcelable(configuration)
135 ?: spOperation->readFromParcelable(operation);
136 if (s != OK) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800137 return binderStatusFromStatusT(s);
Ytai Ben-Tsvif0658f42020-10-26 11:51:14 -0700138 }
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700139
Eric Laurentb5323222017-05-31 15:01:56 -0700140 if (mAudioTrack != 0) {
141 ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700142 VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Eric Laurentb5323222017-05-31 15:01:56 -0700143 if (status < 0) { // a non-negative value is the volume shaper id.
144 ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
145 }
Andy Hung1131b6e2020-12-08 20:47:45 -0800146 return binderStatusFromStatusT(status);
Eric Laurentb5323222017-05-31 15:01:56 -0700147 } else {
148 ALOGD("TrackPlayerBase::applyVolumeShaper()"
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700149 " no AudioTrack for volume control from IPlayer");
150 return binder::Status::ok();
Eric Laurentb5323222017-05-31 15:01:56 -0700151 }
152}
153
154} // namespace android