mediaplayer.cpp revision 10dbb8e97e7a81ca4867663b5517f048820b3094
1/* mediaplayer.cpp
2**
3** Copyright 2006, 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//#define LOG_NDEBUG 0
19#define LOG_TAG "MediaPlayer"
20#include <utils/Log.h>
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26
27#include <binder/IServiceManager.h>
28#include <binder/IPCThreadState.h>
29
30#include <media/mediaplayer.h>
31#include <media/AudioTrack.h>
32
33#include <binder/MemoryBase.h>
34
35namespace android {
36
37// client singleton for binder interface to service
38Mutex MediaPlayer::sServiceLock;
39sp<IMediaPlayerService> MediaPlayer::sMediaPlayerService;
40sp<MediaPlayer::DeathNotifier> MediaPlayer::sDeathNotifier;
41SortedVector< wp<MediaPlayer> > MediaPlayer::sObitRecipients;
42
43// establish binder interface to service
44const sp<IMediaPlayerService>& MediaPlayer::getMediaPlayerService()
45{
46    Mutex::Autolock _l(sServiceLock);
47    if (sMediaPlayerService.get() == 0) {
48        sp<IServiceManager> sm = defaultServiceManager();
49        sp<IBinder> binder;
50        do {
51            binder = sm->getService(String16("media.player"));
52            if (binder != 0)
53                break;
54            LOGW("MediaPlayerService not published, waiting...");
55            usleep(500000); // 0.5 s
56        } while(true);
57        if (sDeathNotifier == NULL) {
58            sDeathNotifier = new DeathNotifier();
59        }
60        binder->linkToDeath(sDeathNotifier);
61        sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);
62    }
63    LOGE_IF(sMediaPlayerService==0, "no MediaPlayerService!?");
64    return sMediaPlayerService;
65}
66
67void MediaPlayer::addObitRecipient(const wp<MediaPlayer>& recipient)
68{
69    Mutex::Autolock _l(sServiceLock);
70    sObitRecipients.add(recipient);
71}
72
73void MediaPlayer::removeObitRecipient(const wp<MediaPlayer>& recipient)
74{
75    Mutex::Autolock _l(sServiceLock);
76    sObitRecipients.remove(recipient);
77}
78
79MediaPlayer::MediaPlayer()
80{
81    LOGV("constructor");
82    mListener = NULL;
83    mCookie = NULL;
84    mDuration = -1;
85    mStreamType = AudioSystem::MUSIC;
86    mCurrentPosition = -1;
87    mSeekPosition = -1;
88    mCurrentState = MEDIA_PLAYER_IDLE;
89    mPrepareSync = false;
90    mPrepareStatus = NO_ERROR;
91    mLoop = false;
92    mLeftVolume = mRightVolume = 1.0;
93    mVideoWidth = mVideoHeight = 0;
94    mLockThreadId = 0;
95}
96
97void MediaPlayer::onFirstRef()
98{
99    addObitRecipient(this);
100}
101
102MediaPlayer::~MediaPlayer()
103{
104    LOGV("destructor");
105    removeObitRecipient(this);
106    disconnect();
107    IPCThreadState::self()->flushCommands();
108}
109
110void MediaPlayer::disconnect()
111{
112    LOGV("disconnect");
113    sp<IMediaPlayer> p;
114    {
115        Mutex::Autolock _l(mLock);
116        p = mPlayer;
117        mPlayer.clear();
118    }
119
120    if (p != 0) {
121        p->disconnect();
122    }
123}
124
125// always call with lock held
126void MediaPlayer::clear_l()
127{
128    mDuration = -1;
129    mCurrentPosition = -1;
130    mSeekPosition = -1;
131    mVideoWidth = mVideoHeight = 0;
132}
133
134status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
135{
136    LOGV("setListener");
137    Mutex::Autolock _l(mLock);
138    mListener = listener;
139    return NO_ERROR;
140}
141
142
143status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player)
144{
145    status_t err = UNKNOWN_ERROR;
146    sp<IMediaPlayer> p;
147    { // scope for the lock
148        Mutex::Autolock _l(mLock);
149
150        if ( !( mCurrentState & ( MEDIA_PLAYER_IDLE | MEDIA_PLAYER_STATE_ERROR ) ) ) {
151            LOGE("setDataSource called in state %d", mCurrentState);
152            return INVALID_OPERATION;
153        }
154
155        clear_l();
156        p = mPlayer;
157        mPlayer = player;
158        if (player != 0) {
159            mCurrentState = MEDIA_PLAYER_INITIALIZED;
160            err = NO_ERROR;
161        } else {
162            LOGE("Unable to to create media player");
163        }
164    }
165
166    if (p != 0) {
167        p->disconnect();
168    }
169
170    return err;
171}
172
173status_t MediaPlayer::setDataSource(const char *url)
174{
175    LOGV("setDataSource(%s)", url);
176    status_t err = BAD_VALUE;
177    if (url != NULL) {
178        const sp<IMediaPlayerService>& service(getMediaPlayerService());
179        if (service != 0) {
180            sp<IMediaPlayer> player(service->create(getpid(), this, url));
181            err = setDataSource(player);
182        }
183    }
184    return err;
185}
186
187status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
188{
189    LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
190    status_t err = UNKNOWN_ERROR;
191    const sp<IMediaPlayerService>& service(getMediaPlayerService());
192    if (service != 0) {
193        sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length));
194        err = setDataSource(player);
195    }
196    return err;
197}
198
199status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
200{
201    Mutex::Autolock _l(mLock);
202    if ((mPlayer != NULL) && ( mCurrentState & MEDIA_PLAYER_INITIALIZED ))
203    {
204         LOGV("invoke %d", request.dataSize());
205         return  mPlayer->invoke(request, reply);
206    }
207    LOGE("invoke failed: wrong state %X", mCurrentState);
208    return INVALID_OPERATION;
209}
210
211status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
212{
213    LOGD("setMetadataFilter");
214    Mutex::Autolock lock(mLock);
215    if (mPlayer == NULL) {
216        return NO_INIT;
217    }
218    return mPlayer->setMetadataFilter(filter);
219}
220
221status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
222{
223    LOGD("getMetadata");
224    Mutex::Autolock lock(mLock);
225    if (mPlayer == NULL) {
226        return NO_INIT;
227    }
228    return mPlayer->getMetadata(update_only, apply_filter, metadata);
229}
230
231status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
232{
233    LOGV("setVideoSurface");
234    Mutex::Autolock _l(mLock);
235    if (mPlayer == 0) return NO_INIT;
236    if (surface != NULL)
237        return  mPlayer->setVideoSurface(surface->getISurface());
238    else
239        return  mPlayer->setVideoSurface(NULL);
240}
241
242// must call with lock held
243status_t MediaPlayer::prepareAsync_l()
244{
245    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
246        mPlayer->setAudioStreamType(mStreamType);
247        mCurrentState = MEDIA_PLAYER_PREPARING;
248        return mPlayer->prepareAsync();
249    }
250    LOGE("prepareAsync called in state %d", mCurrentState);
251    return INVALID_OPERATION;
252}
253
254// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
255// one defined in the Android framework and one provided by the implementation
256// that generated the error. The sync version of prepare returns only 1 error
257// code.
258status_t MediaPlayer::prepare()
259{
260    LOGV("prepare");
261    Mutex::Autolock _l(mLock);
262    mLockThreadId = getThreadId();
263    if (mPrepareSync) {
264        mLockThreadId = 0;
265        return -EALREADY;
266    }
267    mPrepareSync = true;
268    status_t ret = prepareAsync_l();
269    if (ret != NO_ERROR) {
270        mLockThreadId = 0;
271        return ret;
272    }
273
274    if (mPrepareSync) {
275        mSignal.wait(mLock);  // wait for prepare done
276        mPrepareSync = false;
277    }
278    LOGV("prepare complete - status=%d", mPrepareStatus);
279    mLockThreadId = 0;
280    return mPrepareStatus;
281}
282
283status_t MediaPlayer::prepareAsync()
284{
285    LOGV("prepareAsync");
286    Mutex::Autolock _l(mLock);
287    return prepareAsync_l();
288}
289
290status_t MediaPlayer::start()
291{
292    LOGV("start");
293    Mutex::Autolock _l(mLock);
294    if (mCurrentState & MEDIA_PLAYER_STARTED)
295        return NO_ERROR;
296    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
297                    MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
298        mPlayer->setLooping(mLoop);
299        mPlayer->setVolume(mLeftVolume, mRightVolume);
300        mCurrentState = MEDIA_PLAYER_STARTED;
301        status_t ret = mPlayer->start();
302        if (ret != NO_ERROR) {
303            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
304        } else {
305            if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
306                LOGV("playback completed immediately following start()");
307            }
308        }
309        return ret;
310    }
311    LOGE("start called in state %d", mCurrentState);
312    return INVALID_OPERATION;
313}
314
315status_t MediaPlayer::stop()
316{
317    LOGV("stop");
318    Mutex::Autolock _l(mLock);
319    if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
320    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
321                    MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
322        status_t ret = mPlayer->stop();
323        if (ret != NO_ERROR) {
324            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
325        } else {
326            mCurrentState = MEDIA_PLAYER_STOPPED;
327        }
328        return ret;
329    }
330    LOGE("stop called in state %d", mCurrentState);
331    return INVALID_OPERATION;
332}
333
334status_t MediaPlayer::pause()
335{
336    LOGV("pause");
337    Mutex::Autolock _l(mLock);
338    if (mCurrentState & MEDIA_PLAYER_PAUSED)
339        return NO_ERROR;
340    if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
341        status_t ret = mPlayer->pause();
342        if (ret != NO_ERROR) {
343            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
344        } else {
345            mCurrentState = MEDIA_PLAYER_PAUSED;
346        }
347        return ret;
348    }
349    LOGE("pause called in state %d", mCurrentState);
350    return INVALID_OPERATION;
351}
352
353bool MediaPlayer::isPlaying()
354{
355    Mutex::Autolock _l(mLock);
356    if (mPlayer != 0) {
357        bool temp = false;
358        mPlayer->isPlaying(&temp);
359        LOGV("isPlaying: %d", temp);
360        if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
361            LOGE("internal/external state mismatch corrected");
362            mCurrentState = MEDIA_PLAYER_PAUSED;
363        }
364        return temp;
365    }
366    LOGV("isPlaying: no active player");
367    return false;
368}
369
370status_t MediaPlayer::getVideoWidth(int *w)
371{
372    LOGV("getVideoWidth");
373    Mutex::Autolock _l(mLock);
374    if (mPlayer == 0) return INVALID_OPERATION;
375    *w = mVideoWidth;
376    return NO_ERROR;
377}
378
379status_t MediaPlayer::getVideoHeight(int *h)
380{
381    LOGV("getVideoHeight");
382    Mutex::Autolock _l(mLock);
383    if (mPlayer == 0) return INVALID_OPERATION;
384    *h = mVideoHeight;
385    return NO_ERROR;
386}
387
388status_t MediaPlayer::getCurrentPosition(int *msec)
389{
390    LOGV("getCurrentPosition");
391    Mutex::Autolock _l(mLock);
392    if (mPlayer != 0) {
393        if (mCurrentPosition >= 0) {
394            LOGV("Using cached seek position: %d", mCurrentPosition);
395            *msec = mCurrentPosition;
396            return NO_ERROR;
397        }
398        return mPlayer->getCurrentPosition(msec);
399    }
400    return INVALID_OPERATION;
401}
402
403status_t MediaPlayer::getDuration_l(int *msec)
404{
405    LOGV("getDuration");
406    bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
407    if (mPlayer != 0 && isValidState) {
408        status_t ret = NO_ERROR;
409        if (mDuration <= 0)
410            ret = mPlayer->getDuration(&mDuration);
411        if (msec)
412            *msec = mDuration;
413        return ret;
414    }
415    LOGE("Attempt to call getDuration without a valid mediaplayer");
416    return INVALID_OPERATION;
417}
418
419status_t MediaPlayer::getDuration(int *msec)
420{
421    Mutex::Autolock _l(mLock);
422    return getDuration_l(msec);
423}
424
425status_t MediaPlayer::seekTo_l(int msec)
426{
427    LOGV("seekTo %d", msec);
428    if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED |  MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
429        if ( msec < 0 ) {
430            LOGW("Attempt to seek to invalid position: %d", msec);
431            msec = 0;
432        } else if ((mDuration > 0) && (msec > mDuration)) {
433            LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
434            msec = mDuration;
435        }
436        // cache duration
437        mCurrentPosition = msec;
438        if (mSeekPosition < 0) {
439            getDuration_l(NULL);
440            mSeekPosition = msec;
441            return mPlayer->seekTo(msec);
442        }
443        else {
444            LOGV("Seek in progress - queue up seekTo[%d]", msec);
445            return NO_ERROR;
446        }
447    }
448    LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
449    return INVALID_OPERATION;
450}
451
452status_t MediaPlayer::seekTo(int msec)
453{
454    mLockThreadId = getThreadId();
455    Mutex::Autolock _l(mLock);
456    status_t result = seekTo_l(msec);
457    mLockThreadId = 0;
458
459    return result;
460}
461
462status_t MediaPlayer::reset()
463{
464    LOGV("reset");
465    Mutex::Autolock _l(mLock);
466    mLoop = false;
467    if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
468    mPrepareSync = false;
469    if (mPlayer != 0) {
470        status_t ret = mPlayer->reset();
471        if (ret != NO_ERROR) {
472            LOGE("reset() failed with return code (%d)", ret);
473            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
474        } else {
475            mCurrentState = MEDIA_PLAYER_IDLE;
476        }
477        return ret;
478    }
479    clear_l();
480    return NO_ERROR;
481}
482
483status_t MediaPlayer::setAudioStreamType(int type)
484{
485    LOGV("MediaPlayer::setAudioStreamType");
486    Mutex::Autolock _l(mLock);
487    if (mStreamType == type) return NO_ERROR;
488    if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
489                MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
490        // Can't change the stream type after prepare
491        LOGE("setAudioStream called in state %d", mCurrentState);
492        return INVALID_OPERATION;
493    }
494    // cache
495    mStreamType = type;
496    return OK;
497}
498
499status_t MediaPlayer::setLooping(int loop)
500{
501    LOGV("MediaPlayer::setLooping");
502    Mutex::Autolock _l(mLock);
503    mLoop = (loop != 0);
504    if (mPlayer != 0) {
505        return mPlayer->setLooping(loop);
506    }
507    return OK;
508}
509
510bool MediaPlayer::isLooping() {
511    LOGV("isLooping");
512    Mutex::Autolock _l(mLock);
513    if (mPlayer != 0) {
514        return mLoop;
515    }
516    LOGV("isLooping: no active player");
517    return false;
518}
519
520status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
521{
522    LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
523    Mutex::Autolock _l(mLock);
524    mLeftVolume = leftVolume;
525    mRightVolume = rightVolume;
526    if (mPlayer != 0) {
527        return mPlayer->setVolume(leftVolume, rightVolume);
528    }
529    return OK;
530}
531
532void MediaPlayer::notify(int msg, int ext1, int ext2)
533{
534    LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
535    bool send = true;
536    bool locked = false;
537
538    // TODO: In the future, we might be on the same thread if the app is
539    // running in the same process as the media server. In that case,
540    // this will deadlock.
541    //
542    // The threadId hack below works around this for the care of prepare
543    // and seekTo within the same process.
544    // FIXME: Remember, this is a hack, it's not even a hack that is applied
545    // consistently for all use-cases, this needs to be revisited.
546     if (mLockThreadId != getThreadId()) {
547        mLock.lock();
548        locked = true;
549    }
550
551    if (mPlayer == 0) {
552        LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
553        if (locked) mLock.unlock();   // release the lock when done.
554        return;
555    }
556
557    switch (msg) {
558    case MEDIA_NOP: // interface test message
559        break;
560    case MEDIA_PREPARED:
561        LOGV("prepared");
562        mCurrentState = MEDIA_PLAYER_PREPARED;
563        if (mPrepareSync) {
564            LOGV("signal application thread");
565            mPrepareSync = false;
566            mPrepareStatus = NO_ERROR;
567            mSignal.signal();
568        }
569        break;
570    case MEDIA_PLAYBACK_COMPLETE:
571        LOGV("playback complete");
572        if (!mLoop) {
573            mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
574        }
575        break;
576    case MEDIA_ERROR:
577        // Always log errors.
578        // ext1: Media framework error code.
579        // ext2: Implementation dependant error code.
580        LOGE("error (%d, %d)", ext1, ext2);
581        mCurrentState = MEDIA_PLAYER_STATE_ERROR;
582        if (mPrepareSync)
583        {
584            LOGV("signal application thread");
585            mPrepareSync = false;
586            mPrepareStatus = ext1;
587            mSignal.signal();
588            send = false;
589        }
590        break;
591    case MEDIA_INFO:
592        // ext1: Media framework error code.
593        // ext2: Implementation dependant error code.
594        LOGW("info/warning (%d, %d)", ext1, ext2);
595        break;
596    case MEDIA_SEEK_COMPLETE:
597        LOGV("Received seek complete");
598        if (mSeekPosition != mCurrentPosition) {
599            LOGV("Executing queued seekTo(%d)", mSeekPosition);
600            mSeekPosition = -1;
601            seekTo_l(mCurrentPosition);
602        }
603        else {
604            LOGV("All seeks complete - return to regularly scheduled program");
605            mCurrentPosition = mSeekPosition = -1;
606        }
607        break;
608    case MEDIA_BUFFERING_UPDATE:
609        LOGV("buffering %d", ext1);
610        break;
611    case MEDIA_SET_VIDEO_SIZE:
612        LOGV("New video size %d x %d", ext1, ext2);
613        mVideoWidth = ext1;
614        mVideoHeight = ext2;
615        break;
616    default:
617        LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
618        break;
619    }
620
621    sp<MediaPlayerListener> listener = mListener;
622    if (locked) mLock.unlock();
623
624    // this prevents re-entrant calls into client code
625    if ((listener != 0) && send) {
626        Mutex::Autolock _l(mNotifyLock);
627        LOGV("callback application");
628        listener->notify(msg, ext1, ext2);
629        LOGV("back from callback");
630    }
631}
632
633void MediaPlayer::DeathNotifier::binderDied(const wp<IBinder>& who) {
634    LOGW("MediaPlayer server died!");
635
636    // Need to do this with the lock held
637    SortedVector< wp<MediaPlayer> > list;
638    {
639        Mutex::Autolock _l(MediaPlayer::sServiceLock);
640        MediaPlayer::sMediaPlayerService.clear();
641        list = sObitRecipients;
642    }
643
644    // Notify application when media server dies.
645    // Don't hold the static lock during callback in case app
646    // makes a call that needs the lock.
647    size_t count = list.size();
648    for (size_t iter = 0; iter < count; ++iter) {
649        sp<MediaPlayer> player = list[iter].promote();
650        if ((player != 0) && (player->mPlayer != 0)) {
651            player->notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
652        }
653    }
654}
655
656MediaPlayer::DeathNotifier::~DeathNotifier()
657{
658    Mutex::Autolock _l(sServiceLock);
659    sObitRecipients.clear();
660    if (sMediaPlayerService != 0) {
661        sMediaPlayerService->asBinder()->unlinkToDeath(this);
662    }
663}
664
665/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
666{
667    LOGV("decode(%s)", url);
668    sp<IMemory> p;
669    const sp<IMediaPlayerService>& service = getMediaPlayerService();
670    if (service != 0) {
671        p = sMediaPlayerService->decode(url, pSampleRate, pNumChannels, pFormat);
672    } else {
673        LOGE("Unable to locate media service");
674    }
675    return p;
676
677}
678
679/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
680{
681    LOGV("decode(%d, %lld, %lld)", fd, offset, length);
682    sp<IMemory> p;
683    const sp<IMediaPlayerService>& service = getMediaPlayerService();
684    if (service != 0) {
685        p = sMediaPlayerService->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
686    } else {
687        LOGE("Unable to locate media service");
688    }
689    return p;
690
691}
692
693extern "C" {
694#define FLOATING_POINT 1
695#include "fftwrap.h"
696}
697
698static void *ffttable = NULL;
699
700// peeks at the audio data and fills 'data' with the requested kind
701// (currently kind=0 returns mono 16 bit PCM data, and kind=1 returns
702// 256 point FFT data). Return value is number of samples returned,
703// which may be 0.
704/*static*/ int MediaPlayer::snoop(short* data, int len, int kind) {
705
706    sp<IMemory> p;
707    const sp<IMediaPlayerService>& service = getMediaPlayerService();
708    if (service != 0) {
709        // Take a peek at the waveform. The returned data consists of 16 bit mono PCM data.
710        p = service->snoop();
711
712        if (p == NULL) {
713            return 0;
714        }
715
716        if (kind == 0) { // return waveform data
717            int plen = p->size();
718            len *= 2; // number of shorts -> number of bytes
719            short *src = (short*) p->pointer();
720            if (plen > len) {
721                plen = len;
722            }
723            memcpy(data, src, plen);
724            return plen / sizeof(short); // return number of samples
725        } else if (kind == 1) {
726            // TODO: use a more efficient FFT
727            // Right now this uses the speex library, which is compiled to do a float FFT
728            if (!ffttable) ffttable = spx_fft_init(512);
729            short *usrc = (short*) p->pointer();
730            float fsrc[512];
731            for (int i=0;i<512;i++)
732                fsrc[i] = usrc[i];
733            float fdst[512];
734            spx_fft_float(ffttable, fsrc, fdst);
735            if (len > 512) {
736                len = 512;
737            }
738            len /= 2; // only half the output data is valid
739            for (int i=0; i < len; i++)
740                data[i] = fdst[i];
741            return len;
742        }
743
744    } else {
745        LOGE("Unable to locate media service");
746    }
747    return 0;
748}
749
750}; // namespace android
751