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