1 /*
2  * Copyright (C) 2016 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 #define LOG_TAG "IAudioManager"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <binder/Parcel.h>
25 #include <audiomanager/AudioManager.h>
26 #include <audiomanager/IAudioManager.h>
27 
28 namespace android {
29 
30 class BpAudioManager : public BpInterface<IAudioManager>
31 {
32 public:
BpAudioManager(const sp<IBinder> & impl)33     explicit BpAudioManager(const sp<IBinder>& impl)
34         : BpInterface<IAudioManager>(impl)
35     {
36     }
37 
trackPlayer(player_type_t playerType,audio_usage_t usage,audio_content_type_t content,const sp<IBinder> & player,audio_session_t sessionId)38     virtual audio_unique_id_t trackPlayer(player_type_t playerType, audio_usage_t usage,
39             audio_content_type_t content, const sp<IBinder>& player, audio_session_t sessionId) {
40         Parcel data, reply;
41         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
42         data.writeInt32(1); // non-null PlayerIdCard parcelable
43         // marshall PlayerIdCard data
44         data.writeInt32((int32_t) playerType);
45         //   write attributes of PlayerIdCard
46         data.writeInt32((int32_t) usage);
47         data.writeInt32((int32_t) content);
48         data.writeInt32(0 /*source: none here, this is a player*/);
49         data.writeInt32(0 /*flags*/);
50         //   write attributes' tags
51         data.writeInt32(1 /*FLATTEN_TAGS*/);
52         data.writeString16(String16("")); // no tags
53         //   write attributes' bundle
54         data.writeInt32(-1977 /*ATTR_PARCEL_IS_NULL_BUNDLE*/); // no bundle
55         //   write IPlayer
56         data.writeStrongBinder(player);
57         //   write session Id
58         data.writeInt32((int32_t)sessionId);
59         // get new PIId in reply
60         const status_t res = remote()->transact(TRACK_PLAYER, data, &reply, 0);
61         if (res != OK || reply.readExceptionCode() != 0) {
62             ALOGE("trackPlayer() failed, piid is %d", PLAYER_PIID_INVALID);
63             return PLAYER_PIID_INVALID;
64         } else {
65             const audio_unique_id_t piid = (audio_unique_id_t) reply.readInt32();
66             ALOGV("trackPlayer() returned piid %d", piid);
67             return piid;
68         }
69     }
70 
playerAttributes(audio_unique_id_t piid,audio_usage_t usage,audio_content_type_t content)71     virtual status_t playerAttributes(audio_unique_id_t piid, audio_usage_t usage,
72             audio_content_type_t content) {
73         Parcel data, reply;
74         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
75         data.writeInt32((int32_t) piid);
76         data.writeInt32(1); // non-null AudioAttributes parcelable
77         data.writeInt32((int32_t) usage);
78         data.writeInt32((int32_t) content);
79         data.writeInt32(0 /*source: none here, this is a player*/);
80         data.writeInt32(0 /*flags*/);
81         //   write attributes' tags
82         data.writeInt32(1 /*FLATTEN_TAGS*/);
83         data.writeString16(String16("")); // no tags
84         //   write attributes' bundle
85         data.writeInt32(-1977 /*ATTR_PARCEL_IS_NULL_BUNDLE*/); // no bundle
86         return remote()->transact(PLAYER_ATTRIBUTES, data, &reply, IBinder::FLAG_ONEWAY);
87     }
88 
playerEvent(audio_unique_id_t piid,player_state_t event,audio_port_handle_t eventId)89     virtual status_t playerEvent(audio_unique_id_t piid, player_state_t event,
90             audio_port_handle_t eventId) {
91         Parcel data, reply;
92         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
93         data.writeInt32((int32_t) piid);
94         data.writeInt32((int32_t) event);
95         data.writeInt32((int32_t) eventId);
96         return remote()->transact(PLAYER_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
97     }
98 
releasePlayer(audio_unique_id_t piid)99     virtual status_t releasePlayer(audio_unique_id_t piid) {
100         Parcel data, reply;
101         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
102         data.writeInt32((int32_t) piid);
103         return remote()->transact(RELEASE_PLAYER, data, &reply, IBinder::FLAG_ONEWAY);
104     }
105 
trackRecorder(const sp<IBinder> & recorder)106     virtual audio_unique_id_t trackRecorder(const sp<IBinder>& recorder) {
107         Parcel data, reply;
108         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
109         data.writeStrongBinder(recorder);
110         // get new RIId in reply
111         const status_t res = remote()->transact(TRACK_RECORDER, data, &reply, 0);
112         if (res != OK || reply.readExceptionCode() != 0) {
113             ALOGE("trackRecorder() failed, riid is %d", RECORD_RIID_INVALID);
114             return RECORD_RIID_INVALID;
115         } else {
116             const audio_unique_id_t riid = (audio_unique_id_t) reply.readInt32();
117             ALOGV("trackRecorder() returned riid %d", riid);
118             return riid;
119         }
120     }
121 
recorderEvent(audio_unique_id_t riid,recorder_state_t event)122     virtual status_t recorderEvent(audio_unique_id_t riid, recorder_state_t event) {
123         Parcel data, reply;
124         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
125         data.writeInt32((int32_t) riid);
126         data.writeInt32((int32_t) event);
127         return remote()->transact(RECORDER_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
128     }
129 
releaseRecorder(audio_unique_id_t riid)130     virtual status_t releaseRecorder(audio_unique_id_t riid) {
131         Parcel data, reply;
132         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
133         data.writeInt32((int32_t) riid);
134         return remote()->transact(RELEASE_RECORDER, data, &reply, IBinder::FLAG_ONEWAY);
135     }
136 
playerSessionId(audio_unique_id_t piid,audio_session_t sessionId)137     virtual status_t playerSessionId(audio_unique_id_t piid, audio_session_t sessionId) {
138         Parcel data, reply;
139         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
140         data.writeInt32((int32_t) piid);
141         data.writeInt32((int32_t) sessionId);
142         return remote()->transact(PLAYER_SESSION_ID, data, &reply, IBinder::FLAG_ONEWAY);
143     }
144 
portEvent(audio_port_handle_t portId,player_state_t event,const std::unique_ptr<os::PersistableBundle> & extras)145     virtual status_t portEvent(audio_port_handle_t portId, player_state_t event,
146             const std::unique_ptr<os::PersistableBundle>& extras) {
147         Parcel data, reply;
148         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
149         data.writeInt32((int32_t) portId);
150         data.writeInt32((int32_t) event);
151         // TODO: replace PersistableBundle with own struct
152         data.writeNullableParcelable(extras);
153         return remote()->transact(PORT_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
154     }
155 };
156 
157 IMPLEMENT_META_INTERFACE(AudioManager, "android.media.IAudioService");
158 
159 // ----------------------------------------------------------------------------
160 
161 }; // namespace android
162