163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi/*
263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi**
363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** Copyright 2017, The Android Open Source Project
463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi**
563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** Licensed under the Apache License, Version 2.0 (the "License");
663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** you may not use this file except in compliance with the License.
763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** You may obtain a copy of the License at
863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi**
963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi**     http://www.apache.org/licenses/LICENSE-2.0
1063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi**
1163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** Unless required by applicable law or agreed to in writing, software
1263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** distributed under the License is distributed on an "AS IS" BASIS,
1363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** See the License for the specific language governing permissions and
1563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi** limitations under the License.
1663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi*/
1763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
1863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#define LOG_TAG "IPlayer"
1963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi//#define LOG_NDEBUG 0
2063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#include <utils/Log.h>
2163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
2263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#include <stdint.h>
2363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#include <sys/types.h>
2463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
2563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#include <binder/Parcel.h>
2663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
2763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi#include <audiomanager/IPlayer.h>
2863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
2963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivinamespace android {
3063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
3163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivienum {
3263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    START      = IBinder::FIRST_CALL_TRANSACTION,
3363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    PAUSE      = IBinder::FIRST_CALL_TRANSACTION + 1,
3463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    STOP       = IBinder::FIRST_CALL_TRANSACTION + 2,
3563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    SET_VOLUME = IBinder::FIRST_CALL_TRANSACTION + 3,
361c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    SET_PAN    = IBinder::FIRST_CALL_TRANSACTION + 4,
371c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    SET_START_DELAY_MS = IBinder::FIRST_CALL_TRANSACTION + 5,
386108105064f935ccd1ff15485700cc8c3328e456Andy Hung    APPLY_VOLUME_SHAPER = IBinder::FIRST_CALL_TRANSACTION + 6,
3963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi};
4063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
4163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Triviclass BpPlayer : public BpInterface<IPlayer>
4263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi{
4363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivipublic:
4463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    explicit BpPlayer(const sp<IBinder>& impl)
4563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        : BpInterface<IPlayer>(impl)
4663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    {
4763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
4863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
4963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    virtual void start()
5063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    {
5163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        Parcel data, reply;
5263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
5363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        remote()->transact(START, data, &reply);
5463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
5563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
5663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    virtual void pause()
5763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    {
5863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        Parcel data, reply;
5963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
6063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        remote()->transact(PAUSE, data, &reply);
6163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
6263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
6363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    virtual void stop()
6463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    {
6563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        Parcel data, reply;
6663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
6763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        remote()->transact(STOP, data, &reply);
6863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
6963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
7063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    virtual void setVolume(float vol)
7163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    {
7263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        Parcel data, reply;
7363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
7463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        data.writeFloat(vol);
7563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        remote()->transact(SET_VOLUME, data, &reply);
7663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
771c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi
781c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    virtual void setPan(float pan)
791c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    {
801c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        Parcel data, reply;
811c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
821c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        data.writeFloat(pan);
831c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        remote()->transact(SET_PAN, data, &reply);
841c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    }
851c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi
861c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    virtual void setStartDelayMs(int32_t delayMs) {
871c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        Parcel data, reply;
881c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
891c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        data.writeInt32(delayMs);
901c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        remote()->transact(SET_START_DELAY_MS, data, &reply);
911c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi    }
926108105064f935ccd1ff15485700cc8c3328e456Andy Hung
936108105064f935ccd1ff15485700cc8c3328e456Andy Hung    virtual void applyVolumeShaper(
946108105064f935ccd1ff15485700cc8c3328e456Andy Hung            const sp<VolumeShaper::Configuration>& configuration,
956108105064f935ccd1ff15485700cc8c3328e456Andy Hung            const sp<VolumeShaper::Operation>& operation) {
966108105064f935ccd1ff15485700cc8c3328e456Andy Hung        Parcel data, reply;
976108105064f935ccd1ff15485700cc8c3328e456Andy Hung        data.writeInterfaceToken(IPlayer::getInterfaceDescriptor());
986108105064f935ccd1ff15485700cc8c3328e456Andy Hung
996108105064f935ccd1ff15485700cc8c3328e456Andy Hung        status_t status = configuration.get() == nullptr
1006108105064f935ccd1ff15485700cc8c3328e456Andy Hung                ? data.writeInt32(0)
1016108105064f935ccd1ff15485700cc8c3328e456Andy Hung                :  data.writeInt32(1)
1026108105064f935ccd1ff15485700cc8c3328e456Andy Hung                    ?: configuration->writeToParcel(&data);
1036108105064f935ccd1ff15485700cc8c3328e456Andy Hung        if (status != NO_ERROR) {
1046108105064f935ccd1ff15485700cc8c3328e456Andy Hung            ALOGW("applyVolumeShaper failed configuration parceling: %d", status);
1056108105064f935ccd1ff15485700cc8c3328e456Andy Hung            return; // ignore error
1066108105064f935ccd1ff15485700cc8c3328e456Andy Hung        }
1076108105064f935ccd1ff15485700cc8c3328e456Andy Hung
1086108105064f935ccd1ff15485700cc8c3328e456Andy Hung        status = operation.get() == nullptr
1096108105064f935ccd1ff15485700cc8c3328e456Andy Hung                ? status = data.writeInt32(0)
1106108105064f935ccd1ff15485700cc8c3328e456Andy Hung                : data.writeInt32(1)
1116108105064f935ccd1ff15485700cc8c3328e456Andy Hung                    ?: operation->writeToParcel(&data);
1126108105064f935ccd1ff15485700cc8c3328e456Andy Hung        if (status != NO_ERROR) {
1136108105064f935ccd1ff15485700cc8c3328e456Andy Hung            ALOGW("applyVolumeShaper failed operation parceling: %d", status);
1146108105064f935ccd1ff15485700cc8c3328e456Andy Hung            return; // ignore error
1156108105064f935ccd1ff15485700cc8c3328e456Andy Hung        }
1166108105064f935ccd1ff15485700cc8c3328e456Andy Hung
1176108105064f935ccd1ff15485700cc8c3328e456Andy Hung        status = remote()->transact(APPLY_VOLUME_SHAPER, data, &reply);
1186108105064f935ccd1ff15485700cc8c3328e456Andy Hung
1196108105064f935ccd1ff15485700cc8c3328e456Andy Hung        ALOGW_IF(status != NO_ERROR, "applyVolumeShaper failed transact: %d", status);
1206108105064f935ccd1ff15485700cc8c3328e456Andy Hung        return; // one way transaction, ignore error
1216108105064f935ccd1ff15485700cc8c3328e456Andy Hung    }
12263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi};
12363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
12463189325cc429140b14885061eb67d9c771b96a4Jean-Michel TriviIMPLEMENT_META_INTERFACE(Player, "android.media.IPlayer");
12563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
12663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi// ----------------------------------------------------------------------
12763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
12863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivistatus_t BnPlayer::onTransact(
12963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
13063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi{
13163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    switch (code) {
13263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        case START: {
13363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
13463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            start();
13563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            return NO_ERROR;
13663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        } break;
13763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        case PAUSE: {
13863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
13963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            pause();
14063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            return NO_ERROR;
14163189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        }
14263189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        case STOP: {
14363189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
14463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            stop();
14563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            return NO_ERROR;
14663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        } break;
14763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        case SET_VOLUME: {
14863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
14963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            setVolume(data.readFloat());
15063189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            return NO_ERROR;
1511c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        } break;
1521c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        case SET_PAN: {
1531c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
1541c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            setPan(data.readFloat());
1551c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            return NO_ERROR;
1561c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        } break;
1571c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        case SET_START_DELAY_MS: {
1581c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            CHECK_INTERFACE(IPlayer, data, reply);
1591c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            setStartDelayMs(data.readInt32());
1601c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi            return NO_ERROR;
1611c76277338e832384aa734c55436b97837771a15Jean-Michel Trivi        } break;
1626108105064f935ccd1ff15485700cc8c3328e456Andy Hung        case APPLY_VOLUME_SHAPER: {
1636108105064f935ccd1ff15485700cc8c3328e456Andy Hung            CHECK_INTERFACE(IPlayer, data, reply);
1646108105064f935ccd1ff15485700cc8c3328e456Andy Hung            sp<VolumeShaper::Configuration> configuration;
1656108105064f935ccd1ff15485700cc8c3328e456Andy Hung            sp<VolumeShaper::Operation> operation;
1666108105064f935ccd1ff15485700cc8c3328e456Andy Hung
1676108105064f935ccd1ff15485700cc8c3328e456Andy Hung            int32_t present;
1686108105064f935ccd1ff15485700cc8c3328e456Andy Hung            status_t status = data.readInt32(&present);
1696108105064f935ccd1ff15485700cc8c3328e456Andy Hung            if (status == NO_ERROR && present != 0) {
1706108105064f935ccd1ff15485700cc8c3328e456Andy Hung                configuration = new VolumeShaper::Configuration();
1716108105064f935ccd1ff15485700cc8c3328e456Andy Hung                status = configuration->readFromParcel(data);
1726108105064f935ccd1ff15485700cc8c3328e456Andy Hung            }
1736108105064f935ccd1ff15485700cc8c3328e456Andy Hung            status = status ?: data.readInt32(&present);
1746108105064f935ccd1ff15485700cc8c3328e456Andy Hung            if (status == NO_ERROR && present != 0) {
1756108105064f935ccd1ff15485700cc8c3328e456Andy Hung                operation = new VolumeShaper::Operation();
1766108105064f935ccd1ff15485700cc8c3328e456Andy Hung                status = operation->readFromParcel(data);
1776108105064f935ccd1ff15485700cc8c3328e456Andy Hung            }
1786108105064f935ccd1ff15485700cc8c3328e456Andy Hung            if (status == NO_ERROR) {
1796108105064f935ccd1ff15485700cc8c3328e456Andy Hung                // one way transaction, no error returned
1806108105064f935ccd1ff15485700cc8c3328e456Andy Hung                applyVolumeShaper(configuration, operation);
1816108105064f935ccd1ff15485700cc8c3328e456Andy Hung            }
1826108105064f935ccd1ff15485700cc8c3328e456Andy Hung            return NO_ERROR;
1836108105064f935ccd1ff15485700cc8c3328e456Andy Hung        } break;
18463189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi        default:
18563189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi            return BBinder::onTransact(code, data, reply, flags);
18663189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi    }
18763189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi}
18863189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi
18963189325cc429140b14885061eb67d9c771b96a4Jean-Michel Trivi} // namespace android
190