blob: 0f64259012f0bcf9ffadc50a2f26d624e1eb5845 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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>
Mathias Agopian75624082009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021
Mathias Agopian75624082009-05-19 19:08:10 -070022#include <binder/IMemory.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070023#include <utils/Errors.h> // for status_t
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080024#include <media/IMediaPlayerService.h>
25#include <media/IMediaRecorder.h>
26
27namespace android {
28
29enum {
30 CREATE_URL = IBinder::FIRST_CALL_TRANSACTION,
31 CREATE_FD,
32 DECODE_URL,
33 DECODE_FD,
34 CREATE_MEDIA_RECORDER,
35 CREATE_METADATA_RETRIEVER,
36};
37
38class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
39{
40public:
41 BpMediaPlayerService(const sp<IBinder>& impl)
42 : BpInterface<IMediaPlayerService>(impl)
43 {
44 }
45
46 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid)
47 {
48 Parcel data, reply;
49 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
50 data.writeInt32(pid);
51 remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
52 return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
53 }
54
55 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url)
56 {
57 Parcel data, reply;
58 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
59 data.writeInt32(pid);
60 data.writeStrongBinder(client->asBinder());
61 data.writeCString(url);
62 remote()->transact(CREATE_URL, data, &reply);
63 return interface_cast<IMediaPlayer>(reply.readStrongBinder());
64 }
65
66 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid)
67 {
68 Parcel data, reply;
69 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
70 data.writeInt32(pid);
71 remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
72 return interface_cast<IMediaRecorder>(reply.readStrongBinder());
73 }
74
75 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length)
76 {
77 Parcel data, reply;
78 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
79 data.writeInt32(pid);
80 data.writeStrongBinder(client->asBinder());
81 data.writeFileDescriptor(fd);
82 data.writeInt64(offset);
83 data.writeInt64(length);
84 remote()->transact(CREATE_FD, data, &reply);
85 return interface_cast<IMediaPlayer>(reply.readStrongBinder());
86 }
87
88 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
89 {
90 Parcel data, reply;
91 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
92 data.writeCString(url);
93 remote()->transact(DECODE_URL, data, &reply);
94 *pSampleRate = uint32_t(reply.readInt32());
95 *pNumChannels = reply.readInt32();
96 *pFormat = reply.readInt32();
97 return interface_cast<IMemory>(reply.readStrongBinder());
98 }
99
100 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
101 {
102 Parcel data, reply;
103 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
104 data.writeFileDescriptor(fd);
105 data.writeInt64(offset);
106 data.writeInt64(length);
107 remote()->transact(DECODE_FD, data, &reply);
108 *pSampleRate = uint32_t(reply.readInt32());
109 *pNumChannels = reply.readInt32();
110 *pFormat = reply.readInt32();
111 return interface_cast<IMemory>(reply.readStrongBinder());
112 }
113};
114
niko56f0cc52009-06-22 08:49:52 -0700115IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116
117// ----------------------------------------------------------------------
118
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119status_t BnMediaPlayerService::onTransact(
120 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
121{
122 switch(code) {
123 case CREATE_URL: {
124 CHECK_INTERFACE(IMediaPlayerService, data, reply);
125 pid_t pid = data.readInt32();
126 sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder());
127 const char* url = data.readCString();
128 sp<IMediaPlayer> player = create(pid, client, url);
129 reply->writeStrongBinder(player->asBinder());
130 return NO_ERROR;
131 } break;
132 case CREATE_FD: {
133 CHECK_INTERFACE(IMediaPlayerService, data, reply);
134 pid_t pid = data.readInt32();
135 sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder());
136 int fd = dup(data.readFileDescriptor());
137 int64_t offset = data.readInt64();
138 int64_t length = data.readInt64();
139 sp<IMediaPlayer> player = create(pid, client, fd, offset, length);
140 reply->writeStrongBinder(player->asBinder());
141 return NO_ERROR;
142 } break;
143 case DECODE_URL: {
144 CHECK_INTERFACE(IMediaPlayerService, data, reply);
145 const char* url = data.readCString();
146 uint32_t sampleRate;
147 int numChannels;
148 int format;
149 sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format);
150 reply->writeInt32(sampleRate);
151 reply->writeInt32(numChannels);
152 reply->writeInt32(format);
153 reply->writeStrongBinder(player->asBinder());
154 return NO_ERROR;
155 } break;
156 case DECODE_FD: {
157 CHECK_INTERFACE(IMediaPlayerService, data, reply);
158 int fd = dup(data.readFileDescriptor());
159 int64_t offset = data.readInt64();
160 int64_t length = data.readInt64();
161 uint32_t sampleRate;
162 int numChannels;
163 int format;
164 sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format);
165 reply->writeInt32(sampleRate);
166 reply->writeInt32(numChannels);
167 reply->writeInt32(format);
168 reply->writeStrongBinder(player->asBinder());
169 return NO_ERROR;
170 } break;
171 case CREATE_MEDIA_RECORDER: {
172 CHECK_INTERFACE(IMediaPlayerService, data, reply);
173 pid_t pid = data.readInt32();
174 sp<IMediaRecorder> recorder = createMediaRecorder(pid);
175 reply->writeStrongBinder(recorder->asBinder());
176 return NO_ERROR;
177 } break;
178 case CREATE_METADATA_RETRIEVER: {
179 CHECK_INTERFACE(IMediaPlayerService, data, reply);
180 pid_t pid = data.readInt32();
181 sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid);
182 reply->writeStrongBinder(retriever->asBinder());
183 return NO_ERROR;
184 } break;
185 default:
186 return BBinder::onTransact(code, data, reply, flags);
187 }
188}
189
190// ----------------------------------------------------------------------------
191
192}; // namespace android