IMediaPlayerService.cpp revision 10dbb8e97e7a81ca4867663b5517f048820b3094
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
27#include <utils/Errors.h>  // for status_t
28
29namespace android {
30
31enum {
32    CREATE_URL = IBinder::FIRST_CALL_TRANSACTION,
33    CREATE_FD,
34    DECODE_URL,
35    DECODE_FD,
36    CREATE_MEDIA_RECORDER,
37    CREATE_METADATA_RETRIEVER,
38    CREATE_OMX,
39    SNOOP
40};
41
42class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
43{
44public:
45    BpMediaPlayerService(const sp<IBinder>& impl)
46        : BpInterface<IMediaPlayerService>(impl)
47    {
48    }
49
50    virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid)
51    {
52        Parcel data, reply;
53        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
54        data.writeInt32(pid);
55        remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
56        return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
57    }
58
59    virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url)
60    {
61        Parcel data, reply;
62        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
63        data.writeInt32(pid);
64        data.writeStrongBinder(client->asBinder());
65        data.writeCString(url);
66        remote()->transact(CREATE_URL, data, &reply);
67        return interface_cast<IMediaPlayer>(reply.readStrongBinder());
68    }
69
70    virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid)
71    {
72        Parcel data, reply;
73        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
74        data.writeInt32(pid);
75        remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
76        return interface_cast<IMediaRecorder>(reply.readStrongBinder());
77    }
78
79    virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length)
80    {
81        Parcel data, reply;
82        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
83        data.writeInt32(pid);
84        data.writeStrongBinder(client->asBinder());
85        data.writeFileDescriptor(fd);
86        data.writeInt64(offset);
87        data.writeInt64(length);
88        remote()->transact(CREATE_FD, data, &reply);
89        return interface_cast<IMediaPlayer>(reply.readStrongBinder());
90    }
91
92    virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
93    {
94        Parcel data, reply;
95        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
96        data.writeCString(url);
97        remote()->transact(DECODE_URL, data, &reply);
98        *pSampleRate = uint32_t(reply.readInt32());
99        *pNumChannels = reply.readInt32();
100        *pFormat = reply.readInt32();
101        return interface_cast<IMemory>(reply.readStrongBinder());
102    }
103
104    virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
105    {
106        Parcel data, reply;
107        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
108        data.writeFileDescriptor(fd);
109        data.writeInt64(offset);
110        data.writeInt64(length);
111        remote()->transact(DECODE_FD, data, &reply);
112        *pSampleRate = uint32_t(reply.readInt32());
113        *pNumChannels = reply.readInt32();
114        *pFormat = reply.readInt32();
115        return interface_cast<IMemory>(reply.readStrongBinder());
116    }
117
118    virtual sp<IMemory> snoop()
119    {
120        Parcel data, reply;
121        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
122        remote()->transact(SNOOP, data, &reply);
123        return interface_cast<IMemory>(reply.readStrongBinder());
124    }
125
126    virtual sp<IOMX> createOMX() {
127        Parcel data, reply;
128        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
129        remote()->transact(CREATE_OMX, data, &reply);
130        return interface_cast<IOMX>(reply.readStrongBinder());
131    }
132};
133
134IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
135
136// ----------------------------------------------------------------------
137
138status_t BnMediaPlayerService::onTransact(
139    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
140{
141    switch(code) {
142        case CREATE_URL: {
143            CHECK_INTERFACE(IMediaPlayerService, data, reply);
144            pid_t pid = data.readInt32();
145            sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder());
146            const char* url = data.readCString();
147            sp<IMediaPlayer> player = create(pid, client, url);
148            reply->writeStrongBinder(player->asBinder());
149            return NO_ERROR;
150        } break;
151        case CREATE_FD: {
152            CHECK_INTERFACE(IMediaPlayerService, data, reply);
153            pid_t pid = data.readInt32();
154            sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder());
155            int fd = dup(data.readFileDescriptor());
156            int64_t offset = data.readInt64();
157            int64_t length = data.readInt64();
158            sp<IMediaPlayer> player = create(pid, client, fd, offset, length);
159            reply->writeStrongBinder(player->asBinder());
160            return NO_ERROR;
161        } break;
162        case DECODE_URL: {
163            CHECK_INTERFACE(IMediaPlayerService, data, reply);
164            const char* url = data.readCString();
165            uint32_t sampleRate;
166            int numChannels;
167            int format;
168            sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format);
169            reply->writeInt32(sampleRate);
170            reply->writeInt32(numChannels);
171            reply->writeInt32(format);
172            reply->writeStrongBinder(player->asBinder());
173            return NO_ERROR;
174        } break;
175        case DECODE_FD: {
176            CHECK_INTERFACE(IMediaPlayerService, data, reply);
177            int fd = dup(data.readFileDescriptor());
178            int64_t offset = data.readInt64();
179            int64_t length = data.readInt64();
180            uint32_t sampleRate;
181            int numChannels;
182            int format;
183            sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format);
184            reply->writeInt32(sampleRate);
185            reply->writeInt32(numChannels);
186            reply->writeInt32(format);
187            reply->writeStrongBinder(player->asBinder());
188            return NO_ERROR;
189        } break;
190        case SNOOP: {
191            CHECK_INTERFACE(IMediaPlayerService, data, reply);
192            sp<IMemory> snooped_audio = snoop();
193            reply->writeStrongBinder(snooped_audio->asBinder());
194            return NO_ERROR;
195        } break;
196        case CREATE_MEDIA_RECORDER: {
197            CHECK_INTERFACE(IMediaPlayerService, data, reply);
198            pid_t pid = data.readInt32();
199            sp<IMediaRecorder> recorder = createMediaRecorder(pid);
200            reply->writeStrongBinder(recorder->asBinder());
201            return NO_ERROR;
202        } break;
203        case CREATE_METADATA_RETRIEVER: {
204            CHECK_INTERFACE(IMediaPlayerService, data, reply);
205            pid_t pid = data.readInt32();
206            sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid);
207            reply->writeStrongBinder(retriever->asBinder());
208            return NO_ERROR;
209        } break;
210        case CREATE_OMX: {
211            CHECK_INTERFACE(IMediaPlayerService, data, reply);
212            sp<IOMX> omx = createOMX();
213            reply->writeStrongBinder(omx->asBinder());
214            return NO_ERROR;
215        } break;
216        default:
217            return BBinder::onTransact(code, data, reply, flags);
218    }
219}
220
221// ----------------------------------------------------------------------------
222
223}; // namespace android
224