1/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
31
32#include <cutils/atomic.h>
33#include <cutils/properties.h> // for property_get
34
35#include <utils/misc.h>
36
37#include <android_runtime/ActivityManager.h>
38
39#include <binder/IPCThreadState.h>
40#include <binder/IServiceManager.h>
41#include <binder/MemoryHeapBase.h>
42#include <binder/MemoryBase.h>
43#include <gui/SurfaceTextureClient.h>
44#include <utils/Errors.h>  // for status_t
45#include <utils/String8.h>
46#include <utils/SystemClock.h>
47#include <utils/Vector.h>
48#include <cutils/properties.h>
49
50#include <media/MediaPlayerInterface.h>
51#include <media/mediarecorder.h>
52#include <media/MediaMetadataRetrieverInterface.h>
53#include <media/Metadata.h>
54#include <media/AudioTrack.h>
55#include <media/MemoryLeakTrackUtil.h>
56#include <media/stagefright/MediaErrors.h>
57
58#include <system/audio.h>
59
60#include <private/android_filesystem_config.h>
61
62#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
65
66#include "MidiFile.h"
67#include "TestPlayerStub.h"
68#include "StagefrightPlayer.h"
69#include "nuplayer/NuPlayerDriver.h"
70
71#include <OMX.h>
72
73namespace {
74using android::media::Metadata;
75using android::status_t;
76using android::OK;
77using android::BAD_VALUE;
78using android::NOT_ENOUGH_DATA;
79using android::Parcel;
80
81// Max number of entries in the filter.
82const int kMaxFilterSize = 64;  // I pulled that out of thin air.
83
84// FIXME: Move all the metadata related function in the Metadata.cpp
85
86
87// Unmarshall a filter from a Parcel.
88// Filter format in a parcel:
89//
90//  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
91// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92// |                       number of entries (n)                   |
93// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94// |                       metadata type 1                         |
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// |                       metadata type 2                         |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98//  ....
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// |                       metadata type n                         |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102//
103// @param p Parcel that should start with a filter.
104// @param[out] filter On exit contains the list of metadata type to be
105//                    filtered.
106// @param[out] status On exit contains the status code to be returned.
107// @return true if the parcel starts with a valid filter.
108bool unmarshallFilter(const Parcel& p,
109                      Metadata::Filter *filter,
110                      status_t *status)
111{
112    int32_t val;
113    if (p.readInt32(&val) != OK)
114    {
115        LOGE("Failed to read filter's length");
116        *status = NOT_ENOUGH_DATA;
117        return false;
118    }
119
120    if( val > kMaxFilterSize || val < 0)
121    {
122        LOGE("Invalid filter len %d", val);
123        *status = BAD_VALUE;
124        return false;
125    }
126
127    const size_t num = val;
128
129    filter->clear();
130    filter->setCapacity(num);
131
132    size_t size = num * sizeof(Metadata::Type);
133
134
135    if (p.dataAvail() < size)
136    {
137        LOGE("Filter too short expected %d but got %d", size, p.dataAvail());
138        *status = NOT_ENOUGH_DATA;
139        return false;
140    }
141
142    const Metadata::Type *data =
143            static_cast<const Metadata::Type*>(p.readInplace(size));
144
145    if (NULL == data)
146    {
147        LOGE("Filter had no data");
148        *status = BAD_VALUE;
149        return false;
150    }
151
152    // TODO: The stl impl of vector would be more efficient here
153    // because it degenerates into a memcpy on pod types. Try to
154    // replace later or use stl::set.
155    for (size_t i = 0; i < num; ++i)
156    {
157        filter->add(*data);
158        ++data;
159    }
160    *status = OK;
161    return true;
162}
163
164// @param filter Of metadata type.
165// @param val To be searched.
166// @return true if a match was found.
167bool findMetadata(const Metadata::Filter& filter, const int32_t val)
168{
169    // Deal with empty and ANY right away
170    if (filter.isEmpty()) return false;
171    if (filter[0] == Metadata::kAny) return true;
172
173    return filter.indexOf(val) >= 0;
174}
175
176}  // anonymous namespace
177
178
179namespace android {
180
181static bool checkPermission(const char* permissionString) {
182#ifndef HAVE_ANDROID_OS
183    return true;
184#endif
185    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
186    bool ok = checkCallingPermission(String16(permissionString));
187    if (!ok) LOGE("Request requires %s", permissionString);
188    return ok;
189}
190
191// TODO: Temp hack until we can register players
192typedef struct {
193    const char *extension;
194    const player_type playertype;
195} extmap;
196extmap FILE_EXTS [] =  {
197        {".mid", SONIVOX_PLAYER},
198        {".midi", SONIVOX_PLAYER},
199        {".smf", SONIVOX_PLAYER},
200        {".xmf", SONIVOX_PLAYER},
201        {".imy", SONIVOX_PLAYER},
202        {".rtttl", SONIVOX_PLAYER},
203        {".rtx", SONIVOX_PLAYER},
204        {".ota", SONIVOX_PLAYER},
205};
206
207// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
208/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
209/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
210
211void MediaPlayerService::instantiate() {
212    defaultServiceManager()->addService(
213            String16("media.player"), new MediaPlayerService());
214}
215
216MediaPlayerService::MediaPlayerService()
217{
218    LOGV("MediaPlayerService created");
219    mNextConnId = 1;
220
221    mBatteryAudio.refCount = 0;
222    for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
223        mBatteryAudio.deviceOn[i] = 0;
224        mBatteryAudio.lastTime[i] = 0;
225        mBatteryAudio.totalTime[i] = 0;
226    }
227    // speaker is on by default
228    mBatteryAudio.deviceOn[SPEAKER] = 1;
229}
230
231MediaPlayerService::~MediaPlayerService()
232{
233    LOGV("MediaPlayerService destroyed");
234}
235
236sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
237{
238    sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
239    wp<MediaRecorderClient> w = recorder;
240    Mutex::Autolock lock(mLock);
241    mMediaRecorderClients.add(w);
242    LOGV("Create new media recorder client from pid %d", pid);
243    return recorder;
244}
245
246void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
247{
248    Mutex::Autolock lock(mLock);
249    mMediaRecorderClients.remove(client);
250    LOGV("Delete media recorder client");
251}
252
253sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
254{
255    sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
256    LOGV("Create new media retriever from pid %d", pid);
257    return retriever;
258}
259
260sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
261        int audioSessionId)
262{
263    int32_t connId = android_atomic_inc(&mNextConnId);
264
265    sp<Client> c = new Client(
266            this, pid, connId, client, audioSessionId,
267            IPCThreadState::self()->getCallingUid());
268
269    LOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
270         IPCThreadState::self()->getCallingUid());
271
272    wp<Client> w = c;
273    {
274        Mutex::Autolock lock(mLock);
275        mClients.add(w);
276    }
277    return c;
278}
279
280sp<IOMX> MediaPlayerService::getOMX() {
281    Mutex::Autolock autoLock(mLock);
282
283    if (mOMX.get() == NULL) {
284        mOMX = new OMX;
285    }
286
287    return mOMX;
288}
289
290status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
291{
292    const size_t SIZE = 256;
293    char buffer[SIZE];
294    String8 result;
295
296    result.append(" AudioCache\n");
297    if (mHeap != 0) {
298        snprintf(buffer, 255, "  heap base(%p), size(%d), flags(%d), device(%s)\n",
299                mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
300        result.append(buffer);
301    }
302    snprintf(buffer, 255, "  msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
303            mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
304    result.append(buffer);
305    snprintf(buffer, 255, "  sample rate(%d), size(%d), error(%d), command complete(%s)\n",
306            mSampleRate, mSize, mError, mCommandComplete?"true":"false");
307    result.append(buffer);
308    ::write(fd, result.string(), result.size());
309    return NO_ERROR;
310}
311
312status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
313{
314    const size_t SIZE = 256;
315    char buffer[SIZE];
316    String8 result;
317
318    result.append(" AudioOutput\n");
319    snprintf(buffer, 255, "  stream type(%d), left - right volume(%f, %f)\n",
320            mStreamType, mLeftVolume, mRightVolume);
321    result.append(buffer);
322    snprintf(buffer, 255, "  msec per frame(%f), latency (%d)\n",
323            mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
324    result.append(buffer);
325    snprintf(buffer, 255, "  aux effect id(%d), send level (%f)\n",
326            mAuxEffectId, mSendLevel);
327    result.append(buffer);
328
329    ::write(fd, result.string(), result.size());
330    if (mTrack != 0) {
331        mTrack->dump(fd, args);
332    }
333    return NO_ERROR;
334}
335
336status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
337{
338    const size_t SIZE = 256;
339    char buffer[SIZE];
340    String8 result;
341    result.append(" Client\n");
342    snprintf(buffer, 255, "  pid(%d), connId(%d), status(%d), looping(%s)\n",
343            mPid, mConnId, mStatus, mLoop?"true": "false");
344    result.append(buffer);
345    write(fd, result.string(), result.size());
346    if (mPlayer != NULL) {
347        mPlayer->dump(fd, args);
348    }
349    if (mAudioOutput != 0) {
350        mAudioOutput->dump(fd, args);
351    }
352    write(fd, "\n", 1);
353    return NO_ERROR;
354}
355
356status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
357{
358    const size_t SIZE = 256;
359    char buffer[SIZE];
360    String8 result;
361    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
362        snprintf(buffer, SIZE, "Permission Denial: "
363                "can't dump MediaPlayerService from pid=%d, uid=%d\n",
364                IPCThreadState::self()->getCallingPid(),
365                IPCThreadState::self()->getCallingUid());
366        result.append(buffer);
367    } else {
368        Mutex::Autolock lock(mLock);
369        for (int i = 0, n = mClients.size(); i < n; ++i) {
370            sp<Client> c = mClients[i].promote();
371            if (c != 0) c->dump(fd, args);
372        }
373        if (mMediaRecorderClients.size() == 0) {
374                result.append(" No media recorder client\n\n");
375        } else {
376            for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
377                sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
378                if (c != 0) {
379                    snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
380                    result.append(buffer);
381                    write(fd, result.string(), result.size());
382                    result = "\n";
383                    c->dump(fd, args);
384                }
385            }
386        }
387
388        result.append(" Files opened and/or mapped:\n");
389        snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
390        FILE *f = fopen(buffer, "r");
391        if (f) {
392            while (!feof(f)) {
393                fgets(buffer, SIZE, f);
394                if (strstr(buffer, " /mnt/sdcard/") ||
395                    strstr(buffer, " /system/sounds/") ||
396                    strstr(buffer, " /data/") ||
397                    strstr(buffer, " /system/media/")) {
398                    result.append("  ");
399                    result.append(buffer);
400                }
401            }
402            fclose(f);
403        } else {
404            result.append("couldn't open ");
405            result.append(buffer);
406            result.append("\n");
407        }
408
409        snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
410        DIR *d = opendir(buffer);
411        if (d) {
412            struct dirent *ent;
413            while((ent = readdir(d)) != NULL) {
414                if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
415                    snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
416                    struct stat s;
417                    if (lstat(buffer, &s) == 0) {
418                        if ((s.st_mode & S_IFMT) == S_IFLNK) {
419                            char linkto[256];
420                            int len = readlink(buffer, linkto, sizeof(linkto));
421                            if(len > 0) {
422                                if(len > 255) {
423                                    linkto[252] = '.';
424                                    linkto[253] = '.';
425                                    linkto[254] = '.';
426                                    linkto[255] = 0;
427                                } else {
428                                    linkto[len] = 0;
429                                }
430                                if (strstr(linkto, "/mnt/sdcard/") == linkto ||
431                                    strstr(linkto, "/system/sounds/") == linkto ||
432                                    strstr(linkto, "/data/") == linkto ||
433                                    strstr(linkto, "/system/media/") == linkto) {
434                                    result.append("  ");
435                                    result.append(buffer);
436                                    result.append(" -> ");
437                                    result.append(linkto);
438                                    result.append("\n");
439                                }
440                            }
441                        } else {
442                            result.append("  unexpected type for ");
443                            result.append(buffer);
444                            result.append("\n");
445                        }
446                    }
447                }
448            }
449            closedir(d);
450        } else {
451            result.append("couldn't open ");
452            result.append(buffer);
453            result.append("\n");
454        }
455
456        bool dumpMem = false;
457        for (size_t i = 0; i < args.size(); i++) {
458            if (args[i] == String16("-m")) {
459                dumpMem = true;
460            }
461        }
462        if (dumpMem) {
463            dumpMemoryAddresses(fd);
464        }
465    }
466    write(fd, result.string(), result.size());
467    return NO_ERROR;
468}
469
470void MediaPlayerService::removeClient(wp<Client> client)
471{
472    Mutex::Autolock lock(mLock);
473    mClients.remove(client);
474}
475
476MediaPlayerService::Client::Client(
477        const sp<MediaPlayerService>& service, pid_t pid,
478        int32_t connId, const sp<IMediaPlayerClient>& client,
479        int audioSessionId, uid_t uid)
480{
481    LOGV("Client(%d) constructor", connId);
482    mPid = pid;
483    mConnId = connId;
484    mService = service;
485    mClient = client;
486    mLoop = false;
487    mStatus = NO_INIT;
488    mAudioSessionId = audioSessionId;
489    mUID = uid;
490
491#if CALLBACK_ANTAGONIZER
492    LOGD("create Antagonizer");
493    mAntagonizer = new Antagonizer(notify, this);
494#endif
495}
496
497MediaPlayerService::Client::~Client()
498{
499    LOGV("Client(%d) destructor pid = %d", mConnId, mPid);
500    mAudioOutput.clear();
501    wp<Client> client(this);
502    disconnect();
503    mService->removeClient(client);
504}
505
506void MediaPlayerService::Client::disconnect()
507{
508    LOGV("disconnect(%d) from pid %d", mConnId, mPid);
509    // grab local reference and clear main reference to prevent future
510    // access to object
511    sp<MediaPlayerBase> p;
512    {
513        Mutex::Autolock l(mLock);
514        p = mPlayer;
515    }
516    mClient.clear();
517
518    mPlayer.clear();
519
520    // clear the notification to prevent callbacks to dead client
521    // and reset the player. We assume the player will serialize
522    // access to itself if necessary.
523    if (p != 0) {
524        p->setNotifyCallback(0, 0);
525#if CALLBACK_ANTAGONIZER
526        LOGD("kill Antagonizer");
527        mAntagonizer->kill();
528#endif
529        p->reset();
530    }
531
532    disconnectNativeWindow();
533
534    IPCThreadState::self()->flushCommands();
535}
536
537static player_type getDefaultPlayerType() {
538    return STAGEFRIGHT_PLAYER;
539}
540
541player_type getPlayerType(int fd, int64_t offset, int64_t length)
542{
543    char buf[20];
544    lseek(fd, offset, SEEK_SET);
545    read(fd, buf, sizeof(buf));
546    lseek(fd, offset, SEEK_SET);
547
548    long ident = *((long*)buf);
549
550    // Ogg vorbis?
551    if (ident == 0x5367674f) // 'OggS'
552        return STAGEFRIGHT_PLAYER;
553
554    // Some kind of MIDI?
555    EAS_DATA_HANDLE easdata;
556    if (EAS_Init(&easdata) == EAS_SUCCESS) {
557        EAS_FILE locator;
558        locator.path = NULL;
559        locator.fd = fd;
560        locator.offset = offset;
561        locator.length = length;
562        EAS_HANDLE  eashandle;
563        if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
564            EAS_CloseFile(easdata, eashandle);
565            EAS_Shutdown(easdata);
566            return SONIVOX_PLAYER;
567        }
568        EAS_Shutdown(easdata);
569    }
570
571    return getDefaultPlayerType();
572}
573
574player_type getPlayerType(const char* url)
575{
576    if (TestPlayerStub::canBeUsed(url)) {
577        return TEST_PLAYER;
578    }
579
580    if (!strncasecmp("http://", url, 7)
581            || !strncasecmp("https://", url, 8)) {
582        size_t len = strlen(url);
583        if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
584            return NU_PLAYER;
585        }
586
587        if (strstr(url,"m3u8")) {
588            return NU_PLAYER;
589        }
590    }
591
592    if (!strncasecmp("rtsp://", url, 7)) {
593        return NU_PLAYER;
594    }
595
596    // use MidiFile for MIDI extensions
597    int lenURL = strlen(url);
598    for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
599        int len = strlen(FILE_EXTS[i].extension);
600        int start = lenURL - len;
601        if (start > 0) {
602            if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
603                return FILE_EXTS[i].playertype;
604            }
605        }
606    }
607
608    return getDefaultPlayerType();
609}
610
611static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
612        notify_callback_f notifyFunc)
613{
614    sp<MediaPlayerBase> p;
615    switch (playerType) {
616        case SONIVOX_PLAYER:
617            LOGV(" create MidiFile");
618            p = new MidiFile();
619            break;
620        case STAGEFRIGHT_PLAYER:
621            LOGV(" create StagefrightPlayer");
622            p = new StagefrightPlayer;
623            break;
624        case NU_PLAYER:
625            LOGV(" create NuPlayer");
626            p = new NuPlayerDriver;
627            break;
628        case TEST_PLAYER:
629            LOGV("Create Test Player stub");
630            p = new TestPlayerStub();
631            break;
632        default:
633            LOGE("Unknown player type: %d", playerType);
634            return NULL;
635    }
636    if (p != NULL) {
637        if (p->initCheck() == NO_ERROR) {
638            p->setNotifyCallback(cookie, notifyFunc);
639        } else {
640            p.clear();
641        }
642    }
643    if (p == NULL) {
644        LOGE("Failed to create player object");
645    }
646    return p;
647}
648
649sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
650{
651    // determine if we have the right player type
652    sp<MediaPlayerBase> p = mPlayer;
653    if ((p != NULL) && (p->playerType() != playerType)) {
654        LOGV("delete player");
655        p.clear();
656    }
657    if (p == NULL) {
658        p = android::createPlayer(playerType, this, notify);
659    }
660
661    if (p != NULL) {
662        p->setUID(mUID);
663    }
664
665    return p;
666}
667
668status_t MediaPlayerService::Client::setDataSource(
669        const char *url, const KeyedVector<String8, String8> *headers)
670{
671    LOGV("setDataSource(%s)", url);
672    if (url == NULL)
673        return UNKNOWN_ERROR;
674
675    if ((strncmp(url, "http://", 7) == 0) ||
676        (strncmp(url, "https://", 8) == 0) ||
677        (strncmp(url, "rtsp://", 7) == 0)) {
678        if (!checkPermission("android.permission.INTERNET")) {
679            return PERMISSION_DENIED;
680        }
681    }
682
683    if (strncmp(url, "content://", 10) == 0) {
684        // get a filedescriptor for the content Uri and
685        // pass it to the setDataSource(fd) method
686
687        String16 url16(url);
688        int fd = android::openContentProviderFile(url16);
689        if (fd < 0)
690        {
691            LOGE("Couldn't open fd for %s", url);
692            return UNKNOWN_ERROR;
693        }
694        setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
695        close(fd);
696        return mStatus;
697    } else {
698        player_type playerType = getPlayerType(url);
699        LOGV("player type = %d", playerType);
700
701        // create the right type of player
702        sp<MediaPlayerBase> p = createPlayer(playerType);
703        if (p == NULL) return NO_INIT;
704
705        if (!p->hardwareOutput()) {
706            mAudioOutput = new AudioOutput(mAudioSessionId);
707            static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
708        }
709
710        // now set data source
711        LOGV(" setDataSource");
712        mStatus = p->setDataSource(url, headers);
713        if (mStatus == NO_ERROR) {
714            mPlayer = p;
715        } else {
716            LOGE("  error: %d", mStatus);
717        }
718        return mStatus;
719    }
720}
721
722status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
723{
724    LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
725    struct stat sb;
726    int ret = fstat(fd, &sb);
727    if (ret != 0) {
728        LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
729        return UNKNOWN_ERROR;
730    }
731
732    LOGV("st_dev  = %llu", sb.st_dev);
733    LOGV("st_mode = %u", sb.st_mode);
734    LOGV("st_uid  = %lu", sb.st_uid);
735    LOGV("st_gid  = %lu", sb.st_gid);
736    LOGV("st_size = %llu", sb.st_size);
737
738    if (offset >= sb.st_size) {
739        LOGE("offset error");
740        ::close(fd);
741        return UNKNOWN_ERROR;
742    }
743    if (offset + length > sb.st_size) {
744        length = sb.st_size - offset;
745        LOGV("calculated length = %lld", length);
746    }
747
748    player_type playerType = getPlayerType(fd, offset, length);
749    LOGV("player type = %d", playerType);
750
751    // create the right type of player
752    sp<MediaPlayerBase> p = createPlayer(playerType);
753    if (p == NULL) return NO_INIT;
754
755    if (!p->hardwareOutput()) {
756        mAudioOutput = new AudioOutput(mAudioSessionId);
757        static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
758    }
759
760    // now set data source
761    mStatus = p->setDataSource(fd, offset, length);
762    if (mStatus == NO_ERROR) mPlayer = p;
763
764    return mStatus;
765}
766
767status_t MediaPlayerService::Client::setDataSource(
768        const sp<IStreamSource> &source) {
769    // create the right type of player
770    sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
771
772    if (p == NULL) {
773        return NO_INIT;
774    }
775
776    if (!p->hardwareOutput()) {
777        mAudioOutput = new AudioOutput(mAudioSessionId);
778        static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
779    }
780
781    // now set data source
782    mStatus = p->setDataSource(source);
783
784    if (mStatus == OK) {
785        mPlayer = p;
786    }
787
788    return mStatus;
789}
790
791void MediaPlayerService::Client::disconnectNativeWindow() {
792    if (mConnectedWindow != NULL) {
793        status_t err = native_window_api_disconnect(mConnectedWindow.get(),
794                NATIVE_WINDOW_API_MEDIA);
795
796        if (err != OK) {
797            LOGW("native_window_api_disconnect returned an error: %s (%d)",
798                    strerror(-err), err);
799        }
800    }
801    mConnectedWindow.clear();
802}
803
804status_t MediaPlayerService::Client::setVideoSurfaceTexture(
805        const sp<ISurfaceTexture>& surfaceTexture)
806{
807    LOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
808    sp<MediaPlayerBase> p = getPlayer();
809    if (p == 0) return UNKNOWN_ERROR;
810
811    sp<IBinder> binder(surfaceTexture == NULL ? NULL :
812            surfaceTexture->asBinder());
813    if (mConnectedWindowBinder == binder) {
814        return OK;
815    }
816
817    sp<ANativeWindow> anw;
818    if (surfaceTexture != NULL) {
819        anw = new SurfaceTextureClient(surfaceTexture);
820        status_t err = native_window_api_connect(anw.get(),
821                NATIVE_WINDOW_API_MEDIA);
822
823        if (err != OK) {
824            LOGE("setVideoSurfaceTexture failed: %d", err);
825            // Note that we must do the reset before disconnecting from the ANW.
826            // Otherwise queue/dequeue calls could be made on the disconnected
827            // ANW, which may result in errors.
828            reset();
829
830            disconnectNativeWindow();
831
832            return err;
833        }
834    }
835
836    // Note that we must set the player's new SurfaceTexture before
837    // disconnecting the old one.  Otherwise queue/dequeue calls could be made
838    // on the disconnected ANW, which may result in errors.
839    status_t err = p->setVideoSurfaceTexture(surfaceTexture);
840
841    disconnectNativeWindow();
842
843    mConnectedWindow = anw;
844
845    if (err == OK) {
846        mConnectedWindowBinder = binder;
847    } else {
848        disconnectNativeWindow();
849    }
850
851    return err;
852}
853
854status_t MediaPlayerService::Client::invoke(const Parcel& request,
855                                            Parcel *reply)
856{
857    sp<MediaPlayerBase> p = getPlayer();
858    if (p == NULL) return UNKNOWN_ERROR;
859    return p->invoke(request, reply);
860}
861
862// This call doesn't need to access the native player.
863status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
864{
865    status_t status;
866    media::Metadata::Filter allow, drop;
867
868    if (unmarshallFilter(filter, &allow, &status) &&
869        unmarshallFilter(filter, &drop, &status)) {
870        Mutex::Autolock lock(mLock);
871
872        mMetadataAllow = allow;
873        mMetadataDrop = drop;
874    }
875    return status;
876}
877
878status_t MediaPlayerService::Client::getMetadata(
879        bool update_only, bool apply_filter, Parcel *reply)
880{
881    sp<MediaPlayerBase> player = getPlayer();
882    if (player == 0) return UNKNOWN_ERROR;
883
884    status_t status;
885    // Placeholder for the return code, updated by the caller.
886    reply->writeInt32(-1);
887
888    media::Metadata::Filter ids;
889
890    // We don't block notifications while we fetch the data. We clear
891    // mMetadataUpdated first so we don't lose notifications happening
892    // during the rest of this call.
893    {
894        Mutex::Autolock lock(mLock);
895        if (update_only) {
896            ids = mMetadataUpdated;
897        }
898        mMetadataUpdated.clear();
899    }
900
901    media::Metadata metadata(reply);
902
903    metadata.appendHeader();
904    status = player->getMetadata(ids, reply);
905
906    if (status != OK) {
907        metadata.resetParcel();
908        LOGE("getMetadata failed %d", status);
909        return status;
910    }
911
912    // FIXME: Implement filtering on the result. Not critical since
913    // filtering takes place on the update notifications already. This
914    // would be when all the metadata are fetch and a filter is set.
915
916    // Everything is fine, update the metadata length.
917    metadata.updateLength();
918    return OK;
919}
920
921status_t MediaPlayerService::Client::prepareAsync()
922{
923    LOGV("[%d] prepareAsync", mConnId);
924    sp<MediaPlayerBase> p = getPlayer();
925    if (p == 0) return UNKNOWN_ERROR;
926    status_t ret = p->prepareAsync();
927#if CALLBACK_ANTAGONIZER
928    LOGD("start Antagonizer");
929    if (ret == NO_ERROR) mAntagonizer->start();
930#endif
931    return ret;
932}
933
934status_t MediaPlayerService::Client::start()
935{
936    LOGV("[%d] start", mConnId);
937    sp<MediaPlayerBase> p = getPlayer();
938    if (p == 0) return UNKNOWN_ERROR;
939    p->setLooping(mLoop);
940    return p->start();
941}
942
943status_t MediaPlayerService::Client::stop()
944{
945    LOGV("[%d] stop", mConnId);
946    sp<MediaPlayerBase> p = getPlayer();
947    if (p == 0) return UNKNOWN_ERROR;
948    return p->stop();
949}
950
951status_t MediaPlayerService::Client::pause()
952{
953    LOGV("[%d] pause", mConnId);
954    sp<MediaPlayerBase> p = getPlayer();
955    if (p == 0) return UNKNOWN_ERROR;
956    return p->pause();
957}
958
959status_t MediaPlayerService::Client::isPlaying(bool* state)
960{
961    *state = false;
962    sp<MediaPlayerBase> p = getPlayer();
963    if (p == 0) return UNKNOWN_ERROR;
964    *state = p->isPlaying();
965    LOGV("[%d] isPlaying: %d", mConnId, *state);
966    return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
970{
971    LOGV("getCurrentPosition");
972    sp<MediaPlayerBase> p = getPlayer();
973    if (p == 0) return UNKNOWN_ERROR;
974    status_t ret = p->getCurrentPosition(msec);
975    if (ret == NO_ERROR) {
976        LOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
977    } else {
978        LOGE("getCurrentPosition returned %d", ret);
979    }
980    return ret;
981}
982
983status_t MediaPlayerService::Client::getDuration(int *msec)
984{
985    LOGV("getDuration");
986    sp<MediaPlayerBase> p = getPlayer();
987    if (p == 0) return UNKNOWN_ERROR;
988    status_t ret = p->getDuration(msec);
989    if (ret == NO_ERROR) {
990        LOGV("[%d] getDuration = %d", mConnId, *msec);
991    } else {
992        LOGE("getDuration returned %d", ret);
993    }
994    return ret;
995}
996
997status_t MediaPlayerService::Client::seekTo(int msec)
998{
999    LOGV("[%d] seekTo(%d)", mConnId, msec);
1000    sp<MediaPlayerBase> p = getPlayer();
1001    if (p == 0) return UNKNOWN_ERROR;
1002    return p->seekTo(msec);
1003}
1004
1005status_t MediaPlayerService::Client::reset()
1006{
1007    LOGV("[%d] reset", mConnId);
1008    sp<MediaPlayerBase> p = getPlayer();
1009    if (p == 0) return UNKNOWN_ERROR;
1010    return p->reset();
1011}
1012
1013status_t MediaPlayerService::Client::setAudioStreamType(int type)
1014{
1015    LOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1016    // TODO: for hardware output, call player instead
1017    Mutex::Autolock l(mLock);
1018    if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1019    return NO_ERROR;
1020}
1021
1022status_t MediaPlayerService::Client::setLooping(int loop)
1023{
1024    LOGV("[%d] setLooping(%d)", mConnId, loop);
1025    mLoop = loop;
1026    sp<MediaPlayerBase> p = getPlayer();
1027    if (p != 0) return p->setLooping(loop);
1028    return NO_ERROR;
1029}
1030
1031status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1032{
1033    LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1034    // TODO: for hardware output, call player instead
1035    Mutex::Autolock l(mLock);
1036    if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1037    return NO_ERROR;
1038}
1039
1040status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1041{
1042    LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1043    Mutex::Autolock l(mLock);
1044    if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1045    return NO_ERROR;
1046}
1047
1048status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1049{
1050    LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1051    Mutex::Autolock l(mLock);
1052    if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1053    return NO_ERROR;
1054}
1055
1056status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
1057    LOGV("[%d] setParameter(%d)", mConnId, key);
1058    sp<MediaPlayerBase> p = getPlayer();
1059    if (p == 0) return UNKNOWN_ERROR;
1060    return p->setParameter(key, request);
1061}
1062
1063status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
1064    LOGV("[%d] getParameter(%d)", mConnId, key);
1065    sp<MediaPlayerBase> p = getPlayer();
1066    if (p == 0) return UNKNOWN_ERROR;
1067    return p->getParameter(key, reply);
1068}
1069
1070void MediaPlayerService::Client::notify(
1071        void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
1072{
1073    Client* client = static_cast<Client*>(cookie);
1074
1075    if (MEDIA_INFO == msg &&
1076        MEDIA_INFO_METADATA_UPDATE == ext1) {
1077        const media::Metadata::Type metadata_type = ext2;
1078
1079        if(client->shouldDropMetadata(metadata_type)) {
1080            return;
1081        }
1082
1083        // Update the list of metadata that have changed. getMetadata
1084        // also access mMetadataUpdated and clears it.
1085        client->addNewMetadataUpdate(metadata_type);
1086    }
1087    LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1088    client->mClient->notify(msg, ext1, ext2, obj);
1089}
1090
1091
1092bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
1093{
1094    Mutex::Autolock lock(mLock);
1095
1096    if (findMetadata(mMetadataDrop, code)) {
1097        return true;
1098    }
1099
1100    if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1101        return false;
1102    } else {
1103        return true;
1104    }
1105}
1106
1107
1108void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1109    Mutex::Autolock lock(mLock);
1110    if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1111        mMetadataUpdated.add(metadata_type);
1112    }
1113}
1114
1115#if CALLBACK_ANTAGONIZER
1116const int Antagonizer::interval = 10000; // 10 msecs
1117
1118Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1119    mExit(false), mActive(false), mClient(client), mCb(cb)
1120{
1121    createThread(callbackThread, this);
1122}
1123
1124void Antagonizer::kill()
1125{
1126    Mutex::Autolock _l(mLock);
1127    mActive = false;
1128    mExit = true;
1129    mCondition.wait(mLock);
1130}
1131
1132int Antagonizer::callbackThread(void* user)
1133{
1134    LOGD("Antagonizer started");
1135    Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1136    while (!p->mExit) {
1137        if (p->mActive) {
1138            LOGV("send event");
1139            p->mCb(p->mClient, 0, 0, 0);
1140        }
1141        usleep(interval);
1142    }
1143    Mutex::Autolock _l(p->mLock);
1144    p->mCondition.signal();
1145    LOGD("Antagonizer stopped");
1146    return 0;
1147}
1148#endif
1149
1150static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1151
1152sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1153{
1154    LOGV("decode(%s)", url);
1155    sp<MemoryBase> mem;
1156    sp<MediaPlayerBase> player;
1157
1158    // Protect our precious, precious DRMd ringtones by only allowing
1159    // decoding of http, but not filesystem paths or content Uris.
1160    // If the application wants to decode those, it should open a
1161    // filedescriptor for them and use that.
1162    if (url != NULL && strncmp(url, "http://", 7) != 0) {
1163        LOGD("Can't decode %s by path, use filedescriptor instead", url);
1164        return mem;
1165    }
1166
1167    player_type playerType = getPlayerType(url);
1168    LOGV("player type = %d", playerType);
1169
1170    // create the right type of player
1171    sp<AudioCache> cache = new AudioCache(url);
1172    player = android::createPlayer(playerType, cache.get(), cache->notify);
1173    if (player == NULL) goto Exit;
1174    if (player->hardwareOutput()) goto Exit;
1175
1176    static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1177
1178    // set data source
1179    if (player->setDataSource(url) != NO_ERROR) goto Exit;
1180
1181    LOGV("prepare");
1182    player->prepareAsync();
1183
1184    LOGV("wait for prepare");
1185    if (cache->wait() != NO_ERROR) goto Exit;
1186
1187    LOGV("start");
1188    player->start();
1189
1190    LOGV("wait for playback complete");
1191    cache->wait();
1192    // in case of error, return what was successfully decoded.
1193    if (cache->size() == 0) {
1194        goto Exit;
1195    }
1196
1197    mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1198    *pSampleRate = cache->sampleRate();
1199    *pNumChannels = cache->channelCount();
1200    *pFormat = (int)cache->format();
1201    LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1202
1203Exit:
1204    if (player != 0) player->reset();
1205    return mem;
1206}
1207
1208sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
1209{
1210    LOGV("decode(%d, %lld, %lld)", fd, offset, length);
1211    sp<MemoryBase> mem;
1212    sp<MediaPlayerBase> player;
1213
1214    player_type playerType = getPlayerType(fd, offset, length);
1215    LOGV("player type = %d", playerType);
1216
1217    // create the right type of player
1218    sp<AudioCache> cache = new AudioCache("decode_fd");
1219    player = android::createPlayer(playerType, cache.get(), cache->notify);
1220    if (player == NULL) goto Exit;
1221    if (player->hardwareOutput()) goto Exit;
1222
1223    static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1224
1225    // set data source
1226    if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1227
1228    LOGV("prepare");
1229    player->prepareAsync();
1230
1231    LOGV("wait for prepare");
1232    if (cache->wait() != NO_ERROR) goto Exit;
1233
1234    LOGV("start");
1235    player->start();
1236
1237    LOGV("wait for playback complete");
1238    cache->wait();
1239    // in case of error, return what was successfully decoded.
1240    if (cache->size() == 0) {
1241        goto Exit;
1242    }
1243
1244    mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1245    *pSampleRate = cache->sampleRate();
1246    *pNumChannels = cache->channelCount();
1247    *pFormat = cache->format();
1248    LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
1249
1250Exit:
1251    if (player != 0) player->reset();
1252    ::close(fd);
1253    return mem;
1254}
1255
1256
1257#undef LOG_TAG
1258#define LOG_TAG "AudioSink"
1259MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
1260    : mCallback(NULL),
1261      mCallbackCookie(NULL),
1262      mSessionId(sessionId) {
1263    LOGV("AudioOutput(%d)", sessionId);
1264    mTrack = 0;
1265    mStreamType = AUDIO_STREAM_MUSIC;
1266    mLeftVolume = 1.0;
1267    mRightVolume = 1.0;
1268    mMsecsPerFrame = 0;
1269    mAuxEffectId = 0;
1270    mSendLevel = 0.0;
1271    setMinBufferCount();
1272}
1273
1274MediaPlayerService::AudioOutput::~AudioOutput()
1275{
1276    close();
1277}
1278
1279void MediaPlayerService::AudioOutput::setMinBufferCount()
1280{
1281    char value[PROPERTY_VALUE_MAX];
1282    if (property_get("ro.kernel.qemu", value, 0)) {
1283        mIsOnEmulator = true;
1284        mMinBufferCount = 12;  // to prevent systematic buffer underrun for emulator
1285    }
1286}
1287
1288bool MediaPlayerService::AudioOutput::isOnEmulator()
1289{
1290    setMinBufferCount();
1291    return mIsOnEmulator;
1292}
1293
1294int MediaPlayerService::AudioOutput::getMinBufferCount()
1295{
1296    setMinBufferCount();
1297    return mMinBufferCount;
1298}
1299
1300ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1301{
1302    if (mTrack == 0) return NO_INIT;
1303    return mTrack->frameCount() * frameSize();
1304}
1305
1306ssize_t MediaPlayerService::AudioOutput::frameCount() const
1307{
1308    if (mTrack == 0) return NO_INIT;
1309    return mTrack->frameCount();
1310}
1311
1312ssize_t MediaPlayerService::AudioOutput::channelCount() const
1313{
1314    if (mTrack == 0) return NO_INIT;
1315    return mTrack->channelCount();
1316}
1317
1318ssize_t MediaPlayerService::AudioOutput::frameSize() const
1319{
1320    if (mTrack == 0) return NO_INIT;
1321    return mTrack->frameSize();
1322}
1323
1324uint32_t MediaPlayerService::AudioOutput::latency () const
1325{
1326    if (mTrack == 0) return 0;
1327    return mTrack->latency();
1328}
1329
1330float MediaPlayerService::AudioOutput::msecsPerFrame() const
1331{
1332    return mMsecsPerFrame;
1333}
1334
1335status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1336{
1337    if (mTrack == 0) return NO_INIT;
1338    return mTrack->getPosition(position);
1339}
1340
1341status_t MediaPlayerService::AudioOutput::open(
1342        uint32_t sampleRate, int channelCount, int format, int bufferCount,
1343        AudioCallback cb, void *cookie)
1344{
1345    mCallback = cb;
1346    mCallbackCookie = cookie;
1347
1348    // Check argument "bufferCount" against the mininum buffer count
1349    if (bufferCount < mMinBufferCount) {
1350        LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
1351        bufferCount = mMinBufferCount;
1352
1353    }
1354    LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
1355    if (mTrack) close();
1356    int afSampleRate;
1357    int afFrameCount;
1358    int frameCount;
1359
1360    if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1361        return NO_INIT;
1362    }
1363    if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1364        return NO_INIT;
1365    }
1366
1367    frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1368
1369    AudioTrack *t;
1370    if (mCallback != NULL) {
1371        t = new AudioTrack(
1372                mStreamType,
1373                sampleRate,
1374                format,
1375                (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
1376                frameCount,
1377                0 /* flags */,
1378                CallbackWrapper,
1379                this,
1380                0,
1381                mSessionId);
1382    } else {
1383        t = new AudioTrack(
1384                mStreamType,
1385                sampleRate,
1386                format,
1387                (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
1388                frameCount,
1389                0,
1390                NULL,
1391                NULL,
1392                0,
1393                mSessionId);
1394    }
1395
1396    if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1397        LOGE("Unable to create audio track");
1398        delete t;
1399        return NO_INIT;
1400    }
1401
1402    LOGV("setVolume");
1403    t->setVolume(mLeftVolume, mRightVolume);
1404
1405    mMsecsPerFrame = 1.e3 / (float) sampleRate;
1406    mTrack = t;
1407
1408    t->setAuxEffectSendLevel(mSendLevel);
1409    return t->attachAuxEffect(mAuxEffectId);;
1410}
1411
1412void MediaPlayerService::AudioOutput::start()
1413{
1414    LOGV("start");
1415    if (mTrack) {
1416        mTrack->setVolume(mLeftVolume, mRightVolume);
1417        mTrack->setAuxEffectSendLevel(mSendLevel);
1418        mTrack->start();
1419    }
1420}
1421
1422
1423
1424ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1425{
1426    LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1427
1428    //LOGV("write(%p, %u)", buffer, size);
1429    if (mTrack) {
1430        ssize_t ret = mTrack->write(buffer, size);
1431        return ret;
1432    }
1433    return NO_INIT;
1434}
1435
1436void MediaPlayerService::AudioOutput::stop()
1437{
1438    LOGV("stop");
1439    if (mTrack) mTrack->stop();
1440}
1441
1442void MediaPlayerService::AudioOutput::flush()
1443{
1444    LOGV("flush");
1445    if (mTrack) mTrack->flush();
1446}
1447
1448void MediaPlayerService::AudioOutput::pause()
1449{
1450    LOGV("pause");
1451    if (mTrack) mTrack->pause();
1452}
1453
1454void MediaPlayerService::AudioOutput::close()
1455{
1456    LOGV("close");
1457    delete mTrack;
1458    mTrack = 0;
1459}
1460
1461void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1462{
1463    LOGV("setVolume(%f, %f)", left, right);
1464    mLeftVolume = left;
1465    mRightVolume = right;
1466    if (mTrack) {
1467        mTrack->setVolume(left, right);
1468    }
1469}
1470
1471status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1472{
1473    LOGV("setAuxEffectSendLevel(%f)", level);
1474    mSendLevel = level;
1475    if (mTrack) {
1476        return mTrack->setAuxEffectSendLevel(level);
1477    }
1478    return NO_ERROR;
1479}
1480
1481status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1482{
1483    LOGV("attachAuxEffect(%d)", effectId);
1484    mAuxEffectId = effectId;
1485    if (mTrack) {
1486        return mTrack->attachAuxEffect(effectId);
1487    }
1488    return NO_ERROR;
1489}
1490
1491// static
1492void MediaPlayerService::AudioOutput::CallbackWrapper(
1493        int event, void *cookie, void *info) {
1494    //LOGV("callbackwrapper");
1495    if (event != AudioTrack::EVENT_MORE_DATA) {
1496        return;
1497    }
1498
1499    AudioOutput *me = (AudioOutput *)cookie;
1500    AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1501
1502    size_t actualSize = (*me->mCallback)(
1503            me, buffer->raw, buffer->size, me->mCallbackCookie);
1504
1505    if (actualSize == 0 && buffer->size > 0) {
1506        // We've reached EOS but the audio track is not stopped yet,
1507        // keep playing silence.
1508
1509        memset(buffer->raw, 0, buffer->size);
1510        actualSize = buffer->size;
1511    }
1512
1513    buffer->size = actualSize;
1514}
1515
1516int MediaPlayerService::AudioOutput::getSessionId()
1517{
1518    return mSessionId;
1519}
1520
1521#undef LOG_TAG
1522#define LOG_TAG "AudioCache"
1523MediaPlayerService::AudioCache::AudioCache(const char* name) :
1524    mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1525    mError(NO_ERROR), mCommandComplete(false)
1526{
1527    // create ashmem heap
1528    mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1529}
1530
1531uint32_t MediaPlayerService::AudioCache::latency () const
1532{
1533    return 0;
1534}
1535
1536float MediaPlayerService::AudioCache::msecsPerFrame() const
1537{
1538    return mMsecsPerFrame;
1539}
1540
1541status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1542{
1543    if (position == 0) return BAD_VALUE;
1544    *position = mSize;
1545    return NO_ERROR;
1546}
1547
1548////////////////////////////////////////////////////////////////////////////////
1549
1550struct CallbackThread : public Thread {
1551    CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1552                   MediaPlayerBase::AudioSink::AudioCallback cb,
1553                   void *cookie);
1554
1555protected:
1556    virtual ~CallbackThread();
1557
1558    virtual bool threadLoop();
1559
1560private:
1561    wp<MediaPlayerBase::AudioSink> mSink;
1562    MediaPlayerBase::AudioSink::AudioCallback mCallback;
1563    void *mCookie;
1564    void *mBuffer;
1565    size_t mBufferSize;
1566
1567    CallbackThread(const CallbackThread &);
1568    CallbackThread &operator=(const CallbackThread &);
1569};
1570
1571CallbackThread::CallbackThread(
1572        const wp<MediaPlayerBase::AudioSink> &sink,
1573        MediaPlayerBase::AudioSink::AudioCallback cb,
1574        void *cookie)
1575    : mSink(sink),
1576      mCallback(cb),
1577      mCookie(cookie),
1578      mBuffer(NULL),
1579      mBufferSize(0) {
1580}
1581
1582CallbackThread::~CallbackThread() {
1583    if (mBuffer) {
1584        free(mBuffer);
1585        mBuffer = NULL;
1586    }
1587}
1588
1589bool CallbackThread::threadLoop() {
1590    sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1591    if (sink == NULL) {
1592        return false;
1593    }
1594
1595    if (mBuffer == NULL) {
1596        mBufferSize = sink->bufferSize();
1597        mBuffer = malloc(mBufferSize);
1598    }
1599
1600    size_t actualSize =
1601        (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1602
1603    if (actualSize > 0) {
1604        sink->write(mBuffer, actualSize);
1605    }
1606
1607    return true;
1608}
1609
1610////////////////////////////////////////////////////////////////////////////////
1611
1612status_t MediaPlayerService::AudioCache::open(
1613        uint32_t sampleRate, int channelCount, int format, int bufferCount,
1614        AudioCallback cb, void *cookie)
1615{
1616    LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
1617    if (mHeap->getHeapID() < 0) {
1618        return NO_INIT;
1619    }
1620
1621    mSampleRate = sampleRate;
1622    mChannelCount = (uint16_t)channelCount;
1623    mFormat = (uint16_t)format;
1624    mMsecsPerFrame = 1.e3 / (float) sampleRate;
1625
1626    if (cb != NULL) {
1627        mCallbackThread = new CallbackThread(this, cb, cookie);
1628    }
1629    return NO_ERROR;
1630}
1631
1632void MediaPlayerService::AudioCache::start() {
1633    if (mCallbackThread != NULL) {
1634        mCallbackThread->run("AudioCache callback");
1635    }
1636}
1637
1638void MediaPlayerService::AudioCache::stop() {
1639    if (mCallbackThread != NULL) {
1640        mCallbackThread->requestExitAndWait();
1641    }
1642}
1643
1644ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1645{
1646    LOGV("write(%p, %u)", buffer, size);
1647    if ((buffer == 0) || (size == 0)) return size;
1648
1649    uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1650    if (p == NULL) return NO_INIT;
1651    p += mSize;
1652    LOGV("memcpy(%p, %p, %u)", p, buffer, size);
1653    if (mSize + size > mHeap->getSize()) {
1654        LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
1655        size = mHeap->getSize() - mSize;
1656    }
1657    memcpy(p, buffer, size);
1658    mSize += size;
1659    return size;
1660}
1661
1662// call with lock held
1663status_t MediaPlayerService::AudioCache::wait()
1664{
1665    Mutex::Autolock lock(mLock);
1666    while (!mCommandComplete) {
1667        mSignal.wait(mLock);
1668    }
1669    mCommandComplete = false;
1670
1671    if (mError == NO_ERROR) {
1672        LOGV("wait - success");
1673    } else {
1674        LOGV("wait - error");
1675    }
1676    return mError;
1677}
1678
1679void MediaPlayerService::AudioCache::notify(
1680        void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
1681{
1682    LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
1683    AudioCache* p = static_cast<AudioCache*>(cookie);
1684
1685    // ignore buffering messages
1686    switch (msg)
1687    {
1688    case MEDIA_ERROR:
1689        LOGE("Error %d, %d occurred", ext1, ext2);
1690        p->mError = ext1;
1691        break;
1692    case MEDIA_PREPARED:
1693        LOGV("prepared");
1694        break;
1695    case MEDIA_PLAYBACK_COMPLETE:
1696        LOGV("playback complete");
1697        break;
1698    default:
1699        LOGV("ignored");
1700        return;
1701    }
1702
1703    // wake up thread
1704    Mutex::Autolock lock(p->mLock);
1705    p->mCommandComplete = true;
1706    p->mSignal.signal();
1707}
1708
1709int MediaPlayerService::AudioCache::getSessionId()
1710{
1711    return 0;
1712}
1713
1714void MediaPlayerService::addBatteryData(uint32_t params)
1715{
1716    Mutex::Autolock lock(mLock);
1717
1718    int32_t time = systemTime() / 1000000L;
1719
1720    // change audio output devices. This notification comes from AudioFlinger
1721    if ((params & kBatteryDataSpeakerOn)
1722            || (params & kBatteryDataOtherAudioDeviceOn)) {
1723
1724        int deviceOn[NUM_AUDIO_DEVICES];
1725        for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1726            deviceOn[i] = 0;
1727        }
1728
1729        if ((params & kBatteryDataSpeakerOn)
1730                && (params & kBatteryDataOtherAudioDeviceOn)) {
1731            deviceOn[SPEAKER_AND_OTHER] = 1;
1732        } else if (params & kBatteryDataSpeakerOn) {
1733            deviceOn[SPEAKER] = 1;
1734        } else {
1735            deviceOn[OTHER_AUDIO_DEVICE] = 1;
1736        }
1737
1738        for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1739            if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1740
1741                if (mBatteryAudio.refCount > 0) { // if playing audio
1742                    if (!deviceOn[i]) {
1743                        mBatteryAudio.lastTime[i] += time;
1744                        mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1745                        mBatteryAudio.lastTime[i] = 0;
1746                    } else {
1747                        mBatteryAudio.lastTime[i] = 0 - time;
1748                    }
1749                }
1750
1751                mBatteryAudio.deviceOn[i] = deviceOn[i];
1752            }
1753        }
1754        return;
1755    }
1756
1757    // an sudio stream is started
1758    if (params & kBatteryDataAudioFlingerStart) {
1759        // record the start time only if currently no other audio
1760        // is being played
1761        if (mBatteryAudio.refCount == 0) {
1762            for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1763                if (mBatteryAudio.deviceOn[i]) {
1764                    mBatteryAudio.lastTime[i] -= time;
1765                }
1766            }
1767        }
1768
1769        mBatteryAudio.refCount ++;
1770        return;
1771
1772    } else if (params & kBatteryDataAudioFlingerStop) {
1773        if (mBatteryAudio.refCount <= 0) {
1774            LOGW("Battery track warning: refCount is <= 0");
1775            return;
1776        }
1777
1778        // record the stop time only if currently this is the only
1779        // audio being played
1780        if (mBatteryAudio.refCount == 1) {
1781            for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1782                if (mBatteryAudio.deviceOn[i]) {
1783                    mBatteryAudio.lastTime[i] += time;
1784                    mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1785                    mBatteryAudio.lastTime[i] = 0;
1786                }
1787            }
1788        }
1789
1790        mBatteryAudio.refCount --;
1791        return;
1792    }
1793
1794    int uid = IPCThreadState::self()->getCallingUid();
1795    if (uid == AID_MEDIA) {
1796        return;
1797    }
1798    int index = mBatteryData.indexOfKey(uid);
1799
1800    if (index < 0) { // create a new entry for this UID
1801        BatteryUsageInfo info;
1802        info.audioTotalTime = 0;
1803        info.videoTotalTime = 0;
1804        info.audioLastTime = 0;
1805        info.videoLastTime = 0;
1806        info.refCount = 0;
1807
1808        if (mBatteryData.add(uid, info) == NO_MEMORY) {
1809            LOGE("Battery track error: no memory for new app");
1810            return;
1811        }
1812    }
1813
1814    BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1815
1816    if (params & kBatteryDataCodecStarted) {
1817        if (params & kBatteryDataTrackAudio) {
1818            info.audioLastTime -= time;
1819            info.refCount ++;
1820        }
1821        if (params & kBatteryDataTrackVideo) {
1822            info.videoLastTime -= time;
1823            info.refCount ++;
1824        }
1825    } else {
1826        if (info.refCount == 0) {
1827            LOGW("Battery track warning: refCount is already 0");
1828            return;
1829        } else if (info.refCount < 0) {
1830            LOGE("Battery track error: refCount < 0");
1831            mBatteryData.removeItem(uid);
1832            return;
1833        }
1834
1835        if (params & kBatteryDataTrackAudio) {
1836            info.audioLastTime += time;
1837            info.refCount --;
1838        }
1839        if (params & kBatteryDataTrackVideo) {
1840            info.videoLastTime += time;
1841            info.refCount --;
1842        }
1843
1844        // no stream is being played by this UID
1845        if (info.refCount == 0) {
1846            info.audioTotalTime += info.audioLastTime;
1847            info.audioLastTime = 0;
1848            info.videoTotalTime += info.videoLastTime;
1849            info.videoLastTime = 0;
1850        }
1851    }
1852}
1853
1854status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1855    Mutex::Autolock lock(mLock);
1856
1857    // audio output devices usage
1858    int32_t time = systemTime() / 1000000L; //in ms
1859    int32_t totalTime;
1860
1861    for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1862        totalTime = mBatteryAudio.totalTime[i];
1863
1864        if (mBatteryAudio.deviceOn[i]
1865            && (mBatteryAudio.lastTime[i] != 0)) {
1866                int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1867                totalTime += tmpTime;
1868        }
1869
1870        reply->writeInt32(totalTime);
1871        // reset the total time
1872        mBatteryAudio.totalTime[i] = 0;
1873   }
1874
1875    // codec usage
1876    BatteryUsageInfo info;
1877    int size = mBatteryData.size();
1878
1879    reply->writeInt32(size);
1880    int i = 0;
1881
1882    while (i < size) {
1883        info = mBatteryData.valueAt(i);
1884
1885        reply->writeInt32(mBatteryData.keyAt(i)); //UID
1886        reply->writeInt32(info.audioTotalTime);
1887        reply->writeInt32(info.videoTotalTime);
1888
1889        info.audioTotalTime = 0;
1890        info.videoTotalTime = 0;
1891
1892        // remove the UID entry where no stream is being played
1893        if (info.refCount <= 0) {
1894            mBatteryData.removeItemsAt(i);
1895            size --;
1896            i --;
1897        }
1898        i++;
1899    }
1900    return NO_ERROR;
1901}
1902} // namespace android
1903