blob: a42cca5343aa3520a319cc5ec823d7c5f6ac06a9 [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>
Gloria Wangd211f412011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopiana650aaa2009-06-03 17:32:49 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopiana650aaa2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37#include <android_runtime/ActivityManager.h>
Mathias Agopiana650aaa2009-06-03 17:32:49 -070038
Mathias Agopian07952722009-05-19 19:08:10 -070039#include <binder/IPCThreadState.h>
40#include <binder/IServiceManager.h>
41#include <binder/MemoryHeapBase.h>
42#include <binder/MemoryBase.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissenc39d2e32009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include <cutils/properties.h>
48
49#include <media/MediaPlayerInterface.h>
50#include <media/mediarecorder.h>
51#include <media/MediaMetadataRetrieverInterface.h>
nikobc726922009-07-20 15:07:26 -070052#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053#include <media/AudioTrack.h>
54
Gloria Wangd211f412011-02-19 18:37:57 -080055#include <private/android_filesystem_config.h>
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057#include "MediaRecorderClient.h"
58#include "MediaPlayerService.h"
59#include "MetadataRetrieverClient.h"
60
61#include "MidiFile.h"
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070062#include "TestPlayerStub.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070063#include "StagefrightPlayer.h"
Andreas Hubera1587462010-12-15 15:17:42 -080064#include "nuplayer/NuPlayerDriver.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070065
Andreas Hubere46b7be2009-07-14 16:56:47 -070066#include <OMX.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068/* desktop Linux needs a little help with gettid() */
69#if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
70#define __KERNEL__
71# include <linux/unistd.h>
72#ifdef _syscall0
73_syscall0(pid_t,gettid)
74#else
75pid_t gettid() { return syscall(__NR_gettid);}
76#endif
77#undef __KERNEL__
78#endif
79
Nicolas Cataniab2c69392009-07-08 08:57:42 -070080namespace {
nikobc726922009-07-20 15:07:26 -070081using android::media::Metadata;
Nicolas Cataniab2c69392009-07-08 08:57:42 -070082using android::status_t;
83using android::OK;
84using android::BAD_VALUE;
85using android::NOT_ENOUGH_DATA;
86using android::Parcel;
Nicolas Cataniab2c69392009-07-08 08:57:42 -070087
88// Max number of entries in the filter.
89const int kMaxFilterSize = 64; // I pulled that out of thin air.
90
nikobc726922009-07-20 15:07:26 -070091// FIXME: Move all the metadata related function in the Metadata.cpp
niko89948372009-07-16 16:39:53 -070092
Nicolas Cataniab2c69392009-07-08 08:57:42 -070093
94// Unmarshall a filter from a Parcel.
95// Filter format in a parcel:
96//
97// 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
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | number of entries (n) |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 1 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// | metadata type 2 |
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// ....
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type n |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109//
110// @param p Parcel that should start with a filter.
111// @param[out] filter On exit contains the list of metadata type to be
112// filtered.
113// @param[out] status On exit contains the status code to be returned.
114// @return true if the parcel starts with a valid filter.
115bool unmarshallFilter(const Parcel& p,
nikobc726922009-07-20 15:07:26 -0700116 Metadata::Filter *filter,
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700117 status_t *status)
118{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700119 int32_t val;
120 if (p.readInt32(&val) != OK)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700121 {
122 LOGE("Failed to read filter's length");
123 *status = NOT_ENOUGH_DATA;
124 return false;
125 }
126
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700127 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700128 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700129 LOGE("Invalid filter len %d", val);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700130 *status = BAD_VALUE;
131 return false;
132 }
133
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700134 const size_t num = val;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700135
136 filter->clear();
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700137 filter->setCapacity(num);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700138
nikobc726922009-07-20 15:07:26 -0700139 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700140
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700141
142 if (p.dataAvail() < size)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700143 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700144 LOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700145 *status = NOT_ENOUGH_DATA;
146 return false;
147 }
148
nikobc726922009-07-20 15:07:26 -0700149 const Metadata::Type *data =
150 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700151
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700152 if (NULL == data)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700153 {
154 LOGE("Filter had no data");
155 *status = BAD_VALUE;
156 return false;
157 }
158
159 // TODO: The stl impl of vector would be more efficient here
160 // because it degenerates into a memcpy on pod types. Try to
161 // replace later or use stl::set.
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700162 for (size_t i = 0; i < num; ++i)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700163 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700164 filter->add(*data);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700165 ++data;
166 }
167 *status = OK;
168 return true;
169}
170
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700171// @param filter Of metadata type.
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700172// @param val To be searched.
173// @return true if a match was found.
nikobc726922009-07-20 15:07:26 -0700174bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700175{
176 // Deal with empty and ANY right away
177 if (filter.isEmpty()) return false;
nikobc726922009-07-20 15:07:26 -0700178 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700179
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700180 return filter.indexOf(val) >= 0;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700181}
182
183} // anonymous namespace
184
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186namespace android {
187
188// TODO: Temp hack until we can register players
189typedef struct {
190 const char *extension;
191 const player_type playertype;
192} extmap;
193extmap FILE_EXTS [] = {
194 {".mid", SONIVOX_PLAYER},
195 {".midi", SONIVOX_PLAYER},
196 {".smf", SONIVOX_PLAYER},
197 {".xmf", SONIVOX_PLAYER},
198 {".imy", SONIVOX_PLAYER},
199 {".rtttl", SONIVOX_PLAYER},
200 {".rtx", SONIVOX_PLAYER},
201 {".ota", SONIVOX_PLAYER},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202};
203
204// 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 -0800205/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
206/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
207
208void MediaPlayerService::instantiate() {
209 defaultServiceManager()->addService(
210 String16("media.player"), new MediaPlayerService());
211}
212
213MediaPlayerService::MediaPlayerService()
214{
215 LOGV("MediaPlayerService created");
216 mNextConnId = 1;
Gloria Wang9b3f1522011-02-24 14:51:45 -0800217
218 mBatteryAudio.refCount = 0;
219 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
220 mBatteryAudio.deviceOn[i] = 0;
221 mBatteryAudio.lastTime[i] = 0;
222 mBatteryAudio.totalTime[i] = 0;
223 }
224 // speaker is on by default
225 mBatteryAudio.deviceOn[SPEAKER] = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226}
227
228MediaPlayerService::~MediaPlayerService()
229{
230 LOGV("MediaPlayerService destroyed");
231}
232
233sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
234{
Gloria Wang608a2632009-10-29 15:46:37 -0700235 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
236 wp<MediaRecorderClient> w = recorder;
237 Mutex::Autolock lock(mLock);
238 mMediaRecorderClients.add(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 LOGV("Create new media recorder client from pid %d", pid);
240 return recorder;
241}
242
Gloria Wang608a2632009-10-29 15:46:37 -0700243void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
244{
245 Mutex::Autolock lock(mLock);
246 mMediaRecorderClients.remove(client);
247 LOGV("Delete media recorder client");
248}
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
251{
252 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
253 LOGV("Create new media retriever from pid %d", pid);
254 return retriever;
255}
256
Andreas Huber25643002010-01-28 11:19:57 -0800257sp<IMediaPlayer> MediaPlayerService::create(
258 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurent619346f2010-06-21 09:27:30 -0700259 const KeyedVector<String8, String8> *headers, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260{
261 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurent619346f2010-06-21 09:27:30 -0700262 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
263 LOGV("Create new client(%d) from pid %d, url=%s, connId=%d, audioSessionId=%d",
264 connId, pid, url, connId, audioSessionId);
Andreas Huber25643002010-01-28 11:19:57 -0800265 if (NO_ERROR != c->setDataSource(url, headers))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 {
267 c.clear();
268 return c;
269 }
270 wp<Client> w = c;
271 Mutex::Autolock lock(mLock);
272 mClients.add(w);
273 return c;
274}
275
276sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Eric Laurent619346f2010-06-21 09:27:30 -0700277 int fd, int64_t offset, int64_t length, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278{
279 int32_t connId = android_atomic_inc(&mNextConnId);
Eric Laurent619346f2010-06-21 09:27:30 -0700280 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
281 LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld, audioSessionId=%d",
282 connId, pid, fd, offset, length, audioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 if (NO_ERROR != c->setDataSource(fd, offset, length)) {
284 c.clear();
285 } else {
286 wp<Client> w = c;
287 Mutex::Autolock lock(mLock);
288 mClients.add(w);
289 }
290 ::close(fd);
291 return c;
292}
293
Andreas Huber52b52cd2010-11-23 11:41:34 -0800294sp<IMediaPlayer> MediaPlayerService::create(
295 pid_t pid, const sp<IMediaPlayerClient> &client,
296 const sp<IStreamSource> &source, int audioSessionId) {
297 int32_t connId = android_atomic_inc(&mNextConnId);
298 sp<Client> c = new Client(this, pid, connId, client, audioSessionId);
299
300 LOGV("Create new client(%d) from pid %d, audioSessionId=%d",
301 connId, pid, audioSessionId);
302
303 if (OK != c->setDataSource(source)) {
304 c.clear();
305 } else {
306 wp<Client> w = c;
307 Mutex::Autolock lock(mLock);
308 mClients.add(w);
309 }
310
311 return c;
312}
313
Andreas Huber784202e2009-10-15 13:46:54 -0700314sp<IOMX> MediaPlayerService::getOMX() {
315 Mutex::Autolock autoLock(mLock);
316
317 if (mOMX.get() == NULL) {
318 mOMX = new OMX;
319 }
320
321 return mOMX;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700322}
323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
325{
326 const size_t SIZE = 256;
327 char buffer[SIZE];
328 String8 result;
329
330 result.append(" AudioCache\n");
331 if (mHeap != 0) {
332 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
333 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
334 result.append(buffer);
335 }
336 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
337 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
338 result.append(buffer);
339 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
340 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
341 result.append(buffer);
342 ::write(fd, result.string(), result.size());
343 return NO_ERROR;
344}
345
346status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
347{
348 const size_t SIZE = 256;
349 char buffer[SIZE];
350 String8 result;
351
352 result.append(" AudioOutput\n");
353 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
354 mStreamType, mLeftVolume, mRightVolume);
355 result.append(buffer);
356 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
357 mMsecsPerFrame, mLatency);
358 result.append(buffer);
Eric Laurent7070b362010-07-16 07:43:46 -0700359 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
360 mAuxEffectId, mSendLevel);
361 result.append(buffer);
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 ::write(fd, result.string(), result.size());
364 if (mTrack != 0) {
365 mTrack->dump(fd, args);
366 }
367 return NO_ERROR;
368}
369
370status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
371{
372 const size_t SIZE = 256;
373 char buffer[SIZE];
374 String8 result;
375 result.append(" Client\n");
376 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
377 mPid, mConnId, mStatus, mLoop?"true": "false");
378 result.append(buffer);
379 write(fd, result.string(), result.size());
380 if (mAudioOutput != 0) {
381 mAudioOutput->dump(fd, args);
382 }
383 write(fd, "\n", 1);
384 return NO_ERROR;
385}
386
387static int myTid() {
388#ifdef HAVE_GETTID
389 return gettid();
390#else
391 return getpid();
392#endif
393}
394
395#if defined(__arm__)
396extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
397 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
398extern "C" void free_malloc_leak_info(uint8_t* info);
399
Andreas Huber27123462009-10-27 15:50:04 -0700400// Use the String-class below instead of String8 to allocate all memory
401// beforehand and not reenter the heap while we are examining it...
402struct MyString8 {
403 static const size_t MAX_SIZE = 256 * 1024;
404
405 MyString8()
406 : mPtr((char *)malloc(MAX_SIZE)) {
407 *mPtr = '\0';
408 }
409
410 ~MyString8() {
411 free(mPtr);
412 }
413
414 void append(const char *s) {
415 strcat(mPtr, s);
416 }
417
418 const char *string() const {
419 return mPtr;
420 }
421
422 size_t size() const {
423 return strlen(mPtr);
424 }
425
426private:
427 char *mPtr;
428
429 MyString8(const MyString8 &);
430 MyString8 &operator=(const MyString8 &);
431};
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433void memStatus(int fd, const Vector<String16>& args)
434{
435 const size_t SIZE = 256;
436 char buffer[SIZE];
Andreas Huber27123462009-10-27 15:50:04 -0700437 MyString8 result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 typedef struct {
440 size_t size;
441 size_t dups;
442 intptr_t * backtrace;
443 } AllocEntry;
444
445 uint8_t *info = NULL;
446 size_t overallSize = 0;
447 size_t infoSize = 0;
448 size_t totalMemory = 0;
449 size_t backtraceSize = 0;
450
451 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
452 if (info) {
453 uint8_t *ptr = info;
454 size_t count = overallSize / infoSize;
455
456 snprintf(buffer, SIZE, " Allocation count %i\n", count);
457 result.append(buffer);
James Dong3d23a612010-02-25 10:06:32 -0800458 snprintf(buffer, SIZE, " Total memory %i\n", totalMemory);
459 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
461 AllocEntry * entries = new AllocEntry[count];
462
463 for (size_t i = 0; i < count; i++) {
464 // Each entry should be size_t, size_t, intptr_t[backtraceSize]
465 AllocEntry *e = &entries[i];
466
467 e->size = *reinterpret_cast<size_t *>(ptr);
468 ptr += sizeof(size_t);
469
470 e->dups = *reinterpret_cast<size_t *>(ptr);
471 ptr += sizeof(size_t);
472
473 e->backtrace = reinterpret_cast<intptr_t *>(ptr);
474 ptr += sizeof(intptr_t) * backtraceSize;
475 }
476
477 // Now we need to sort the entries. They come sorted by size but
478 // not by stack trace which causes problems using diff.
479 bool moved;
480 do {
481 moved = false;
482 for (size_t i = 0; i < (count - 1); i++) {
483 AllocEntry *e1 = &entries[i];
484 AllocEntry *e2 = &entries[i+1];
485
486 bool swap = e1->size < e2->size;
487 if (e1->size == e2->size) {
488 for(size_t j = 0; j < backtraceSize; j++) {
489 if (e1->backtrace[j] == e2->backtrace[j]) {
490 continue;
491 }
492 swap = e1->backtrace[j] < e2->backtrace[j];
493 break;
494 }
495 }
496 if (swap) {
497 AllocEntry t = entries[i];
498 entries[i] = entries[i+1];
499 entries[i+1] = t;
500 moved = true;
501 }
502 }
503 } while (moved);
504
505 for (size_t i = 0; i < count; i++) {
506 AllocEntry *e = &entries[i];
507
James Dong3d23a612010-02-25 10:06:32 -0800508 snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 result.append(buffer);
510 for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) {
511 if (ct) {
512 result.append(", ");
513 }
514 snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]);
515 result.append(buffer);
516 }
517 result.append("\n");
518 }
519
520 delete[] entries;
521 free_malloc_leak_info(info);
522 }
523
524 write(fd, result.string(), result.size());
525}
526#endif
527
528status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
529{
530 const size_t SIZE = 256;
531 char buffer[SIZE];
532 String8 result;
533 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
534 snprintf(buffer, SIZE, "Permission Denial: "
535 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
536 IPCThreadState::self()->getCallingPid(),
537 IPCThreadState::self()->getCallingUid());
538 result.append(buffer);
539 } else {
540 Mutex::Autolock lock(mLock);
541 for (int i = 0, n = mClients.size(); i < n; ++i) {
542 sp<Client> c = mClients[i].promote();
543 if (c != 0) c->dump(fd, args);
544 }
James Dong929642e2010-07-08 11:16:11 -0700545 if (mMediaRecorderClients.size() == 0) {
546 result.append(" No media recorder client\n\n");
547 } else {
548 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
549 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
550 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
551 result.append(buffer);
552 write(fd, result.string(), result.size());
553 result = "\n";
554 c->dump(fd, args);
555 }
Gloria Wang608a2632009-10-29 15:46:37 -0700556 }
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 result.append(" Files opened and/or mapped:\n");
559 snprintf(buffer, SIZE, "/proc/%d/maps", myTid());
560 FILE *f = fopen(buffer, "r");
561 if (f) {
562 while (!feof(f)) {
563 fgets(buffer, SIZE, f);
Dave Sparks124a3462010-09-27 16:55:18 -0700564 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 strstr(buffer, " /system/sounds/") ||
Dave Sparks124a3462010-09-27 16:55:18 -0700566 strstr(buffer, " /data/") ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 strstr(buffer, " /system/media/")) {
568 result.append(" ");
569 result.append(buffer);
570 }
571 }
572 fclose(f);
573 } else {
574 result.append("couldn't open ");
575 result.append(buffer);
576 result.append("\n");
577 }
578
579 snprintf(buffer, SIZE, "/proc/%d/fd", myTid());
580 DIR *d = opendir(buffer);
581 if (d) {
582 struct dirent *ent;
583 while((ent = readdir(d)) != NULL) {
584 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
585 snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name);
586 struct stat s;
587 if (lstat(buffer, &s) == 0) {
588 if ((s.st_mode & S_IFMT) == S_IFLNK) {
589 char linkto[256];
590 int len = readlink(buffer, linkto, sizeof(linkto));
591 if(len > 0) {
592 if(len > 255) {
593 linkto[252] = '.';
594 linkto[253] = '.';
595 linkto[254] = '.';
596 linkto[255] = 0;
597 } else {
598 linkto[len] = 0;
599 }
Dave Sparks124a3462010-09-27 16:55:18 -0700600 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks124a3462010-09-27 16:55:18 -0700602 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 strstr(linkto, "/system/media/") == linkto) {
604 result.append(" ");
605 result.append(buffer);
606 result.append(" -> ");
607 result.append(linkto);
608 result.append("\n");
609 }
610 }
611 } else {
612 result.append(" unexpected type for ");
613 result.append(buffer);
614 result.append("\n");
615 }
616 }
617 }
618 }
619 closedir(d);
620 } else {
621 result.append("couldn't open ");
622 result.append(buffer);
623 result.append("\n");
624 }
625
626#if defined(__arm__)
627 bool dumpMem = false;
628 for (size_t i = 0; i < args.size(); i++) {
629 if (args[i] == String16("-m")) {
630 dumpMem = true;
631 }
632 }
633 if (dumpMem) {
634 memStatus(fd, args);
635 }
636#endif
637 }
638 write(fd, result.string(), result.size());
639 return NO_ERROR;
640}
641
642void MediaPlayerService::removeClient(wp<Client> client)
643{
644 Mutex::Autolock lock(mLock);
645 mClients.remove(client);
646}
647
648MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid,
Eric Laurent619346f2010-06-21 09:27:30 -0700649 int32_t connId, const sp<IMediaPlayerClient>& client, int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650{
651 LOGV("Client(%d) constructor", connId);
652 mPid = pid;
653 mConnId = connId;
654 mService = service;
655 mClient = client;
656 mLoop = false;
657 mStatus = NO_INIT;
Eric Laurent619346f2010-06-21 09:27:30 -0700658 mAudioSessionId = audioSessionId;
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660#if CALLBACK_ANTAGONIZER
661 LOGD("create Antagonizer");
662 mAntagonizer = new Antagonizer(notify, this);
663#endif
664}
665
666MediaPlayerService::Client::~Client()
667{
668 LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
669 mAudioOutput.clear();
670 wp<Client> client(this);
671 disconnect();
672 mService->removeClient(client);
673}
674
675void MediaPlayerService::Client::disconnect()
676{
677 LOGV("disconnect(%d) from pid %d", mConnId, mPid);
678 // grab local reference and clear main reference to prevent future
679 // access to object
680 sp<MediaPlayerBase> p;
681 {
682 Mutex::Autolock l(mLock);
683 p = mPlayer;
684 }
Dave Sparkscb9a44e2009-03-24 17:57:12 -0700685 mClient.clear();
Andreas Hubere46b7be2009-07-14 16:56:47 -0700686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 mPlayer.clear();
688
689 // clear the notification to prevent callbacks to dead client
690 // and reset the player. We assume the player will serialize
691 // access to itself if necessary.
692 if (p != 0) {
693 p->setNotifyCallback(0, 0);
694#if CALLBACK_ANTAGONIZER
695 LOGD("kill Antagonizer");
696 mAntagonizer->kill();
697#endif
698 p->reset();
699 }
700
701 IPCThreadState::self()->flushCommands();
702}
703
Andreas Huber0d596d42009-08-07 09:30:32 -0700704static player_type getDefaultPlayerType() {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700705 return STAGEFRIGHT_PLAYER;
Andreas Huber0d596d42009-08-07 09:30:32 -0700706}
707
James Dong392ff3b2009-09-06 14:29:45 -0700708player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709{
710 char buf[20];
711 lseek(fd, offset, SEEK_SET);
712 read(fd, buf, sizeof(buf));
713 lseek(fd, offset, SEEK_SET);
714
715 long ident = *((long*)buf);
716
717 // Ogg vorbis?
718 if (ident == 0x5367674f) // 'OggS'
Andreas Huber8d65dd22010-06-23 16:40:57 -0700719 return STAGEFRIGHT_PLAYER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
721 // Some kind of MIDI?
722 EAS_DATA_HANDLE easdata;
723 if (EAS_Init(&easdata) == EAS_SUCCESS) {
724 EAS_FILE locator;
725 locator.path = NULL;
726 locator.fd = fd;
727 locator.offset = offset;
728 locator.length = length;
729 EAS_HANDLE eashandle;
730 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
731 EAS_CloseFile(easdata, eashandle);
732 EAS_Shutdown(easdata);
733 return SONIVOX_PLAYER;
734 }
735 EAS_Shutdown(easdata);
736 }
737
Andreas Huber0d596d42009-08-07 09:30:32 -0700738 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739}
740
James Dong392ff3b2009-09-06 14:29:45 -0700741player_type getPlayerType(const char* url)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742{
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700743 if (TestPlayerStub::canBeUsed(url)) {
744 return TEST_PLAYER;
745 }
746
Andreas Huberd6c421f2011-02-16 09:05:38 -0800747 if (!strncasecmp("http://", url, 7)) {
748 size_t len = strlen(url);
749 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
750 return NU_PLAYER;
751 }
Andreas Huber54e66492010-12-23 10:27:40 -0800752
Andreas Huberd6c421f2011-02-16 09:05:38 -0800753 if (strstr(url,"m3u8")) {
754 return NU_PLAYER;
Andreas Huber54e66492010-12-23 10:27:40 -0800755 }
756 }
757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 // use MidiFile for MIDI extensions
759 int lenURL = strlen(url);
760 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
761 int len = strlen(FILE_EXTS[i].extension);
762 int start = lenURL - len;
763 if (start > 0) {
Atsushi Enoebcc51d2010-03-19 23:18:02 +0900764 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700765 return FILE_EXTS[i].playertype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 }
768 }
769
Andreas Huber0d596d42009-08-07 09:30:32 -0700770 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771}
772
773static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
774 notify_callback_f notifyFunc)
775{
776 sp<MediaPlayerBase> p;
777 switch (playerType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 case SONIVOX_PLAYER:
779 LOGV(" create MidiFile");
780 p = new MidiFile();
781 break;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700782 case STAGEFRIGHT_PLAYER:
783 LOGV(" create StagefrightPlayer");
784 p = new StagefrightPlayer;
785 break;
Andreas Hubera1587462010-12-15 15:17:42 -0800786 case NU_PLAYER:
787 LOGV(" create NuPlayer");
788 p = new NuPlayerDriver;
789 break;
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700790 case TEST_PLAYER:
791 LOGV("Create Test Player stub");
792 p = new TestPlayerStub();
793 break;
James Dong8ec2d9a2010-11-10 18:42:40 -0800794 default:
795 LOGE("Unknown player type: %d", playerType);
796 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798 if (p != NULL) {
799 if (p->initCheck() == NO_ERROR) {
800 p->setNotifyCallback(cookie, notifyFunc);
801 } else {
802 p.clear();
803 }
804 }
805 if (p == NULL) {
806 LOGE("Failed to create player object");
807 }
808 return p;
809}
810
811sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
812{
813 // determine if we have the right player type
814 sp<MediaPlayerBase> p = mPlayer;
815 if ((p != NULL) && (p->playerType() != playerType)) {
816 LOGV("delete player");
817 p.clear();
818 }
819 if (p == NULL) {
820 p = android::createPlayer(playerType, this, notify);
821 }
822 return p;
823}
824
Andreas Huber25643002010-01-28 11:19:57 -0800825status_t MediaPlayerService::Client::setDataSource(
826 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827{
828 LOGV("setDataSource(%s)", url);
829 if (url == NULL)
830 return UNKNOWN_ERROR;
831
832 if (strncmp(url, "content://", 10) == 0) {
833 // get a filedescriptor for the content Uri and
834 // pass it to the setDataSource(fd) method
835
836 String16 url16(url);
837 int fd = android::openContentProviderFile(url16);
838 if (fd < 0)
839 {
840 LOGE("Couldn't open fd for %s", url);
841 return UNKNOWN_ERROR;
842 }
843 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
844 close(fd);
845 return mStatus;
846 } else {
847 player_type playerType = getPlayerType(url);
848 LOGV("player type = %d", playerType);
849
850 // create the right type of player
851 sp<MediaPlayerBase> p = createPlayer(playerType);
852 if (p == NULL) return NO_INIT;
853
854 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700855 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
857 }
858
859 // now set data source
860 LOGV(" setDataSource");
Andreas Huber25643002010-01-28 11:19:57 -0800861 mStatus = p->setDataSource(url, headers);
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700862 if (mStatus == NO_ERROR) {
863 mPlayer = p;
864 } else {
865 LOGE(" error: %d", mStatus);
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 return mStatus;
868 }
869}
870
871status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
872{
873 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
874 struct stat sb;
875 int ret = fstat(fd, &sb);
876 if (ret != 0) {
877 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
878 return UNKNOWN_ERROR;
879 }
880
881 LOGV("st_dev = %llu", sb.st_dev);
882 LOGV("st_mode = %u", sb.st_mode);
883 LOGV("st_uid = %lu", sb.st_uid);
884 LOGV("st_gid = %lu", sb.st_gid);
885 LOGV("st_size = %llu", sb.st_size);
886
887 if (offset >= sb.st_size) {
888 LOGE("offset error");
889 ::close(fd);
890 return UNKNOWN_ERROR;
891 }
892 if (offset + length > sb.st_size) {
893 length = sb.st_size - offset;
894 LOGV("calculated length = %lld", length);
895 }
896
897 player_type playerType = getPlayerType(fd, offset, length);
898 LOGV("player type = %d", playerType);
899
900 // create the right type of player
901 sp<MediaPlayerBase> p = createPlayer(playerType);
902 if (p == NULL) return NO_INIT;
903
904 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700905 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
907 }
908
909 // now set data source
910 mStatus = p->setDataSource(fd, offset, length);
911 if (mStatus == NO_ERROR) mPlayer = p;
912 return mStatus;
913}
914
Andreas Huber52b52cd2010-11-23 11:41:34 -0800915status_t MediaPlayerService::Client::setDataSource(
916 const sp<IStreamSource> &source) {
917 // create the right type of player
Andreas Hubera1587462010-12-15 15:17:42 -0800918 sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
Andreas Huber52b52cd2010-11-23 11:41:34 -0800919
920 if (p == NULL) {
921 return NO_INIT;
922 }
923
924 if (!p->hardwareOutput()) {
925 mAudioOutput = new AudioOutput(mAudioSessionId);
926 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
927 }
928
929 // now set data source
930 mStatus = p->setDataSource(source);
931
932 if (mStatus == OK) {
933 mPlayer = p;
934 }
935
936 return mStatus;
937}
938
Andreas Hubere3c01832010-08-16 08:49:37 -0700939status_t MediaPlayerService::Client::setVideoSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940{
941 LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get());
942 sp<MediaPlayerBase> p = getPlayer();
943 if (p == 0) return UNKNOWN_ERROR;
944 return p->setVideoSurface(surface);
945}
946
Glenn Kastencc562a32011-02-08 17:26:17 -0800947status_t MediaPlayerService::Client::setVideoSurfaceTexture(
948 const sp<ISurfaceTexture>& surfaceTexture)
949{
950 LOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
951 sp<MediaPlayerBase> p = getPlayer();
952 if (p == 0) return UNKNOWN_ERROR;
953 return p->setVideoSurfaceTexture(surfaceTexture);
954}
955
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700956status_t MediaPlayerService::Client::invoke(const Parcel& request,
957 Parcel *reply)
958{
959 sp<MediaPlayerBase> p = getPlayer();
960 if (p == NULL) return UNKNOWN_ERROR;
961 return p->invoke(request, reply);
962}
963
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700964// This call doesn't need to access the native player.
965status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
966{
967 status_t status;
nikobc726922009-07-20 15:07:26 -0700968 media::Metadata::Filter allow, drop;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700969
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700970 if (unmarshallFilter(filter, &allow, &status) &&
971 unmarshallFilter(filter, &drop, &status)) {
972 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700973
974 mMetadataAllow = allow;
975 mMetadataDrop = drop;
976 }
977 return status;
978}
979
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700980status_t MediaPlayerService::Client::getMetadata(
981 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania5d55c712009-07-09 09:21:33 -0700982{
nikobc726922009-07-20 15:07:26 -0700983 sp<MediaPlayerBase> player = getPlayer();
984 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700985
niko89948372009-07-16 16:39:53 -0700986 status_t status;
987 // Placeholder for the return code, updated by the caller.
988 reply->writeInt32(-1);
989
nikobc726922009-07-20 15:07:26 -0700990 media::Metadata::Filter ids;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700991
992 // We don't block notifications while we fetch the data. We clear
993 // mMetadataUpdated first so we don't lose notifications happening
994 // during the rest of this call.
995 {
996 Mutex::Autolock lock(mLock);
997 if (update_only) {
niko89948372009-07-16 16:39:53 -0700998 ids = mMetadataUpdated;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700999 }
1000 mMetadataUpdated.clear();
1001 }
Nicolas Catania5d55c712009-07-09 09:21:33 -07001002
nikobc726922009-07-20 15:07:26 -07001003 media::Metadata metadata(reply);
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001004
nikobc726922009-07-20 15:07:26 -07001005 metadata.appendHeader();
1006 status = player->getMetadata(ids, reply);
niko89948372009-07-16 16:39:53 -07001007
1008 if (status != OK) {
nikobc726922009-07-20 15:07:26 -07001009 metadata.resetParcel();
niko89948372009-07-16 16:39:53 -07001010 LOGE("getMetadata failed %d", status);
1011 return status;
1012 }
1013
1014 // FIXME: Implement filtering on the result. Not critical since
1015 // filtering takes place on the update notifications already. This
1016 // would be when all the metadata are fetch and a filter is set.
1017
niko89948372009-07-16 16:39:53 -07001018 // Everything is fine, update the metadata length.
nikobc726922009-07-20 15:07:26 -07001019 metadata.updateLength();
niko89948372009-07-16 16:39:53 -07001020 return OK;
Nicolas Catania5d55c712009-07-09 09:21:33 -07001021}
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023status_t MediaPlayerService::Client::prepareAsync()
1024{
1025 LOGV("[%d] prepareAsync", mConnId);
1026 sp<MediaPlayerBase> p = getPlayer();
1027 if (p == 0) return UNKNOWN_ERROR;
1028 status_t ret = p->prepareAsync();
1029#if CALLBACK_ANTAGONIZER
1030 LOGD("start Antagonizer");
1031 if (ret == NO_ERROR) mAntagonizer->start();
1032#endif
1033 return ret;
1034}
1035
1036status_t MediaPlayerService::Client::start()
1037{
1038 LOGV("[%d] start", mConnId);
1039 sp<MediaPlayerBase> p = getPlayer();
1040 if (p == 0) return UNKNOWN_ERROR;
1041 p->setLooping(mLoop);
1042 return p->start();
1043}
1044
1045status_t MediaPlayerService::Client::stop()
1046{
1047 LOGV("[%d] stop", mConnId);
1048 sp<MediaPlayerBase> p = getPlayer();
1049 if (p == 0) return UNKNOWN_ERROR;
1050 return p->stop();
1051}
1052
1053status_t MediaPlayerService::Client::pause()
1054{
1055 LOGV("[%d] pause", mConnId);
1056 sp<MediaPlayerBase> p = getPlayer();
1057 if (p == 0) return UNKNOWN_ERROR;
1058 return p->pause();
1059}
1060
1061status_t MediaPlayerService::Client::isPlaying(bool* state)
1062{
1063 *state = false;
1064 sp<MediaPlayerBase> p = getPlayer();
1065 if (p == 0) return UNKNOWN_ERROR;
1066 *state = p->isPlaying();
1067 LOGV("[%d] isPlaying: %d", mConnId, *state);
1068 return NO_ERROR;
1069}
1070
1071status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1072{
1073 LOGV("getCurrentPosition");
1074 sp<MediaPlayerBase> p = getPlayer();
1075 if (p == 0) return UNKNOWN_ERROR;
1076 status_t ret = p->getCurrentPosition(msec);
1077 if (ret == NO_ERROR) {
1078 LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1079 } else {
1080 LOGE("getCurrentPosition returned %d", ret);
1081 }
1082 return ret;
1083}
1084
1085status_t MediaPlayerService::Client::getDuration(int *msec)
1086{
1087 LOGV("getDuration");
1088 sp<MediaPlayerBase> p = getPlayer();
1089 if (p == 0) return UNKNOWN_ERROR;
1090 status_t ret = p->getDuration(msec);
1091 if (ret == NO_ERROR) {
1092 LOGV("[%d] getDuration = %d", mConnId, *msec);
1093 } else {
1094 LOGE("getDuration returned %d", ret);
1095 }
1096 return ret;
1097}
1098
1099status_t MediaPlayerService::Client::seekTo(int msec)
1100{
1101 LOGV("[%d] seekTo(%d)", mConnId, msec);
1102 sp<MediaPlayerBase> p = getPlayer();
1103 if (p == 0) return UNKNOWN_ERROR;
1104 return p->seekTo(msec);
1105}
1106
1107status_t MediaPlayerService::Client::reset()
1108{
1109 LOGV("[%d] reset", mConnId);
1110 sp<MediaPlayerBase> p = getPlayer();
1111 if (p == 0) return UNKNOWN_ERROR;
1112 return p->reset();
1113}
1114
1115status_t MediaPlayerService::Client::setAudioStreamType(int type)
1116{
1117 LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1118 // TODO: for hardware output, call player instead
1119 Mutex::Autolock l(mLock);
1120 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1121 return NO_ERROR;
1122}
1123
1124status_t MediaPlayerService::Client::setLooping(int loop)
1125{
1126 LOGV("[%d] setLooping(%d)", mConnId, loop);
1127 mLoop = loop;
1128 sp<MediaPlayerBase> p = getPlayer();
1129 if (p != 0) return p->setLooping(loop);
1130 return NO_ERROR;
1131}
1132
1133status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1134{
1135 LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1136 // TODO: for hardware output, call player instead
1137 Mutex::Autolock l(mLock);
1138 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1139 return NO_ERROR;
1140}
1141
Eric Laurent7070b362010-07-16 07:43:46 -07001142status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1143{
1144 LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1145 Mutex::Autolock l(mLock);
1146 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1147 return NO_ERROR;
1148}
1149
1150status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1151{
1152 LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1153 Mutex::Autolock l(mLock);
1154 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1155 return NO_ERROR;
1156}
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2)
1159{
1160 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001161
1162 if (MEDIA_INFO == msg &&
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001163 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikobc726922009-07-20 15:07:26 -07001164 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001165
1166 if(client->shouldDropMetadata(metadata_type)) {
1167 return;
1168 }
1169
1170 // Update the list of metadata that have changed. getMetadata
1171 // also access mMetadataUpdated and clears it.
1172 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1175 client->mClient->notify(msg, ext1, ext2);
1176}
1177
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001178
nikobc726922009-07-20 15:07:26 -07001179bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001180{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001181 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001182
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001183 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001184 return true;
1185 }
1186
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001187 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001188 return false;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001189 } else {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001190 return true;
1191 }
1192}
1193
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001194
nikobc726922009-07-20 15:07:26 -07001195void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001196 Mutex::Autolock lock(mLock);
1197 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1198 mMetadataUpdated.add(metadata_type);
1199 }
1200}
1201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202#if CALLBACK_ANTAGONIZER
1203const int Antagonizer::interval = 10000; // 10 msecs
1204
1205Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1206 mExit(false), mActive(false), mClient(client), mCb(cb)
1207{
1208 createThread(callbackThread, this);
1209}
1210
1211void Antagonizer::kill()
1212{
1213 Mutex::Autolock _l(mLock);
1214 mActive = false;
1215 mExit = true;
1216 mCondition.wait(mLock);
1217}
1218
1219int Antagonizer::callbackThread(void* user)
1220{
1221 LOGD("Antagonizer started");
1222 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1223 while (!p->mExit) {
1224 if (p->mActive) {
1225 LOGV("send event");
1226 p->mCb(p->mClient, 0, 0, 0);
1227 }
1228 usleep(interval);
1229 }
1230 Mutex::Autolock _l(p->mLock);
1231 p->mCondition.signal();
1232 LOGD("Antagonizer stopped");
1233 return 0;
1234}
1235#endif
1236
1237static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1238
1239sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1240{
1241 LOGV("decode(%s)", url);
1242 sp<MemoryBase> mem;
1243 sp<MediaPlayerBase> player;
1244
1245 // Protect our precious, precious DRMd ringtones by only allowing
1246 // decoding of http, but not filesystem paths or content Uris.
1247 // If the application wants to decode those, it should open a
1248 // filedescriptor for them and use that.
1249 if (url != NULL && strncmp(url, "http://", 7) != 0) {
1250 LOGD("Can't decode %s by path, use filedescriptor instead", url);
1251 return mem;
1252 }
1253
1254 player_type playerType = getPlayerType(url);
1255 LOGV("player type = %d", playerType);
1256
1257 // create the right type of player
1258 sp<AudioCache> cache = new AudioCache(url);
1259 player = android::createPlayer(playerType, cache.get(), cache->notify);
1260 if (player == NULL) goto Exit;
1261 if (player->hardwareOutput()) goto Exit;
1262
1263 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1264
1265 // set data source
1266 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1267
1268 LOGV("prepare");
1269 player->prepareAsync();
1270
1271 LOGV("wait for prepare");
1272 if (cache->wait() != NO_ERROR) goto Exit;
1273
1274 LOGV("start");
1275 player->start();
1276
1277 LOGV("wait for playback complete");
1278 if (cache->wait() != NO_ERROR) goto Exit;
1279
1280 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1281 *pSampleRate = cache->sampleRate();
1282 *pNumChannels = cache->channelCount();
1283 *pFormat = cache->format();
1284 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1285
1286Exit:
1287 if (player != 0) player->reset();
1288 return mem;
1289}
1290
1291sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1292{
1293 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1294 sp<MemoryBase> mem;
1295 sp<MediaPlayerBase> player;
1296
1297 player_type playerType = getPlayerType(fd, offset, length);
1298 LOGV("player type = %d", playerType);
1299
1300 // create the right type of player
1301 sp<AudioCache> cache = new AudioCache("decode_fd");
1302 player = android::createPlayer(playerType, cache.get(), cache->notify);
1303 if (player == NULL) goto Exit;
1304 if (player->hardwareOutput()) goto Exit;
1305
1306 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1307
1308 // set data source
1309 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1310
1311 LOGV("prepare");
1312 player->prepareAsync();
1313
1314 LOGV("wait for prepare");
1315 if (cache->wait() != NO_ERROR) goto Exit;
1316
1317 LOGV("start");
1318 player->start();
1319
1320 LOGV("wait for playback complete");
1321 if (cache->wait() != NO_ERROR) goto Exit;
1322
1323 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1324 *pSampleRate = cache->sampleRate();
1325 *pNumChannels = cache->channelCount();
1326 *pFormat = cache->format();
1327 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1328
1329Exit:
1330 if (player != 0) player->reset();
1331 ::close(fd);
1332 return mem;
1333}
1334
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336#undef LOG_TAG
1337#define LOG_TAG "AudioSink"
Eric Laurent619346f2010-06-21 09:27:30 -07001338MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Hubere46b7be2009-07-14 16:56:47 -07001339 : mCallback(NULL),
Eric Laurent619346f2010-06-21 09:27:30 -07001340 mCallbackCookie(NULL),
1341 mSessionId(sessionId) {
1342 LOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 mTrack = 0;
1344 mStreamType = AudioSystem::MUSIC;
1345 mLeftVolume = 1.0;
1346 mRightVolume = 1.0;
1347 mLatency = 0;
1348 mMsecsPerFrame = 0;
Eric Laurent7070b362010-07-16 07:43:46 -07001349 mAuxEffectId = 0;
1350 mSendLevel = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 setMinBufferCount();
1352}
1353
1354MediaPlayerService::AudioOutput::~AudioOutput()
1355{
1356 close();
1357}
1358
1359void MediaPlayerService::AudioOutput::setMinBufferCount()
1360{
1361 char value[PROPERTY_VALUE_MAX];
1362 if (property_get("ro.kernel.qemu", value, 0)) {
1363 mIsOnEmulator = true;
1364 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1365 }
1366}
1367
1368bool MediaPlayerService::AudioOutput::isOnEmulator()
1369{
1370 setMinBufferCount();
1371 return mIsOnEmulator;
1372}
1373
1374int MediaPlayerService::AudioOutput::getMinBufferCount()
1375{
1376 setMinBufferCount();
1377 return mMinBufferCount;
1378}
1379
1380ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1381{
1382 if (mTrack == 0) return NO_INIT;
1383 return mTrack->frameCount() * frameSize();
1384}
1385
1386ssize_t MediaPlayerService::AudioOutput::frameCount() const
1387{
1388 if (mTrack == 0) return NO_INIT;
1389 return mTrack->frameCount();
1390}
1391
1392ssize_t MediaPlayerService::AudioOutput::channelCount() const
1393{
1394 if (mTrack == 0) return NO_INIT;
1395 return mTrack->channelCount();
1396}
1397
1398ssize_t MediaPlayerService::AudioOutput::frameSize() const
1399{
1400 if (mTrack == 0) return NO_INIT;
1401 return mTrack->frameSize();
1402}
1403
1404uint32_t MediaPlayerService::AudioOutput::latency () const
1405{
1406 return mLatency;
1407}
1408
1409float MediaPlayerService::AudioOutput::msecsPerFrame() const
1410{
1411 return mMsecsPerFrame;
1412}
1413
Eric Laurent0986e792010-01-19 17:37:09 -08001414status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1415{
1416 if (mTrack == 0) return NO_INIT;
1417 return mTrack->getPosition(position);
1418}
1419
Andreas Hubere46b7be2009-07-14 16:56:47 -07001420status_t MediaPlayerService::AudioOutput::open(
1421 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1422 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001424 mCallback = cb;
1425 mCallbackCookie = cookie;
1426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 // Check argument "bufferCount" against the mininum buffer count
1428 if (bufferCount < mMinBufferCount) {
1429 LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1430 bufferCount = mMinBufferCount;
1431
1432 }
Eric Laurent619346f2010-06-21 09:27:30 -07001433 LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 if (mTrack) close();
1435 int afSampleRate;
1436 int afFrameCount;
1437 int frameCount;
1438
1439 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1440 return NO_INIT;
1441 }
1442 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1443 return NO_INIT;
1444 }
1445
1446 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Hubere46b7be2009-07-14 16:56:47 -07001447
1448 AudioTrack *t;
1449 if (mCallback != NULL) {
1450 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001451 mStreamType,
1452 sampleRate,
1453 format,
1454 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1455 frameCount,
1456 0 /* flags */,
1457 CallbackWrapper,
Eric Laurent619346f2010-06-21 09:27:30 -07001458 this,
1459 0,
1460 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001461 } else {
1462 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001463 mStreamType,
1464 sampleRate,
1465 format,
1466 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
Eric Laurent619346f2010-06-21 09:27:30 -07001467 frameCount,
1468 0,
1469 NULL,
1470 NULL,
1471 0,
1472 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1476 LOGE("Unable to create audio track");
1477 delete t;
1478 return NO_INIT;
1479 }
1480
1481 LOGV("setVolume");
1482 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Dave Sparksb904c2a2009-12-03 10:13:32 -08001485 mLatency = t->latency();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 mTrack = t;
Eric Laurent7070b362010-07-16 07:43:46 -07001487
1488 t->setAuxEffectSendLevel(mSendLevel);
1489 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490}
1491
1492void MediaPlayerService::AudioOutput::start()
1493{
1494 LOGV("start");
1495 if (mTrack) {
1496 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001497 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 mTrack->start();
1499 }
1500}
1501
Marco Nelissene274db12010-01-12 09:23:54 -08001502
1503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1505{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001506 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 //LOGV("write(%p, %u)", buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001509 if (mTrack) {
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001510 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001511 return ret;
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 return NO_INIT;
1514}
1515
1516void MediaPlayerService::AudioOutput::stop()
1517{
1518 LOGV("stop");
1519 if (mTrack) mTrack->stop();
1520}
1521
1522void MediaPlayerService::AudioOutput::flush()
1523{
1524 LOGV("flush");
1525 if (mTrack) mTrack->flush();
1526}
1527
1528void MediaPlayerService::AudioOutput::pause()
1529{
1530 LOGV("pause");
1531 if (mTrack) mTrack->pause();
1532}
1533
1534void MediaPlayerService::AudioOutput::close()
1535{
1536 LOGV("close");
1537 delete mTrack;
1538 mTrack = 0;
1539}
1540
1541void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1542{
1543 LOGV("setVolume(%f, %f)", left, right);
1544 mLeftVolume = left;
1545 mRightVolume = right;
1546 if (mTrack) {
1547 mTrack->setVolume(left, right);
1548 }
1549}
1550
Eric Laurent7070b362010-07-16 07:43:46 -07001551status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1552{
1553 LOGV("setAuxEffectSendLevel(%f)", level);
1554 mSendLevel = level;
1555 if (mTrack) {
1556 return mTrack->setAuxEffectSendLevel(level);
1557 }
1558 return NO_ERROR;
1559}
1560
1561status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1562{
1563 LOGV("attachAuxEffect(%d)", effectId);
1564 mAuxEffectId = effectId;
1565 if (mTrack) {
1566 return mTrack->attachAuxEffect(effectId);
1567 }
1568 return NO_ERROR;
1569}
1570
Andreas Hubere46b7be2009-07-14 16:56:47 -07001571// static
1572void MediaPlayerService::AudioOutput::CallbackWrapper(
1573 int event, void *cookie, void *info) {
Marco Nelissene274db12010-01-12 09:23:54 -08001574 //LOGV("callbackwrapper");
Andreas Hubere46b7be2009-07-14 16:56:47 -07001575 if (event != AudioTrack::EVENT_MORE_DATA) {
1576 return;
1577 }
1578
1579 AudioOutput *me = (AudioOutput *)cookie;
1580 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1581
Andreas Huber6ed937e2010-02-09 16:59:18 -08001582 size_t actualSize = (*me->mCallback)(
Andreas Hubere46b7be2009-07-14 16:56:47 -07001583 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber6ed937e2010-02-09 16:59:18 -08001584
Andreas Huber406a18b2010-02-18 16:45:13 -08001585 buffer->size = actualSize;
1586
Andreas Hubere46b7be2009-07-14 16:56:47 -07001587}
1588
Eric Laurentb3bdf3f2010-10-07 18:23:03 -07001589int MediaPlayerService::AudioOutput::getSessionId()
1590{
1591 return mSessionId;
1592}
1593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594#undef LOG_TAG
1595#define LOG_TAG "AudioCache"
1596MediaPlayerService::AudioCache::AudioCache(const char* name) :
1597 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1598 mError(NO_ERROR), mCommandComplete(false)
1599{
1600 // create ashmem heap
1601 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1602}
1603
1604uint32_t MediaPlayerService::AudioCache::latency () const
1605{
1606 return 0;
1607}
1608
1609float MediaPlayerService::AudioCache::msecsPerFrame() const
1610{
1611 return mMsecsPerFrame;
1612}
1613
Eric Laurent0986e792010-01-19 17:37:09 -08001614status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1615{
1616 if (position == 0) return BAD_VALUE;
1617 *position = mSize;
1618 return NO_ERROR;
1619}
1620
Andreas Huber6ed937e2010-02-09 16:59:18 -08001621////////////////////////////////////////////////////////////////////////////////
1622
1623struct CallbackThread : public Thread {
1624 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1625 MediaPlayerBase::AudioSink::AudioCallback cb,
1626 void *cookie);
1627
1628protected:
1629 virtual ~CallbackThread();
1630
1631 virtual bool threadLoop();
1632
1633private:
1634 wp<MediaPlayerBase::AudioSink> mSink;
1635 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1636 void *mCookie;
1637 void *mBuffer;
1638 size_t mBufferSize;
1639
1640 CallbackThread(const CallbackThread &);
1641 CallbackThread &operator=(const CallbackThread &);
1642};
1643
1644CallbackThread::CallbackThread(
1645 const wp<MediaPlayerBase::AudioSink> &sink,
1646 MediaPlayerBase::AudioSink::AudioCallback cb,
1647 void *cookie)
1648 : mSink(sink),
1649 mCallback(cb),
1650 mCookie(cookie),
1651 mBuffer(NULL),
1652 mBufferSize(0) {
1653}
1654
1655CallbackThread::~CallbackThread() {
1656 if (mBuffer) {
1657 free(mBuffer);
1658 mBuffer = NULL;
1659 }
1660}
1661
1662bool CallbackThread::threadLoop() {
1663 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1664 if (sink == NULL) {
1665 return false;
1666 }
1667
1668 if (mBuffer == NULL) {
1669 mBufferSize = sink->bufferSize();
1670 mBuffer = malloc(mBufferSize);
1671 }
1672
1673 size_t actualSize =
1674 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1675
1676 if (actualSize > 0) {
1677 sink->write(mBuffer, actualSize);
1678 }
1679
1680 return true;
1681}
1682
1683////////////////////////////////////////////////////////////////////////////////
1684
Andreas Hubere46b7be2009-07-14 16:56:47 -07001685status_t MediaPlayerService::AudioCache::open(
1686 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1687 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688{
Dave Sparks14f89402009-12-09 20:20:26 -08001689 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks14f89402009-12-09 20:20:26 -08001690 if (mHeap->getHeapID() < 0) {
1691 return NO_INIT;
1692 }
Andreas Hubere46b7be2009-07-14 16:56:47 -07001693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 mSampleRate = sampleRate;
1695 mChannelCount = (uint16_t)channelCount;
1696 mFormat = (uint16_t)format;
1697 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber6ed937e2010-02-09 16:59:18 -08001698
1699 if (cb != NULL) {
1700 mCallbackThread = new CallbackThread(this, cb, cookie);
1701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 return NO_ERROR;
1703}
1704
Andreas Huber6ed937e2010-02-09 16:59:18 -08001705void MediaPlayerService::AudioCache::start() {
1706 if (mCallbackThread != NULL) {
1707 mCallbackThread->run("AudioCache callback");
1708 }
1709}
1710
1711void MediaPlayerService::AudioCache::stop() {
1712 if (mCallbackThread != NULL) {
1713 mCallbackThread->requestExitAndWait();
1714 }
1715}
1716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1718{
1719 LOGV("write(%p, %u)", buffer, size);
1720 if ((buffer == 0) || (size == 0)) return size;
1721
1722 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1723 if (p == NULL) return NO_INIT;
1724 p += mSize;
1725 LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1726 if (mSize + size > mHeap->getSize()) {
1727 LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1728 size = mHeap->getSize() - mSize;
1729 }
1730 memcpy(p, buffer, size);
1731 mSize += size;
1732 return size;
1733}
1734
1735// call with lock held
1736status_t MediaPlayerService::AudioCache::wait()
1737{
1738 Mutex::Autolock lock(mLock);
Dave Sparks16433e22010-03-01 19:29:58 -08001739 while (!mCommandComplete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 mSignal.wait(mLock);
1741 }
1742 mCommandComplete = false;
1743
1744 if (mError == NO_ERROR) {
1745 LOGV("wait - success");
1746 } else {
1747 LOGV("wait - error");
1748 }
1749 return mError;
1750}
1751
1752void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2)
1753{
1754 LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1755 AudioCache* p = static_cast<AudioCache*>(cookie);
1756
1757 // ignore buffering messages
Dave Sparks14f89402009-12-09 20:20:26 -08001758 switch (msg)
1759 {
1760 case MEDIA_ERROR:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 LOGE("Error %d, %d occurred", ext1, ext2);
1762 p->mError = ext1;
Dave Sparks14f89402009-12-09 20:20:26 -08001763 break;
1764 case MEDIA_PREPARED:
1765 LOGV("prepared");
1766 break;
1767 case MEDIA_PLAYBACK_COMPLETE:
1768 LOGV("playback complete");
1769 break;
1770 default:
1771 LOGV("ignored");
1772 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774
1775 // wake up thread
Dave Sparks6c26fe42010-03-02 12:56:37 -08001776 Mutex::Autolock lock(p->mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 p->mCommandComplete = true;
1778 p->mSignal.signal();
1779}
1780
Eric Laurentb3bdf3f2010-10-07 18:23:03 -07001781int MediaPlayerService::AudioCache::getSessionId()
1782{
1783 return 0;
1784}
1785
Gloria Wangd211f412011-02-19 18:37:57 -08001786void MediaPlayerService::addBatteryData(uint32_t params)
1787{
1788 Mutex::Autolock lock(mLock);
Gloria Wang9b3f1522011-02-24 14:51:45 -08001789
1790 int32_t time = systemTime() / 1000000L;
1791
1792 // change audio output devices. This notification comes from AudioFlinger
1793 if ((params & kBatteryDataSpeakerOn)
1794 || (params & kBatteryDataOtherAudioDeviceOn)) {
1795
1796 int deviceOn[NUM_AUDIO_DEVICES];
1797 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1798 deviceOn[i] = 0;
1799 }
1800
1801 if ((params & kBatteryDataSpeakerOn)
1802 && (params & kBatteryDataOtherAudioDeviceOn)) {
1803 deviceOn[SPEAKER_AND_OTHER] = 1;
1804 } else if (params & kBatteryDataSpeakerOn) {
1805 deviceOn[SPEAKER] = 1;
1806 } else {
1807 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1808 }
1809
1810 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1811 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1812
1813 if (mBatteryAudio.refCount > 0) { // if playing audio
1814 if (!deviceOn[i]) {
1815 mBatteryAudio.lastTime[i] += time;
1816 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1817 mBatteryAudio.lastTime[i] = 0;
1818 } else {
1819 mBatteryAudio.lastTime[i] = 0 - time;
1820 }
1821 }
1822
1823 mBatteryAudio.deviceOn[i] = deviceOn[i];
1824 }
1825 }
1826 return;
1827 }
1828
1829 // an sudio stream is started
1830 if (params & kBatteryDataAudioFlingerStart) {
1831 // record the start time only if currently no other audio
1832 // is being played
1833 if (mBatteryAudio.refCount == 0) {
1834 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1835 if (mBatteryAudio.deviceOn[i]) {
1836 mBatteryAudio.lastTime[i] -= time;
1837 }
1838 }
1839 }
1840
1841 mBatteryAudio.refCount ++;
1842 return;
1843
1844 } else if (params & kBatteryDataAudioFlingerStop) {
1845 if (mBatteryAudio.refCount <= 0) {
1846 LOGW("Battery track warning: refCount is <= 0");
1847 return;
1848 }
1849
1850 // record the stop time only if currently this is the only
1851 // audio being played
1852 if (mBatteryAudio.refCount == 1) {
1853 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1854 if (mBatteryAudio.deviceOn[i]) {
1855 mBatteryAudio.lastTime[i] += time;
1856 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1857 mBatteryAudio.lastTime[i] = 0;
1858 }
1859 }
1860 }
1861
1862 mBatteryAudio.refCount --;
1863 return;
1864 }
1865
Gloria Wangd211f412011-02-19 18:37:57 -08001866 int uid = IPCThreadState::self()->getCallingUid();
1867 if (uid == AID_MEDIA) {
1868 return;
1869 }
1870 int index = mBatteryData.indexOfKey(uid);
Gloria Wangd211f412011-02-19 18:37:57 -08001871
1872 if (index < 0) { // create a new entry for this UID
1873 BatteryUsageInfo info;
1874 info.audioTotalTime = 0;
1875 info.videoTotalTime = 0;
1876 info.audioLastTime = 0;
1877 info.videoLastTime = 0;
1878 info.refCount = 0;
1879
Gloria Wang9b3f1522011-02-24 14:51:45 -08001880 if (mBatteryData.add(uid, info) == NO_MEMORY) {
1881 LOGE("Battery track error: no memory for new app");
1882 return;
1883 }
Gloria Wangd211f412011-02-19 18:37:57 -08001884 }
1885
1886 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1887
1888 if (params & kBatteryDataCodecStarted) {
1889 if (params & kBatteryDataTrackAudio) {
1890 info.audioLastTime -= time;
1891 info.refCount ++;
1892 }
1893 if (params & kBatteryDataTrackVideo) {
1894 info.videoLastTime -= time;
1895 info.refCount ++;
1896 }
1897 } else {
1898 if (info.refCount == 0) {
1899 LOGW("Battery track warning: refCount is already 0");
1900 return;
1901 } else if (info.refCount < 0) {
1902 LOGE("Battery track error: refCount < 0");
1903 mBatteryData.removeItem(uid);
1904 return;
1905 }
1906
1907 if (params & kBatteryDataTrackAudio) {
1908 info.audioLastTime += time;
1909 info.refCount --;
1910 }
1911 if (params & kBatteryDataTrackVideo) {
1912 info.videoLastTime += time;
1913 info.refCount --;
1914 }
1915
1916 // no stream is being played by this UID
1917 if (info.refCount == 0) {
1918 info.audioTotalTime += info.audioLastTime;
1919 info.audioLastTime = 0;
1920 info.videoTotalTime += info.videoLastTime;
1921 info.videoLastTime = 0;
1922 }
1923 }
1924}
1925
1926status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1927 Mutex::Autolock lock(mLock);
Gloria Wang9b3f1522011-02-24 14:51:45 -08001928
1929 // audio output devices usage
1930 int32_t time = systemTime() / 1000000L; //in ms
1931 int32_t totalTime;
1932
1933 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1934 totalTime = mBatteryAudio.totalTime[i];
1935
1936 if (mBatteryAudio.deviceOn[i]
1937 && (mBatteryAudio.lastTime[i] != 0)) {
1938 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1939 totalTime += tmpTime;
1940 }
1941
1942 reply->writeInt32(totalTime);
1943 // reset the total time
1944 mBatteryAudio.totalTime[i] = 0;
1945 }
1946
1947 // codec usage
Gloria Wangd211f412011-02-19 18:37:57 -08001948 BatteryUsageInfo info;
1949 int size = mBatteryData.size();
1950
1951 reply->writeInt32(size);
1952 int i = 0;
1953
1954 while (i < size) {
1955 info = mBatteryData.valueAt(i);
1956
1957 reply->writeInt32(mBatteryData.keyAt(i)); //UID
1958 reply->writeInt32(info.audioTotalTime);
1959 reply->writeInt32(info.videoTotalTime);
1960
1961 info.audioTotalTime = 0;
1962 info.videoTotalTime = 0;
1963
1964 // remove the UID entry where no stream is being played
1965 if (info.refCount <= 0) {
1966 mBatteryData.removeItemsAt(i);
1967 size --;
1968 i --;
1969 }
1970 i++;
1971 }
1972 return NO_ERROR;
1973}
nikobc726922009-07-20 15:07:26 -07001974} // namespace android