• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
getNativeInterface()38     virtual sp<media::IAudioManagerNative> getNativeInterface() {
39         Parcel data, reply;
40         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
41         const status_t res = remote()->transact(GET_NATIVE_INTERFACE, data, &reply, 0);
42         if (res == DEAD_OBJECT) return nullptr;
43         LOG_ALWAYS_FATAL_IF(res != OK, "%s failed with result %d", __func__, res);
44         const int ex = reply.readExceptionCode();
45         LOG_ALWAYS_FATAL_IF(ex != binder::Status::EX_NONE, "%s failed with exception %d",
46                             __func__,
47                             ex);
48         sp<IBinder> binder;
49         const status_t err = reply.readNullableStrongBinder(&binder);
50         LOG_ALWAYS_FATAL_IF(binder == nullptr, "%s failed unexpected nullptr %d", __func__, err);
51         const auto iface = checked_interface_cast<media::IAudioManagerNative>(binder);
52         LOG_ALWAYS_FATAL_IF(iface == nullptr, "%s failed unexpected interface", __func__);
53         return iface;
54     }
55 
trackPlayer(player_type_t playerType,audio_usage_t usage,audio_content_type_t content,const sp<IBinder> & player,audio_session_t sessionId)56     virtual audio_unique_id_t trackPlayer(player_type_t playerType, audio_usage_t usage,
57             audio_content_type_t content, const sp<IBinder>& player, audio_session_t sessionId) {
58         Parcel data, reply;
59         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
60         data.writeInt32(1); // non-null PlayerIdCard parcelable
61         // marshall PlayerIdCard data
62         data.writeInt32((int32_t) playerType);
63         //   write attributes of PlayerIdCard
64         data.writeInt32((int32_t) usage);
65         data.writeInt32((int32_t) content);
66         data.writeInt32(0 /*source: none here, this is a player*/);
67         data.writeInt32(0 /*flags*/);
68         //   write attributes' tags
69         data.writeInt32(1 /*FLATTEN_TAGS*/);
70         data.writeString16(String16("")); // no tags
71         //   write attributes' bundle
72         data.writeInt32(-1977 /*ATTR_PARCEL_IS_NULL_BUNDLE*/); // no bundle
73         //   write IPlayer
74         data.writeStrongBinder(player);
75         //   write session Id
76         data.writeInt32((int32_t)sessionId);
77         // get new PIId in reply
78         const status_t res = remote()->transact(TRACK_PLAYER, data, &reply, 0);
79         if (res != OK || reply.readExceptionCode() != 0) {
80             ALOGE("trackPlayer() failed, piid is %d", PLAYER_PIID_INVALID);
81             return PLAYER_PIID_INVALID;
82         } else {
83             const audio_unique_id_t piid = (audio_unique_id_t) reply.readInt32();
84             ALOGV("trackPlayer() returned piid %d", piid);
85             return piid;
86         }
87     }
88 
playerAttributes(audio_unique_id_t piid,audio_usage_t usage,audio_content_type_t content)89     virtual status_t playerAttributes(audio_unique_id_t piid, audio_usage_t usage,
90             audio_content_type_t content) {
91         Parcel data, reply;
92         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
93         data.writeInt32((int32_t) piid);
94         data.writeInt32(1); // non-null AudioAttributes parcelable
95         data.writeInt32((int32_t) usage);
96         data.writeInt32((int32_t) content);
97         data.writeInt32(0 /*source: none here, this is a player*/);
98         data.writeInt32(0 /*flags*/);
99         //   write attributes' tags
100         data.writeInt32(1 /*FLATTEN_TAGS*/);
101         data.writeString16(String16("")); // no tags
102         //   write attributes' bundle
103         data.writeInt32(-1977 /*ATTR_PARCEL_IS_NULL_BUNDLE*/); // no bundle
104         return remote()->transact(PLAYER_ATTRIBUTES, data, &reply, IBinder::FLAG_ONEWAY);
105     }
106 
playerEvent(audio_unique_id_t piid,player_state_t event,const std::vector<audio_port_handle_t> & eventIds)107     virtual status_t playerEvent(audio_unique_id_t piid, player_state_t event,
108             const std::vector<audio_port_handle_t>& eventIds) {
109         Parcel data, reply;
110         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
111         data.writeInt32((int32_t) piid);
112         data.writeInt32((int32_t) event);
113         data.writeInt32((int32_t) eventIds.size());
114         for (auto eventId: eventIds) {
115             data.writeInt32((int32_t) eventId);
116         }
117         return remote()->transact(PLAYER_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
118     }
119 
releasePlayer(audio_unique_id_t piid)120     virtual status_t releasePlayer(audio_unique_id_t piid) {
121         Parcel data, reply;
122         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
123         data.writeInt32((int32_t) piid);
124         return remote()->transact(RELEASE_PLAYER, data, &reply, IBinder::FLAG_ONEWAY);
125     }
126 
trackRecorder(const sp<IBinder> & recorder)127     virtual audio_unique_id_t trackRecorder(const sp<IBinder>& recorder) {
128         Parcel data, reply;
129         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
130         data.writeStrongBinder(recorder);
131         // get new RIId in reply
132         const status_t res = remote()->transact(TRACK_RECORDER, data, &reply, 0);
133         if (res != OK || reply.readExceptionCode() != 0) {
134             ALOGE("trackRecorder() failed, riid is %d", RECORD_RIID_INVALID);
135             return RECORD_RIID_INVALID;
136         } else {
137             const audio_unique_id_t riid = (audio_unique_id_t) reply.readInt32();
138             ALOGV("trackRecorder() returned riid %d", riid);
139             return riid;
140         }
141     }
142 
recorderEvent(audio_unique_id_t riid,recorder_state_t event)143     virtual status_t recorderEvent(audio_unique_id_t riid, recorder_state_t event) {
144         Parcel data, reply;
145         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
146         data.writeInt32((int32_t) riid);
147         data.writeInt32((int32_t) event);
148         return remote()->transact(RECORDER_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
149     }
150 
releaseRecorder(audio_unique_id_t riid)151     virtual status_t releaseRecorder(audio_unique_id_t riid) {
152         Parcel data, reply;
153         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
154         data.writeInt32((int32_t) riid);
155         return remote()->transact(RELEASE_RECORDER, data, &reply, IBinder::FLAG_ONEWAY);
156     }
157 
playerSessionId(audio_unique_id_t piid,audio_session_t sessionId)158     virtual status_t playerSessionId(audio_unique_id_t piid, audio_session_t sessionId) {
159         Parcel data, reply;
160         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
161         data.writeInt32((int32_t) piid);
162         data.writeInt32((int32_t) sessionId);
163         return remote()->transact(PLAYER_SESSION_ID, data, &reply, IBinder::FLAG_ONEWAY);
164     }
165 
portEvent(audio_port_handle_t portId,player_state_t event,const std::unique_ptr<os::PersistableBundle> & extras)166     virtual status_t portEvent(audio_port_handle_t portId, player_state_t event,
167             const std::unique_ptr<os::PersistableBundle>& extras) {
168         Parcel data, reply;
169         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
170         data.writeInt32((int32_t) portId);
171         data.writeInt32((int32_t) event);
172         // TODO: replace PersistableBundle with own struct
173         data.writeNullableParcelable(extras);
174         return remote()->transact(PORT_EVENT, data, &reply, IBinder::FLAG_ONEWAY);
175     }
176 
permissionUpdateBarrier()177     virtual status_t permissionUpdateBarrier() {
178         Parcel data, reply;
179         data.writeInterfaceToken(IAudioManager::getInterfaceDescriptor());
180         return remote()->transact(PERMISSION_UPDATE_BARRIER, data, &reply, 0);
181     }
182 };
183 
184 IMPLEMENT_META_INTERFACE(AudioManager, "android.media.IAudioService");
185 
186 // ----------------------------------------------------------------------------
187 
188 }; // namespace android
189