PlayerBase.cpp revision 1d32e9f8e5ba52d69c6319270c8a63a995d2c4f2
1b5323222bd524876dda1ebf89694f186278e2229Eric Laurent/*
2b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * Copyright (C) 2017 The Android Open Source Project
3b5323222bd524876dda1ebf89694f186278e2229Eric Laurent *
4b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * you may not use this file except in compliance with the License.
6b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * You may obtain a copy of the License at
7b5323222bd524876dda1ebf89694f186278e2229Eric Laurent *
8b5323222bd524876dda1ebf89694f186278e2229Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9b5323222bd524876dda1ebf89694f186278e2229Eric Laurent *
10b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * Unless required by applicable law or agreed to in writing, software
11b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * See the License for the specific language governing permissions and
14b5323222bd524876dda1ebf89694f186278e2229Eric Laurent * limitations under the License.
15b5323222bd524876dda1ebf89694f186278e2229Eric Laurent */
16b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
17b5323222bd524876dda1ebf89694f186278e2229Eric Laurent#include <binder/IServiceManager.h>
18b5323222bd524876dda1ebf89694f186278e2229Eric Laurent#include <media/PlayerBase.h>
19b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
20b5323222bd524876dda1ebf89694f186278e2229Eric Laurent#define max(a, b) ((a) > (b) ? (a) : (b))
21b5323222bd524876dda1ebf89694f186278e2229Eric Laurent#define min(a, b) ((a) < (b) ? (a) : (b))
22b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
23b5323222bd524876dda1ebf89694f186278e2229Eric Laurentnamespace android {
24b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
25b5323222bd524876dda1ebf89694f186278e2229Eric Laurent//--------------------------------------------------------------------------------------------------
26b5323222bd524876dda1ebf89694f186278e2229Eric LaurentPlayerBase::PlayerBase() : BnPlayer(),
27b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPanMultiplierL(1.0f), mPanMultiplierR(1.0f),
28b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierL(1.0f), mVolumeMultiplierR(1.0f),
29b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN)
30b5323222bd524876dda1ebf89694f186278e2229Eric Laurent{
31b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGD("PlayerBase::PlayerBase()");
32b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    // use checkService() to avoid blocking if audio service is not up yet
33b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio"));
34b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (binder == 0) {
35b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGE("PlayerBase(): binding to audio service failed, service up?");
36b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
37b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager = interface_cast<IAudioManager>(binder);
38b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
39b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
40b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
41b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
42b5323222bd524876dda1ebf89694f186278e2229Eric LaurentPlayerBase::~PlayerBase() {
43b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGD("PlayerBase::~PlayerBase()");
44b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    baseDestroy();
45b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
46b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
47b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::init(player_type_t playerType, audio_usage_t usage) {
48b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager == 0) {
49b5323222bd524876dda1ebf89694f186278e2229Eric Laurent                ALOGE("AudioPlayer realize: no audio service, player will not be registered");
50b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
51b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this);
52b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
53b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
54b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
55b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::baseDestroy() {
56b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    serviceReleasePlayer();
57b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0) {
58b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager.clear();
59b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
60b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
61b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
62b5323222bd524876dda1ebf89694f186278e2229Eric Laurent//------------------------------------------------------------------------------
63b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::servicePlayerEvent(player_state_t event) {
64b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0) {
65b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        // only report state change
66b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mPlayerStateLock);
67b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        if (event != mLastReportedEvent
68b5323222bd524876dda1ebf89694f186278e2229Eric Laurent                && mPIId != PLAYER_PIID_INVALID) {
69b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mLastReportedEvent = event;
70b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mAudioManager->playerEvent(mPIId, event);
71b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        }
72b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
73b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
74b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
75b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::serviceReleasePlayer() {
76b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0
77b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            && mPIId != PLAYER_PIID_INVALID) {
78b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager->releasePlayer(mPIId);
79b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
80b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
81b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
82b5323222bd524876dda1ebf89694f186278e2229Eric Laurent//FIXME temporary method while some AudioTrack state is outside of this class
83b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::reportEvent(player_state_t event) {
84b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    servicePlayerEvent(event);
85b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
86b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
871d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurentstatus_t PlayerBase::startWithStatus() {
881d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    status_t status = playerStart();
891d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    if (status == NO_ERROR) {
90b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::start() from IPlayer");
91b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        servicePlayerEvent(PLAYER_STATE_STARTED);
92b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
93b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::start() no AudioTrack to start from IPlayer");
94b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
951d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    return status;
961d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent}
971d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent
981d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent//------------------------------------------------------------------------------
991d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent// Implementation of IPlayer
1001d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurentvoid PlayerBase::start() {
1011d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    (void)startWithStatus();
102b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
103b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
104b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::pause() {
105b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (playerPause() == NO_ERROR) {
106b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::pause() from IPlayer");
107b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        servicePlayerEvent(PLAYER_STATE_PAUSED);
108b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
109b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::pause() no AudioTrack to pause from IPlayer");
110b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
111b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
112b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
113b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
114b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::stop() {
115b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (playerStop() == NO_ERROR) {
116b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::stop() from IPlayer");
117b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        servicePlayerEvent(PLAYER_STATE_STOPPED);
118b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
119b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::stop() no AudioTrack to stop from IPlayer");
120b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
121b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
122b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
123b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::setVolume(float vol) {
124b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    {
125b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mSettingsLock);
126b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierL = vol;
127b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierR = vol;
128b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
129b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (playerSetVolume() == NO_ERROR) {
130b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::setVolume() from IPlayer");
131b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
132b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::setVolume() no AudioTrack for volume control from IPlayer");
133b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
134b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
135b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
136b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::setPan(float pan) {
137b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    {
138b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mSettingsLock);
139b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        pan = min(max(-1.0f, pan), 1.0f);
140b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        if (pan >= 0.0f) {
141b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierL = 1.0f - pan;
142b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierR = 1.0f;
143b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        } else {
144b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierL = 1.0f;
145b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierR = 1.0f + pan;
146b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        }
147b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
148b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (playerSetVolume() == NO_ERROR) {
149b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::setPan() from IPlayer");
150b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
151b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGD("PlayerBase::setPan() no AudioTrack for volume control from IPlayer");
152b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
153b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
154b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
155b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::setStartDelayMs(int32_t delayMs __unused) {
156b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGW("setStartDelay() is not supported");
157b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
158b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
159b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::applyVolumeShaper(
160b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        const sp<VolumeShaper::Configuration>& configuration  __unused,
161b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        const sp<VolumeShaper::Operation>& operation __unused) {
162b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGW("applyVolumeShaper() is not supported");
163b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
164b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
165b5323222bd524876dda1ebf89694f186278e2229Eric Laurentstatus_t PlayerBase::onTransact(
166b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
167b5323222bd524876dda1ebf89694f186278e2229Eric Laurent{
168b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    return BnPlayer::onTransact(code, data, reply, flags);
169b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
170b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
171b5323222bd524876dda1ebf89694f186278e2229Eric Laurent} // namespace android
172