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/IMediaCodecList.h>
24 #include <media/IMediaHTTPService.h>
25 #include <media/IMediaPlayerService.h>
26 #include <media/IMediaRecorder.h>
27 #include <media/IOMX.h>
28 #include <media/IRemoteDisplay.h>
29 #include <media/IRemoteDisplayClient.h>
30 #include <media/IStreamSource.h>
31
32 #include <utils/Errors.h> // for status_t
33 #include <utils/String8.h>
34
35 namespace android {
36
37 enum {
38 CREATE = IBinder::FIRST_CALL_TRANSACTION,
39 CREATE_MEDIA_RECORDER,
40 CREATE_METADATA_RETRIEVER,
41 ADD_BATTERY_DATA,
42 PULL_BATTERY_DATA,
43 LISTEN_FOR_REMOTE_DISPLAY,
44 GET_CODEC_LIST,
45 };
46
47 class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
48 {
49 public:
BpMediaPlayerService(const sp<IBinder> & impl)50 explicit BpMediaPlayerService(const sp<IBinder>& impl)
51 : BpInterface<IMediaPlayerService>(impl)
52 {
53 }
54
createMetadataRetriever()55 virtual sp<IMediaMetadataRetriever> createMetadataRetriever()
56 {
57 Parcel data, reply;
58 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
59 remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
60 return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
61 }
62
create(const sp<IMediaPlayerClient> & client,audio_session_t audioSessionId)63 virtual sp<IMediaPlayer> create(
64 const sp<IMediaPlayerClient>& client, audio_session_t audioSessionId) {
65 Parcel data, reply;
66 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
67 data.writeStrongBinder(IInterface::asBinder(client));
68 data.writeInt32(audioSessionId);
69
70 remote()->transact(CREATE, data, &reply);
71 return interface_cast<IMediaPlayer>(reply.readStrongBinder());
72 }
73
createMediaRecorder(const String16 & opPackageName)74 virtual sp<IMediaRecorder> createMediaRecorder(const String16 &opPackageName)
75 {
76 Parcel data, reply;
77 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
78 data.writeString16(opPackageName);
79 remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
80 return interface_cast<IMediaRecorder>(reply.readStrongBinder());
81 }
82
addBatteryData(uint32_t params)83 virtual void addBatteryData(uint32_t params) {
84 Parcel data, reply;
85 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
86 data.writeInt32(params);
87 remote()->transact(ADD_BATTERY_DATA, data, &reply);
88 }
89
pullBatteryData(Parcel * reply)90 virtual status_t pullBatteryData(Parcel* reply) {
91 Parcel data;
92 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
93 return remote()->transact(PULL_BATTERY_DATA, data, reply);
94 }
95
listenForRemoteDisplay(const String16 & opPackageName,const sp<IRemoteDisplayClient> & client,const String8 & iface)96 virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
97 const sp<IRemoteDisplayClient>& client, const String8& iface)
98 {
99 Parcel data, reply;
100 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
101 data.writeString16(opPackageName);
102 data.writeStrongBinder(IInterface::asBinder(client));
103 data.writeString8(iface);
104 remote()->transact(LISTEN_FOR_REMOTE_DISPLAY, data, &reply);
105 return interface_cast<IRemoteDisplay>(reply.readStrongBinder());
106 }
107
getCodecList() const108 virtual sp<IMediaCodecList> getCodecList() const {
109 Parcel data, reply;
110 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
111 remote()->transact(GET_CODEC_LIST, data, &reply);
112 return interface_cast<IMediaCodecList>(reply.readStrongBinder());
113 }
114 };
115
116 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
117
118 // ----------------------------------------------------------------------
119
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)120 status_t BnMediaPlayerService::onTransact(
121 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
122 {
123 switch (code) {
124 case CREATE: {
125 CHECK_INTERFACE(IMediaPlayerService, data, reply);
126 sp<IMediaPlayerClient> client =
127 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
128 audio_session_t audioSessionId = (audio_session_t) data.readInt32();
129 sp<IMediaPlayer> player = create(client, audioSessionId);
130 reply->writeStrongBinder(IInterface::asBinder(player));
131 return NO_ERROR;
132 } break;
133 case CREATE_MEDIA_RECORDER: {
134 CHECK_INTERFACE(IMediaPlayerService, data, reply);
135 const String16 opPackageName = data.readString16();
136 sp<IMediaRecorder> recorder = createMediaRecorder(opPackageName);
137 reply->writeStrongBinder(IInterface::asBinder(recorder));
138 return NO_ERROR;
139 } break;
140 case CREATE_METADATA_RETRIEVER: {
141 CHECK_INTERFACE(IMediaPlayerService, data, reply);
142 sp<IMediaMetadataRetriever> retriever = createMetadataRetriever();
143 reply->writeStrongBinder(IInterface::asBinder(retriever));
144 return NO_ERROR;
145 } break;
146 case ADD_BATTERY_DATA: {
147 CHECK_INTERFACE(IMediaPlayerService, data, reply);
148 uint32_t params = data.readInt32();
149 addBatteryData(params);
150 return NO_ERROR;
151 } break;
152 case PULL_BATTERY_DATA: {
153 CHECK_INTERFACE(IMediaPlayerService, data, reply);
154 pullBatteryData(reply);
155 return NO_ERROR;
156 } break;
157 case LISTEN_FOR_REMOTE_DISPLAY: {
158 CHECK_INTERFACE(IMediaPlayerService, data, reply);
159 const String16 opPackageName = data.readString16();
160 sp<IRemoteDisplayClient> client(
161 interface_cast<IRemoteDisplayClient>(data.readStrongBinder()));
162 if (client == NULL) {
163 reply->writeStrongBinder(NULL);
164 return NO_ERROR;
165 }
166 String8 iface(data.readString8());
167 sp<IRemoteDisplay> display(listenForRemoteDisplay(opPackageName, client, iface));
168 reply->writeStrongBinder(IInterface::asBinder(display));
169 return NO_ERROR;
170 } break;
171 case GET_CODEC_LIST: {
172 CHECK_INTERFACE(IMediaPlayerService, data, reply);
173 sp<IMediaCodecList> mcl = getCodecList();
174 reply->writeStrongBinder(IInterface::asBinder(mcl));
175 return NO_ERROR;
176 } break;
177 default:
178 return BBinder::onTransact(code, data, reply, flags);
179 }
180 }
181
182 // ----------------------------------------------------------------------------
183
184 } // namespace android
185