blob: bb86e0550ba5e967c39e638890e2b59558b61d3e [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// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <dirent.h>
27#include <unistd.h>
28
29#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070030
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070032#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070033
34#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035
36#include <android_runtime/ActivityManager.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070037
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046#include <cutils/properties.h>
47
48#include <media/MediaPlayerInterface.h>
49#include <media/mediarecorder.h>
50#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070051#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/AudioTrack.h>
53
54#include "MediaRecorderClient.h"
55#include "MediaPlayerService.h"
56#include "MetadataRetrieverClient.h"
57
58#include "MidiFile.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080059#include <media/PVPlayer.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070060#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070061#include "StagefrightPlayer.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070062
Andreas Huber20111aa2009-07-14 16:56:47 -070063#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070064
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065/* desktop Linux needs a little help with gettid() */
66#if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
67#define __KERNEL__
68# include <linux/unistd.h>
69#ifdef _syscall0
70_syscall0(pid_t,gettid)
71#else
72pid_t gettid() { return syscall(__NR_gettid);}
73#endif
74#undef __KERNEL__
75#endif
76
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070077namespace {
nikoa64c8c72009-07-20 15:07:26 -070078using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070079using android::status_t;
80using android::OK;
81using android::BAD_VALUE;
82using android::NOT_ENOUGH_DATA;
83using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070084
85// Max number of entries in the filter.
86const int kMaxFilterSize = 64; // I pulled that out of thin air.
87
nikoa64c8c72009-07-20 15:07:26 -070088// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070089
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070090
91// Unmarshall a filter from a Parcel.
92// Filter format in a parcel:
93//
94// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | number of entries (n) |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | metadata type 1 |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// | metadata type 2 |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// ....
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | metadata type n |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106//
107// @param p Parcel that should start with a filter.
108// @param[out] filter On exit contains the list of metadata type to be
109// filtered.
110// @param[out] status On exit contains the status code to be returned.
111// @return true if the parcel starts with a valid filter.
112bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700113 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700114 status_t *status)
115{
Nicolas Catania48290382009-07-10 13:53:06 -0700116 int32_t val;
117 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700118 {
119 LOGE("Failed to read filter's length");
120 *status = NOT_ENOUGH_DATA;
121 return false;
122 }
123
Nicolas Catania48290382009-07-10 13:53:06 -0700124 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 {
Nicolas Catania48290382009-07-10 13:53:06 -0700126 LOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 *status = BAD_VALUE;
128 return false;
129 }
130
Nicolas Catania48290382009-07-10 13:53:06 -0700131 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132
133 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700134 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
nikoa64c8c72009-07-20 15:07:26 -0700136 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137
Nicolas Catania48290382009-07-10 13:53:06 -0700138
139 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 {
Nicolas Catania48290382009-07-10 13:53:06 -0700141 LOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142 *status = NOT_ENOUGH_DATA;
143 return false;
144 }
145
nikoa64c8c72009-07-20 15:07:26 -0700146 const Metadata::Type *data =
147 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148
Nicolas Catania48290382009-07-10 13:53:06 -0700149 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 {
151 LOGE("Filter had no data");
152 *status = BAD_VALUE;
153 return false;
154 }
155
156 // TODO: The stl impl of vector would be more efficient here
157 // because it degenerates into a memcpy on pod types. Try to
158 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700159 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 {
Nicolas Catania48290382009-07-10 13:53:06 -0700161 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 ++data;
163 }
164 *status = OK;
165 return true;
166}
167
Nicolas Catania48290382009-07-10 13:53:06 -0700168// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169// @param val To be searched.
170// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700171bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172{
173 // Deal with empty and ANY right away
174 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700175 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176
Nicolas Catania48290382009-07-10 13:53:06 -0700177 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178}
179
180} // anonymous namespace
181
182
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183namespace android {
184
185// TODO: Temp hack until we can register players
186typedef struct {
187 const char *extension;
188 const player_type playertype;
189} extmap;
190extmap FILE_EXTS [] = {
191 {".mid", SONIVOX_PLAYER},
192 {".midi", SONIVOX_PLAYER},
193 {".smf", SONIVOX_PLAYER},
194 {".xmf", SONIVOX_PLAYER},
195 {".imy", SONIVOX_PLAYER},
196 {".rtttl", SONIVOX_PLAYER},
197 {".rtx", SONIVOX_PLAYER},
198 {".ota", SONIVOX_PLAYER},
Andreas Huberb49676a2010-01-20 16:11:15 -0800199#ifndef NO_OPENCORE
200 {".wma", PV_PLAYER},
201 {".wmv", PV_PLAYER},
202 {".asf", PV_PLAYER},
203#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204};
205
206// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
208/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
209
210void MediaPlayerService::instantiate() {
211 defaultServiceManager()->addService(
212 String16("media.player"), new MediaPlayerService());
213}
214
215MediaPlayerService::MediaPlayerService()
216{
217 LOGV("MediaPlayerService created");
218 mNextConnId = 1;
219}
220
221MediaPlayerService::~MediaPlayerService()
222{
223 LOGV("MediaPlayerService destroyed");
224}
225
226sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
227{
Gloria Wangdac6a312009-10-29 15:46:37 -0700228 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
229 wp<MediaRecorderClient> w = recorder;
230 Mutex::Autolock lock(mLock);
231 mMediaRecorderClients.add(w);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232 LOGV("Create new media recorder client from pid %d", pid);
233 return recorder;
234}
235
Gloria Wangdac6a312009-10-29 15:46:37 -0700236void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
237{
238 Mutex::Autolock lock(mLock);
239 mMediaRecorderClients.remove(client);
240 LOGV("Delete media recorder client");
241}
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
244{
245 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
246 LOGV("Create new media retriever from pid %d", pid);
247 return retriever;
248}
249
Andreas Huber2db84552010-01-28 11:19:57 -0800250sp<IMediaPlayer> MediaPlayerService::create(
251 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700252 const KeyedVector<String8, String8> *headers, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253{
254 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700255 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
256 LOGV("Create new client(%d) from pid %d, url=%s, connId=%d, audioSessionId=%d",
257 connId, pid, url, connId, audioSessionId);
Andreas Huber2db84552010-01-28 11:19:57 -0800258 if (NO_ERROR != c->setDataSource(url, headers))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 {
260 c.clear();
261 return c;
262 }
263 wp<Client> w = c;
264 Mutex::Autolock lock(mLock);
265 mClients.add(w);
266 return c;
267}
268
269sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700270 int fd, int64_t offset, int64_t length, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271{
272 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700273 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
274 LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld, audioSessionId=%d",
275 connId, pid, fd, offset, length, audioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 if (NO_ERROR != c->setDataSource(fd, offset, length)) {
277 c.clear();
278 } else {
279 wp<Client> w = c;
280 Mutex::Autolock lock(mLock);
281 mClients.add(w);
282 }
283 ::close(fd);
284 return c;
285}
286
Andreas Huber318ad9c2009-10-15 13:46:54 -0700287sp<IOMX> MediaPlayerService::getOMX() {
288 Mutex::Autolock autoLock(mLock);
289
290 if (mOMX.get() == NULL) {
291 mOMX = new OMX;
292 }
293
294 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700295}
296
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
298{
299 const size_t SIZE = 256;
300 char buffer[SIZE];
301 String8 result;
302
303 result.append(" AudioCache\n");
304 if (mHeap != 0) {
305 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
306 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
307 result.append(buffer);
308 }
309 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
310 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
311 result.append(buffer);
312 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
313 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
314 result.append(buffer);
315 ::write(fd, result.string(), result.size());
316 return NO_ERROR;
317}
318
319status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
320{
321 const size_t SIZE = 256;
322 char buffer[SIZE];
323 String8 result;
324
325 result.append(" AudioOutput\n");
326 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
327 mStreamType, mLeftVolume, mRightVolume);
328 result.append(buffer);
329 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
330 mMsecsPerFrame, mLatency);
331 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700332 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
333 mAuxEffectId, mSendLevel);
334 result.append(buffer);
335
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 ::write(fd, result.string(), result.size());
337 if (mTrack != 0) {
338 mTrack->dump(fd, args);
339 }
340 return NO_ERROR;
341}
342
343status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
344{
345 const size_t SIZE = 256;
346 char buffer[SIZE];
347 String8 result;
348 result.append(" Client\n");
349 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
350 mPid, mConnId, mStatus, mLoop?"true": "false");
351 result.append(buffer);
352 write(fd, result.string(), result.size());
353 if (mAudioOutput != 0) {
354 mAudioOutput->dump(fd, args);
355 }
356 write(fd, "\n", 1);
357 return NO_ERROR;
358}
359
360static int myTid() {
361#ifdef HAVE_GETTID
362 return gettid();
363#else
364 return getpid();
365#endif
366}
367
368#if defined(__arm__)
369extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
370 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
371extern "C" void free_malloc_leak_info(uint8_t* info);
372
Andreas Huber1eea7f52009-10-27 15:50:04 -0700373// Use the String-class below instead of String8 to allocate all memory
374// beforehand and not reenter the heap while we are examining it...
375struct MyString8 {
376 static const size_t MAX_SIZE = 256 * 1024;
377
378 MyString8()
379 : mPtr((char *)malloc(MAX_SIZE)) {
380 *mPtr = '\0';
381 }
382
383 ~MyString8() {
384 free(mPtr);
385 }
386
387 void append(const char *s) {
388 strcat(mPtr, s);
389 }
390
391 const char *string() const {
392 return mPtr;
393 }
394
395 size_t size() const {
396 return strlen(mPtr);
397 }
398
399private:
400 char *mPtr;
401
402 MyString8(const MyString8 &);
403 MyString8 &operator=(const MyString8 &);
404};
405
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406void memStatus(int fd, const Vector<String16>& args)
407{
408 const size_t SIZE = 256;
409 char buffer[SIZE];
Andreas Huber1eea7f52009-10-27 15:50:04 -0700410 MyString8 result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411
412 typedef struct {
413 size_t size;
414 size_t dups;
415 intptr_t * backtrace;
416 } AllocEntry;
417
418 uint8_t *info = NULL;
419 size_t overallSize = 0;
420 size_t infoSize = 0;
421 size_t totalMemory = 0;
422 size_t backtraceSize = 0;
423
424 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
425 if (info) {
426 uint8_t *ptr = info;
427 size_t count = overallSize / infoSize;
428
429 snprintf(buffer, SIZE, " Allocation count %i\n", count);
430 result.append(buffer);
James Dong1edee162010-02-25 10:06:32 -0800431 snprintf(buffer, SIZE, " Total memory %i\n", totalMemory);
432 result.append(buffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433
434 AllocEntry * entries = new AllocEntry[count];
435
436 for (size_t i = 0; i < count; i++) {
437 // Each entry should be size_t, size_t, intptr_t[backtraceSize]
438 AllocEntry *e = &entries[i];
439
440 e->size = *reinterpret_cast<size_t *>(ptr);
441 ptr += sizeof(size_t);
442
443 e->dups = *reinterpret_cast<size_t *>(ptr);
444 ptr += sizeof(size_t);
445
446 e->backtrace = reinterpret_cast<intptr_t *>(ptr);
447 ptr += sizeof(intptr_t) * backtraceSize;
448 }
449
450 // Now we need to sort the entries. They come sorted by size but
451 // not by stack trace which causes problems using diff.
452 bool moved;
453 do {
454 moved = false;
455 for (size_t i = 0; i < (count - 1); i++) {
456 AllocEntry *e1 = &entries[i];
457 AllocEntry *e2 = &entries[i+1];
458
459 bool swap = e1->size < e2->size;
460 if (e1->size == e2->size) {
461 for(size_t j = 0; j < backtraceSize; j++) {
462 if (e1->backtrace[j] == e2->backtrace[j]) {
463 continue;
464 }
465 swap = e1->backtrace[j] < e2->backtrace[j];
466 break;
467 }
468 }
469 if (swap) {
470 AllocEntry t = entries[i];
471 entries[i] = entries[i+1];
472 entries[i+1] = t;
473 moved = true;
474 }
475 }
476 } while (moved);
477
478 for (size_t i = 0; i < count; i++) {
479 AllocEntry *e = &entries[i];
480
James Dong1edee162010-02-25 10:06:32 -0800481 snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 result.append(buffer);
483 for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) {
484 if (ct) {
485 result.append(", ");
486 }
487 snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]);
488 result.append(buffer);
489 }
490 result.append("\n");
491 }
492
493 delete[] entries;
494 free_malloc_leak_info(info);
495 }
496
497 write(fd, result.string(), result.size());
498}
499#endif
500
501status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
502{
503 const size_t SIZE = 256;
504 char buffer[SIZE];
505 String8 result;
506 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
507 snprintf(buffer, SIZE, "Permission Denial: "
508 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
509 IPCThreadState::self()->getCallingPid(),
510 IPCThreadState::self()->getCallingUid());
511 result.append(buffer);
512 } else {
513 Mutex::Autolock lock(mLock);
514 for (int i = 0, n = mClients.size(); i < n; ++i) {
515 sp<Client> c = mClients[i].promote();
516 if (c != 0) c->dump(fd, args);
517 }
James Dongb9141222010-07-08 11:16:11 -0700518 if (mMediaRecorderClients.size() == 0) {
519 result.append(" No media recorder client\n\n");
520 } else {
521 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
522 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
523 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
524 result.append(buffer);
525 write(fd, result.string(), result.size());
526 result = "\n";
527 c->dump(fd, args);
528 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700529 }
530
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531 result.append(" Files opened and/or mapped:\n");
532 snprintf(buffer, SIZE, "/proc/%d/maps", myTid());
533 FILE *f = fopen(buffer, "r");
534 if (f) {
535 while (!feof(f)) {
536 fgets(buffer, SIZE, f);
Dave Sparks02fa8342010-09-27 16:55:18 -0700537 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700539 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 strstr(buffer, " /system/media/")) {
541 result.append(" ");
542 result.append(buffer);
543 }
544 }
545 fclose(f);
546 } else {
547 result.append("couldn't open ");
548 result.append(buffer);
549 result.append("\n");
550 }
551
552 snprintf(buffer, SIZE, "/proc/%d/fd", myTid());
553 DIR *d = opendir(buffer);
554 if (d) {
555 struct dirent *ent;
556 while((ent = readdir(d)) != NULL) {
557 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
558 snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name);
559 struct stat s;
560 if (lstat(buffer, &s) == 0) {
561 if ((s.st_mode & S_IFMT) == S_IFLNK) {
562 char linkto[256];
563 int len = readlink(buffer, linkto, sizeof(linkto));
564 if(len > 0) {
565 if(len > 255) {
566 linkto[252] = '.';
567 linkto[253] = '.';
568 linkto[254] = '.';
569 linkto[255] = 0;
570 } else {
571 linkto[len] = 0;
572 }
Dave Sparks02fa8342010-09-27 16:55:18 -0700573 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700575 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 strstr(linkto, "/system/media/") == linkto) {
577 result.append(" ");
578 result.append(buffer);
579 result.append(" -> ");
580 result.append(linkto);
581 result.append("\n");
582 }
583 }
584 } else {
585 result.append(" unexpected type for ");
586 result.append(buffer);
587 result.append("\n");
588 }
589 }
590 }
591 }
592 closedir(d);
593 } else {
594 result.append("couldn't open ");
595 result.append(buffer);
596 result.append("\n");
597 }
598
599#if defined(__arm__)
600 bool dumpMem = false;
601 for (size_t i = 0; i < args.size(); i++) {
602 if (args[i] == String16("-m")) {
603 dumpMem = true;
604 }
605 }
606 if (dumpMem) {
607 memStatus(fd, args);
608 }
609#endif
610 }
611 write(fd, result.string(), result.size());
612 return NO_ERROR;
613}
614
615void MediaPlayerService::removeClient(wp<Client> client)
616{
617 Mutex::Autolock lock(mLock);
618 mClients.remove(client);
619}
620
621MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700622 int32_t connId, const sp<IMediaPlayerClient>& client, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623{
624 LOGV("Client(%d) constructor", connId);
625 mPid = pid;
626 mConnId = connId;
627 mService = service;
628 mClient = client;
629 mLoop = false;
630 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700631 mAudioSessionId = audioSessionId;
632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633#if CALLBACK_ANTAGONIZER
634 LOGD("create Antagonizer");
635 mAntagonizer = new Antagonizer(notify, this);
636#endif
637}
638
639MediaPlayerService::Client::~Client()
640{
641 LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
642 mAudioOutput.clear();
643 wp<Client> client(this);
644 disconnect();
645 mService->removeClient(client);
646}
647
648void MediaPlayerService::Client::disconnect()
649{
650 LOGV("disconnect(%d) from pid %d", mConnId, mPid);
651 // grab local reference and clear main reference to prevent future
652 // access to object
653 sp<MediaPlayerBase> p;
654 {
655 Mutex::Autolock l(mLock);
656 p = mPlayer;
657 }
Dave Sparks795fa582009-03-24 17:57:12 -0700658 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700659
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660 mPlayer.clear();
661
662 // clear the notification to prevent callbacks to dead client
663 // and reset the player. We assume the player will serialize
664 // access to itself if necessary.
665 if (p != 0) {
666 p->setNotifyCallback(0, 0);
667#if CALLBACK_ANTAGONIZER
668 LOGD("kill Antagonizer");
669 mAntagonizer->kill();
670#endif
671 p->reset();
672 }
673
674 IPCThreadState::self()->flushCommands();
675}
676
Andreas Huber47f59cf2009-08-07 09:30:32 -0700677static player_type getDefaultPlayerType() {
Andreas Huber608d77b2010-06-23 16:40:57 -0700678 return STAGEFRIGHT_PLAYER;
Andreas Huber47f59cf2009-08-07 09:30:32 -0700679}
680
James Dong148c1a22009-09-06 14:29:45 -0700681player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682{
683 char buf[20];
684 lseek(fd, offset, SEEK_SET);
685 read(fd, buf, sizeof(buf));
686 lseek(fd, offset, SEEK_SET);
687
688 long ident = *((long*)buf);
689
690 // Ogg vorbis?
691 if (ident == 0x5367674f) // 'OggS'
Andreas Huber608d77b2010-06-23 16:40:57 -0700692 return STAGEFRIGHT_PLAYER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693
Andreas Huberb49676a2010-01-20 16:11:15 -0800694#ifndef NO_OPENCORE
695 if (ident == 0x75b22630) {
696 // The magic number for .asf files, i.e. wmv and wma content.
697 // These are not currently supported through stagefright.
698 return PV_PLAYER;
699 }
700#endif
701
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 // Some kind of MIDI?
703 EAS_DATA_HANDLE easdata;
704 if (EAS_Init(&easdata) == EAS_SUCCESS) {
705 EAS_FILE locator;
706 locator.path = NULL;
707 locator.fd = fd;
708 locator.offset = offset;
709 locator.length = length;
710 EAS_HANDLE eashandle;
711 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
712 EAS_CloseFile(easdata, eashandle);
713 EAS_Shutdown(easdata);
714 return SONIVOX_PLAYER;
715 }
716 EAS_Shutdown(easdata);
717 }
718
Andreas Huber47f59cf2009-08-07 09:30:32 -0700719 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720}
721
James Dong148c1a22009-09-06 14:29:45 -0700722player_type getPlayerType(const char* url)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723{
Nicolas Catania14d27472009-07-13 14:37:49 -0700724 if (TestPlayerStub::canBeUsed(url)) {
725 return TEST_PLAYER;
726 }
727
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 // use MidiFile for MIDI extensions
729 int lenURL = strlen(url);
730 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
731 int len = strlen(FILE_EXTS[i].extension);
732 int start = lenURL - len;
733 if (start > 0) {
Atsushi Enofc1c7b92010-03-19 23:18:02 +0900734 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber608d77b2010-06-23 16:40:57 -0700735 return FILE_EXTS[i].playertype;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736 }
737 }
738 }
739
James Dong27fde952010-04-13 21:33:26 -0700740 if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Hubercf7b9c72010-06-07 15:19:40 -0700741 char value[PROPERTY_VALUE_MAX];
Andreas Hubera1ffe492010-08-30 12:46:12 -0700742 if (property_get("media.stagefright.enable-rtsp", value, NULL)
743 && (strcmp(value, "1") && strcasecmp(value, "true"))) {
Andreas Hubercf7b9c72010-06-07 15:19:40 -0700744 // For now, we're going to use PV for rtsp-based playback
745 // by default until we can clear up a few more issues.
746 return PV_PLAYER;
747 }
James Dong27fde952010-04-13 21:33:26 -0700748 }
749
Andreas Huber47f59cf2009-08-07 09:30:32 -0700750 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751}
752
753static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
754 notify_callback_f notifyFunc)
755{
756 sp<MediaPlayerBase> p;
757 switch (playerType) {
Jean-Baptiste Queru6c5b2102009-03-21 11:40:18 -0700758#ifndef NO_OPENCORE
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 case PV_PLAYER:
760 LOGV(" create PVPlayer");
761 p = new PVPlayer();
762 break;
Jean-Baptiste Queru6c5b2102009-03-21 11:40:18 -0700763#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800764 case SONIVOX_PLAYER:
765 LOGV(" create MidiFile");
766 p = new MidiFile();
767 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700768 case STAGEFRIGHT_PLAYER:
769 LOGV(" create StagefrightPlayer");
770 p = new StagefrightPlayer;
771 break;
Nicolas Catania14d27472009-07-13 14:37:49 -0700772 case TEST_PLAYER:
773 LOGV("Create Test Player stub");
774 p = new TestPlayerStub();
775 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800776 }
777 if (p != NULL) {
778 if (p->initCheck() == NO_ERROR) {
779 p->setNotifyCallback(cookie, notifyFunc);
780 } else {
781 p.clear();
782 }
783 }
784 if (p == NULL) {
785 LOGE("Failed to create player object");
786 }
787 return p;
788}
789
790sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
791{
792 // determine if we have the right player type
793 sp<MediaPlayerBase> p = mPlayer;
794 if ((p != NULL) && (p->playerType() != playerType)) {
795 LOGV("delete player");
796 p.clear();
797 }
798 if (p == NULL) {
799 p = android::createPlayer(playerType, this, notify);
800 }
801 return p;
802}
803
Andreas Huber2db84552010-01-28 11:19:57 -0800804status_t MediaPlayerService::Client::setDataSource(
805 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800806{
807 LOGV("setDataSource(%s)", url);
808 if (url == NULL)
809 return UNKNOWN_ERROR;
810
811 if (strncmp(url, "content://", 10) == 0) {
812 // get a filedescriptor for the content Uri and
813 // pass it to the setDataSource(fd) method
814
815 String16 url16(url);
816 int fd = android::openContentProviderFile(url16);
817 if (fd < 0)
818 {
819 LOGE("Couldn't open fd for %s", url);
820 return UNKNOWN_ERROR;
821 }
822 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
823 close(fd);
824 return mStatus;
825 } else {
826 player_type playerType = getPlayerType(url);
827 LOGV("player type = %d", playerType);
828
829 // create the right type of player
830 sp<MediaPlayerBase> p = createPlayer(playerType);
831 if (p == NULL) return NO_INIT;
832
833 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700834 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
836 }
837
838 // now set data source
839 LOGV(" setDataSource");
Andreas Huber2db84552010-01-28 11:19:57 -0800840 mStatus = p->setDataSource(url, headers);
Nicolas Catania14d27472009-07-13 14:37:49 -0700841 if (mStatus == NO_ERROR) {
842 mPlayer = p;
843 } else {
844 LOGE(" error: %d", mStatus);
845 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800846 return mStatus;
847 }
848}
849
850status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
851{
852 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
853 struct stat sb;
854 int ret = fstat(fd, &sb);
855 if (ret != 0) {
856 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
857 return UNKNOWN_ERROR;
858 }
859
860 LOGV("st_dev = %llu", sb.st_dev);
861 LOGV("st_mode = %u", sb.st_mode);
862 LOGV("st_uid = %lu", sb.st_uid);
863 LOGV("st_gid = %lu", sb.st_gid);
864 LOGV("st_size = %llu", sb.st_size);
865
866 if (offset >= sb.st_size) {
867 LOGE("offset error");
868 ::close(fd);
869 return UNKNOWN_ERROR;
870 }
871 if (offset + length > sb.st_size) {
872 length = sb.st_size - offset;
873 LOGV("calculated length = %lld", length);
874 }
875
876 player_type playerType = getPlayerType(fd, offset, length);
877 LOGV("player type = %d", playerType);
878
879 // create the right type of player
880 sp<MediaPlayerBase> p = createPlayer(playerType);
881 if (p == NULL) return NO_INIT;
882
883 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700884 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800885 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
886 }
887
888 // now set data source
889 mStatus = p->setDataSource(fd, offset, length);
890 if (mStatus == NO_ERROR) mPlayer = p;
891 return mStatus;
892}
893
Andreas Huber5daeb122010-08-16 08:49:37 -0700894status_t MediaPlayerService::Client::setVideoISurface(const sp<ISurface>& surface)
895{
896 LOGV("[%d] setVideoISurface(%p)", mConnId, surface.get());
897 sp<MediaPlayerBase> p = getPlayer();
898 if (p == 0) return UNKNOWN_ERROR;
899 return p->setVideoISurface(surface);
900}
901
902status_t MediaPlayerService::Client::setVideoSurface(const sp<Surface>& surface)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903{
904 LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get());
905 sp<MediaPlayerBase> p = getPlayer();
906 if (p == 0) return UNKNOWN_ERROR;
907 return p->setVideoSurface(surface);
908}
909
Nicolas Catania1d187f12009-05-12 23:25:55 -0700910status_t MediaPlayerService::Client::invoke(const Parcel& request,
911 Parcel *reply)
912{
913 sp<MediaPlayerBase> p = getPlayer();
914 if (p == NULL) return UNKNOWN_ERROR;
915 return p->invoke(request, reply);
916}
917
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700918// This call doesn't need to access the native player.
919status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
920{
921 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700922 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700923
Nicolas Catania48290382009-07-10 13:53:06 -0700924 if (unmarshallFilter(filter, &allow, &status) &&
925 unmarshallFilter(filter, &drop, &status)) {
926 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700927
928 mMetadataAllow = allow;
929 mMetadataDrop = drop;
930 }
931 return status;
932}
933
Nicolas Catania48290382009-07-10 13:53:06 -0700934status_t MediaPlayerService::Client::getMetadata(
935 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700936{
nikoa64c8c72009-07-20 15:07:26 -0700937 sp<MediaPlayerBase> player = getPlayer();
938 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700939
nikod608a812009-07-16 16:39:53 -0700940 status_t status;
941 // Placeholder for the return code, updated by the caller.
942 reply->writeInt32(-1);
943
nikoa64c8c72009-07-20 15:07:26 -0700944 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700945
946 // We don't block notifications while we fetch the data. We clear
947 // mMetadataUpdated first so we don't lose notifications happening
948 // during the rest of this call.
949 {
950 Mutex::Autolock lock(mLock);
951 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700952 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700953 }
954 mMetadataUpdated.clear();
955 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700956
nikoa64c8c72009-07-20 15:07:26 -0700957 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700958
nikoa64c8c72009-07-20 15:07:26 -0700959 metadata.appendHeader();
960 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700961
962 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700963 metadata.resetParcel();
nikod608a812009-07-16 16:39:53 -0700964 LOGE("getMetadata failed %d", status);
965 return status;
966 }
967
968 // FIXME: Implement filtering on the result. Not critical since
969 // filtering takes place on the update notifications already. This
970 // would be when all the metadata are fetch and a filter is set.
971
nikod608a812009-07-16 16:39:53 -0700972 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700973 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700974 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700975}
976
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977status_t MediaPlayerService::Client::prepareAsync()
978{
979 LOGV("[%d] prepareAsync", mConnId);
980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
982 status_t ret = p->prepareAsync();
983#if CALLBACK_ANTAGONIZER
984 LOGD("start Antagonizer");
985 if (ret == NO_ERROR) mAntagonizer->start();
986#endif
987 return ret;
988}
989
990status_t MediaPlayerService::Client::start()
991{
992 LOGV("[%d] start", mConnId);
993 sp<MediaPlayerBase> p = getPlayer();
994 if (p == 0) return UNKNOWN_ERROR;
995 p->setLooping(mLoop);
996 return p->start();
997}
998
999status_t MediaPlayerService::Client::stop()
1000{
1001 LOGV("[%d] stop", mConnId);
1002 sp<MediaPlayerBase> p = getPlayer();
1003 if (p == 0) return UNKNOWN_ERROR;
1004 return p->stop();
1005}
1006
1007status_t MediaPlayerService::Client::pause()
1008{
1009 LOGV("[%d] pause", mConnId);
1010 sp<MediaPlayerBase> p = getPlayer();
1011 if (p == 0) return UNKNOWN_ERROR;
1012 return p->pause();
1013}
1014
1015status_t MediaPlayerService::Client::isPlaying(bool* state)
1016{
1017 *state = false;
1018 sp<MediaPlayerBase> p = getPlayer();
1019 if (p == 0) return UNKNOWN_ERROR;
1020 *state = p->isPlaying();
1021 LOGV("[%d] isPlaying: %d", mConnId, *state);
1022 return NO_ERROR;
1023}
1024
1025status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1026{
1027 LOGV("getCurrentPosition");
1028 sp<MediaPlayerBase> p = getPlayer();
1029 if (p == 0) return UNKNOWN_ERROR;
1030 status_t ret = p->getCurrentPosition(msec);
1031 if (ret == NO_ERROR) {
1032 LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1033 } else {
1034 LOGE("getCurrentPosition returned %d", ret);
1035 }
1036 return ret;
1037}
1038
1039status_t MediaPlayerService::Client::getDuration(int *msec)
1040{
1041 LOGV("getDuration");
1042 sp<MediaPlayerBase> p = getPlayer();
1043 if (p == 0) return UNKNOWN_ERROR;
1044 status_t ret = p->getDuration(msec);
1045 if (ret == NO_ERROR) {
1046 LOGV("[%d] getDuration = %d", mConnId, *msec);
1047 } else {
1048 LOGE("getDuration returned %d", ret);
1049 }
1050 return ret;
1051}
1052
1053status_t MediaPlayerService::Client::seekTo(int msec)
1054{
1055 LOGV("[%d] seekTo(%d)", mConnId, msec);
1056 sp<MediaPlayerBase> p = getPlayer();
1057 if (p == 0) return UNKNOWN_ERROR;
1058 return p->seekTo(msec);
1059}
1060
1061status_t MediaPlayerService::Client::reset()
1062{
1063 LOGV("[%d] reset", mConnId);
1064 sp<MediaPlayerBase> p = getPlayer();
1065 if (p == 0) return UNKNOWN_ERROR;
1066 return p->reset();
1067}
1068
1069status_t MediaPlayerService::Client::setAudioStreamType(int type)
1070{
1071 LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1072 // TODO: for hardware output, call player instead
1073 Mutex::Autolock l(mLock);
1074 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1075 return NO_ERROR;
1076}
1077
1078status_t MediaPlayerService::Client::setLooping(int loop)
1079{
1080 LOGV("[%d] setLooping(%d)", mConnId, loop);
1081 mLoop = loop;
1082 sp<MediaPlayerBase> p = getPlayer();
1083 if (p != 0) return p->setLooping(loop);
1084 return NO_ERROR;
1085}
1086
1087status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1088{
1089 LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1090 // TODO: for hardware output, call player instead
1091 Mutex::Autolock l(mLock);
1092 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1093 return NO_ERROR;
1094}
1095
Eric Laurent2beeb502010-07-16 07:43:46 -07001096status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1097{
1098 LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1099 Mutex::Autolock l(mLock);
1100 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1101 return NO_ERROR;
1102}
1103
1104status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1105{
1106 LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1107 Mutex::Autolock l(mLock);
1108 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1109 return NO_ERROR;
1110}
Nicolas Catania48290382009-07-10 13:53:06 -07001111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001112void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2)
1113{
1114 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001115
1116 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001117 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001118 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001119
1120 if(client->shouldDropMetadata(metadata_type)) {
1121 return;
1122 }
1123
1124 // Update the list of metadata that have changed. getMetadata
1125 // also access mMetadataUpdated and clears it.
1126 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001127 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1129 client->mClient->notify(msg, ext1, ext2);
1130}
1131
Nicolas Catania48290382009-07-10 13:53:06 -07001132
nikoa64c8c72009-07-20 15:07:26 -07001133bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001134{
Nicolas Catania48290382009-07-10 13:53:06 -07001135 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001136
Nicolas Catania48290382009-07-10 13:53:06 -07001137 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001138 return true;
1139 }
1140
Nicolas Catania48290382009-07-10 13:53:06 -07001141 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001142 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001143 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001144 return true;
1145 }
1146}
1147
Nicolas Catania48290382009-07-10 13:53:06 -07001148
nikoa64c8c72009-07-20 15:07:26 -07001149void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001150 Mutex::Autolock lock(mLock);
1151 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1152 mMetadataUpdated.add(metadata_type);
1153 }
1154}
1155
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001156#if CALLBACK_ANTAGONIZER
1157const int Antagonizer::interval = 10000; // 10 msecs
1158
1159Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1160 mExit(false), mActive(false), mClient(client), mCb(cb)
1161{
1162 createThread(callbackThread, this);
1163}
1164
1165void Antagonizer::kill()
1166{
1167 Mutex::Autolock _l(mLock);
1168 mActive = false;
1169 mExit = true;
1170 mCondition.wait(mLock);
1171}
1172
1173int Antagonizer::callbackThread(void* user)
1174{
1175 LOGD("Antagonizer started");
1176 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1177 while (!p->mExit) {
1178 if (p->mActive) {
1179 LOGV("send event");
1180 p->mCb(p->mClient, 0, 0, 0);
1181 }
1182 usleep(interval);
1183 }
1184 Mutex::Autolock _l(p->mLock);
1185 p->mCondition.signal();
1186 LOGD("Antagonizer stopped");
1187 return 0;
1188}
1189#endif
1190
1191static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1192
1193sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1194{
1195 LOGV("decode(%s)", url);
1196 sp<MemoryBase> mem;
1197 sp<MediaPlayerBase> player;
1198
1199 // Protect our precious, precious DRMd ringtones by only allowing
1200 // decoding of http, but not filesystem paths or content Uris.
1201 // If the application wants to decode those, it should open a
1202 // filedescriptor for them and use that.
1203 if (url != NULL && strncmp(url, "http://", 7) != 0) {
1204 LOGD("Can't decode %s by path, use filedescriptor instead", url);
1205 return mem;
1206 }
1207
1208 player_type playerType = getPlayerType(url);
1209 LOGV("player type = %d", playerType);
1210
1211 // create the right type of player
1212 sp<AudioCache> cache = new AudioCache(url);
1213 player = android::createPlayer(playerType, cache.get(), cache->notify);
1214 if (player == NULL) goto Exit;
1215 if (player->hardwareOutput()) goto Exit;
1216
1217 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1218
1219 // set data source
1220 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1221
1222 LOGV("prepare");
1223 player->prepareAsync();
1224
1225 LOGV("wait for prepare");
1226 if (cache->wait() != NO_ERROR) goto Exit;
1227
1228 LOGV("start");
1229 player->start();
1230
1231 LOGV("wait for playback complete");
1232 if (cache->wait() != NO_ERROR) goto Exit;
1233
1234 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1235 *pSampleRate = cache->sampleRate();
1236 *pNumChannels = cache->channelCount();
1237 *pFormat = cache->format();
1238 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1239
1240Exit:
1241 if (player != 0) player->reset();
1242 return mem;
1243}
1244
1245sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1246{
1247 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1248 sp<MemoryBase> mem;
1249 sp<MediaPlayerBase> player;
1250
1251 player_type playerType = getPlayerType(fd, offset, length);
1252 LOGV("player type = %d", playerType);
1253
1254 // create the right type of player
1255 sp<AudioCache> cache = new AudioCache("decode_fd");
1256 player = android::createPlayer(playerType, cache.get(), cache->notify);
1257 if (player == NULL) goto Exit;
1258 if (player->hardwareOutput()) goto Exit;
1259
1260 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1261
1262 // set data source
1263 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1264
1265 LOGV("prepare");
1266 player->prepareAsync();
1267
1268 LOGV("wait for prepare");
1269 if (cache->wait() != NO_ERROR) goto Exit;
1270
1271 LOGV("start");
1272 player->start();
1273
1274 LOGV("wait for playback complete");
1275 if (cache->wait() != NO_ERROR) goto Exit;
1276
1277 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1278 *pSampleRate = cache->sampleRate();
1279 *pNumChannels = cache->channelCount();
1280 *pFormat = cache->format();
1281 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1282
1283Exit:
1284 if (player != 0) player->reset();
1285 ::close(fd);
1286 return mem;
1287}
1288
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290#undef LOG_TAG
1291#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001292MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001293 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001294 mCallbackCookie(NULL),
1295 mSessionId(sessionId) {
1296 LOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 mTrack = 0;
1298 mStreamType = AudioSystem::MUSIC;
1299 mLeftVolume = 1.0;
1300 mRightVolume = 1.0;
1301 mLatency = 0;
1302 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001303 mAuxEffectId = 0;
1304 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 setMinBufferCount();
1306}
1307
1308MediaPlayerService::AudioOutput::~AudioOutput()
1309{
1310 close();
1311}
1312
1313void MediaPlayerService::AudioOutput::setMinBufferCount()
1314{
1315 char value[PROPERTY_VALUE_MAX];
1316 if (property_get("ro.kernel.qemu", value, 0)) {
1317 mIsOnEmulator = true;
1318 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1319 }
1320}
1321
1322bool MediaPlayerService::AudioOutput::isOnEmulator()
1323{
1324 setMinBufferCount();
1325 return mIsOnEmulator;
1326}
1327
1328int MediaPlayerService::AudioOutput::getMinBufferCount()
1329{
1330 setMinBufferCount();
1331 return mMinBufferCount;
1332}
1333
1334ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1335{
1336 if (mTrack == 0) return NO_INIT;
1337 return mTrack->frameCount() * frameSize();
1338}
1339
1340ssize_t MediaPlayerService::AudioOutput::frameCount() const
1341{
1342 if (mTrack == 0) return NO_INIT;
1343 return mTrack->frameCount();
1344}
1345
1346ssize_t MediaPlayerService::AudioOutput::channelCount() const
1347{
1348 if (mTrack == 0) return NO_INIT;
1349 return mTrack->channelCount();
1350}
1351
1352ssize_t MediaPlayerService::AudioOutput::frameSize() const
1353{
1354 if (mTrack == 0) return NO_INIT;
1355 return mTrack->frameSize();
1356}
1357
1358uint32_t MediaPlayerService::AudioOutput::latency () const
1359{
1360 return mLatency;
1361}
1362
1363float MediaPlayerService::AudioOutput::msecsPerFrame() const
1364{
1365 return mMsecsPerFrame;
1366}
1367
Eric Laurent342e9cf2010-01-19 17:37:09 -08001368status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1369{
1370 if (mTrack == 0) return NO_INIT;
1371 return mTrack->getPosition(position);
1372}
1373
Andreas Huber20111aa2009-07-14 16:56:47 -07001374status_t MediaPlayerService::AudioOutput::open(
1375 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1376 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377{
Andreas Huber20111aa2009-07-14 16:56:47 -07001378 mCallback = cb;
1379 mCallbackCookie = cookie;
1380
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381 // Check argument "bufferCount" against the mininum buffer count
1382 if (bufferCount < mMinBufferCount) {
1383 LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1384 bufferCount = mMinBufferCount;
1385
1386 }
Eric Laurenta514bdb2010-06-21 09:27:30 -07001387 LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001388 if (mTrack) close();
1389 int afSampleRate;
1390 int afFrameCount;
1391 int frameCount;
1392
1393 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1394 return NO_INIT;
1395 }
1396 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1397 return NO_INIT;
1398 }
1399
1400 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001401
1402 AudioTrack *t;
1403 if (mCallback != NULL) {
1404 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001405 mStreamType,
1406 sampleRate,
1407 format,
1408 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1409 frameCount,
1410 0 /* flags */,
1411 CallbackWrapper,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001412 this,
1413 0,
1414 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001415 } else {
1416 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001417 mStreamType,
1418 sampleRate,
1419 format,
1420 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001421 frameCount,
1422 0,
1423 NULL,
1424 NULL,
1425 0,
1426 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001427 }
1428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1430 LOGE("Unable to create audio track");
1431 delete t;
1432 return NO_INIT;
1433 }
1434
1435 LOGV("setVolume");
1436 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001437
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Dave Sparks1d711f62009-12-03 10:13:32 -08001439 mLatency = t->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001441
1442 t->setAuxEffectSendLevel(mSendLevel);
1443 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444}
1445
1446void MediaPlayerService::AudioOutput::start()
1447{
1448 LOGV("start");
1449 if (mTrack) {
1450 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001451 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 mTrack->start();
1453 }
1454}
1455
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001456
1457
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1459{
Andreas Huber20111aa2009-07-14 16:56:47 -07001460 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1461
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001462 //LOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001463 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001464 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001465 return ret;
1466 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467 return NO_INIT;
1468}
1469
1470void MediaPlayerService::AudioOutput::stop()
1471{
1472 LOGV("stop");
1473 if (mTrack) mTrack->stop();
1474}
1475
1476void MediaPlayerService::AudioOutput::flush()
1477{
1478 LOGV("flush");
1479 if (mTrack) mTrack->flush();
1480}
1481
1482void MediaPlayerService::AudioOutput::pause()
1483{
1484 LOGV("pause");
1485 if (mTrack) mTrack->pause();
1486}
1487
1488void MediaPlayerService::AudioOutput::close()
1489{
1490 LOGV("close");
1491 delete mTrack;
1492 mTrack = 0;
1493}
1494
1495void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1496{
1497 LOGV("setVolume(%f, %f)", left, right);
1498 mLeftVolume = left;
1499 mRightVolume = right;
1500 if (mTrack) {
1501 mTrack->setVolume(left, right);
1502 }
1503}
1504
Eric Laurent2beeb502010-07-16 07:43:46 -07001505status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1506{
1507 LOGV("setAuxEffectSendLevel(%f)", level);
1508 mSendLevel = level;
1509 if (mTrack) {
1510 return mTrack->setAuxEffectSendLevel(level);
1511 }
1512 return NO_ERROR;
1513}
1514
1515status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1516{
1517 LOGV("attachAuxEffect(%d)", effectId);
1518 mAuxEffectId = effectId;
1519 if (mTrack) {
1520 return mTrack->attachAuxEffect(effectId);
1521 }
1522 return NO_ERROR;
1523}
1524
Andreas Huber20111aa2009-07-14 16:56:47 -07001525// static
1526void MediaPlayerService::AudioOutput::CallbackWrapper(
1527 int event, void *cookie, void *info) {
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001528 //LOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001529 if (event != AudioTrack::EVENT_MORE_DATA) {
1530 return;
1531 }
1532
1533 AudioOutput *me = (AudioOutput *)cookie;
1534 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1535
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001536 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001537 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001538
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001539 buffer->size = actualSize;
1540
Andreas Huber20111aa2009-07-14 16:56:47 -07001541}
1542
Eric Laurent8c563ed2010-10-07 18:23:03 -07001543int MediaPlayerService::AudioOutput::getSessionId()
1544{
1545 return mSessionId;
1546}
1547
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548#undef LOG_TAG
1549#define LOG_TAG "AudioCache"
1550MediaPlayerService::AudioCache::AudioCache(const char* name) :
1551 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1552 mError(NO_ERROR), mCommandComplete(false)
1553{
1554 // create ashmem heap
1555 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1556}
1557
1558uint32_t MediaPlayerService::AudioCache::latency () const
1559{
1560 return 0;
1561}
1562
1563float MediaPlayerService::AudioCache::msecsPerFrame() const
1564{
1565 return mMsecsPerFrame;
1566}
1567
Eric Laurent342e9cf2010-01-19 17:37:09 -08001568status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1569{
1570 if (position == 0) return BAD_VALUE;
1571 *position = mSize;
1572 return NO_ERROR;
1573}
1574
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001575////////////////////////////////////////////////////////////////////////////////
1576
1577struct CallbackThread : public Thread {
1578 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1579 MediaPlayerBase::AudioSink::AudioCallback cb,
1580 void *cookie);
1581
1582protected:
1583 virtual ~CallbackThread();
1584
1585 virtual bool threadLoop();
1586
1587private:
1588 wp<MediaPlayerBase::AudioSink> mSink;
1589 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1590 void *mCookie;
1591 void *mBuffer;
1592 size_t mBufferSize;
1593
1594 CallbackThread(const CallbackThread &);
1595 CallbackThread &operator=(const CallbackThread &);
1596};
1597
1598CallbackThread::CallbackThread(
1599 const wp<MediaPlayerBase::AudioSink> &sink,
1600 MediaPlayerBase::AudioSink::AudioCallback cb,
1601 void *cookie)
1602 : mSink(sink),
1603 mCallback(cb),
1604 mCookie(cookie),
1605 mBuffer(NULL),
1606 mBufferSize(0) {
1607}
1608
1609CallbackThread::~CallbackThread() {
1610 if (mBuffer) {
1611 free(mBuffer);
1612 mBuffer = NULL;
1613 }
1614}
1615
1616bool CallbackThread::threadLoop() {
1617 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1618 if (sink == NULL) {
1619 return false;
1620 }
1621
1622 if (mBuffer == NULL) {
1623 mBufferSize = sink->bufferSize();
1624 mBuffer = malloc(mBufferSize);
1625 }
1626
1627 size_t actualSize =
1628 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1629
1630 if (actualSize > 0) {
1631 sink->write(mBuffer, actualSize);
1632 }
1633
1634 return true;
1635}
1636
1637////////////////////////////////////////////////////////////////////////////////
1638
Andreas Huber20111aa2009-07-14 16:56:47 -07001639status_t MediaPlayerService::AudioCache::open(
1640 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1641 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642{
Dave Sparks8eb80112009-12-09 20:20:26 -08001643 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001644 if (mHeap->getHeapID() < 0) {
1645 return NO_INIT;
1646 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001647
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 mSampleRate = sampleRate;
1649 mChannelCount = (uint16_t)channelCount;
1650 mFormat = (uint16_t)format;
1651 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001652
1653 if (cb != NULL) {
1654 mCallbackThread = new CallbackThread(this, cb, cookie);
1655 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656 return NO_ERROR;
1657}
1658
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001659void MediaPlayerService::AudioCache::start() {
1660 if (mCallbackThread != NULL) {
1661 mCallbackThread->run("AudioCache callback");
1662 }
1663}
1664
1665void MediaPlayerService::AudioCache::stop() {
1666 if (mCallbackThread != NULL) {
1667 mCallbackThread->requestExitAndWait();
1668 }
1669}
1670
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1672{
1673 LOGV("write(%p, %u)", buffer, size);
1674 if ((buffer == 0) || (size == 0)) return size;
1675
1676 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1677 if (p == NULL) return NO_INIT;
1678 p += mSize;
1679 LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1680 if (mSize + size > mHeap->getSize()) {
1681 LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1682 size = mHeap->getSize() - mSize;
1683 }
1684 memcpy(p, buffer, size);
1685 mSize += size;
1686 return size;
1687}
1688
1689// call with lock held
1690status_t MediaPlayerService::AudioCache::wait()
1691{
1692 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001693 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694 mSignal.wait(mLock);
1695 }
1696 mCommandComplete = false;
1697
1698 if (mError == NO_ERROR) {
1699 LOGV("wait - success");
1700 } else {
1701 LOGV("wait - error");
1702 }
1703 return mError;
1704}
1705
1706void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2)
1707{
1708 LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1709 AudioCache* p = static_cast<AudioCache*>(cookie);
1710
1711 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001712 switch (msg)
1713 {
1714 case MEDIA_ERROR:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001715 LOGE("Error %d, %d occurred", ext1, ext2);
1716 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001717 break;
1718 case MEDIA_PREPARED:
1719 LOGV("prepared");
1720 break;
1721 case MEDIA_PLAYBACK_COMPLETE:
1722 LOGV("playback complete");
1723 break;
1724 default:
1725 LOGV("ignored");
1726 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001727 }
1728
1729 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001730 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001731 p->mCommandComplete = true;
1732 p->mSignal.signal();
1733}
1734
Eric Laurent8c563ed2010-10-07 18:23:03 -07001735int MediaPlayerService::AudioCache::getSessionId()
1736{
1737 return 0;
1738}
1739
nikoa64c8c72009-07-20 15:07:26 -07001740} // namespace android