3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Proxy for media player implementations
20 //#define LOG_NDEBUG 0
21 #define LOG_TAG "MediaPlayerService"
22 #include <utils/Log.h>
24 #include <sys/types.h>
31 #include <cutils/atomic.h>
32 #include <cutils/properties.h> // for property_get
34 #include <utils/misc.h>
36 #include <android_runtime/ActivityManager.h>
38 #include <binder/IPCThreadState.h>
39 #include <binder/IServiceManager.h>
40 #include <binder/MemoryHeapBase.h>
41 #include <binder/MemoryBase.h>
42 #include <utils/Errors.h> // for status_t
43 #include <utils/String8.h>
44 #include <utils/SystemClock.h>
45 #include <utils/Vector.h>
46 #include <cutils/properties.h>
48 #include <media/MediaPlayerInterface.h>
49 #include <media/mediarecorder.h>
50 #include <media/MediaMetadataRetrieverInterface.h>
51 #include <media/Metadata.h>
52 #include <media/AudioTrack.h>
54 #include "MediaRecorderClient.h"
55 #include "MediaPlayerService.h"
56 #include "MetadataRetrieverClient.h"
59 #include "VorbisPlayer.h"
60 #include <media/PVPlayer.h>
61 #include "TestPlayerStub.h"
62 #include "StagefrightPlayer.h"
66 /* desktop Linux needs a little help with gettid() */
67 #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
69 # include <linux/unistd.h>
71 _syscall0(pid_t,gettid)
73 pid_t gettid() { return syscall(__NR_gettid);}
79 using android::media::Metadata;
80 using android::status_t;
82 using android::BAD_VALUE;
83 using android::NOT_ENOUGH_DATA;
84 using android::Parcel;
86 // Max number of entries in the filter.
87 const int kMaxFilterSize = 64; // I pulled that out of thin air.
89 // FIXME: Move all the metadata related function in the Metadata.cpp
92 // Unmarshall a filter from a Parcel.
93 // Filter format in a parcel:
95 // 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
96 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 // | number of entries (n) |
98 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 // | metadata type 1 |
100 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 // | metadata type 2 |
102 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 // | metadata type n |
106 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 // @param p Parcel that should start with a filter.
109 // @param[out] filter On exit contains the list of metadata type to be
111 // @param[out] status On exit contains the status code to be returned.
112 // @return true if the parcel starts with a valid filter.
113 bool unmarshallFilter(const Parcel& p,
114 Metadata::Filter *filter,
118 if (p.readInt32(&val) != OK)
120 LOGE("Failed to read filter's length");
121 *status = NOT_ENOUGH_DATA;
125 if( val > kMaxFilterSize || val < 0)
127 LOGE("Invalid filter len %d", val);
132 const size_t num = val;
135 filter->setCapacity(num);
137 size_t size = num * sizeof(Metadata::Type);
140 if (p.dataAvail() < size)
142 LOGE("Filter too short expected %d but got %d", size, p.dataAvail());
143 *status = NOT_ENOUGH_DATA;
147 const Metadata::Type *data =
148 static_cast<const Metadata::Type*>(p.readInplace(size));
152 LOGE("Filter had no data");
157 // TODO: The stl impl of vector would be more efficient here
158 // because it degenerates into a memcpy on pod types. Try to
159 // replace later or use stl::set.
160 for (size_t i = 0; i < num; ++i)
169 // @param filter Of metadata type.
170 // @param val To be searched.
171 // @return true if a match was found.
172 bool findMetadata(const Metadata::Filter& filter, const int32_t val)
174 // Deal with empty and ANY right away
175 if (filter.isEmpty()) return false;
176 if (filter[0] == Metadata::kAny) return true;
178 return filter.indexOf(val) >= 0;
181 } // anonymous namespace
186 // TODO: Temp hack until we can register players
188 const char *extension;
189 const player_type playertype;
191 extmap FILE_EXTS [] = {
192 {".mid", SONIVOX_PLAYER},
193 {".midi", SONIVOX_PLAYER},
194 {".smf", SONIVOX_PLAYER},
195 {".xmf", SONIVOX_PLAYER},
196 {".imy", SONIVOX_PLAYER},
197 {".rtttl", SONIVOX_PLAYER},
198 {".rtx", SONIVOX_PLAYER},
199 {".ota", SONIVOX_PLAYER},
200 {".ogg", VORBIS_PLAYER},
201 {".oga", VORBIS_PLAYER},
209 // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
210 /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
211 /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
213 void MediaPlayerService::instantiate() {
214 defaultServiceManager()->addService(
215 String16("media.player"), new MediaPlayerService());
218 MediaPlayerService::MediaPlayerService()
220 LOGV("MediaPlayerService created");
224 MediaPlayerService::~MediaPlayerService()
226 LOGV("MediaPlayerService destroyed");
229 sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
232 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
233 wp<MediaRecorderClient> w = recorder;
234 Mutex::Autolock lock(mLock);
235 mMediaRecorderClients.add(w);
237 sp<MediaRecorderClient> recorder = NULL;
239 LOGV("Create new media recorder client from pid %d", pid);
243 void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
245 Mutex::Autolock lock(mLock);
246 mMediaRecorderClients.remove(client);
247 LOGV("Delete media recorder client");
250 sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
252 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
253 LOGV("Create new media retriever from pid %d", pid);
257 sp<IMediaPlayer> MediaPlayerService::create(
258 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
259 const KeyedVector<String8, String8> *headers)
261 int32_t connId = android_atomic_inc(&mNextConnId);
262 sp<Client> c = new Client(this, pid, connId, client);
263 LOGV("Create new client(%d) from pid %d, url=%s, connId=%d", connId, pid, url, connId);
264 if (NO_ERROR != c->setDataSource(url, headers))
270 Mutex::Autolock lock(mLock);
275 sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
276 int fd, int64_t offset, int64_t length)
278 int32_t connId = android_atomic_inc(&mNextConnId);
279 sp<Client> c = new Client(this, pid, connId, client);
280 LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld",
281 connId, pid, fd, offset, length);
282 if (NO_ERROR != c->setDataSource(fd, offset, length)) {
286 Mutex::Autolock lock(mLock);
293 sp<IOMX> MediaPlayerService::getOMX() {
294 Mutex::Autolock autoLock(mLock);
296 if (mOMX.get() == NULL) {
303 status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
305 const size_t SIZE = 256;
309 result.append(" AudioCache\n");
311 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
312 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
313 result.append(buffer);
315 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
316 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
317 result.append(buffer);
318 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
319 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
320 result.append(buffer);
321 ::write(fd, result.string(), result.size());
325 status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
327 const size_t SIZE = 256;
331 result.append(" AudioOutput\n");
332 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
333 mStreamType, mLeftVolume, mRightVolume);
334 result.append(buffer);
335 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
336 mMsecsPerFrame, mLatency);
337 result.append(buffer);
338 ::write(fd, result.string(), result.size());
340 mTrack->dump(fd, args);
345 status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
347 const size_t SIZE = 256;
350 result.append(" Client\n");
351 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
352 mPid, mConnId, mStatus, mLoop?"true": "false");
353 result.append(buffer);
354 write(fd, result.string(), result.size());
355 if (mAudioOutput != 0) {
356 mAudioOutput->dump(fd, args);
371 extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
372 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
373 extern "C" void free_malloc_leak_info(uint8_t* info);
375 // Use the String-class below instead of String8 to allocate all memory
376 // beforehand and not reenter the heap while we are examining it...
378 static const size_t MAX_SIZE = 256 * 1024;
381 : mPtr((char *)malloc(MAX_SIZE)) {
389 void append(const char *s) {
393 const char *string() const {
397 size_t size() const {
404 MyString8(const MyString8 &);
405 MyString8 &operator=(const MyString8 &);
408 void memStatus(int fd, const Vector<String16>& args)
410 const size_t SIZE = 256;
417 intptr_t * backtrace;
420 uint8_t *info = NULL;
421 size_t overallSize = 0;
423 size_t totalMemory = 0;
424 size_t backtraceSize = 0;
426 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
429 size_t count = overallSize / infoSize;
431 snprintf(buffer, SIZE, " Allocation count %i\n", count);
432 result.append(buffer);
433 snprintf(buffer, SIZE, " Total memory %i\n", totalMemory);
434 result.append(buffer);
436 AllocEntry * entries = new AllocEntry[count];
438 for (size_t i = 0; i < count; i++) {
439 // Each entry should be size_t, size_t, intptr_t[backtraceSize]
440 AllocEntry *e = &entries[i];
442 e->size = *reinterpret_cast<size_t *>(ptr);
443 ptr += sizeof(size_t);
445 e->dups = *reinterpret_cast<size_t *>(ptr);
446 ptr += sizeof(size_t);
448 e->backtrace = reinterpret_cast<intptr_t *>(ptr);
449 ptr += sizeof(intptr_t) * backtraceSize;
452 // Now we need to sort the entries. They come sorted by size but
453 // not by stack trace which causes problems using diff.
457 for (size_t i = 0; i < (count - 1); i++) {
458 AllocEntry *e1 = &entries[i];
459 AllocEntry *e2 = &entries[i+1];
461 bool swap = e1->size < e2->size;
462 if (e1->size == e2->size) {
463 for(size_t j = 0; j < backtraceSize; j++) {
464 if (e1->backtrace[j] == e2->backtrace[j]) {
467 swap = e1->backtrace[j] < e2->backtrace[j];
472 AllocEntry t = entries[i];
473 entries[i] = entries[i+1];
480 for (size_t i = 0; i < count; i++) {
481 AllocEntry *e = &entries[i];
483 snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups);
484 result.append(buffer);
485 for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) {
489 snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]);
490 result.append(buffer);
496 free_malloc_leak_info(info);
499 write(fd, result.string(), result.size());
503 status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
505 const size_t SIZE = 256;
508 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
509 snprintf(buffer, SIZE, "Permission Denial: "
510 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
511 IPCThreadState::self()->getCallingPid(),
512 IPCThreadState::self()->getCallingUid());
513 result.append(buffer);
515 Mutex::Autolock lock(mLock);
516 for (int i = 0, n = mClients.size(); i < n; ++i) {
517 sp<Client> c = mClients[i].promote();
518 if (c != 0) c->dump(fd, args);
520 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
521 result.append(" MediaRecorderClient\n");
522 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
523 snprintf(buffer, 255, " pid(%d)\n\n", c->mPid);
524 result.append(buffer);
527 result.append(" Files opened and/or mapped:\n");
528 snprintf(buffer, SIZE, "/proc/%d/maps", myTid());
529 FILE *f = fopen(buffer, "r");
532 fgets(buffer, SIZE, f);
533 if (strstr(buffer, " /sdcard/") ||
534 strstr(buffer, " /system/sounds/") ||
535 strstr(buffer, " /system/media/")) {
537 result.append(buffer);
542 result.append("couldn't open ");
543 result.append(buffer);
547 snprintf(buffer, SIZE, "/proc/%d/fd", myTid());
548 DIR *d = opendir(buffer);
551 while((ent = readdir(d)) != NULL) {
552 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
553 snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name);
555 if (lstat(buffer, &s) == 0) {
556 if ((s.st_mode & S_IFMT) == S_IFLNK) {
558 int len = readlink(buffer, linkto, sizeof(linkto));
568 if (strstr(linkto, "/sdcard/") == linkto ||
569 strstr(linkto, "/system/sounds/") == linkto ||
570 strstr(linkto, "/system/media/") == linkto) {
572 result.append(buffer);
573 result.append(" -> ");
574 result.append(linkto);
579 result.append(" unexpected type for ");
580 result.append(buffer);
588 result.append("couldn't open ");
589 result.append(buffer);
594 bool dumpMem = false;
595 for (size_t i = 0; i < args.size(); i++) {
596 if (args[i] == String16("-m")) {
605 write(fd, result.string(), result.size());
609 void MediaPlayerService::removeClient(wp<Client> client)
611 Mutex::Autolock lock(mLock);
612 mClients.remove(client);
615 MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid,
616 int32_t connId, const sp<IMediaPlayerClient>& client)
618 LOGV("Client(%d) constructor", connId);
625 #if CALLBACK_ANTAGONIZER
626 LOGD("create Antagonizer");
627 mAntagonizer = new Antagonizer(notify, this);
631 MediaPlayerService::Client::~Client()
633 LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
634 mAudioOutput.clear();
635 wp<Client> client(this);
637 mService->removeClient(client);
640 void MediaPlayerService::Client::disconnect()
642 LOGV("disconnect(%d) from pid %d", mConnId, mPid);
643 // grab local reference and clear main reference to prevent future
645 sp<MediaPlayerBase> p;
647 Mutex::Autolock l(mLock);
654 // clear the notification to prevent callbacks to dead client
655 // and reset the player. We assume the player will serialize
656 // access to itself if necessary.
658 p->setNotifyCallback(0, 0);
659 #if CALLBACK_ANTAGONIZER
660 LOGD("kill Antagonizer");
661 mAntagonizer->kill();
666 IPCThreadState::self()->flushCommands();
669 static player_type getDefaultPlayerType() {
670 #if BUILD_WITH_FULL_STAGEFRIGHT
671 char value[PROPERTY_VALUE_MAX];
672 if (property_get("media.stagefright.enable-player", value, NULL)
673 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
674 return STAGEFRIGHT_PLAYER;
681 // By default we use the VORBIS_PLAYER for vorbis playback (duh!),
682 // but if the magic property is set we will use our new experimental
683 // stagefright code instead.
684 static player_type OverrideStagefrightForVorbis(player_type player) {
685 if (player != VORBIS_PLAYER) {
689 #if BUILD_WITH_FULL_STAGEFRIGHT
690 char value[PROPERTY_VALUE_MAX];
691 if (property_get("media.stagefright.enable-vorbis", value, NULL)
692 && (!strcmp(value, "1") || !strcmp(value, "true"))) {
693 return STAGEFRIGHT_PLAYER;
697 return VORBIS_PLAYER;
701 player_type getPlayerType(int fd, int64_t offset, int64_t length)
704 lseek(fd, offset, SEEK_SET);
705 read(fd, buf, sizeof(buf));
706 lseek(fd, offset, SEEK_SET);
708 long ident = *((long*)buf);
711 if (ident == 0x5367674f) // 'OggS'
712 return OverrideStagefrightForVorbis(VORBIS_PLAYER);
715 if (ident == 0x75b22630) {
716 // The magic number for .asf files, i.e. wmv and wma content.
717 // These are not currently supported through stagefright.
722 // Some kind of MIDI?
723 EAS_DATA_HANDLE easdata;
724 if (EAS_Init(&easdata) == EAS_SUCCESS) {
728 locator.offset = offset;
729 locator.length = length;
730 EAS_HANDLE eashandle;
731 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
732 EAS_CloseFile(easdata, eashandle);
733 EAS_Shutdown(easdata);
734 return SONIVOX_PLAYER;
736 EAS_Shutdown(easdata);
739 return getDefaultPlayerType();
742 player_type getPlayerType(const char* url)
744 if (TestPlayerStub::canBeUsed(url)) {
748 bool useStagefrightForHTTP = false;
749 char value[PROPERTY_VALUE_MAX];
750 if (property_get("media.stagefright.enable-http", value, NULL)
751 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
752 useStagefrightForHTTP = true;
755 // use MidiFile for MIDI extensions
756 int lenURL = strlen(url);
757 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
758 int len = strlen(FILE_EXTS[i].extension);
759 int start = lenURL - len;
761 if (!strncmp(url + start, FILE_EXTS[i].extension, len)) {
762 if (FILE_EXTS[i].playertype == VORBIS_PLAYER
763 && !strncasecmp(url, "http://", 7)
764 && useStagefrightForHTTP) {
765 return STAGEFRIGHT_PLAYER;
767 return OverrideStagefrightForVorbis(FILE_EXTS[i].playertype);
772 if (!strncasecmp(url, "http://", 7)) {
773 if (!useStagefrightForHTTP) {
778 // Use PV_PLAYER for rtsp for now
779 if (!strncasecmp(url, "rtsp://", 7)) {
783 return getDefaultPlayerType();
786 static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
787 notify_callback_f notifyFunc)
789 sp<MediaPlayerBase> p;
790 switch (playerType) {
793 LOGV(" create PVPlayer");
798 LOGV(" create MidiFile");
802 LOGV(" create VorbisPlayer");
803 p = new VorbisPlayer();
805 #if BUILD_WITH_FULL_STAGEFRIGHT
806 case STAGEFRIGHT_PLAYER:
807 LOGV(" create StagefrightPlayer");
808 p = new StagefrightPlayer;
812 LOGV("Create Test Player stub");
813 p = new TestPlayerStub();
817 if (p->initCheck() == NO_ERROR) {
818 p->setNotifyCallback(cookie, notifyFunc);
824 LOGE("Failed to create player object");
829 sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
831 // determine if we have the right player type
832 sp<MediaPlayerBase> p = mPlayer;
833 if ((p != NULL) && (p->playerType() != playerType)) {
834 LOGV("delete player");
838 p = android::createPlayer(playerType, this, notify);
843 status_t MediaPlayerService::Client::setDataSource(
844 const char *url, const KeyedVector<String8, String8> *headers)
846 LOGV("setDataSource(%s)", url);
848 return UNKNOWN_ERROR;
850 if (strncmp(url, "content://", 10) == 0) {
851 // get a filedescriptor for the content Uri and
852 // pass it to the setDataSource(fd) method
855 int fd = android::openContentProviderFile(url16);
858 LOGE("Couldn't open fd for %s", url);
859 return UNKNOWN_ERROR;
861 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
865 player_type playerType = getPlayerType(url);
866 LOGV("player type = %d", playerType);
868 // create the right type of player
869 sp<MediaPlayerBase> p = createPlayer(playerType);
870 if (p == NULL) return NO_INIT;
872 if (!p->hardwareOutput()) {
873 mAudioOutput = new AudioOutput();
874 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
877 // now set data source
878 LOGV(" setDataSource");
879 mStatus = p->setDataSource(url, headers);
880 if (mStatus == NO_ERROR) {
883 LOGE(" error: %d", mStatus);
889 status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
891 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
893 int ret = fstat(fd, &sb);
895 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
896 return UNKNOWN_ERROR;
899 LOGV("st_dev = %llu", sb.st_dev);
900 LOGV("st_mode = %u", sb.st_mode);
901 LOGV("st_uid = %lu", sb.st_uid);
902 LOGV("st_gid = %lu", sb.st_gid);
903 LOGV("st_size = %llu", sb.st_size);
905 if (offset >= sb.st_size) {
906 LOGE("offset error");
908 return UNKNOWN_ERROR;
910 if (offset + length > sb.st_size) {
911 length = sb.st_size - offset;
912 LOGV("calculated length = %lld", length);
915 player_type playerType = getPlayerType(fd, offset, length);
916 LOGV("player type = %d", playerType);
918 // create the right type of player
919 sp<MediaPlayerBase> p = createPlayer(playerType);
920 if (p == NULL) return NO_INIT;
922 if (!p->hardwareOutput()) {
923 mAudioOutput = new AudioOutput();
924 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
927 // now set data source
928 mStatus = p->setDataSource(fd, offset, length);
929 if (mStatus == NO_ERROR) mPlayer = p;
933 status_t MediaPlayerService::Client::setVideoSurface(const sp<ISurface>& surface)
935 LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get());
936 sp<MediaPlayerBase> p = getPlayer();
937 if (p == 0) return UNKNOWN_ERROR;
938 return p->setVideoSurface(surface);
941 status_t MediaPlayerService::Client::invoke(const Parcel& request,
944 sp<MediaPlayerBase> p = getPlayer();
945 if (p == NULL) return UNKNOWN_ERROR;
946 return p->invoke(request, reply);
949 // This call doesn't need to access the native player.
950 status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
953 media::Metadata::Filter allow, drop;
955 if (unmarshallFilter(filter, &allow, &status) &&
956 unmarshallFilter(filter, &drop, &status)) {
957 Mutex::Autolock lock(mLock);
959 mMetadataAllow = allow;
960 mMetadataDrop = drop;
965 status_t MediaPlayerService::Client::getMetadata(
966 bool update_only, bool apply_filter, Parcel *reply)
968 sp<MediaPlayerBase> player = getPlayer();
969 if (player == 0) return UNKNOWN_ERROR;
972 // Placeholder for the return code, updated by the caller.
973 reply->writeInt32(-1);
975 media::Metadata::Filter ids;
977 // We don't block notifications while we fetch the data. We clear
978 // mMetadataUpdated first so we don't lose notifications happening
979 // during the rest of this call.
981 Mutex::Autolock lock(mLock);
983 ids = mMetadataUpdated;
985 mMetadataUpdated.clear();
988 media::Metadata metadata(reply);
990 metadata.appendHeader();
991 status = player->getMetadata(ids, reply);
994 metadata.resetParcel();
995 LOGE("getMetadata failed %d", status);
999 // FIXME: Implement filtering on the result. Not critical since
1000 // filtering takes place on the update notifications already. This
1001 // would be when all the metadata are fetch and a filter is set.
1003 // Everything is fine, update the metadata length.
1004 metadata.updateLength();
1008 status_t MediaPlayerService::Client::suspend() {
1009 sp<MediaPlayerBase> p = getPlayer();
1010 if (p == 0) return UNKNOWN_ERROR;
1012 return p->suspend();
1015 status_t MediaPlayerService::Client::resume() {
1016 sp<MediaPlayerBase> p = getPlayer();
1017 if (p == 0) return UNKNOWN_ERROR;
1022 status_t MediaPlayerService::Client::prepareAsync()
1024 LOGV("[%d] prepareAsync", mConnId);
1025 sp<MediaPlayerBase> p = getPlayer();
1026 if (p == 0) return UNKNOWN_ERROR;
1027 status_t ret = p->prepareAsync();
1028 #if CALLBACK_ANTAGONIZER
1029 LOGD("start Antagonizer");
1030 if (ret == NO_ERROR) mAntagonizer->start();
1035 status_t MediaPlayerService::Client::start()
1037 LOGV("[%d] start", mConnId);
1038 sp<MediaPlayerBase> p = getPlayer();
1039 if (p == 0) return UNKNOWN_ERROR;
1040 p->setLooping(mLoop);
1044 status_t MediaPlayerService::Client::stop()
1046 LOGV("[%d] stop", mConnId);
1047 sp<MediaPlayerBase> p = getPlayer();
1048 if (p == 0) return UNKNOWN_ERROR;
1052 status_t MediaPlayerService::Client::pause()
1054 LOGV("[%d] pause", mConnId);
1055 sp<MediaPlayerBase> p = getPlayer();
1056 if (p == 0) return UNKNOWN_ERROR;
1060 status_t MediaPlayerService::Client::isPlaying(bool* state)
1063 sp<MediaPlayerBase> p = getPlayer();
1064 if (p == 0) return UNKNOWN_ERROR;
1065 *state = p->isPlaying();
1066 LOGV("[%d] isPlaying: %d", mConnId, *state);
1070 status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1072 LOGV("getCurrentPosition");
1073 sp<MediaPlayerBase> p = getPlayer();
1074 if (p == 0) return UNKNOWN_ERROR;
1075 status_t ret = p->getCurrentPosition(msec);
1076 if (ret == NO_ERROR) {
1077 LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1079 LOGE("getCurrentPosition returned %d", ret);
1084 status_t MediaPlayerService::Client::getDuration(int *msec)
1086 LOGV("getDuration");
1087 sp<MediaPlayerBase> p = getPlayer();
1088 if (p == 0) return UNKNOWN_ERROR;
1089 status_t ret = p->getDuration(msec);
1090 if (ret == NO_ERROR) {
1091 LOGV("[%d] getDuration = %d", mConnId, *msec);
1093 LOGE("getDuration returned %d", ret);
1098 status_t MediaPlayerService::Client::seekTo(int msec)
1100 LOGV("[%d] seekTo(%d)", mConnId, msec);
1101 sp<MediaPlayerBase> p = getPlayer();
1102 if (p == 0) return UNKNOWN_ERROR;
1103 return p->seekTo(msec);
1106 status_t MediaPlayerService::Client::reset()
1108 LOGV("[%d] reset", mConnId);
1109 sp<MediaPlayerBase> p = getPlayer();
1110 if (p == 0) return UNKNOWN_ERROR;
1114 status_t MediaPlayerService::Client::setAudioStreamType(int type)
1116 LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1117 // TODO: for hardware output, call player instead
1118 Mutex::Autolock l(mLock);
1119 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1123 status_t MediaPlayerService::Client::setLooping(int loop)
1125 LOGV("[%d] setLooping(%d)", mConnId, loop);
1127 sp<MediaPlayerBase> p = getPlayer();
1128 if (p != 0) return p->setLooping(loop);
1132 status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1134 LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1135 // TODO: for hardware output, call player instead
1136 Mutex::Autolock l(mLock);
1137 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1142 void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2)
1144 Client* client = static_cast<Client*>(cookie);
1146 if (MEDIA_INFO == msg &&
1147 MEDIA_INFO_METADATA_UPDATE == ext1) {
1148 const media::Metadata::Type metadata_type = ext2;
1150 if(client->shouldDropMetadata(metadata_type)) {
1154 // Update the list of metadata that have changed. getMetadata
1155 // also access mMetadataUpdated and clears it.
1156 client->addNewMetadataUpdate(metadata_type);
1158 LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1159 client->mClient->notify(msg, ext1, ext2);
1163 bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
1165 Mutex::Autolock lock(mLock);
1167 if (findMetadata(mMetadataDrop, code)) {
1171 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1179 void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1180 Mutex::Autolock lock(mLock);
1181 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1182 mMetadataUpdated.add(metadata_type);
1186 #if CALLBACK_ANTAGONIZER
1187 const int Antagonizer::interval = 10000; // 10 msecs
1189 Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1190 mExit(false), mActive(false), mClient(client), mCb(cb)
1192 createThread(callbackThread, this);
1195 void Antagonizer::kill()
1197 Mutex::Autolock _l(mLock);
1200 mCondition.wait(mLock);
1203 int Antagonizer::callbackThread(void* user)
1205 LOGD("Antagonizer started");
1206 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1210 p->mCb(p->mClient, 0, 0, 0);
1214 Mutex::Autolock _l(p->mLock);
1215 p->mCondition.signal();
1216 LOGD("Antagonizer stopped");
1221 static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1223 sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1225 LOGV("decode(%s)", url);
1227 sp<MediaPlayerBase> player;
1229 // Protect our precious, precious DRMd ringtones by only allowing
1230 // decoding of http, but not filesystem paths or content Uris.
1231 // If the application wants to decode those, it should open a
1232 // filedescriptor for them and use that.
1233 if (url != NULL && strncmp(url, "http://", 7) != 0) {
1234 LOGD("Can't decode %s by path, use filedescriptor instead", url);
1238 player_type playerType = getPlayerType(url);
1239 LOGV("player type = %d", playerType);
1241 // create the right type of player
1242 sp<AudioCache> cache = new AudioCache(url);
1243 player = android::createPlayer(playerType, cache.get(), cache->notify);
1244 if (player == NULL) goto Exit;
1245 if (player->hardwareOutput()) goto Exit;
1247 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1250 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1253 player->prepareAsync();
1255 LOGV("wait for prepare");
1256 if (cache->wait() != NO_ERROR) goto Exit;
1261 LOGV("wait for playback complete");
1262 if (cache->wait() != NO_ERROR) goto Exit;
1264 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1265 *pSampleRate = cache->sampleRate();
1266 *pNumChannels = cache->channelCount();
1267 *pFormat = cache->format();
1268 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1271 if (player != 0) player->reset();
1275 sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1277 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1279 sp<MediaPlayerBase> player;
1281 player_type playerType = getPlayerType(fd, offset, length);
1282 LOGV("player type = %d", playerType);
1284 // create the right type of player
1285 sp<AudioCache> cache = new AudioCache("decode_fd");
1286 player = android::createPlayer(playerType, cache.get(), cache->notify);
1287 if (player == NULL) goto Exit;
1288 if (player->hardwareOutput()) goto Exit;
1290 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1293 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1296 player->prepareAsync();
1298 LOGV("wait for prepare");
1299 if (cache->wait() != NO_ERROR) goto Exit;
1304 LOGV("wait for playback complete");
1305 if (cache->wait() != NO_ERROR) goto Exit;
1307 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1308 *pSampleRate = cache->sampleRate();
1309 *pNumChannels = cache->channelCount();
1310 *pFormat = cache->format();
1311 LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1314 if (player != 0) player->reset();
1320 * Avert your eyes, ugly hack ahead.
1321 * The following is to support music visualizations.
1324 static const int NUMVIZBUF = 32;
1325 static const int VIZBUFFRAMES = 1024;
1326 static const int BUFTIMEMSEC = NUMVIZBUF * VIZBUFFRAMES * 1000 / 44100;
1327 static const int TOTALBUFTIMEMSEC = NUMVIZBUF * BUFTIMEMSEC;
1329 static bool gotMem = false;
1330 static sp<MemoryHeapBase> heap;
1331 static sp<MemoryBase> mem[NUMVIZBUF];
1332 static uint64_t endTime;
1333 static uint64_t lastReadTime;
1334 static uint64_t lastWriteTime;
1335 static int writeIdx = 0;
1337 static void allocVizBufs() {
1339 heap = new MemoryHeapBase(NUMVIZBUF * VIZBUFFRAMES * 2, 0, "snooper");
1340 for (int i=0;i<NUMVIZBUF;i++) {
1341 mem[i] = new MemoryBase(heap, VIZBUFFRAMES * 2 * i, VIZBUFFRAMES * 2);
1350 * Get a buffer of audio data that is about to be played.
1351 * We don't synchronize this because in practice the writer
1352 * is ahead of the reader, and even if we did happen to catch
1353 * a buffer while it's being written, it's just a visualization,
1356 static sp<MemoryBase> getVizBuffer() {
1360 lastReadTime = uptimeMillis();
1362 // if there is no recent buffer (yet), just return empty handed
1363 if (lastWriteTime + TOTALBUFTIMEMSEC < lastReadTime) {
1364 //LOGI("@@@@ no audio data to look at yet: %d + %d < %d", (int)lastWriteTime, TOTALBUFTIMEMSEC, (int)lastReadTime);
1368 int timedelta = endTime - lastReadTime;
1369 if (timedelta < 0) timedelta = 0;
1370 int framedelta = timedelta * 44100 / 1000;
1371 int headIdx = (writeIdx - framedelta) / VIZBUFFRAMES - 1;
1372 while (headIdx < 0) {
1373 headIdx += NUMVIZBUF;
1375 return mem[headIdx];
1378 // Append the data to the vizualization buffer
1379 static void makeVizBuffers(const char *data, int len, uint64_t time) {
1383 uint64_t startTime = time;
1384 const int frameSize = 4; // 16 bit stereo sample is 4 bytes
1385 int offset = writeIdx;
1386 int maxoff = heap->getSize() / 2; // in shorts
1387 short *base = (short*)heap->getBase();
1388 short *src = (short*)data;
1391 // Degrade quality by mixing to mono and clearing the lowest 3 bits.
1392 // This should still be good enough for a visualization
1393 base[offset++] = ((int(src[0]) + int(src[1])) >> 1) & ~0x7;
1396 if (offset >= maxoff) {
1401 endTime = time + (len / frameSize) / 44;
1402 //LOGI("@@@ stored buffers from %d to %d", uint32_t(startTime), uint32_t(time));
1405 sp<IMemory> MediaPlayerService::snoop()
1407 sp<MemoryBase> mem = getVizBuffer();
1413 #define LOG_TAG "AudioSink"
1414 MediaPlayerService::AudioOutput::AudioOutput()
1416 mCallbackCookie(NULL) {
1418 mStreamType = AudioSystem::MUSIC;
1423 mNumFramesWritten = 0;
1424 setMinBufferCount();
1427 MediaPlayerService::AudioOutput::~AudioOutput()
1432 void MediaPlayerService::AudioOutput::setMinBufferCount()
1434 char value[PROPERTY_VALUE_MAX];
1435 if (property_get("ro.kernel.qemu", value, 0)) {
1436 mIsOnEmulator = true;
1437 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1441 bool MediaPlayerService::AudioOutput::isOnEmulator()
1443 setMinBufferCount();
1444 return mIsOnEmulator;
1447 int MediaPlayerService::AudioOutput::getMinBufferCount()
1449 setMinBufferCount();
1450 return mMinBufferCount;
1453 ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1455 if (mTrack == 0) return NO_INIT;
1456 return mTrack->frameCount() * frameSize();
1459 ssize_t MediaPlayerService::AudioOutput::frameCount() const
1461 if (mTrack == 0) return NO_INIT;
1462 return mTrack->frameCount();
1465 ssize_t MediaPlayerService::AudioOutput::channelCount() const
1467 if (mTrack == 0) return NO_INIT;
1468 return mTrack->channelCount();
1471 ssize_t MediaPlayerService::AudioOutput::frameSize() const
1473 if (mTrack == 0) return NO_INIT;
1474 return mTrack->frameSize();
1477 uint32_t MediaPlayerService::AudioOutput::latency () const
1482 float MediaPlayerService::AudioOutput::msecsPerFrame() const
1484 return mMsecsPerFrame;
1487 status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1489 if (mTrack == 0) return NO_INIT;
1490 return mTrack->getPosition(position);
1493 status_t MediaPlayerService::AudioOutput::open(
1494 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1495 AudioCallback cb, void *cookie)
1498 mCallbackCookie = cookie;
1500 // Check argument "bufferCount" against the mininum buffer count
1501 if (bufferCount < mMinBufferCount) {
1502 LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1503 bufferCount = mMinBufferCount;
1506 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
1507 if (mTrack) close();
1512 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1515 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1519 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1522 if (mCallback != NULL) {
1527 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1537 (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,
1541 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1542 LOGE("Unable to create audio track");
1548 t->setVolume(mLeftVolume, mRightVolume);
1549 mMsecsPerFrame = 1.e3 / (float) sampleRate;
1550 mLatency = t->latency();
1555 void MediaPlayerService::AudioOutput::start()
1559 mTrack->setVolume(mLeftVolume, mRightVolume);
1561 mTrack->getPosition(&mNumFramesWritten);
1565 void MediaPlayerService::AudioOutput::snoopWrite(const void* buffer, size_t size) {
1566 // Only make visualization buffers if anyone recently requested visualization data
1567 uint64_t now = uptimeMillis();
1568 if (lastReadTime + TOTALBUFTIMEMSEC >= now) {
1569 // Based on the current play counter, the number of frames written and
1570 // the current real time we can calculate the approximate real start
1571 // time of the buffer we're about to write.
1573 mTrack->getPosition(&pos);
1575 // we're writing ahead by this many frames:
1576 int ahead = mNumFramesWritten - pos;
1577 //LOGI("@@@ written: %d, playpos: %d, latency: %d", mNumFramesWritten, pos, mTrack->latency());
1578 // which is this many milliseconds, assuming 44100 Hz:
1581 makeVizBuffers((const char*)buffer, size, now + ahead + mTrack->latency());
1582 lastWriteTime = now;
1587 ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1589 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1591 //LOGV("write(%p, %u)", buffer, size);
1593 snoopWrite(buffer, size);
1594 ssize_t ret = mTrack->write(buffer, size);
1595 mNumFramesWritten += ret / 4; // assume 16 bit stereo
1601 void MediaPlayerService::AudioOutput::stop()
1604 if (mTrack) mTrack->stop();
1608 void MediaPlayerService::AudioOutput::flush()
1611 if (mTrack) mTrack->flush();
1614 void MediaPlayerService::AudioOutput::pause()
1617 if (mTrack) mTrack->pause();
1621 void MediaPlayerService::AudioOutput::close()
1628 void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1630 LOGV("setVolume(%f, %f)", left, right);
1632 mRightVolume = right;
1634 mTrack->setVolume(left, right);
1639 void MediaPlayerService::AudioOutput::CallbackWrapper(
1640 int event, void *cookie, void *info) {
1641 //LOGV("callbackwrapper");
1642 if (event != AudioTrack::EVENT_MORE_DATA) {
1646 AudioOutput *me = (AudioOutput *)cookie;
1647 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1649 size_t actualSize = (*me->mCallback)(
1650 me, buffer->raw, buffer->size, me->mCallbackCookie);
1652 buffer->size = actualSize;
1654 if (actualSize > 0) {
1655 me->snoopWrite(buffer->raw, actualSize);
1660 #define LOG_TAG "AudioCache"
1661 MediaPlayerService::AudioCache::AudioCache(const char* name) :
1662 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1663 mError(NO_ERROR), mCommandComplete(false)
1665 // create ashmem heap
1666 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1669 uint32_t MediaPlayerService::AudioCache::latency () const
1674 float MediaPlayerService::AudioCache::msecsPerFrame() const
1676 return mMsecsPerFrame;
1679 status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1681 if (position == 0) return BAD_VALUE;
1686 ////////////////////////////////////////////////////////////////////////////////
1688 struct CallbackThread : public Thread {
1689 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1690 MediaPlayerBase::AudioSink::AudioCallback cb,
1694 virtual ~CallbackThread();
1696 virtual bool threadLoop();
1699 wp<MediaPlayerBase::AudioSink> mSink;
1700 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1705 CallbackThread(const CallbackThread &);
1706 CallbackThread &operator=(const CallbackThread &);
1709 CallbackThread::CallbackThread(
1710 const wp<MediaPlayerBase::AudioSink> &sink,
1711 MediaPlayerBase::AudioSink::AudioCallback cb,
1720 CallbackThread::~CallbackThread() {
1727 bool CallbackThread::threadLoop() {
1728 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1733 if (mBuffer == NULL) {
1734 mBufferSize = sink->bufferSize();
1735 mBuffer = malloc(mBufferSize);
1739 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1741 if (actualSize > 0) {
1742 sink->write(mBuffer, actualSize);
1748 ////////////////////////////////////////////////////////////////////////////////
1750 status_t MediaPlayerService::AudioCache::open(
1751 uint32_t sampleRate, int channelCount, int format, int bufferCount,
1752 AudioCallback cb, void *cookie)
1754 LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
1755 if (mHeap->getHeapID() < 0) {
1759 mSampleRate = sampleRate;
1760 mChannelCount = (uint16_t)channelCount;
1761 mFormat = (uint16_t)format;
1762 mMsecsPerFrame = 1.e3 / (float) sampleRate;
1765 mCallbackThread = new CallbackThread(this, cb, cookie);
1770 void MediaPlayerService::AudioCache::start() {
1771 if (mCallbackThread != NULL) {
1772 mCallbackThread->run("AudioCache callback");
1776 void MediaPlayerService::AudioCache::stop() {
1777 if (mCallbackThread != NULL) {
1778 mCallbackThread->requestExitAndWait();
1782 ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1784 LOGV("write(%p, %u)", buffer, size);
1785 if ((buffer == 0) || (size == 0)) return size;
1787 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1788 if (p == NULL) return NO_INIT;
1790 LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1791 if (mSize + size > mHeap->getSize()) {
1792 LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1793 size = mHeap->getSize() - mSize;
1795 memcpy(p, buffer, size);
1800 // call with lock held
1801 status_t MediaPlayerService::AudioCache::wait()
1803 Mutex::Autolock lock(mLock);
1804 while (!mCommandComplete) {
1805 mSignal.wait(mLock);
1807 mCommandComplete = false;
1809 if (mError == NO_ERROR) {
1810 LOGV("wait - success");
1812 LOGV("wait - error");
1817 void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2)
1819 LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1820 AudioCache* p = static_cast<AudioCache*>(cookie);
1822 // ignore buffering messages
1826 LOGE("Error %d, %d occurred", ext1, ext2);
1829 case MEDIA_PREPARED:
1832 case MEDIA_PLAYBACK_COMPLETE:
1833 LOGV("playback complete");
1841 Mutex::Autolock lock(p->mLock);
1842 p->mCommandComplete = true;
1843 p->mSignal.signal();
1846 } // namespace android