1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <stdint.h>
19 #include <sys/types.h>
20
21 #include <binder/Parcel.h>
22 #include <binder/IMemory.h>
23 #include <media/IHDCP.h>
24 #include <media/IMediaCodecList.h>
25 #include <media/IMediaHTTPService.h>
26 #include <media/IMediaPlayerService.h>
27 #include <media/IMediaRecorder.h>
28 #include <media/IOMX.h>
29 #include <media/IRemoteDisplay.h>
30 #include <media/IRemoteDisplayClient.h>
31 #include <media/IStreamSource.h>
32
33 #include <utils/Errors.h> // for status_t
34 #include <utils/String8.h>
35
36 namespace android {
37
38 enum {
39 CREATE = IBinder::FIRST_CALL_TRANSACTION,
40 CREATE_MEDIA_RECORDER,
41 CREATE_METADATA_RETRIEVER,
42 GET_OMX,
43 MAKE_HDCP,
44 ADD_BATTERY_DATA,
45 PULL_BATTERY_DATA,
46 LISTEN_FOR_REMOTE_DISPLAY,
47 GET_CODEC_LIST,
48 };
49
50 class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
51 {
52 public:
BpMediaPlayerService(const sp<IBinder> & impl)53 explicit BpMediaPlayerService(const sp<IBinder>& impl)
54 : BpInterface<IMediaPlayerService>(impl)
55 {
56 }
57
createMetadataRetriever()58 virtual sp<IMediaMetadataRetriever> createMetadataRetriever()
59 {
60 Parcel data, reply;
61 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
62 remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
63 return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
64 }
65
create(const sp<IMediaPlayerClient> & client,audio_session_t audioSessionId)66 virtual sp<IMediaPlayer> create(
67 const sp<IMediaPlayerClient>& client, audio_session_t audioSessionId) {
68 Parcel data, reply;
69 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
70 data.writeStrongBinder(IInterface::asBinder(client));
71 data.writeInt32(audioSessionId);
72
73 remote()->transact(CREATE, data, &reply);
74 return interface_cast<IMediaPlayer>(reply.readStrongBinder());
75 }
76
createMediaRecorder(const String16 & opPackageName)77 virtual sp<IMediaRecorder> createMediaRecorder(const String16 &opPackageName)
78 {
79 Parcel data, reply;
80 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
81 data.writeString16(opPackageName);
82 remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
83 return interface_cast<IMediaRecorder>(reply.readStrongBinder());
84 }
85
getOMX()86 virtual sp<IOMX> getOMX() {
87 Parcel data, reply;
88 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
89 remote()->transact(GET_OMX, data, &reply);
90 return interface_cast<IOMX>(reply.readStrongBinder());
91 }
92
makeHDCP(bool createEncryptionModule)93 virtual sp<IHDCP> makeHDCP(bool createEncryptionModule) {
94 Parcel data, reply;
95 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
96 data.writeInt32(createEncryptionModule);
97 remote()->transact(MAKE_HDCP, data, &reply);
98 return interface_cast<IHDCP>(reply.readStrongBinder());
99 }
100
addBatteryData(uint32_t params)101 virtual void addBatteryData(uint32_t params) {
102 Parcel data, reply;
103 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
104 data.writeInt32(params);
105 remote()->transact(ADD_BATTERY_DATA, data, &reply);
106 }
107
pullBatteryData(Parcel * reply)108 virtual status_t pullBatteryData(Parcel* reply) {
109 Parcel data;
110 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
111 return remote()->transact(PULL_BATTERY_DATA, data, reply);
112 }
113
listenForRemoteDisplay(const String16 & opPackageName,const sp<IRemoteDisplayClient> & client,const String8 & iface)114 virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
115 const sp<IRemoteDisplayClient>& client, const String8& iface)
116 {
117 Parcel data, reply;
118 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
119 data.writeString16(opPackageName);
120 data.writeStrongBinder(IInterface::asBinder(client));
121 data.writeString8(iface);
122 remote()->transact(LISTEN_FOR_REMOTE_DISPLAY, data, &reply);
123 return interface_cast<IRemoteDisplay>(reply.readStrongBinder());
124 }
125
getCodecList() const126 virtual sp<IMediaCodecList> getCodecList() const {
127 Parcel data, reply;
128 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
129 remote()->transact(GET_CODEC_LIST, data, &reply);
130 return interface_cast<IMediaCodecList>(reply.readStrongBinder());
131 }
132 };
133
134 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
135
136 // ----------------------------------------------------------------------
137
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)138 status_t BnMediaPlayerService::onTransact(
139 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
140 {
141 switch (code) {
142 case CREATE: {
143 CHECK_INTERFACE(IMediaPlayerService, data, reply);
144 sp<IMediaPlayerClient> client =
145 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
146 audio_session_t audioSessionId = (audio_session_t) data.readInt32();
147 sp<IMediaPlayer> player = create(client, audioSessionId);
148 reply->writeStrongBinder(IInterface::asBinder(player));
149 return NO_ERROR;
150 } break;
151 case CREATE_MEDIA_RECORDER: {
152 CHECK_INTERFACE(IMediaPlayerService, data, reply);
153 const String16 opPackageName = data.readString16();
154 sp<IMediaRecorder> recorder = createMediaRecorder(opPackageName);
155 reply->writeStrongBinder(IInterface::asBinder(recorder));
156 return NO_ERROR;
157 } break;
158 case CREATE_METADATA_RETRIEVER: {
159 CHECK_INTERFACE(IMediaPlayerService, data, reply);
160 sp<IMediaMetadataRetriever> retriever = createMetadataRetriever();
161 reply->writeStrongBinder(IInterface::asBinder(retriever));
162 return NO_ERROR;
163 } break;
164 case GET_OMX: {
165 CHECK_INTERFACE(IMediaPlayerService, data, reply);
166 sp<IOMX> omx = getOMX();
167 reply->writeStrongBinder(IInterface::asBinder(omx));
168 return NO_ERROR;
169 } break;
170 case MAKE_HDCP: {
171 CHECK_INTERFACE(IMediaPlayerService, data, reply);
172 bool createEncryptionModule = data.readInt32();
173 sp<IHDCP> hdcp = makeHDCP(createEncryptionModule);
174 reply->writeStrongBinder(IInterface::asBinder(hdcp));
175 return NO_ERROR;
176 } break;
177 case ADD_BATTERY_DATA: {
178 CHECK_INTERFACE(IMediaPlayerService, data, reply);
179 uint32_t params = data.readInt32();
180 addBatteryData(params);
181 return NO_ERROR;
182 } break;
183 case PULL_BATTERY_DATA: {
184 CHECK_INTERFACE(IMediaPlayerService, data, reply);
185 pullBatteryData(reply);
186 return NO_ERROR;
187 } break;
188 case LISTEN_FOR_REMOTE_DISPLAY: {
189 CHECK_INTERFACE(IMediaPlayerService, data, reply);
190 const String16 opPackageName = data.readString16();
191 sp<IRemoteDisplayClient> client(
192 interface_cast<IRemoteDisplayClient>(data.readStrongBinder()));
193 if (client == NULL) {
194 reply->writeStrongBinder(NULL);
195 return NO_ERROR;
196 }
197 String8 iface(data.readString8());
198 sp<IRemoteDisplay> display(listenForRemoteDisplay(opPackageName, client, iface));
199 reply->writeStrongBinder(IInterface::asBinder(display));
200 return NO_ERROR;
201 } break;
202 case GET_CODEC_LIST: {
203 CHECK_INTERFACE(IMediaPlayerService, data, reply);
204 sp<IMediaCodecList> mcl = getCodecList();
205 reply->writeStrongBinder(IInterface::asBinder(mcl));
206 return NO_ERROR;
207 } break;
208 default:
209 return BBinder::onTransact(code, data, reply, flags);
210 }
211 }
212
213 // ----------------------------------------------------------------------------
214
215 } // namespace android
216