blob: 60bdd6233b918b3d030b40bfc28ef7fe074c79d1 [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"
Nicolas Catania14d27472009-07-13 14:37:49 -070059#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070060#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080061#include "nuplayer/NuPlayerDriver.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},
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199};
200
201// 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 -0800202/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
203/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
204
205void MediaPlayerService::instantiate() {
206 defaultServiceManager()->addService(
207 String16("media.player"), new MediaPlayerService());
208}
209
210MediaPlayerService::MediaPlayerService()
211{
212 LOGV("MediaPlayerService created");
213 mNextConnId = 1;
214}
215
216MediaPlayerService::~MediaPlayerService()
217{
218 LOGV("MediaPlayerService destroyed");
219}
220
221sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
222{
Gloria Wangdac6a312009-10-29 15:46:37 -0700223 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
224 wp<MediaRecorderClient> w = recorder;
225 Mutex::Autolock lock(mLock);
226 mMediaRecorderClients.add(w);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 LOGV("Create new media recorder client from pid %d", pid);
228 return recorder;
229}
230
Gloria Wangdac6a312009-10-29 15:46:37 -0700231void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
232{
233 Mutex::Autolock lock(mLock);
234 mMediaRecorderClients.remove(client);
235 LOGV("Delete media recorder client");
236}
237
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
239{
240 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
241 LOGV("Create new media retriever from pid %d", pid);
242 return retriever;
243}
244
Andreas Huber2db84552010-01-28 11:19:57 -0800245sp<IMediaPlayer> MediaPlayerService::create(
246 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700247 const KeyedVector<String8, String8> *headers, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248{
249 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700250 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
251 LOGV("Create new client(%d) from pid %d, url=%s, connId=%d, audioSessionId=%d",
252 connId, pid, url, connId, audioSessionId);
Andreas Huber2db84552010-01-28 11:19:57 -0800253 if (NO_ERROR != c->setDataSource(url, headers))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 {
255 c.clear();
256 return c;
257 }
258 wp<Client> w = c;
259 Mutex::Autolock lock(mLock);
260 mClients.add(w);
261 return c;
262}
263
264sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700265 int fd, int64_t offset, int64_t length, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266{
267 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700268 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
269 LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld, audioSessionId=%d",
270 connId, pid, fd, offset, length, audioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 if (NO_ERROR != c->setDataSource(fd, offset, length)) {
272 c.clear();
273 } else {
274 wp<Client> w = c;
275 Mutex::Autolock lock(mLock);
276 mClients.add(w);
277 }
278 ::close(fd);
279 return c;
280}
281
Andreas Hubere2b10282010-11-23 11:41:34 -0800282sp<IMediaPlayer> MediaPlayerService::create(
283 pid_t pid, const sp<IMediaPlayerClient> &client,
284 const sp<IStreamSource> &source, int audioSessionId) {
285 int32_t connId = android_atomic_inc(&mNextConnId);
286 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
287
288 LOGV("Create new client(%d) from pid %d, audioSessionId=%d",
289 connId, pid, audioSessionId);
290
291 if (OK != c->setDataSource(source)) {
292 c.clear();
293 } else {
294 wp<Client> w = c;
295 Mutex::Autolock lock(mLock);
296 mClients.add(w);
297 }
298
299 return c;
300}
301
Andreas Huber318ad9c2009-10-15 13:46:54 -0700302sp<IOMX> MediaPlayerService::getOMX() {
303 Mutex::Autolock autoLock(mLock);
304
305 if (mOMX.get() == NULL) {
306 mOMX = new OMX;
307 }
308
309 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700310}
311
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
313{
314 const size_t SIZE = 256;
315 char buffer[SIZE];
316 String8 result;
317
318 result.append(" AudioCache\n");
319 if (mHeap != 0) {
320 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
321 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
322 result.append(buffer);
323 }
324 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
325 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
326 result.append(buffer);
327 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
328 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
329 result.append(buffer);
330 ::write(fd, result.string(), result.size());
331 return NO_ERROR;
332}
333
334status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
335{
336 const size_t SIZE = 256;
337 char buffer[SIZE];
338 String8 result;
339
340 result.append(" AudioOutput\n");
341 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
342 mStreamType, mLeftVolume, mRightVolume);
343 result.append(buffer);
344 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
345 mMsecsPerFrame, mLatency);
346 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700347 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
348 mAuxEffectId, mSendLevel);
349 result.append(buffer);
350
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 ::write(fd, result.string(), result.size());
352 if (mTrack != 0) {
353 mTrack->dump(fd, args);
354 }
355 return NO_ERROR;
356}
357
358status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
359{
360 const size_t SIZE = 256;
361 char buffer[SIZE];
362 String8 result;
363 result.append(" Client\n");
364 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
365 mPid, mConnId, mStatus, mLoop?"true": "false");
366 result.append(buffer);
367 write(fd, result.string(), result.size());
368 if (mAudioOutput != 0) {
369 mAudioOutput->dump(fd, args);
370 }
371 write(fd, "\n", 1);
372 return NO_ERROR;
373}
374
375static int myTid() {
376#ifdef HAVE_GETTID
377 return gettid();
378#else
379 return getpid();
380#endif
381}
382
383#if defined(__arm__)
384extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
385 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
386extern "C" void free_malloc_leak_info(uint8_t* info);
387
Andreas Huber1eea7f52009-10-27 15:50:04 -0700388// Use the String-class below instead of String8 to allocate all memory
389// beforehand and not reenter the heap while we are examining it...
390struct MyString8 {
391 static const size_t MAX_SIZE = 256 * 1024;
392
393 MyString8()
394 : mPtr((char *)malloc(MAX_SIZE)) {
395 *mPtr = '\0';
396 }
397
398 ~MyString8() {
399 free(mPtr);
400 }
401
402 void append(const char *s) {
403 strcat(mPtr, s);
404 }
405
406 const char *string() const {
407 return mPtr;
408 }
409
410 size_t size() const {
411 return strlen(mPtr);
412 }
413
414private:
415 char *mPtr;
416
417 MyString8(const MyString8 &);
418 MyString8 &operator=(const MyString8 &);
419};
420
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421void memStatus(int fd, const Vector<String16>& args)
422{
423 const size_t SIZE = 256;
424 char buffer[SIZE];
Andreas Huber1eea7f52009-10-27 15:50:04 -0700425 MyString8 result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426
427 typedef struct {
428 size_t size;
429 size_t dups;
430 intptr_t * backtrace;
431 } AllocEntry;
432
433 uint8_t *info = NULL;
434 size_t overallSize = 0;
435 size_t infoSize = 0;
436 size_t totalMemory = 0;
437 size_t backtraceSize = 0;
438
439 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
440 if (info) {
441 uint8_t *ptr = info;
442 size_t count = overallSize / infoSize;
443
444 snprintf(buffer, SIZE, " Allocation count %i\n", count);
445 result.append(buffer);
James Dong1edee162010-02-25 10:06:32 -0800446 snprintf(buffer, SIZE, " Total memory %i\n", totalMemory);
447 result.append(buffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448
449 AllocEntry * entries = new AllocEntry[count];
450
451 for (size_t i = 0; i < count; i++) {
452 // Each entry should be size_t, size_t, intptr_t[backtraceSize]
453 AllocEntry *e = &entries[i];
454
455 e->size = *reinterpret_cast<size_t *>(ptr);
456 ptr += sizeof(size_t);
457
458 e->dups = *reinterpret_cast<size_t *>(ptr);
459 ptr += sizeof(size_t);
460
461 e->backtrace = reinterpret_cast<intptr_t *>(ptr);
462 ptr += sizeof(intptr_t) * backtraceSize;
463 }
464
465 // Now we need to sort the entries. They come sorted by size but
466 // not by stack trace which causes problems using diff.
467 bool moved;
468 do {
469 moved = false;
470 for (size_t i = 0; i < (count - 1); i++) {
471 AllocEntry *e1 = &entries[i];
472 AllocEntry *e2 = &entries[i+1];
473
474 bool swap = e1->size < e2->size;
475 if (e1->size == e2->size) {
476 for(size_t j = 0; j < backtraceSize; j++) {
477 if (e1->backtrace[j] == e2->backtrace[j]) {
478 continue;
479 }
480 swap = e1->backtrace[j] < e2->backtrace[j];
481 break;
482 }
483 }
484 if (swap) {
485 AllocEntry t = entries[i];
486 entries[i] = entries[i+1];
487 entries[i+1] = t;
488 moved = true;
489 }
490 }
491 } while (moved);
492
493 for (size_t i = 0; i < count; i++) {
494 AllocEntry *e = &entries[i];
495
James Dong1edee162010-02-25 10:06:32 -0800496 snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497 result.append(buffer);
498 for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) {
499 if (ct) {
500 result.append(", ");
501 }
502 snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]);
503 result.append(buffer);
504 }
505 result.append("\n");
506 }
507
508 delete[] entries;
509 free_malloc_leak_info(info);
510 }
511
512 write(fd, result.string(), result.size());
513}
514#endif
515
516status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
517{
518 const size_t SIZE = 256;
519 char buffer[SIZE];
520 String8 result;
521 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
522 snprintf(buffer, SIZE, "Permission Denial: "
523 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
524 IPCThreadState::self()->getCallingPid(),
525 IPCThreadState::self()->getCallingUid());
526 result.append(buffer);
527 } else {
528 Mutex::Autolock lock(mLock);
529 for (int i = 0, n = mClients.size(); i < n; ++i) {
530 sp<Client> c = mClients[i].promote();
531 if (c != 0) c->dump(fd, args);
532 }
James Dongb9141222010-07-08 11:16:11 -0700533 if (mMediaRecorderClients.size() == 0) {
534 result.append(" No media recorder client\n\n");
535 } else {
536 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
537 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
538 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
539 result.append(buffer);
540 write(fd, result.string(), result.size());
541 result = "\n";
542 c->dump(fd, args);
543 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700544 }
545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 result.append(" Files opened and/or mapped:\n");
547 snprintf(buffer, SIZE, "/proc/%d/maps", myTid());
548 FILE *f = fopen(buffer, "r");
549 if (f) {
550 while (!feof(f)) {
551 fgets(buffer, SIZE, f);
Dave Sparks02fa8342010-09-27 16:55:18 -0700552 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700554 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 strstr(buffer, " /system/media/")) {
556 result.append(" ");
557 result.append(buffer);
558 }
559 }
560 fclose(f);
561 } else {
562 result.append("couldn't open ");
563 result.append(buffer);
564 result.append("\n");
565 }
566
567 snprintf(buffer, SIZE, "/proc/%d/fd", myTid());
568 DIR *d = opendir(buffer);
569 if (d) {
570 struct dirent *ent;
571 while((ent = readdir(d)) != NULL) {
572 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
573 snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name);
574 struct stat s;
575 if (lstat(buffer, &s) == 0) {
576 if ((s.st_mode & S_IFMT) == S_IFLNK) {
577 char linkto[256];
578 int len = readlink(buffer, linkto, sizeof(linkto));
579 if(len > 0) {
580 if(len > 255) {
581 linkto[252] = '.';
582 linkto[253] = '.';
583 linkto[254] = '.';
584 linkto[255] = 0;
585 } else {
586 linkto[len] = 0;
587 }
Dave Sparks02fa8342010-09-27 16:55:18 -0700588 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700590 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 strstr(linkto, "/system/media/") == linkto) {
592 result.append(" ");
593 result.append(buffer);
594 result.append(" -> ");
595 result.append(linkto);
596 result.append("\n");
597 }
598 }
599 } else {
600 result.append(" unexpected type for ");
601 result.append(buffer);
602 result.append("\n");
603 }
604 }
605 }
606 }
607 closedir(d);
608 } else {
609 result.append("couldn't open ");
610 result.append(buffer);
611 result.append("\n");
612 }
613
614#if defined(__arm__)
615 bool dumpMem = false;
616 for (size_t i = 0; i < args.size(); i++) {
617 if (args[i] == String16("-m")) {
618 dumpMem = true;
619 }
620 }
621 if (dumpMem) {
622 memStatus(fd, args);
623 }
624#endif
625 }
626 write(fd, result.string(), result.size());
627 return NO_ERROR;
628}
629
630void MediaPlayerService::removeClient(wp<Client> client)
631{
632 Mutex::Autolock lock(mLock);
633 mClients.remove(client);
634}
635
636MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700637 int32_t connId, const sp<IMediaPlayerClient>& client, int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638{
639 LOGV("Client(%d) constructor", connId);
640 mPid = pid;
641 mConnId = connId;
642 mService = service;
643 mClient = client;
644 mLoop = false;
645 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700646 mAudioSessionId = audioSessionId;
647
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648#if CALLBACK_ANTAGONIZER
649 LOGD("create Antagonizer");
650 mAntagonizer = new Antagonizer(notify, this);
651#endif
652}
653
654MediaPlayerService::Client::~Client()
655{
656 LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
657 mAudioOutput.clear();
658 wp<Client> client(this);
659 disconnect();
660 mService->removeClient(client);
661}
662
663void MediaPlayerService::Client::disconnect()
664{
665 LOGV("disconnect(%d) from pid %d", mConnId, mPid);
666 // grab local reference and clear main reference to prevent future
667 // access to object
668 sp<MediaPlayerBase> p;
669 {
670 Mutex::Autolock l(mLock);
671 p = mPlayer;
672 }
Dave Sparks795fa582009-03-24 17:57:12 -0700673 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700674
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675 mPlayer.clear();
676
677 // clear the notification to prevent callbacks to dead client
678 // and reset the player. We assume the player will serialize
679 // access to itself if necessary.
680 if (p != 0) {
681 p->setNotifyCallback(0, 0);
682#if CALLBACK_ANTAGONIZER
683 LOGD("kill Antagonizer");
684 mAntagonizer->kill();
685#endif
686 p->reset();
687 }
688
689 IPCThreadState::self()->flushCommands();
690}
691
Andreas Huber47f59cf2009-08-07 09:30:32 -0700692static player_type getDefaultPlayerType() {
Andreas Huber608d77b2010-06-23 16:40:57 -0700693 return STAGEFRIGHT_PLAYER;
Andreas Huber47f59cf2009-08-07 09:30:32 -0700694}
695
James Dong148c1a22009-09-06 14:29:45 -0700696player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697{
698 char buf[20];
699 lseek(fd, offset, SEEK_SET);
700 read(fd, buf, sizeof(buf));
701 lseek(fd, offset, SEEK_SET);
702
703 long ident = *((long*)buf);
704
705 // Ogg vorbis?
706 if (ident == 0x5367674f) // 'OggS'
Andreas Huber608d77b2010-06-23 16:40:57 -0700707 return STAGEFRIGHT_PLAYER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708
709 // Some kind of MIDI?
710 EAS_DATA_HANDLE easdata;
711 if (EAS_Init(&easdata) == EAS_SUCCESS) {
712 EAS_FILE locator;
713 locator.path = NULL;
714 locator.fd = fd;
715 locator.offset = offset;
716 locator.length = length;
717 EAS_HANDLE eashandle;
718 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
719 EAS_CloseFile(easdata, eashandle);
720 EAS_Shutdown(easdata);
721 return SONIVOX_PLAYER;
722 }
723 EAS_Shutdown(easdata);
724 }
725
Andreas Huber47f59cf2009-08-07 09:30:32 -0700726 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800727}
728
James Dong148c1a22009-09-06 14:29:45 -0700729player_type getPlayerType(const char* url)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730{
Nicolas Catania14d27472009-07-13 14:37:49 -0700731 if (TestPlayerStub::canBeUsed(url)) {
732 return TEST_PLAYER;
733 }
734
Andreas Hubered8d14f2011-02-16 09:05:38 -0800735 if (!strncasecmp("http://", url, 7)) {
736 size_t len = strlen(url);
737 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
738 return NU_PLAYER;
739 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800740
Andreas Hubered8d14f2011-02-16 09:05:38 -0800741 if (strstr(url,"m3u8")) {
742 return NU_PLAYER;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800743 }
744 }
745
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 // use MidiFile for MIDI extensions
747 int lenURL = strlen(url);
748 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
749 int len = strlen(FILE_EXTS[i].extension);
750 int start = lenURL - len;
751 if (start > 0) {
Atsushi Enofc1c7b92010-03-19 23:18:02 +0900752 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber608d77b2010-06-23 16:40:57 -0700753 return FILE_EXTS[i].playertype;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754 }
755 }
756 }
757
Andreas Huber47f59cf2009-08-07 09:30:32 -0700758 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759}
760
761static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
762 notify_callback_f notifyFunc)
763{
764 sp<MediaPlayerBase> p;
765 switch (playerType) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766 case SONIVOX_PLAYER:
767 LOGV(" create MidiFile");
768 p = new MidiFile();
769 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700770 case STAGEFRIGHT_PLAYER:
771 LOGV(" create StagefrightPlayer");
772 p = new StagefrightPlayer;
773 break;
Andreas Huberf9334412010-12-15 15:17:42 -0800774 case NU_PLAYER:
775 LOGV(" create NuPlayer");
776 p = new NuPlayerDriver;
777 break;
Nicolas Catania14d27472009-07-13 14:37:49 -0700778 case TEST_PLAYER:
779 LOGV("Create Test Player stub");
780 p = new TestPlayerStub();
781 break;
James Dong30d713a2010-11-10 18:42:40 -0800782 default:
783 LOGE("Unknown player type: %d", playerType);
784 return NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785 }
786 if (p != NULL) {
787 if (p->initCheck() == NO_ERROR) {
788 p->setNotifyCallback(cookie, notifyFunc);
789 } else {
790 p.clear();
791 }
792 }
793 if (p == NULL) {
794 LOGE("Failed to create player object");
795 }
796 return p;
797}
798
799sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
800{
801 // determine if we have the right player type
802 sp<MediaPlayerBase> p = mPlayer;
803 if ((p != NULL) && (p->playerType() != playerType)) {
804 LOGV("delete player");
805 p.clear();
806 }
807 if (p == NULL) {
808 p = android::createPlayer(playerType, this, notify);
809 }
810 return p;
811}
812
Andreas Huber2db84552010-01-28 11:19:57 -0800813status_t MediaPlayerService::Client::setDataSource(
814 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800815{
816 LOGV("setDataSource(%s)", url);
817 if (url == NULL)
818 return UNKNOWN_ERROR;
819
820 if (strncmp(url, "content://", 10) == 0) {
821 // get a filedescriptor for the content Uri and
822 // pass it to the setDataSource(fd) method
823
824 String16 url16(url);
825 int fd = android::openContentProviderFile(url16);
826 if (fd < 0)
827 {
828 LOGE("Couldn't open fd for %s", url);
829 return UNKNOWN_ERROR;
830 }
831 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
832 close(fd);
833 return mStatus;
834 } else {
835 player_type playerType = getPlayerType(url);
836 LOGV("player type = %d", playerType);
837
838 // create the right type of player
839 sp<MediaPlayerBase> p = createPlayer(playerType);
840 if (p == NULL) return NO_INIT;
841
842 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700843 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
845 }
846
847 // now set data source
848 LOGV(" setDataSource");
Andreas Huber2db84552010-01-28 11:19:57 -0800849 mStatus = p->setDataSource(url, headers);
Nicolas Catania14d27472009-07-13 14:37:49 -0700850 if (mStatus == NO_ERROR) {
851 mPlayer = p;
852 } else {
853 LOGE(" error: %d", mStatus);
854 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 return mStatus;
856 }
857}
858
859status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
860{
861 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
862 struct stat sb;
863 int ret = fstat(fd, &sb);
864 if (ret != 0) {
865 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
866 return UNKNOWN_ERROR;
867 }
868
869 LOGV("st_dev = %llu", sb.st_dev);
870 LOGV("st_mode = %u", sb.st_mode);
871 LOGV("st_uid = %lu", sb.st_uid);
872 LOGV("st_gid = %lu", sb.st_gid);
873 LOGV("st_size = %llu", sb.st_size);
874
875 if (offset >= sb.st_size) {
876 LOGE("offset error");
877 ::close(fd);
878 return UNKNOWN_ERROR;
879 }
880 if (offset + length > sb.st_size) {
881 length = sb.st_size - offset;
882 LOGV("calculated length = %lld", length);
883 }
884
885 player_type playerType = getPlayerType(fd, offset, length);
886 LOGV("player type = %d", playerType);
887
888 // create the right type of player
889 sp<MediaPlayerBase> p = createPlayer(playerType);
890 if (p == NULL) return NO_INIT;
891
892 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700893 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800894 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
895 }
896
897 // now set data source
898 mStatus = p->setDataSource(fd, offset, length);
899 if (mStatus == NO_ERROR) mPlayer = p;
900 return mStatus;
901}
902
Andreas Hubere2b10282010-11-23 11:41:34 -0800903status_t MediaPlayerService::Client::setDataSource(
904 const sp<IStreamSource> &source) {
905 // create the right type of player
Andreas Huberf9334412010-12-15 15:17:42 -0800906 sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
Andreas Hubere2b10282010-11-23 11:41:34 -0800907
908 if (p == NULL) {
909 return NO_INIT;
910 }
911
912 if (!p->hardwareOutput()) {
913 mAudioOutput = new AudioOutput(mAudioSessionId);
914 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
915 }
916
917 // now set data source
918 mStatus = p->setDataSource(source);
919
920 if (mStatus == OK) {
921 mPlayer = p;
922 }
923
924 return mStatus;
925}
926
Andreas Huber5daeb122010-08-16 08:49:37 -0700927status_t MediaPlayerService::Client::setVideoSurface(const sp<Surface>& surface)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928{
929 LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get());
930 sp<MediaPlayerBase> p = getPlayer();
931 if (p == 0) return UNKNOWN_ERROR;
932 return p->setVideoSurface(surface);
933}
934
Nicolas Catania1d187f12009-05-12 23:25:55 -0700935status_t MediaPlayerService::Client::invoke(const Parcel& request,
936 Parcel *reply)
937{
938 sp<MediaPlayerBase> p = getPlayer();
939 if (p == NULL) return UNKNOWN_ERROR;
940 return p->invoke(request, reply);
941}
942
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700943// This call doesn't need to access the native player.
944status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
945{
946 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700947 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700948
Nicolas Catania48290382009-07-10 13:53:06 -0700949 if (unmarshallFilter(filter, &allow, &status) &&
950 unmarshallFilter(filter, &drop, &status)) {
951 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700952
953 mMetadataAllow = allow;
954 mMetadataDrop = drop;
955 }
956 return status;
957}
958
Nicolas Catania48290382009-07-10 13:53:06 -0700959status_t MediaPlayerService::Client::getMetadata(
960 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700961{
nikoa64c8c72009-07-20 15:07:26 -0700962 sp<MediaPlayerBase> player = getPlayer();
963 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700964
nikod608a812009-07-16 16:39:53 -0700965 status_t status;
966 // Placeholder for the return code, updated by the caller.
967 reply->writeInt32(-1);
968
nikoa64c8c72009-07-20 15:07:26 -0700969 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700970
971 // We don't block notifications while we fetch the data. We clear
972 // mMetadataUpdated first so we don't lose notifications happening
973 // during the rest of this call.
974 {
975 Mutex::Autolock lock(mLock);
976 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700977 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700978 }
979 mMetadataUpdated.clear();
980 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700981
nikoa64c8c72009-07-20 15:07:26 -0700982 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700983
nikoa64c8c72009-07-20 15:07:26 -0700984 metadata.appendHeader();
985 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700986
987 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700988 metadata.resetParcel();
nikod608a812009-07-16 16:39:53 -0700989 LOGE("getMetadata failed %d", status);
990 return status;
991 }
992
993 // FIXME: Implement filtering on the result. Not critical since
994 // filtering takes place on the update notifications already. This
995 // would be when all the metadata are fetch and a filter is set.
996
nikod608a812009-07-16 16:39:53 -0700997 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700998 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700999 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001000}
1001
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001002status_t MediaPlayerService::Client::prepareAsync()
1003{
1004 LOGV("[%d] prepareAsync", mConnId);
1005 sp<MediaPlayerBase> p = getPlayer();
1006 if (p == 0) return UNKNOWN_ERROR;
1007 status_t ret = p->prepareAsync();
1008#if CALLBACK_ANTAGONIZER
1009 LOGD("start Antagonizer");
1010 if (ret == NO_ERROR) mAntagonizer->start();
1011#endif
1012 return ret;
1013}
1014
1015status_t MediaPlayerService::Client::start()
1016{
1017 LOGV("[%d] start", mConnId);
1018 sp<MediaPlayerBase> p = getPlayer();
1019 if (p == 0) return UNKNOWN_ERROR;
1020 p->setLooping(mLoop);
1021 return p->start();
1022}
1023
1024status_t MediaPlayerService::Client::stop()
1025{
1026 LOGV("[%d] stop", mConnId);
1027 sp<MediaPlayerBase> p = getPlayer();
1028 if (p == 0) return UNKNOWN_ERROR;
1029 return p->stop();
1030}
1031
1032status_t MediaPlayerService::Client::pause()
1033{
1034 LOGV("[%d] pause", mConnId);
1035 sp<MediaPlayerBase> p = getPlayer();
1036 if (p == 0) return UNKNOWN_ERROR;
1037 return p->pause();
1038}
1039
1040status_t MediaPlayerService::Client::isPlaying(bool* state)
1041{
1042 *state = false;
1043 sp<MediaPlayerBase> p = getPlayer();
1044 if (p == 0) return UNKNOWN_ERROR;
1045 *state = p->isPlaying();
1046 LOGV("[%d] isPlaying: %d", mConnId, *state);
1047 return NO_ERROR;
1048}
1049
1050status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1051{
1052 LOGV("getCurrentPosition");
1053 sp<MediaPlayerBase> p = getPlayer();
1054 if (p == 0) return UNKNOWN_ERROR;
1055 status_t ret = p->getCurrentPosition(msec);
1056 if (ret == NO_ERROR) {
1057 LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1058 } else {
1059 LOGE("getCurrentPosition returned %d", ret);
1060 }
1061 return ret;
1062}
1063
1064status_t MediaPlayerService::Client::getDuration(int *msec)
1065{
1066 LOGV("getDuration");
1067 sp<MediaPlayerBase> p = getPlayer();
1068 if (p == 0) return UNKNOWN_ERROR;
1069 status_t ret = p->getDuration(msec);
1070 if (ret == NO_ERROR) {
1071 LOGV("[%d] getDuration = %d", mConnId, *msec);
1072 } else {
1073 LOGE("getDuration returned %d", ret);
1074 }
1075 return ret;
1076}
1077
1078status_t MediaPlayerService::Client::seekTo(int msec)
1079{
1080 LOGV("[%d] seekTo(%d)", mConnId, msec);
1081 sp<MediaPlayerBase> p = getPlayer();
1082 if (p == 0) return UNKNOWN_ERROR;
1083 return p->seekTo(msec);
1084}
1085
1086status_t MediaPlayerService::Client::reset()
1087{
1088 LOGV("[%d] reset", mConnId);
1089 sp<MediaPlayerBase> p = getPlayer();
1090 if (p == 0) return UNKNOWN_ERROR;
1091 return p->reset();
1092}
1093
1094status_t MediaPlayerService::Client::setAudioStreamType(int type)
1095{
1096 LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1097 // TODO: for hardware output, call player instead
1098 Mutex::Autolock l(mLock);
1099 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1100 return NO_ERROR;
1101}
1102
1103status_t MediaPlayerService::Client::setLooping(int loop)
1104{
1105 LOGV("[%d] setLooping(%d)", mConnId, loop);
1106 mLoop = loop;
1107 sp<MediaPlayerBase> p = getPlayer();
1108 if (p != 0) return p->setLooping(loop);
1109 return NO_ERROR;
1110}
1111
1112status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1113{
1114 LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1115 // TODO: for hardware output, call player instead
1116 Mutex::Autolock l(mLock);
1117 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1118 return NO_ERROR;
1119}
1120
Eric Laurent2beeb502010-07-16 07:43:46 -07001121status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1122{
1123 LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1124 Mutex::Autolock l(mLock);
1125 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1126 return NO_ERROR;
1127}
1128
1129status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1130{
1131 LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1132 Mutex::Autolock l(mLock);
1133 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1134 return NO_ERROR;
1135}
Nicolas Catania48290382009-07-10 13:53:06 -07001136
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001137void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2)
1138{
1139 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001140
1141 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001142 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001143 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001144
1145 if(client->shouldDropMetadata(metadata_type)) {
1146 return;
1147 }
1148
1149 // Update the list of metadata that have changed. getMetadata
1150 // also access mMetadataUpdated and clears it.
1151 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001152 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001153 LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1154 client->mClient->notify(msg, ext1, ext2);
1155}
1156
Nicolas Catania48290382009-07-10 13:53:06 -07001157
nikoa64c8c72009-07-20 15:07:26 -07001158bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001159{
Nicolas Catania48290382009-07-10 13:53:06 -07001160 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001161
Nicolas Catania48290382009-07-10 13:53:06 -07001162 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001163 return true;
1164 }
1165
Nicolas Catania48290382009-07-10 13:53:06 -07001166 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001167 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001168 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001169 return true;
1170 }
1171}
1172
Nicolas Catania48290382009-07-10 13:53:06 -07001173
nikoa64c8c72009-07-20 15:07:26 -07001174void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001175 Mutex::Autolock lock(mLock);
1176 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1177 mMetadataUpdated.add(metadata_type);
1178 }
1179}
1180
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001181#if CALLBACK_ANTAGONIZER
1182const int Antagonizer::interval = 10000; // 10 msecs
1183
1184Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1185 mExit(false), mActive(false), mClient(client), mCb(cb)
1186{
1187 createThread(callbackThread, this);
1188}
1189
1190void Antagonizer::kill()
1191{
1192 Mutex::Autolock _l(mLock);
1193 mActive = false;
1194 mExit = true;
1195 mCondition.wait(mLock);
1196}
1197
1198int Antagonizer::callbackThread(void* user)
1199{
1200 LOGD("Antagonizer started");
1201 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1202 while (!p->mExit) {
1203 if (p->mActive) {
1204 LOGV("send event");
1205 p->mCb(p->mClient, 0, 0, 0);
1206 }
1207 usleep(interval);
1208 }
1209 Mutex::Autolock _l(p->mLock);
1210 p->mCondition.signal();
1211 LOGD("Antagonizer stopped");
1212 return 0;
1213}
1214#endif
1215
1216static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1217
1218sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1219{
1220 LOGV("decode(%s)", url);
1221 sp<MemoryBase> mem;
1222 sp<MediaPlayerBase> player;
1223
1224 // Protect our precious, precious DRMd ringtones by only allowing
1225 // decoding of http, but not filesystem paths or content Uris.
1226 // If the application wants to decode those, it should open a
1227 // filedescriptor for them and use that.
1228 if (url != NULL && strncmp(url, "http://", 7) != 0) {
1229 LOGD("Can't decode %s by path, use filedescriptor instead", url);
1230 return mem;
1231 }
1232
1233 player_type playerType = getPlayerType(url);
1234 LOGV("player type = %d", playerType);
1235
1236 // create the right type of player
1237 sp<AudioCache> cache = new AudioCache(url);
1238 player = android::createPlayer(playerType, cache.get(), cache->notify);
1239 if (player == NULL) goto Exit;
1240 if (player->hardwareOutput()) goto Exit;
1241
1242 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1243
1244 // set data source
1245 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1246
1247 LOGV("prepare");
1248 player->prepareAsync();
1249
1250 LOGV("wait for prepare");
1251 if (cache->wait() != NO_ERROR) goto Exit;
1252
1253 LOGV("start");
1254 player->start();
1255
1256 LOGV("wait for playback complete");
1257 if (cache->wait() != NO_ERROR) goto Exit;
1258
1259 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1260 *pSampleRate = cache->sampleRate();
1261 *pNumChannels = cache->channelCount();
1262 *pFormat = cache->format();
1263 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1264
1265Exit:
1266 if (player != 0) player->reset();
1267 return mem;
1268}
1269
1270sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1271{
1272 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1273 sp<MemoryBase> mem;
1274 sp<MediaPlayerBase> player;
1275
1276 player_type playerType = getPlayerType(fd, offset, length);
1277 LOGV("player type = %d", playerType);
1278
1279 // create the right type of player
1280 sp<AudioCache> cache = new AudioCache("decode_fd");
1281 player = android::createPlayer(playerType, cache.get(), cache->notify);
1282 if (player == NULL) goto Exit;
1283 if (player->hardwareOutput()) goto Exit;
1284
1285 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1286
1287 // set data source
1288 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1289
1290 LOGV("prepare");
1291 player->prepareAsync();
1292
1293 LOGV("wait for prepare");
1294 if (cache->wait() != NO_ERROR) goto Exit;
1295
1296 LOGV("start");
1297 player->start();
1298
1299 LOGV("wait for playback complete");
1300 if (cache->wait() != NO_ERROR) goto Exit;
1301
1302 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1303 *pSampleRate = cache->sampleRate();
1304 *pNumChannels = cache->channelCount();
1305 *pFormat = cache->format();
1306 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1307
1308Exit:
1309 if (player != 0) player->reset();
1310 ::close(fd);
1311 return mem;
1312}
1313
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001314
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315#undef LOG_TAG
1316#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001317MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001318 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001319 mCallbackCookie(NULL),
1320 mSessionId(sessionId) {
1321 LOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322 mTrack = 0;
1323 mStreamType = AudioSystem::MUSIC;
1324 mLeftVolume = 1.0;
1325 mRightVolume = 1.0;
1326 mLatency = 0;
1327 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001328 mAuxEffectId = 0;
1329 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001330 setMinBufferCount();
1331}
1332
1333MediaPlayerService::AudioOutput::~AudioOutput()
1334{
1335 close();
1336}
1337
1338void MediaPlayerService::AudioOutput::setMinBufferCount()
1339{
1340 char value[PROPERTY_VALUE_MAX];
1341 if (property_get("ro.kernel.qemu", value, 0)) {
1342 mIsOnEmulator = true;
1343 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1344 }
1345}
1346
1347bool MediaPlayerService::AudioOutput::isOnEmulator()
1348{
1349 setMinBufferCount();
1350 return mIsOnEmulator;
1351}
1352
1353int MediaPlayerService::AudioOutput::getMinBufferCount()
1354{
1355 setMinBufferCount();
1356 return mMinBufferCount;
1357}
1358
1359ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1360{
1361 if (mTrack == 0) return NO_INIT;
1362 return mTrack->frameCount() * frameSize();
1363}
1364
1365ssize_t MediaPlayerService::AudioOutput::frameCount() const
1366{
1367 if (mTrack == 0) return NO_INIT;
1368 return mTrack->frameCount();
1369}
1370
1371ssize_t MediaPlayerService::AudioOutput::channelCount() const
1372{
1373 if (mTrack == 0) return NO_INIT;
1374 return mTrack->channelCount();
1375}
1376
1377ssize_t MediaPlayerService::AudioOutput::frameSize() const
1378{
1379 if (mTrack == 0) return NO_INIT;
1380 return mTrack->frameSize();
1381}
1382
1383uint32_t MediaPlayerService::AudioOutput::latency () const
1384{
1385 return mLatency;
1386}
1387
1388float MediaPlayerService::AudioOutput::msecsPerFrame() const
1389{
1390 return mMsecsPerFrame;
1391}
1392
Eric Laurent342e9cf2010-01-19 17:37:09 -08001393status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1394{
1395 if (mTrack == 0) return NO_INIT;
1396 return mTrack->getPosition(position);
1397}
1398
Andreas Huber20111aa2009-07-14 16:56:47 -07001399status_t MediaPlayerService::AudioOutput::open(
1400 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1401 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001402{
Andreas Huber20111aa2009-07-14 16:56:47 -07001403 mCallback = cb;
1404 mCallbackCookie = cookie;
1405
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406 // Check argument "bufferCount" against the mininum buffer count
1407 if (bufferCount < mMinBufferCount) {
1408 LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1409 bufferCount = mMinBufferCount;
1410
1411 }
Eric Laurenta514bdb2010-06-21 09:27:30 -07001412 LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413 if (mTrack) close();
1414 int afSampleRate;
1415 int afFrameCount;
1416 int frameCount;
1417
1418 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1419 return NO_INIT;
1420 }
1421 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1422 return NO_INIT;
1423 }
1424
1425 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001426
1427 AudioTrack *t;
1428 if (mCallback != NULL) {
1429 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001430 mStreamType,
1431 sampleRate,
1432 format,
1433 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1434 frameCount,
1435 0 /* flags */,
1436 CallbackWrapper,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001437 this,
1438 0,
1439 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001440 } else {
1441 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001442 mStreamType,
1443 sampleRate,
1444 format,
1445 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001446 frameCount,
1447 0,
1448 NULL,
1449 NULL,
1450 0,
1451 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001452 }
1453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1455 LOGE("Unable to create audio track");
1456 delete t;
1457 return NO_INIT;
1458 }
1459
1460 LOGV("setVolume");
1461 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001462
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001463 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Dave Sparks1d711f62009-12-03 10:13:32 -08001464 mLatency = t->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001466
1467 t->setAuxEffectSendLevel(mSendLevel);
1468 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001469}
1470
1471void MediaPlayerService::AudioOutput::start()
1472{
1473 LOGV("start");
1474 if (mTrack) {
1475 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001476 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001477 mTrack->start();
1478 }
1479}
1480
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001481
1482
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001483ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1484{
Andreas Huber20111aa2009-07-14 16:56:47 -07001485 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487 //LOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001488 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001489 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001490 return ret;
1491 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492 return NO_INIT;
1493}
1494
1495void MediaPlayerService::AudioOutput::stop()
1496{
1497 LOGV("stop");
1498 if (mTrack) mTrack->stop();
1499}
1500
1501void MediaPlayerService::AudioOutput::flush()
1502{
1503 LOGV("flush");
1504 if (mTrack) mTrack->flush();
1505}
1506
1507void MediaPlayerService::AudioOutput::pause()
1508{
1509 LOGV("pause");
1510 if (mTrack) mTrack->pause();
1511}
1512
1513void MediaPlayerService::AudioOutput::close()
1514{
1515 LOGV("close");
1516 delete mTrack;
1517 mTrack = 0;
1518}
1519
1520void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1521{
1522 LOGV("setVolume(%f, %f)", left, right);
1523 mLeftVolume = left;
1524 mRightVolume = right;
1525 if (mTrack) {
1526 mTrack->setVolume(left, right);
1527 }
1528}
1529
Eric Laurent2beeb502010-07-16 07:43:46 -07001530status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1531{
1532 LOGV("setAuxEffectSendLevel(%f)", level);
1533 mSendLevel = level;
1534 if (mTrack) {
1535 return mTrack->setAuxEffectSendLevel(level);
1536 }
1537 return NO_ERROR;
1538}
1539
1540status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1541{
1542 LOGV("attachAuxEffect(%d)", effectId);
1543 mAuxEffectId = effectId;
1544 if (mTrack) {
1545 return mTrack->attachAuxEffect(effectId);
1546 }
1547 return NO_ERROR;
1548}
1549
Andreas Huber20111aa2009-07-14 16:56:47 -07001550// static
1551void MediaPlayerService::AudioOutput::CallbackWrapper(
1552 int event, void *cookie, void *info) {
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001553 //LOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001554 if (event != AudioTrack::EVENT_MORE_DATA) {
1555 return;
1556 }
1557
1558 AudioOutput *me = (AudioOutput *)cookie;
1559 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1560
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001561 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001562 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001563
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001564 buffer->size = actualSize;
1565
Andreas Huber20111aa2009-07-14 16:56:47 -07001566}
1567
Eric Laurent8c563ed2010-10-07 18:23:03 -07001568int MediaPlayerService::AudioOutput::getSessionId()
1569{
1570 return mSessionId;
1571}
1572
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573#undef LOG_TAG
1574#define LOG_TAG "AudioCache"
1575MediaPlayerService::AudioCache::AudioCache(const char* name) :
1576 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1577 mError(NO_ERROR), mCommandComplete(false)
1578{
1579 // create ashmem heap
1580 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1581}
1582
1583uint32_t MediaPlayerService::AudioCache::latency () const
1584{
1585 return 0;
1586}
1587
1588float MediaPlayerService::AudioCache::msecsPerFrame() const
1589{
1590 return mMsecsPerFrame;
1591}
1592
Eric Laurent342e9cf2010-01-19 17:37:09 -08001593status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1594{
1595 if (position == 0) return BAD_VALUE;
1596 *position = mSize;
1597 return NO_ERROR;
1598}
1599
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001600////////////////////////////////////////////////////////////////////////////////
1601
1602struct CallbackThread : public Thread {
1603 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1604 MediaPlayerBase::AudioSink::AudioCallback cb,
1605 void *cookie);
1606
1607protected:
1608 virtual ~CallbackThread();
1609
1610 virtual bool threadLoop();
1611
1612private:
1613 wp<MediaPlayerBase::AudioSink> mSink;
1614 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1615 void *mCookie;
1616 void *mBuffer;
1617 size_t mBufferSize;
1618
1619 CallbackThread(const CallbackThread &);
1620 CallbackThread &operator=(const CallbackThread &);
1621};
1622
1623CallbackThread::CallbackThread(
1624 const wp<MediaPlayerBase::AudioSink> &sink,
1625 MediaPlayerBase::AudioSink::AudioCallback cb,
1626 void *cookie)
1627 : mSink(sink),
1628 mCallback(cb),
1629 mCookie(cookie),
1630 mBuffer(NULL),
1631 mBufferSize(0) {
1632}
1633
1634CallbackThread::~CallbackThread() {
1635 if (mBuffer) {
1636 free(mBuffer);
1637 mBuffer = NULL;
1638 }
1639}
1640
1641bool CallbackThread::threadLoop() {
1642 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1643 if (sink == NULL) {
1644 return false;
1645 }
1646
1647 if (mBuffer == NULL) {
1648 mBufferSize = sink->bufferSize();
1649 mBuffer = malloc(mBufferSize);
1650 }
1651
1652 size_t actualSize =
1653 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1654
1655 if (actualSize > 0) {
1656 sink->write(mBuffer, actualSize);
1657 }
1658
1659 return true;
1660}
1661
1662////////////////////////////////////////////////////////////////////////////////
1663
Andreas Huber20111aa2009-07-14 16:56:47 -07001664status_t MediaPlayerService::AudioCache::open(
1665 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1666 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001667{
Dave Sparks8eb80112009-12-09 20:20:26 -08001668 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001669 if (mHeap->getHeapID() < 0) {
1670 return NO_INIT;
1671 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001672
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001673 mSampleRate = sampleRate;
1674 mChannelCount = (uint16_t)channelCount;
1675 mFormat = (uint16_t)format;
1676 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001677
1678 if (cb != NULL) {
1679 mCallbackThread = new CallbackThread(this, cb, cookie);
1680 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681 return NO_ERROR;
1682}
1683
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001684void MediaPlayerService::AudioCache::start() {
1685 if (mCallbackThread != NULL) {
1686 mCallbackThread->run("AudioCache callback");
1687 }
1688}
1689
1690void MediaPlayerService::AudioCache::stop() {
1691 if (mCallbackThread != NULL) {
1692 mCallbackThread->requestExitAndWait();
1693 }
1694}
1695
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001696ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1697{
1698 LOGV("write(%p, %u)", buffer, size);
1699 if ((buffer == 0) || (size == 0)) return size;
1700
1701 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1702 if (p == NULL) return NO_INIT;
1703 p += mSize;
1704 LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1705 if (mSize + size > mHeap->getSize()) {
1706 LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1707 size = mHeap->getSize() - mSize;
1708 }
1709 memcpy(p, buffer, size);
1710 mSize += size;
1711 return size;
1712}
1713
1714// call with lock held
1715status_t MediaPlayerService::AudioCache::wait()
1716{
1717 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001718 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001719 mSignal.wait(mLock);
1720 }
1721 mCommandComplete = false;
1722
1723 if (mError == NO_ERROR) {
1724 LOGV("wait - success");
1725 } else {
1726 LOGV("wait - error");
1727 }
1728 return mError;
1729}
1730
1731void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2)
1732{
1733 LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1734 AudioCache* p = static_cast<AudioCache*>(cookie);
1735
1736 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001737 switch (msg)
1738 {
1739 case MEDIA_ERROR:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001740 LOGE("Error %d, %d occurred", ext1, ext2);
1741 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001742 break;
1743 case MEDIA_PREPARED:
1744 LOGV("prepared");
1745 break;
1746 case MEDIA_PLAYBACK_COMPLETE:
1747 LOGV("playback complete");
1748 break;
1749 default:
1750 LOGV("ignored");
1751 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001752 }
1753
1754 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001755 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001756 p->mCommandComplete = true;
1757 p->mSignal.signal();
1758}
1759
Eric Laurent8c563ed2010-10-07 18:23:03 -07001760int MediaPlayerService::AudioCache::getSessionId()
1761{
1762 return 0;
1763}
1764
nikoa64c8c72009-07-20 15:07:26 -07001765} // namespace android