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/IMediaPlayer.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 using android::content::AttributionSourceState;
39 
40 enum {
41     CREATE = IBinder::FIRST_CALL_TRANSACTION,
42     CREATE_MEDIA_RECORDER,
43     CREATE_METADATA_RETRIEVER,
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,const AttributionSourceState & attributionSource)66     virtual sp<IMediaPlayer> create(
67             const sp<IMediaPlayerClient>& client, audio_session_t audioSessionId,
68             const AttributionSourceState& attributionSource) {
69         Parcel data, reply;
70         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
71         data.writeStrongBinder(IInterface::asBinder(client));
72         data.writeInt32(audioSessionId);
73         data.writeParcelable(attributionSource);
74 
75         remote()->transact(CREATE, data, &reply);
76         return interface_cast<IMediaPlayer>(reply.readStrongBinder());
77     }
78 
createMediaRecorder(const AttributionSourceState & attributionSource)79     virtual sp<IMediaRecorder> createMediaRecorder(const AttributionSourceState& attributionSource)
80     {
81         Parcel data, reply;
82         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
83         data.writeParcelable(attributionSource);
84         remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
85         return interface_cast<IMediaRecorder>(reply.readStrongBinder());
86     }
87 
addBatteryData(uint32_t params)88     virtual void addBatteryData(uint32_t params) {
89         Parcel data, reply;
90         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
91         data.writeInt32(params);
92         remote()->transact(ADD_BATTERY_DATA, data, &reply);
93     }
94 
pullBatteryData(Parcel * reply)95     virtual status_t pullBatteryData(Parcel* reply) {
96         Parcel data;
97         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
98         return remote()->transact(PULL_BATTERY_DATA, data, reply);
99     }
100 
listenForRemoteDisplay(const String16 & opPackageName,const sp<IRemoteDisplayClient> & client,const String8 & iface)101     virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
102             const sp<IRemoteDisplayClient>& client, const String8& iface)
103     {
104         Parcel data, reply;
105         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
106         data.writeString16(opPackageName);
107         data.writeStrongBinder(IInterface::asBinder(client));
108         data.writeString8(iface);
109         remote()->transact(LISTEN_FOR_REMOTE_DISPLAY, data, &reply);
110         return interface_cast<IRemoteDisplay>(reply.readStrongBinder());
111     }
112 
getCodecList() const113     virtual sp<IMediaCodecList> getCodecList() const {
114         Parcel data, reply;
115         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
116         remote()->transact(GET_CODEC_LIST, data, &reply);
117         return interface_cast<IMediaCodecList>(reply.readStrongBinder());
118     }
119 };
120 
121 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
122 
123 // ----------------------------------------------------------------------
124 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)125 status_t BnMediaPlayerService::onTransact(
126     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
127 {
128     switch (code) {
129         case CREATE: {
130             CHECK_INTERFACE(IMediaPlayerService, data, reply);
131             sp<IMediaPlayerClient> client =
132                 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
133             audio_session_t audioSessionId = (audio_session_t) data.readInt32();
134             AttributionSourceState attributionSource;
135             status_t status = data.readParcelable(&attributionSource);
136             if (status != NO_ERROR) {
137                 return status;
138             }
139             sp<IMediaPlayer> player = create(client, audioSessionId, attributionSource);
140             reply->writeStrongBinder(IInterface::asBinder(player));
141             return NO_ERROR;
142         } break;
143         case CREATE_MEDIA_RECORDER: {
144             CHECK_INTERFACE(IMediaPlayerService, data, reply);
145             AttributionSourceState attributionSource;
146             status_t status = data.readParcelable(&attributionSource);
147             if (status != NO_ERROR) {
148                 return status;
149             }
150             sp<IMediaRecorder> recorder = createMediaRecorder(attributionSource);
151             reply->writeStrongBinder(IInterface::asBinder(recorder));
152             return NO_ERROR;
153         } break;
154         case CREATE_METADATA_RETRIEVER: {
155             CHECK_INTERFACE(IMediaPlayerService, data, reply);
156             sp<IMediaMetadataRetriever> retriever = createMetadataRetriever();
157             reply->writeStrongBinder(IInterface::asBinder(retriever));
158             return NO_ERROR;
159         } break;
160         case ADD_BATTERY_DATA: {
161             CHECK_INTERFACE(IMediaPlayerService, data, reply);
162             uint32_t params = data.readInt32();
163             addBatteryData(params);
164             return NO_ERROR;
165         } break;
166         case PULL_BATTERY_DATA: {
167             CHECK_INTERFACE(IMediaPlayerService, data, reply);
168             pullBatteryData(reply);
169             return NO_ERROR;
170         } break;
171         case LISTEN_FOR_REMOTE_DISPLAY: {
172             CHECK_INTERFACE(IMediaPlayerService, data, reply);
173             const String16 opPackageName = data.readString16();
174             sp<IRemoteDisplayClient> client(
175                     interface_cast<IRemoteDisplayClient>(data.readStrongBinder()));
176             if (client == NULL) {
177                 reply->writeStrongBinder(NULL);
178                 return NO_ERROR;
179             }
180             String8 iface(data.readString8());
181             sp<IRemoteDisplay> display(listenForRemoteDisplay(opPackageName, client, iface));
182             reply->writeStrongBinder(IInterface::asBinder(display));
183             return NO_ERROR;
184         } break;
185         case GET_CODEC_LIST: {
186             CHECK_INTERFACE(IMediaPlayerService, data, reply);
187             sp<IMediaCodecList> mcl = getCodecList();
188             reply->writeStrongBinder(IInterface::asBinder(mcl));
189             return NO_ERROR;
190         } break;
191         default:
192             return BBinder::onTransact(code, data, reply, flags);
193     }
194 }
195 
196 // ----------------------------------------------------------------------------
197 
198 } // namespace android
199