Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef __ANDROID_GENERICMEDIAPLAYER_H__
     18 #define __ANDROID_GENERICMEDIAPLAYER_H__
     19 
     20 #include "android_GenericPlayer.h"
     21 
     22 #include <binder/IServiceManager.h>
     23 #include <surfaceflinger/Surface.h>
     24 #include <gui/ISurfaceTexture.h>
     25 
     26 
     27 //--------------------------------------------------------------------------------------------------
     28 namespace android {
     29 
     30 class GenericMediaPlayer;
     31 class MediaPlayerNotificationClient : public BnMediaPlayerClient
     32 {
     33 public:
     34     MediaPlayerNotificationClient(GenericMediaPlayer* gmp);
     35     virtual ~MediaPlayerNotificationClient();
     36 
     37     // IMediaPlayerClient implementation
     38     virtual void notify(int msg, int ext1, int ext2, const Parcel *obj);
     39 
     40     // Call before enqueuing a prepare event
     41     void beforePrepare();
     42 
     43     // Call after enqueueing the prepare event; returns true if the prepare
     44     // completed successfully, or false if it completed unsuccessfully
     45     bool blockUntilPlayerPrepared();
     46 
     47 private:
     48     const wp<GenericMediaPlayer> mGenericMediaPlayer;
     49     Mutex mLock;                        // protects mPlayerPrepared
     50     Condition mPlayerPreparedCondition; // signalled when mPlayerPrepared is changed
     51     enum {
     52         PREPARE_NOT_STARTED,
     53         PREPARE_IN_PROGRESS,
     54         PREPARE_COMPLETED_SUCCESSFULLY,
     55         PREPARE_COMPLETED_UNSUCCESSFULLY
     56     } mPlayerPrepared;
     57 };
     58 
     59 
     60 //--------------------------------------------------------------------------------------------------
     61 class GenericMediaPlayer : public GenericPlayer
     62 {
     63 public:
     64 
     65     GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo);
     66     virtual ~GenericMediaPlayer();
     67 
     68     virtual void preDestroy();
     69 
     70     // overridden from GenericPlayer
     71     virtual void getPositionMsec(int* msec); // ANDROID_UNKNOWN_TIME if unknown
     72 
     73     virtual void setVideoSurface(const sp<Surface> &surface);
     74     virtual void setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
     75 
     76 protected:
     77     friend class MediaPlayerNotificationClient;
     78 
     79     // Async event handlers (called from GenericPlayer's event loop)
     80     virtual void onPrepare();
     81     virtual void onPlay();
     82     virtual void onPause();
     83     virtual void onSeek(const sp<AMessage> &msg);
     84     virtual void onLoop(const sp<AMessage> &msg);
     85     virtual void onSeekComplete();
     86     virtual void onVolumeUpdate();
     87     virtual void onBufferingUpdate(const sp<AMessage> &msg);
     88     virtual void onAttachAuxEffect(const sp<AMessage> &msg);
     89     virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg);
     90 
     91     const bool mHasVideo;   // const allows MediaPlayerNotificationClient::notify to safely access
     92     int32_t mSeekTimeMsec;
     93 
     94     // at most one of mVideoSurface and mVideoSurfaceTexture is non-NULL
     95     sp<Surface> mVideoSurface;
     96     sp<ISurfaceTexture> mVideoSurfaceTexture;
     97 
     98     // only safe to access from within Realize and looper
     99     sp<IMediaPlayer> mPlayer;
    100     // Receives Android MediaPlayer events from mPlayer
    101     const sp<MediaPlayerNotificationClient> mPlayerClient;
    102 
    103     // Return a reference to the media player service, or LOGE and return NULL after retries fail
    104     static const sp<IMediaPlayerService> getMediaPlayerService() {
    105         return IMediaDeathNotifier::getMediaPlayerService();
    106     }
    107 
    108 private:
    109     DISALLOW_EVIL_CONSTRUCTORS(GenericMediaPlayer);
    110     void afterMediaPlayerPreparedSuccessfully();
    111 
    112 protected:  // FIXME temporary
    113     Mutex mPreparedPlayerLock;          // protects mPreparedPlayer
    114     sp<IMediaPlayer> mPreparedPlayer;   // non-NULL if MediaPlayer exists and prepared, write once
    115 private:
    116     void getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer);   // safely read mPreparedPlayer
    117 
    118 };
    119 
    120 } // namespace android
    121 
    122 // is the specified URI a known distant protocol?
    123 bool isDistantProtocol(const char *uri);
    124 
    125 #endif /* __ANDROID_GENERICMEDIAPLAYER_H__ */
    126