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
258cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanousing media::VolumeShaper;
268cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano
27b5323222bd524876dda1ebf89694f186278e2229Eric Laurent//--------------------------------------------------------------------------------------------------
28b5323222bd524876dda1ebf89694f186278e2229Eric LaurentPlayerBase::PlayerBase() : BnPlayer(),
29b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPanMultiplierL(1.0f), mPanMultiplierR(1.0f),
30b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierL(1.0f), mVolumeMultiplierR(1.0f),
31b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPIId(PLAYER_PIID_INVALID), mLastReportedEvent(PLAYER_STATE_UNKNOWN)
32b5323222bd524876dda1ebf89694f186278e2229Eric Laurent{
33b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGD("PlayerBase::PlayerBase()");
34b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    // use checkService() to avoid blocking if audio service is not up yet
35b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio"));
36b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (binder == 0) {
37b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        ALOGE("PlayerBase(): binding to audio service failed, service up?");
38b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
39b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager = interface_cast<IAudioManager>(binder);
40b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
41b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
42b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
43b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
44b5323222bd524876dda1ebf89694f186278e2229Eric LaurentPlayerBase::~PlayerBase() {
45b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGD("PlayerBase::~PlayerBase()");
46b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    baseDestroy();
47b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
48b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
49b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::init(player_type_t playerType, audio_usage_t usage) {
50b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager == 0) {
51b5323222bd524876dda1ebf89694f186278e2229Eric Laurent                ALOGE("AudioPlayer realize: no audio service, player will not be registered");
52b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
53b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mPIId = mAudioManager->trackPlayer(playerType, usage, AUDIO_CONTENT_TYPE_UNKNOWN, this);
54b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
55b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
56b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
57b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::baseDestroy() {
58b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    serviceReleasePlayer();
59b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0) {
60b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager.clear();
61b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
62b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
63b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
64b5323222bd524876dda1ebf89694f186278e2229Eric Laurent//------------------------------------------------------------------------------
65b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::servicePlayerEvent(player_state_t event) {
66b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0) {
67b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        // only report state change
68b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mPlayerStateLock);
69b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        if (event != mLastReportedEvent
70b5323222bd524876dda1ebf89694f186278e2229Eric Laurent                && mPIId != PLAYER_PIID_INVALID) {
71b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mLastReportedEvent = event;
72b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mAudioManager->playerEvent(mPIId, event);
73b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        }
74b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
75b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
76b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
77b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::serviceReleasePlayer() {
78b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    if (mAudioManager != 0
79b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            && mPIId != PLAYER_PIID_INVALID) {
80b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mAudioManager->releasePlayer(mPIId);
81b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
82b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
83b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
84a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent//FIXME temporary method while some player state is outside of this class
85b5323222bd524876dda1ebf89694f186278e2229Eric Laurentvoid PlayerBase::reportEvent(player_state_t event) {
86b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    servicePlayerEvent(event);
87b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
88b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
891d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurentstatus_t PlayerBase::startWithStatus() {
901d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    status_t status = playerStart();
911d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    if (status == NO_ERROR) {
92b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        servicePlayerEvent(PLAYER_STATE_STARTED);
93b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    } else {
94a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        ALOGW("PlayerBase::start() error %d", status);
95a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    }
96a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    return status;
97a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent}
98a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent
99a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurentstatus_t PlayerBase::pauseWithStatus() {
100a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    status_t status = playerPause();
101a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    if (status == NO_ERROR) {
102a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        servicePlayerEvent(PLAYER_STATE_PAUSED);
103a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    } else {
104a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        ALOGW("PlayerBase::pause() error %d", status);
105a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    }
106a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    return status;
107a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent}
108a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent
109a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent
110a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurentstatus_t PlayerBase::stopWithStatus() {
111a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    status_t status = playerStop();
112a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    if (status == NO_ERROR) {
113a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        servicePlayerEvent(PLAYER_STATE_STOPPED);
114a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    } else {
115a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        ALOGW("PlayerBase::stop() error %d", status);
116b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
1171d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    return status;
1181d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent}
1191d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent
1201d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent//------------------------------------------------------------------------------
1211d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent// Implementation of IPlayer
1228cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::start() {
123a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    ALOGD("PlayerBase::start() from IPlayer");
1241d32e9f8e5ba52d69c6319270c8a63a995d2c4f2Eric Laurent    (void)startWithStatus();
1258cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::ok();
126b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
127b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1288cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::pause() {
129a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    ALOGD("PlayerBase::pause() from IPlayer");
130a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    (void)pauseWithStatus();
1318cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::ok();
132b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
133b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
134b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1358cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::stop() {
136a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    ALOGD("PlayerBase::stop() from IPlayer");
137a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    (void)stopWithStatus();
1388cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::ok();
139b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
140b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1418cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::setVolume(float vol) {
142a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    ALOGD("PlayerBase::setVolume() from IPlayer");
143b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    {
144b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mSettingsLock);
145b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierL = vol;
146b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        mVolumeMultiplierR = vol;
147b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
148a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    status_t status = playerSetVolume();
149a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    if (status != NO_ERROR) {
150a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        ALOGW("PlayerBase::setVolume() error %d", status);
151b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
1528cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::fromStatusT(status);
153b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
154b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1558cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::setPan(float pan) {
156a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    ALOGD("PlayerBase::setPan() from IPlayer");
157b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    {
158b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        Mutex::Autolock _l(mSettingsLock);
159b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        pan = min(max(-1.0f, pan), 1.0f);
160b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        if (pan >= 0.0f) {
161b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierL = 1.0f - pan;
162b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierR = 1.0f;
163b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        } else {
164b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierL = 1.0f;
165b5323222bd524876dda1ebf89694f186278e2229Eric Laurent            mPanMultiplierR = 1.0f + pan;
166b5323222bd524876dda1ebf89694f186278e2229Eric Laurent        }
167b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
168a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    status_t status = playerSetVolume();
169a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent    if (status != NO_ERROR) {
170a2f296e06bee54cc83130a17f136cab1006f55d5Eric Laurent        ALOGW("PlayerBase::setPan() error %d", status);
171b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    }
1728cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::fromStatusT(status);
173b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
174b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1758cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::setStartDelayMs(int32_t delayMs __unused) {
176b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGW("setStartDelay() is not supported");
1778cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::ok();
178b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
179b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
1808cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozanobinder::Status PlayerBase::applyVolumeShaper(
1818cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano            const VolumeShaper::Configuration& configuration __unused,
1828cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano            const VolumeShaper::Operation& operation __unused) {
183b5323222bd524876dda1ebf89694f186278e2229Eric Laurent    ALOGW("applyVolumeShaper() is not supported");
1848cf3a0788df2ee184b498086b1b8da322eeadccaIvan Lozano    return binder::Status::ok();
185b5323222bd524876dda1ebf89694f186278e2229Eric Laurent}
186b5323222bd524876dda1ebf89694f186278e2229Eric Laurent
187b5323222bd524876dda1ebf89694f186278e2229Eric Laurent} // namespace android
188