blob: b5972e761f5499fc81f185172b7e1b87acfce964 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopiana650aaa2009-06-03 17:32:49 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <cutils/atomic.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070032#include <cutils/properties.h> // for property_get
Mathias Agopiana650aaa2009-06-03 17:32:49 -070033
34#include <utils/misc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36#include <android_runtime/ActivityManager.h>
Mathias Agopiana650aaa2009-06-03 17:32:49 -070037
Mathias Agopian07952722009-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 Catania20cb94e2009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissenc39d2e32009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070045#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-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>
nikobc726922009-07-20 15:07:26 -070051#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080059#include <media/PVPlayer.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070060#include "TestPlayerStub.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070061#include "StagefrightPlayer.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070062
Andreas Hubere46b7be2009-07-14 16:56:47 -070063#include <OMX.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070064
The Android Open Source Project9066cfe2009-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 Cataniab2c69392009-07-08 08:57:42 -070077namespace {
nikobc726922009-07-20 15:07:26 -070078using android::media::Metadata;
Nicolas Cataniab2c69392009-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 Cataniab2c69392009-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
nikobc726922009-07-20 15:07:26 -070088// FIXME: Move all the metadata related function in the Metadata.cpp
niko89948372009-07-16 16:39:53 -070089
Nicolas Cataniab2c69392009-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,
nikobc726922009-07-20 15:07:26 -0700113 Metadata::Filter *filter,
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700114 status_t *status)
115{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700116 int32_t val;
117 if (p.readInt32(&val) != OK)
Nicolas Cataniab2c69392009-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 Catania4df8b2c2009-07-10 13:53:06 -0700124 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700125 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700126 LOGE("Invalid filter len %d", val);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700127 *status = BAD_VALUE;
128 return false;
129 }
130
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700131 const size_t num = val;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700132
133 filter->clear();
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700134 filter->setCapacity(num);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700135
nikobc726922009-07-20 15:07:26 -0700136 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700137
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700138
139 if (p.dataAvail() < size)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700140 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700141 LOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700142 *status = NOT_ENOUGH_DATA;
143 return false;
144 }
145
nikobc726922009-07-20 15:07:26 -0700146 const Metadata::Type *data =
147 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700148
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700149 if (NULL == data)
Nicolas Cataniab2c69392009-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 Catania4df8b2c2009-07-10 13:53:06 -0700159 for (size_t i = 0; i < num; ++i)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700160 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700161 filter->add(*data);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700162 ++data;
163 }
164 *status = OK;
165 return true;
166}
167
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700168// @param filter Of metadata type.
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700169// @param val To be searched.
170// @return true if a match was found.
nikobc726922009-07-20 15:07:26 -0700171bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700172{
173 // Deal with empty and ANY right away
174 if (filter.isEmpty()) return false;
nikobc726922009-07-20 15:07:26 -0700175 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700176
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700177 return filter.indexOf(val) >= 0;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700178}
179
180} // anonymous namespace
181
182
The Android Open Source Project9066cfe2009-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 Huber2cb5c9c2010-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 Project9066cfe2009-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 Project9066cfe2009-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 Wang608a2632009-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 Project9066cfe2009-03-03 19:31:44 -0800232 LOGV("Create new media recorder client from pid %d", pid);
233 return recorder;
234}
235
Gloria Wang608a2632009-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 Project9066cfe2009-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 Huber25643002010-01-28 11:19:57 -0800250sp<IMediaPlayer> MediaPlayerService::create(
251 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurent619346f2010-06-21 09:27:30 -0700252 const KeyedVector<String8, String8> *headers, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253{
254 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurent619346f2010-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 Huber25643002010-01-28 11:19:57 -0800258 if (NO_ERROR != c->setDataSource(url, headers))
The Android Open Source Project9066cfe2009-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 Laurent619346f2010-06-21 09:27:30 -0700270 int fd, int64_t offset, int64_t length, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271{
272 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurent619346f2010-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 Project9066cfe2009-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 Huber784202e2009-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 Hubere46b7be2009-07-14 16:56:47 -0700295}
296
The Android Open Source Project9066cfe2009-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 Laurent7070b362010-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 Project9066cfe2009-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 Huber27123462009-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 Project9066cfe2009-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 Huber27123462009-10-27 15:50:04 -0700410 MyString8 result;
The Android Open Source Project9066cfe2009-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 Dong3d23a612010-02-25 10:06:32 -0800431 snprintf(buffer, SIZE, " Total memory %i\n", totalMemory);
432 result.append(buffer);
The Android Open Source Project9066cfe2009-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 Dong3d23a612010-02-25 10:06:32 -0800481 snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups);
The Android Open Source Project9066cfe2009-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 Dong929642e2010-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 Wang608a2632009-10-29 15:46:37 -0700529 }
530
The Android Open Source Project9066cfe2009-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);
537 if (strstr(buffer, " /sdcard/") ||
538 strstr(buffer, " /system/sounds/") ||
539 strstr(buffer, " /system/media/")) {
540 result.append(" ");
541 result.append(buffer);
542 }
543 }
544 fclose(f);
545 } else {
546 result.append("couldn't open ");
547 result.append(buffer);
548 result.append("\n");
549 }
550
551 snprintf(buffer, SIZE, "/proc/%d/fd", myTid());
552 DIR *d = opendir(buffer);
553 if (d) {
554 struct dirent *ent;
555 while((ent = readdir(d)) != NULL) {
556 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
557 snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name);
558 struct stat s;
559 if (lstat(buffer, &s) == 0) {
560 if ((s.st_mode & S_IFMT) == S_IFLNK) {
561 char linkto[256];
562 int len = readlink(buffer, linkto, sizeof(linkto));
563 if(len > 0) {
564 if(len > 255) {
565 linkto[252] = '.';
566 linkto[253] = '.';
567 linkto[254] = '.';
568 linkto[255] = 0;
569 } else {
570 linkto[len] = 0;
571 }
572 if (strstr(linkto, "/sdcard/") == linkto ||
573 strstr(linkto, "/system/sounds/") == linkto ||
574 strstr(linkto, "/system/media/") == linkto) {
575 result.append(" ");
576 result.append(buffer);
577 result.append(" -> ");
578 result.append(linkto);
579 result.append("\n");
580 }
581 }
582 } else {
583 result.append(" unexpected type for ");
584 result.append(buffer);
585 result.append("\n");
586 }
587 }
588 }
589 }
590 closedir(d);
591 } else {
592 result.append("couldn't open ");
593 result.append(buffer);
594 result.append("\n");
595 }
596
597#if defined(__arm__)
598 bool dumpMem = false;
599 for (size_t i = 0; i < args.size(); i++) {
600 if (args[i] == String16("-m")) {
601 dumpMem = true;
602 }
603 }
604 if (dumpMem) {
605 memStatus(fd, args);
606 }
607#endif
608 }
609 write(fd, result.string(), result.size());
610 return NO_ERROR;
611}
612
613void MediaPlayerService::removeClient(wp<Client> client)
614{
615 Mutex::Autolock lock(mLock);
616 mClients.remove(client);
617}
618
619MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid,
Eric Laurent619346f2010-06-21 09:27:30 -0700620 int32_t connId, const sp<IMediaPlayerClient>& client, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621{
622 LOGV("Client(%d) constructor", connId);
623 mPid = pid;
624 mConnId = connId;
625 mService = service;
626 mClient = client;
627 mLoop = false;
628 mStatus = NO_INIT;
Eric Laurent619346f2010-06-21 09:27:30 -0700629 mAudioSessionId = audioSessionId;
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631#if CALLBACK_ANTAGONIZER
632 LOGD("create Antagonizer");
633 mAntagonizer = new Antagonizer(notify, this);
634#endif
635}
636
637MediaPlayerService::Client::~Client()
638{
639 LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
640 mAudioOutput.clear();
641 wp<Client> client(this);
642 disconnect();
643 mService->removeClient(client);
644}
645
646void MediaPlayerService::Client::disconnect()
647{
648 LOGV("disconnect(%d) from pid %d", mConnId, mPid);
649 // grab local reference and clear main reference to prevent future
650 // access to object
651 sp<MediaPlayerBase> p;
652 {
653 Mutex::Autolock l(mLock);
654 p = mPlayer;
655 }
Dave Sparkscb9a44e2009-03-24 17:57:12 -0700656 mClient.clear();
Andreas Hubere46b7be2009-07-14 16:56:47 -0700657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 mPlayer.clear();
659
660 // clear the notification to prevent callbacks to dead client
661 // and reset the player. We assume the player will serialize
662 // access to itself if necessary.
663 if (p != 0) {
664 p->setNotifyCallback(0, 0);
665#if CALLBACK_ANTAGONIZER
666 LOGD("kill Antagonizer");
667 mAntagonizer->kill();
668#endif
669 p->reset();
670 }
671
672 IPCThreadState::self()->flushCommands();
673}
674
Andreas Huber0d596d42009-08-07 09:30:32 -0700675static player_type getDefaultPlayerType() {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700676 return STAGEFRIGHT_PLAYER;
Andreas Huber0d596d42009-08-07 09:30:32 -0700677}
678
James Dong392ff3b2009-09-06 14:29:45 -0700679player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680{
681 char buf[20];
682 lseek(fd, offset, SEEK_SET);
683 read(fd, buf, sizeof(buf));
684 lseek(fd, offset, SEEK_SET);
685
686 long ident = *((long*)buf);
687
688 // Ogg vorbis?
689 if (ident == 0x5367674f) // 'OggS'
Andreas Huber8d65dd22010-06-23 16:40:57 -0700690 return STAGEFRIGHT_PLAYER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
Andreas Huber2cb5c9c2010-01-20 16:11:15 -0800692#ifndef NO_OPENCORE
693 if (ident == 0x75b22630) {
694 // The magic number for .asf files, i.e. wmv and wma content.
695 // These are not currently supported through stagefright.
696 return PV_PLAYER;
697 }
698#endif
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 // Some kind of MIDI?
701 EAS_DATA_HANDLE easdata;
702 if (EAS_Init(&easdata) == EAS_SUCCESS) {
703 EAS_FILE locator;
704 locator.path = NULL;
705 locator.fd = fd;
706 locator.offset = offset;
707 locator.length = length;
708 EAS_HANDLE eashandle;
709 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
710 EAS_CloseFile(easdata, eashandle);
711 EAS_Shutdown(easdata);
712 return SONIVOX_PLAYER;
713 }
714 EAS_Shutdown(easdata);
715 }
716
Andreas Huber0d596d42009-08-07 09:30:32 -0700717 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718}
719
James Dong392ff3b2009-09-06 14:29:45 -0700720player_type getPlayerType(const char* url)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721{
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700722 if (TestPlayerStub::canBeUsed(url)) {
723 return TEST_PLAYER;
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 // use MidiFile for MIDI extensions
727 int lenURL = strlen(url);
728 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
729 int len = strlen(FILE_EXTS[i].extension);
730 int start = lenURL - len;
731 if (start > 0) {
Atsushi Enoebcc51d2010-03-19 23:18:02 +0900732 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700733 return FILE_EXTS[i].playertype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735 }
736 }
737
James Dong42d66572010-04-13 21:33:26 -0700738 if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber7a747b82010-06-07 15:19:40 -0700739 char value[PROPERTY_VALUE_MAX];
740 if (!property_get("media.stagefright.enable-rtsp", value, NULL)
741 || (strcmp(value, "1") && strcasecmp(value, "true"))) {
742 // For now, we're going to use PV for rtsp-based playback
743 // by default until we can clear up a few more issues.
744 return PV_PLAYER;
745 }
James Dong42d66572010-04-13 21:33:26 -0700746 }
747
Andreas Huber0d596d42009-08-07 09:30:32 -0700748 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
751static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
752 notify_callback_f notifyFunc)
753{
754 sp<MediaPlayerBase> p;
755 switch (playerType) {
Jean-Baptiste Queru680f8c72009-03-21 11:40:18 -0700756#ifndef NO_OPENCORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 case PV_PLAYER:
758 LOGV(" create PVPlayer");
759 p = new PVPlayer();
760 break;
Jean-Baptiste Queru680f8c72009-03-21 11:40:18 -0700761#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 case SONIVOX_PLAYER:
763 LOGV(" create MidiFile");
764 p = new MidiFile();
765 break;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700766 case STAGEFRIGHT_PLAYER:
767 LOGV(" create StagefrightPlayer");
768 p = new StagefrightPlayer;
769 break;
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700770 case TEST_PLAYER:
771 LOGV("Create Test Player stub");
772 p = new TestPlayerStub();
773 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 if (p != NULL) {
776 if (p->initCheck() == NO_ERROR) {
777 p->setNotifyCallback(cookie, notifyFunc);
778 } else {
779 p.clear();
780 }
781 }
782 if (p == NULL) {
783 LOGE("Failed to create player object");
784 }
785 return p;
786}
787
788sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
789{
790 // determine if we have the right player type
791 sp<MediaPlayerBase> p = mPlayer;
792 if ((p != NULL) && (p->playerType() != playerType)) {
793 LOGV("delete player");
794 p.clear();
795 }
796 if (p == NULL) {
797 p = android::createPlayer(playerType, this, notify);
798 }
799 return p;
800}
801
Andreas Huber25643002010-01-28 11:19:57 -0800802status_t MediaPlayerService::Client::setDataSource(
803 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804{
805 LOGV("setDataSource(%s)", url);
806 if (url == NULL)
807 return UNKNOWN_ERROR;
808
809 if (strncmp(url, "content://", 10) == 0) {
810 // get a filedescriptor for the content Uri and
811 // pass it to the setDataSource(fd) method
812
813 String16 url16(url);
814 int fd = android::openContentProviderFile(url16);
815 if (fd < 0)
816 {
817 LOGE("Couldn't open fd for %s", url);
818 return UNKNOWN_ERROR;
819 }
820 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
821 close(fd);
822 return mStatus;
823 } else {
824 player_type playerType = getPlayerType(url);
825 LOGV("player type = %d", playerType);
826
827 // create the right type of player
828 sp<MediaPlayerBase> p = createPlayer(playerType);
829 if (p == NULL) return NO_INIT;
830
831 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700832 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
834 }
835
836 // now set data source
837 LOGV(" setDataSource");
Andreas Huber25643002010-01-28 11:19:57 -0800838 mStatus = p->setDataSource(url, headers);
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700839 if (mStatus == NO_ERROR) {
840 mPlayer = p;
841 } else {
842 LOGE(" error: %d", mStatus);
843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 return mStatus;
845 }
846}
847
848status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
849{
850 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
851 struct stat sb;
852 int ret = fstat(fd, &sb);
853 if (ret != 0) {
854 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
855 return UNKNOWN_ERROR;
856 }
857
858 LOGV("st_dev = %llu", sb.st_dev);
859 LOGV("st_mode = %u", sb.st_mode);
860 LOGV("st_uid = %lu", sb.st_uid);
861 LOGV("st_gid = %lu", sb.st_gid);
862 LOGV("st_size = %llu", sb.st_size);
863
864 if (offset >= sb.st_size) {
865 LOGE("offset error");
866 ::close(fd);
867 return UNKNOWN_ERROR;
868 }
869 if (offset + length > sb.st_size) {
870 length = sb.st_size - offset;
871 LOGV("calculated length = %lld", length);
872 }
873
874 player_type playerType = getPlayerType(fd, offset, length);
875 LOGV("player type = %d", playerType);
876
877 // create the right type of player
878 sp<MediaPlayerBase> p = createPlayer(playerType);
879 if (p == NULL) return NO_INIT;
880
881 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700882 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
884 }
885
886 // now set data source
887 mStatus = p->setDataSource(fd, offset, length);
888 if (mStatus == NO_ERROR) mPlayer = p;
889 return mStatus;
890}
891
892status_t MediaPlayerService::Client::setVideoSurface(const sp<ISurface>& surface)
893{
894 LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get());
895 sp<MediaPlayerBase> p = getPlayer();
896 if (p == 0) return UNKNOWN_ERROR;
897 return p->setVideoSurface(surface);
898}
899
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700900status_t MediaPlayerService::Client::invoke(const Parcel& request,
901 Parcel *reply)
902{
903 sp<MediaPlayerBase> p = getPlayer();
904 if (p == NULL) return UNKNOWN_ERROR;
905 return p->invoke(request, reply);
906}
907
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700908// This call doesn't need to access the native player.
909status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
910{
911 status_t status;
nikobc726922009-07-20 15:07:26 -0700912 media::Metadata::Filter allow, drop;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700913
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700914 if (unmarshallFilter(filter, &allow, &status) &&
915 unmarshallFilter(filter, &drop, &status)) {
916 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700917
918 mMetadataAllow = allow;
919 mMetadataDrop = drop;
920 }
921 return status;
922}
923
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700924status_t MediaPlayerService::Client::getMetadata(
925 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania5d55c712009-07-09 09:21:33 -0700926{
nikobc726922009-07-20 15:07:26 -0700927 sp<MediaPlayerBase> player = getPlayer();
928 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700929
niko89948372009-07-16 16:39:53 -0700930 status_t status;
931 // Placeholder for the return code, updated by the caller.
932 reply->writeInt32(-1);
933
nikobc726922009-07-20 15:07:26 -0700934 media::Metadata::Filter ids;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700935
936 // We don't block notifications while we fetch the data. We clear
937 // mMetadataUpdated first so we don't lose notifications happening
938 // during the rest of this call.
939 {
940 Mutex::Autolock lock(mLock);
941 if (update_only) {
niko89948372009-07-16 16:39:53 -0700942 ids = mMetadataUpdated;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700943 }
944 mMetadataUpdated.clear();
945 }
Nicolas Catania5d55c712009-07-09 09:21:33 -0700946
nikobc726922009-07-20 15:07:26 -0700947 media::Metadata metadata(reply);
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700948
nikobc726922009-07-20 15:07:26 -0700949 metadata.appendHeader();
950 status = player->getMetadata(ids, reply);
niko89948372009-07-16 16:39:53 -0700951
952 if (status != OK) {
nikobc726922009-07-20 15:07:26 -0700953 metadata.resetParcel();
niko89948372009-07-16 16:39:53 -0700954 LOGE("getMetadata failed %d", status);
955 return status;
956 }
957
958 // FIXME: Implement filtering on the result. Not critical since
959 // filtering takes place on the update notifications already. This
960 // would be when all the metadata are fetch and a filter is set.
961
niko89948372009-07-16 16:39:53 -0700962 // Everything is fine, update the metadata length.
nikobc726922009-07-20 15:07:26 -0700963 metadata.updateLength();
niko89948372009-07-16 16:39:53 -0700964 return OK;
Nicolas Catania5d55c712009-07-09 09:21:33 -0700965}
966
Andreas Huberfbb38852010-02-12 12:35:58 -0800967status_t MediaPlayerService::Client::suspend() {
968 sp<MediaPlayerBase> p = getPlayer();
969 if (p == 0) return UNKNOWN_ERROR;
970
971 return p->suspend();
972}
973
974status_t MediaPlayerService::Client::resume() {
975 sp<MediaPlayerBase> p = getPlayer();
976 if (p == 0) return UNKNOWN_ERROR;
977
978 return p->resume();
979}
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981status_t MediaPlayerService::Client::prepareAsync()
982{
983 LOGV("[%d] prepareAsync", mConnId);
984 sp<MediaPlayerBase> p = getPlayer();
985 if (p == 0) return UNKNOWN_ERROR;
986 status_t ret = p->prepareAsync();
987#if CALLBACK_ANTAGONIZER
988 LOGD("start Antagonizer");
989 if (ret == NO_ERROR) mAntagonizer->start();
990#endif
991 return ret;
992}
993
994status_t MediaPlayerService::Client::start()
995{
996 LOGV("[%d] start", mConnId);
997 sp<MediaPlayerBase> p = getPlayer();
998 if (p == 0) return UNKNOWN_ERROR;
999 p->setLooping(mLoop);
1000 return p->start();
1001}
1002
1003status_t MediaPlayerService::Client::stop()
1004{
1005 LOGV("[%d] stop", mConnId);
1006 sp<MediaPlayerBase> p = getPlayer();
1007 if (p == 0) return UNKNOWN_ERROR;
1008 return p->stop();
1009}
1010
1011status_t MediaPlayerService::Client::pause()
1012{
1013 LOGV("[%d] pause", mConnId);
1014 sp<MediaPlayerBase> p = getPlayer();
1015 if (p == 0) return UNKNOWN_ERROR;
1016 return p->pause();
1017}
1018
1019status_t MediaPlayerService::Client::isPlaying(bool* state)
1020{
1021 *state = false;
1022 sp<MediaPlayerBase> p = getPlayer();
1023 if (p == 0) return UNKNOWN_ERROR;
1024 *state = p->isPlaying();
1025 LOGV("[%d] isPlaying: %d", mConnId, *state);
1026 return NO_ERROR;
1027}
1028
1029status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1030{
1031 LOGV("getCurrentPosition");
1032 sp<MediaPlayerBase> p = getPlayer();
1033 if (p == 0) return UNKNOWN_ERROR;
1034 status_t ret = p->getCurrentPosition(msec);
1035 if (ret == NO_ERROR) {
1036 LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1037 } else {
1038 LOGE("getCurrentPosition returned %d", ret);
1039 }
1040 return ret;
1041}
1042
1043status_t MediaPlayerService::Client::getDuration(int *msec)
1044{
1045 LOGV("getDuration");
1046 sp<MediaPlayerBase> p = getPlayer();
1047 if (p == 0) return UNKNOWN_ERROR;
1048 status_t ret = p->getDuration(msec);
1049 if (ret == NO_ERROR) {
1050 LOGV("[%d] getDuration = %d", mConnId, *msec);
1051 } else {
1052 LOGE("getDuration returned %d", ret);
1053 }
1054 return ret;
1055}
1056
1057status_t MediaPlayerService::Client::seekTo(int msec)
1058{
1059 LOGV("[%d] seekTo(%d)", mConnId, msec);
1060 sp<MediaPlayerBase> p = getPlayer();
1061 if (p == 0) return UNKNOWN_ERROR;
1062 return p->seekTo(msec);
1063}
1064
1065status_t MediaPlayerService::Client::reset()
1066{
1067 LOGV("[%d] reset", mConnId);
1068 sp<MediaPlayerBase> p = getPlayer();
1069 if (p == 0) return UNKNOWN_ERROR;
1070 return p->reset();
1071}
1072
1073status_t MediaPlayerService::Client::setAudioStreamType(int type)
1074{
1075 LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1076 // TODO: for hardware output, call player instead
1077 Mutex::Autolock l(mLock);
1078 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1079 return NO_ERROR;
1080}
1081
1082status_t MediaPlayerService::Client::setLooping(int loop)
1083{
1084 LOGV("[%d] setLooping(%d)", mConnId, loop);
1085 mLoop = loop;
1086 sp<MediaPlayerBase> p = getPlayer();
1087 if (p != 0) return p->setLooping(loop);
1088 return NO_ERROR;
1089}
1090
1091status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1092{
1093 LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1094 // TODO: for hardware output, call player instead
1095 Mutex::Autolock l(mLock);
1096 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1097 return NO_ERROR;
1098}
1099
Eric Laurent7070b362010-07-16 07:43:46 -07001100status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1101{
1102 LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1103 Mutex::Autolock l(mLock);
1104 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1105 return NO_ERROR;
1106}
1107
1108status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1109{
1110 LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1111 Mutex::Autolock l(mLock);
1112 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1113 return NO_ERROR;
1114}
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2)
1117{
1118 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001119
1120 if (MEDIA_INFO == msg &&
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001121 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikobc726922009-07-20 15:07:26 -07001122 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001123
1124 if(client->shouldDropMetadata(metadata_type)) {
1125 return;
1126 }
1127
1128 // Update the list of metadata that have changed. getMetadata
1129 // also access mMetadataUpdated and clears it.
1130 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1133 client->mClient->notify(msg, ext1, ext2);
1134}
1135
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001136
nikobc726922009-07-20 15:07:26 -07001137bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001138{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001139 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001140
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001141 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001142 return true;
1143 }
1144
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001145 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001146 return false;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001147 } else {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001148 return true;
1149 }
1150}
1151
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001152
nikobc726922009-07-20 15:07:26 -07001153void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001154 Mutex::Autolock lock(mLock);
1155 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1156 mMetadataUpdated.add(metadata_type);
1157 }
1158}
1159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160#if CALLBACK_ANTAGONIZER
1161const int Antagonizer::interval = 10000; // 10 msecs
1162
1163Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1164 mExit(false), mActive(false), mClient(client), mCb(cb)
1165{
1166 createThread(callbackThread, this);
1167}
1168
1169void Antagonizer::kill()
1170{
1171 Mutex::Autolock _l(mLock);
1172 mActive = false;
1173 mExit = true;
1174 mCondition.wait(mLock);
1175}
1176
1177int Antagonizer::callbackThread(void* user)
1178{
1179 LOGD("Antagonizer started");
1180 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1181 while (!p->mExit) {
1182 if (p->mActive) {
1183 LOGV("send event");
1184 p->mCb(p->mClient, 0, 0, 0);
1185 }
1186 usleep(interval);
1187 }
1188 Mutex::Autolock _l(p->mLock);
1189 p->mCondition.signal();
1190 LOGD("Antagonizer stopped");
1191 return 0;
1192}
1193#endif
1194
1195static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1196
1197sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1198{
1199 LOGV("decode(%s)", url);
1200 sp<MemoryBase> mem;
1201 sp<MediaPlayerBase> player;
1202
1203 // Protect our precious, precious DRMd ringtones by only allowing
1204 // decoding of http, but not filesystem paths or content Uris.
1205 // If the application wants to decode those, it should open a
1206 // filedescriptor for them and use that.
1207 if (url != NULL && strncmp(url, "http://", 7) != 0) {
1208 LOGD("Can't decode %s by path, use filedescriptor instead", url);
1209 return mem;
1210 }
1211
1212 player_type playerType = getPlayerType(url);
1213 LOGV("player type = %d", playerType);
1214
1215 // create the right type of player
1216 sp<AudioCache> cache = new AudioCache(url);
1217 player = android::createPlayer(playerType, cache.get(), cache->notify);
1218 if (player == NULL) goto Exit;
1219 if (player->hardwareOutput()) goto Exit;
1220
1221 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1222
1223 // set data source
1224 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1225
1226 LOGV("prepare");
1227 player->prepareAsync();
1228
1229 LOGV("wait for prepare");
1230 if (cache->wait() != NO_ERROR) goto Exit;
1231
1232 LOGV("start");
1233 player->start();
1234
1235 LOGV("wait for playback complete");
1236 if (cache->wait() != NO_ERROR) goto Exit;
1237
1238 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1239 *pSampleRate = cache->sampleRate();
1240 *pNumChannels = cache->channelCount();
1241 *pFormat = cache->format();
1242 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1243
1244Exit:
1245 if (player != 0) player->reset();
1246 return mem;
1247}
1248
1249sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1250{
1251 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1252 sp<MemoryBase> mem;
1253 sp<MediaPlayerBase> player;
1254
1255 player_type playerType = getPlayerType(fd, offset, length);
1256 LOGV("player type = %d", playerType);
1257
1258 // create the right type of player
1259 sp<AudioCache> cache = new AudioCache("decode_fd");
1260 player = android::createPlayer(playerType, cache.get(), cache->notify);
1261 if (player == NULL) goto Exit;
1262 if (player->hardwareOutput()) goto Exit;
1263
1264 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1265
1266 // set data source
1267 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1268
1269 LOGV("prepare");
1270 player->prepareAsync();
1271
1272 LOGV("wait for prepare");
1273 if (cache->wait() != NO_ERROR) goto Exit;
1274
1275 LOGV("start");
1276 player->start();
1277
1278 LOGV("wait for playback complete");
1279 if (cache->wait() != NO_ERROR) goto Exit;
1280
1281 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1282 *pSampleRate = cache->sampleRate();
1283 *pNumChannels = cache->channelCount();
1284 *pFormat = cache->format();
1285 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1286
1287Exit:
1288 if (player != 0) player->reset();
1289 ::close(fd);
1290 return mem;
1291}
1292
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294#undef LOG_TAG
1295#define LOG_TAG "AudioSink"
Eric Laurent619346f2010-06-21 09:27:30 -07001296MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Hubere46b7be2009-07-14 16:56:47 -07001297 : mCallback(NULL),
Eric Laurent619346f2010-06-21 09:27:30 -07001298 mCallbackCookie(NULL),
1299 mSessionId(sessionId) {
1300 LOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 mTrack = 0;
1302 mStreamType = AudioSystem::MUSIC;
1303 mLeftVolume = 1.0;
1304 mRightVolume = 1.0;
1305 mLatency = 0;
1306 mMsecsPerFrame = 0;
Eric Laurent7070b362010-07-16 07:43:46 -07001307 mAuxEffectId = 0;
1308 mSendLevel = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 setMinBufferCount();
1310}
1311
1312MediaPlayerService::AudioOutput::~AudioOutput()
1313{
1314 close();
1315}
1316
1317void MediaPlayerService::AudioOutput::setMinBufferCount()
1318{
1319 char value[PROPERTY_VALUE_MAX];
1320 if (property_get("ro.kernel.qemu", value, 0)) {
1321 mIsOnEmulator = true;
1322 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1323 }
1324}
1325
1326bool MediaPlayerService::AudioOutput::isOnEmulator()
1327{
1328 setMinBufferCount();
1329 return mIsOnEmulator;
1330}
1331
1332int MediaPlayerService::AudioOutput::getMinBufferCount()
1333{
1334 setMinBufferCount();
1335 return mMinBufferCount;
1336}
1337
1338ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1339{
1340 if (mTrack == 0) return NO_INIT;
1341 return mTrack->frameCount() * frameSize();
1342}
1343
1344ssize_t MediaPlayerService::AudioOutput::frameCount() const
1345{
1346 if (mTrack == 0) return NO_INIT;
1347 return mTrack->frameCount();
1348}
1349
1350ssize_t MediaPlayerService::AudioOutput::channelCount() const
1351{
1352 if (mTrack == 0) return NO_INIT;
1353 return mTrack->channelCount();
1354}
1355
1356ssize_t MediaPlayerService::AudioOutput::frameSize() const
1357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->frameSize();
1360}
1361
1362uint32_t MediaPlayerService::AudioOutput::latency () const
1363{
1364 return mLatency;
1365}
1366
1367float MediaPlayerService::AudioOutput::msecsPerFrame() const
1368{
1369 return mMsecsPerFrame;
1370}
1371
Eric Laurent0986e792010-01-19 17:37:09 -08001372status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1373{
1374 if (mTrack == 0) return NO_INIT;
1375 return mTrack->getPosition(position);
1376}
1377
Andreas Hubere46b7be2009-07-14 16:56:47 -07001378status_t MediaPlayerService::AudioOutput::open(
1379 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1380 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001382 mCallback = cb;
1383 mCallbackCookie = cookie;
1384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 // Check argument "bufferCount" against the mininum buffer count
1386 if (bufferCount < mMinBufferCount) {
1387 LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1388 bufferCount = mMinBufferCount;
1389
1390 }
Eric Laurent619346f2010-06-21 09:27:30 -07001391 LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 if (mTrack) close();
1393 int afSampleRate;
1394 int afFrameCount;
1395 int frameCount;
1396
1397 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1398 return NO_INIT;
1399 }
1400 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1401 return NO_INIT;
1402 }
1403
1404 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Hubere46b7be2009-07-14 16:56:47 -07001405
1406 AudioTrack *t;
1407 if (mCallback != NULL) {
1408 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001409 mStreamType,
1410 sampleRate,
1411 format,
1412 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1413 frameCount,
1414 0 /* flags */,
1415 CallbackWrapper,
Eric Laurent619346f2010-06-21 09:27:30 -07001416 this,
1417 0,
1418 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001419 } else {
1420 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001421 mStreamType,
1422 sampleRate,
1423 format,
1424 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
Eric Laurent619346f2010-06-21 09:27:30 -07001425 frameCount,
1426 0,
1427 NULL,
1428 NULL,
1429 0,
1430 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001431 }
1432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1434 LOGE("Unable to create audio track");
1435 delete t;
1436 return NO_INIT;
1437 }
1438
1439 LOGV("setVolume");
1440 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Dave Sparksb904c2a2009-12-03 10:13:32 -08001443 mLatency = t->latency();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mTrack = t;
Eric Laurent7070b362010-07-16 07:43:46 -07001445
1446 t->setAuxEffectSendLevel(mSendLevel);
1447 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448}
1449
1450void MediaPlayerService::AudioOutput::start()
1451{
1452 LOGV("start");
1453 if (mTrack) {
1454 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001455 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 mTrack->start();
1457 }
1458}
1459
Marco Nelissene274db12010-01-12 09:23:54 -08001460
1461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1463{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001464 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 //LOGV("write(%p, %u)", buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001467 if (mTrack) {
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001468 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001469 return ret;
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 return NO_INIT;
1472}
1473
1474void MediaPlayerService::AudioOutput::stop()
1475{
1476 LOGV("stop");
1477 if (mTrack) mTrack->stop();
1478}
1479
1480void MediaPlayerService::AudioOutput::flush()
1481{
1482 LOGV("flush");
1483 if (mTrack) mTrack->flush();
1484}
1485
1486void MediaPlayerService::AudioOutput::pause()
1487{
1488 LOGV("pause");
1489 if (mTrack) mTrack->pause();
1490}
1491
1492void MediaPlayerService::AudioOutput::close()
1493{
1494 LOGV("close");
1495 delete mTrack;
1496 mTrack = 0;
1497}
1498
1499void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1500{
1501 LOGV("setVolume(%f, %f)", left, right);
1502 mLeftVolume = left;
1503 mRightVolume = right;
1504 if (mTrack) {
1505 mTrack->setVolume(left, right);
1506 }
1507}
1508
Eric Laurent7070b362010-07-16 07:43:46 -07001509status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1510{
1511 LOGV("setAuxEffectSendLevel(%f)", level);
1512 mSendLevel = level;
1513 if (mTrack) {
1514 return mTrack->setAuxEffectSendLevel(level);
1515 }
1516 return NO_ERROR;
1517}
1518
1519status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1520{
1521 LOGV("attachAuxEffect(%d)", effectId);
1522 mAuxEffectId = effectId;
1523 if (mTrack) {
1524 return mTrack->attachAuxEffect(effectId);
1525 }
1526 return NO_ERROR;
1527}
1528
Andreas Hubere46b7be2009-07-14 16:56:47 -07001529// static
1530void MediaPlayerService::AudioOutput::CallbackWrapper(
1531 int event, void *cookie, void *info) {
Marco Nelissene274db12010-01-12 09:23:54 -08001532 //LOGV("callbackwrapper");
Andreas Hubere46b7be2009-07-14 16:56:47 -07001533 if (event != AudioTrack::EVENT_MORE_DATA) {
1534 return;
1535 }
1536
1537 AudioOutput *me = (AudioOutput *)cookie;
1538 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1539
Andreas Huber6ed937e2010-02-09 16:59:18 -08001540 size_t actualSize = (*me->mCallback)(
Andreas Hubere46b7be2009-07-14 16:56:47 -07001541 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber6ed937e2010-02-09 16:59:18 -08001542
Andreas Huber406a18b2010-02-18 16:45:13 -08001543 buffer->size = actualSize;
1544
Andreas Hubere46b7be2009-07-14 16:56:47 -07001545}
1546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547#undef LOG_TAG
1548#define LOG_TAG "AudioCache"
1549MediaPlayerService::AudioCache::AudioCache(const char* name) :
1550 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1551 mError(NO_ERROR), mCommandComplete(false)
1552{
1553 // create ashmem heap
1554 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1555}
1556
1557uint32_t MediaPlayerService::AudioCache::latency () const
1558{
1559 return 0;
1560}
1561
1562float MediaPlayerService::AudioCache::msecsPerFrame() const
1563{
1564 return mMsecsPerFrame;
1565}
1566
Eric Laurent0986e792010-01-19 17:37:09 -08001567status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1568{
1569 if (position == 0) return BAD_VALUE;
1570 *position = mSize;
1571 return NO_ERROR;
1572}
1573
Andreas Huber6ed937e2010-02-09 16:59:18 -08001574////////////////////////////////////////////////////////////////////////////////
1575
1576struct CallbackThread : public Thread {
1577 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1578 MediaPlayerBase::AudioSink::AudioCallback cb,
1579 void *cookie);
1580
1581protected:
1582 virtual ~CallbackThread();
1583
1584 virtual bool threadLoop();
1585
1586private:
1587 wp<MediaPlayerBase::AudioSink> mSink;
1588 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1589 void *mCookie;
1590 void *mBuffer;
1591 size_t mBufferSize;
1592
1593 CallbackThread(const CallbackThread &);
1594 CallbackThread &operator=(const CallbackThread &);
1595};
1596
1597CallbackThread::CallbackThread(
1598 const wp<MediaPlayerBase::AudioSink> &sink,
1599 MediaPlayerBase::AudioSink::AudioCallback cb,
1600 void *cookie)
1601 : mSink(sink),
1602 mCallback(cb),
1603 mCookie(cookie),
1604 mBuffer(NULL),
1605 mBufferSize(0) {
1606}
1607
1608CallbackThread::~CallbackThread() {
1609 if (mBuffer) {
1610 free(mBuffer);
1611 mBuffer = NULL;
1612 }
1613}
1614
1615bool CallbackThread::threadLoop() {
1616 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1617 if (sink == NULL) {
1618 return false;
1619 }
1620
1621 if (mBuffer == NULL) {
1622 mBufferSize = sink->bufferSize();
1623 mBuffer = malloc(mBufferSize);
1624 }
1625
1626 size_t actualSize =
1627 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1628
1629 if (actualSize > 0) {
1630 sink->write(mBuffer, actualSize);
1631 }
1632
1633 return true;
1634}
1635
1636////////////////////////////////////////////////////////////////////////////////
1637
Andreas Hubere46b7be2009-07-14 16:56:47 -07001638status_t MediaPlayerService::AudioCache::open(
1639 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1640 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641{
Dave Sparks14f89402009-12-09 20:20:26 -08001642 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks14f89402009-12-09 20:20:26 -08001643 if (mHeap->getHeapID() < 0) {
1644 return NO_INIT;
1645 }
Andreas Hubere46b7be2009-07-14 16:56:47 -07001646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 mSampleRate = sampleRate;
1648 mChannelCount = (uint16_t)channelCount;
1649 mFormat = (uint16_t)format;
1650 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber6ed937e2010-02-09 16:59:18 -08001651
1652 if (cb != NULL) {
1653 mCallbackThread = new CallbackThread(this, cb, cookie);
1654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 return NO_ERROR;
1656}
1657
Andreas Huber6ed937e2010-02-09 16:59:18 -08001658void MediaPlayerService::AudioCache::start() {
1659 if (mCallbackThread != NULL) {
1660 mCallbackThread->run("AudioCache callback");
1661 }
1662}
1663
1664void MediaPlayerService::AudioCache::stop() {
1665 if (mCallbackThread != NULL) {
1666 mCallbackThread->requestExitAndWait();
1667 }
1668}
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1671{
1672 LOGV("write(%p, %u)", buffer, size);
1673 if ((buffer == 0) || (size == 0)) return size;
1674
1675 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1676 if (p == NULL) return NO_INIT;
1677 p += mSize;
1678 LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1679 if (mSize + size > mHeap->getSize()) {
1680 LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1681 size = mHeap->getSize() - mSize;
1682 }
1683 memcpy(p, buffer, size);
1684 mSize += size;
1685 return size;
1686}
1687
1688// call with lock held
1689status_t MediaPlayerService::AudioCache::wait()
1690{
1691 Mutex::Autolock lock(mLock);
Dave Sparks16433e22010-03-01 19:29:58 -08001692 while (!mCommandComplete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 mSignal.wait(mLock);
1694 }
1695 mCommandComplete = false;
1696
1697 if (mError == NO_ERROR) {
1698 LOGV("wait - success");
1699 } else {
1700 LOGV("wait - error");
1701 }
1702 return mError;
1703}
1704
1705void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2)
1706{
1707 LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1708 AudioCache* p = static_cast<AudioCache*>(cookie);
1709
1710 // ignore buffering messages
Dave Sparks14f89402009-12-09 20:20:26 -08001711 switch (msg)
1712 {
1713 case MEDIA_ERROR:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 LOGE("Error %d, %d occurred", ext1, ext2);
1715 p->mError = ext1;
Dave Sparks14f89402009-12-09 20:20:26 -08001716 break;
1717 case MEDIA_PREPARED:
1718 LOGV("prepared");
1719 break;
1720 case MEDIA_PLAYBACK_COMPLETE:
1721 LOGV("playback complete");
1722 break;
1723 default:
1724 LOGV("ignored");
1725 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727
1728 // wake up thread
Dave Sparks6c26fe42010-03-02 12:56:37 -08001729 Mutex::Autolock lock(p->mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 p->mCommandComplete = true;
1731 p->mSignal.signal();
1732}
1733
nikobc726922009-07-20 15:07:26 -07001734} // namespace android