IMediaPlayerService.cpp revision 7cf180c9bff69e5cc4a2f4e53b432db45ebbebab
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/IMediaPlayerService.h>
24#include <media/IMediaRecorder.h>
25#include <media/IOMX.h>
26#include <media/IStreamSource.h>
27
28#include <utils/Errors.h>  // for status_t
29
30namespace android {
31
32enum {
33    CREATE_URL = IBinder::FIRST_CALL_TRANSACTION,
34    CREATE_FD,
35    CREATE_STREAM,
36    DECODE_URL,
37    DECODE_FD,
38    CREATE_MEDIA_RECORDER,
39    CREATE_METADATA_RETRIEVER,
40    GET_OMX,
41    ADD_BATTERY_DATA,
42    PULL_BATTERY_DATA
43};
44
45class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
46{
47public:
48    BpMediaPlayerService(const sp<IBinder>& impl)
49        : BpInterface<IMediaPlayerService>(impl)
50    {
51    }
52
53    virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid)
54    {
55        Parcel data, reply;
56        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
57        data.writeInt32(pid);
58        remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
59        return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
60    }
61
62    virtual sp<IMediaPlayer> create(
63            pid_t pid, const sp<IMediaPlayerClient>& client,
64            const char* url, const KeyedVector<String8, String8> *headers, int audioSessionId) {
65        Parcel data, reply;
66        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
67        data.writeInt32(pid);
68        data.writeStrongBinder(client->asBinder());
69        data.writeCString(url);
70
71        if (headers == NULL) {
72            data.writeInt32(0);
73        } else {
74            // serialize the headers
75            data.writeInt32(headers->size());
76            for (size_t i = 0; i < headers->size(); ++i) {
77                data.writeString8(headers->keyAt(i));
78                data.writeString8(headers->valueAt(i));
79            }
80        }
81        data.writeInt32(audioSessionId);
82
83        remote()->transact(CREATE_URL, data, &reply);
84
85        return interface_cast<IMediaPlayer>(reply.readStrongBinder());
86    }
87
88    virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid)
89    {
90        Parcel data, reply;
91        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
92        data.writeInt32(pid);
93        remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
94        return interface_cast<IMediaRecorder>(reply.readStrongBinder());
95    }
96
97    virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd,
98            int64_t offset, int64_t length, int audioSessionId)
99    {
100        Parcel data, reply;
101        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
102        data.writeInt32(pid);
103        data.writeStrongBinder(client->asBinder());
104        data.writeFileDescriptor(fd);
105        data.writeInt64(offset);
106        data.writeInt64(length);
107        data.writeInt32(audioSessionId);
108
109        remote()->transact(CREATE_FD, data, &reply);
110
111        return interface_cast<IMediaPlayer>(reply.readStrongBinder());;
112    }
113
114    virtual sp<IMediaPlayer> create(
115            pid_t pid, const sp<IMediaPlayerClient> &client,
116            const sp<IStreamSource> &source, int audioSessionId) {
117        Parcel data, reply;
118        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
119        data.writeInt32(static_cast<int32_t>(pid));
120        data.writeStrongBinder(client->asBinder());
121        data.writeStrongBinder(source->asBinder());
122        data.writeInt32(static_cast<int32_t>(audioSessionId));
123
124        remote()->transact(CREATE_STREAM, data, &reply);
125
126        return interface_cast<IMediaPlayer>(reply.readStrongBinder());;
127    }
128
129    virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
130    {
131        Parcel data, reply;
132        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
133        data.writeCString(url);
134        remote()->transact(DECODE_URL, data, &reply);
135        *pSampleRate = uint32_t(reply.readInt32());
136        *pNumChannels = reply.readInt32();
137        *pFormat = reply.readInt32();
138        return interface_cast<IMemory>(reply.readStrongBinder());
139    }
140
141    virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
142    {
143        Parcel data, reply;
144        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
145        data.writeFileDescriptor(fd);
146        data.writeInt64(offset);
147        data.writeInt64(length);
148        remote()->transact(DECODE_FD, data, &reply);
149        *pSampleRate = uint32_t(reply.readInt32());
150        *pNumChannels = reply.readInt32();
151        *pFormat = reply.readInt32();
152        return interface_cast<IMemory>(reply.readStrongBinder());
153    }
154
155    virtual sp<IOMX> getOMX() {
156        Parcel data, reply;
157        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
158        remote()->transact(GET_OMX, data, &reply);
159        return interface_cast<IOMX>(reply.readStrongBinder());
160    }
161
162    virtual void addBatteryData(uint32_t params) {
163        Parcel data, reply;
164        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
165        data.writeInt32(params);
166        remote()->transact(ADD_BATTERY_DATA, data, &reply);
167    }
168
169    virtual status_t pullBatteryData(Parcel* reply) {
170        Parcel data;
171        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
172        return remote()->transact(PULL_BATTERY_DATA, data, reply);
173    }
174};
175
176IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
177
178// ----------------------------------------------------------------------
179
180status_t BnMediaPlayerService::onTransact(
181    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
182{
183    switch(code) {
184        case CREATE_URL: {
185            CHECK_INTERFACE(IMediaPlayerService, data, reply);
186            pid_t pid = data.readInt32();
187            sp<IMediaPlayerClient> client =
188                interface_cast<IMediaPlayerClient>(data.readStrongBinder());
189            const char* url = data.readCString();
190
191            KeyedVector<String8, String8> headers;
192            int32_t numHeaders = data.readInt32();
193            for (int i = 0; i < numHeaders; ++i) {
194                String8 key = data.readString8();
195                String8 value = data.readString8();
196                headers.add(key, value);
197            }
198            int audioSessionId = data.readInt32();
199
200            sp<IMediaPlayer> player = create(
201                    pid, client, url, numHeaders > 0 ? &headers : NULL, audioSessionId);
202
203            reply->writeStrongBinder(player->asBinder());
204            return NO_ERROR;
205        } break;
206        case CREATE_FD: {
207            CHECK_INTERFACE(IMediaPlayerService, data, reply);
208            pid_t pid = data.readInt32();
209            sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder());
210            int fd = dup(data.readFileDescriptor());
211            int64_t offset = data.readInt64();
212            int64_t length = data.readInt64();
213            int audioSessionId = data.readInt32();
214
215            sp<IMediaPlayer> player = create(pid, client, fd, offset, length, audioSessionId);
216            reply->writeStrongBinder(player->asBinder());
217            return NO_ERROR;
218        } break;
219        case CREATE_STREAM:
220        {
221            CHECK_INTERFACE(IMediaPlayerService, data, reply);
222
223            pid_t pid = static_cast<pid_t>(data.readInt32());
224
225            sp<IMediaPlayerClient> client =
226                interface_cast<IMediaPlayerClient>(data.readStrongBinder());
227
228            sp<IStreamSource> source =
229                interface_cast<IStreamSource>(data.readStrongBinder());
230
231            int audioSessionId = static_cast<int>(data.readInt32());
232
233            sp<IMediaPlayer> player =
234                create(pid, client, source, audioSessionId);
235
236            reply->writeStrongBinder(player->asBinder());
237            return OK;
238            break;
239        }
240        case DECODE_URL: {
241            CHECK_INTERFACE(IMediaPlayerService, data, reply);
242            const char* url = data.readCString();
243            uint32_t sampleRate;
244            int numChannels;
245            int format;
246            sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format);
247            reply->writeInt32(sampleRate);
248            reply->writeInt32(numChannels);
249            reply->writeInt32(format);
250            reply->writeStrongBinder(player->asBinder());
251            return NO_ERROR;
252        } break;
253        case DECODE_FD: {
254            CHECK_INTERFACE(IMediaPlayerService, data, reply);
255            int fd = dup(data.readFileDescriptor());
256            int64_t offset = data.readInt64();
257            int64_t length = data.readInt64();
258            uint32_t sampleRate;
259            int numChannels;
260            int format;
261            sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format);
262            reply->writeInt32(sampleRate);
263            reply->writeInt32(numChannels);
264            reply->writeInt32(format);
265            reply->writeStrongBinder(player->asBinder());
266            return NO_ERROR;
267        } break;
268        case CREATE_MEDIA_RECORDER: {
269            CHECK_INTERFACE(IMediaPlayerService, data, reply);
270            pid_t pid = data.readInt32();
271            sp<IMediaRecorder> recorder = createMediaRecorder(pid);
272            reply->writeStrongBinder(recorder->asBinder());
273            return NO_ERROR;
274        } break;
275        case CREATE_METADATA_RETRIEVER: {
276            CHECK_INTERFACE(IMediaPlayerService, data, reply);
277            pid_t pid = data.readInt32();
278            sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid);
279            reply->writeStrongBinder(retriever->asBinder());
280            return NO_ERROR;
281        } break;
282        case GET_OMX: {
283            CHECK_INTERFACE(IMediaPlayerService, data, reply);
284            sp<IOMX> omx = getOMX();
285            reply->writeStrongBinder(omx->asBinder());
286            return NO_ERROR;
287        } break;
288        case ADD_BATTERY_DATA: {
289            CHECK_INTERFACE(IMediaPlayerService, data, reply);
290            uint32_t params = data.readInt32();
291            addBatteryData(params);
292            return NO_ERROR;
293        } break;
294        case PULL_BATTERY_DATA: {
295            CHECK_INTERFACE(IMediaPlayerService, data, reply);
296            pullBatteryData(reply);
297            return NO_ERROR;
298        } break;
299        default:
300            return BBinder::onTransact(code, data, reply, flags);
301    }
302}
303
304// ----------------------------------------------------------------------------
305
306}; // namespace android
307