android_GenericMediaPlayer.h revision 01e4a8ff63523bba5c8f919a72e0adb66daf4b98
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 <gui/ISurfaceTexture.h>
24
25
26//--------------------------------------------------------------------------------------------------
27namespace android {
28
29class GenericMediaPlayer;
30class MediaPlayerNotificationClient : public BnMediaPlayerClient
31{
32public:
33    MediaPlayerNotificationClient(GenericMediaPlayer* gmp);
34
35    // IMediaPlayerClient implementation
36    virtual void notify(int msg, int ext1, int ext2, const Parcel *obj);
37
38    // Call before enqueuing a prepare event
39    void beforePrepare();
40
41    // Call after enqueueing the prepare event; returns true if the prepare
42    // completed successfully, or false if it completed unsuccessfully
43    bool blockUntilPlayerPrepared();
44
45protected:
46    virtual ~MediaPlayerNotificationClient();
47
48private:
49    const wp<GenericMediaPlayer> mGenericMediaPlayer;
50    Mutex mLock;                        // protects mPlayerPrepared
51    Condition mPlayerPreparedCondition; // signalled when mPlayerPrepared is changed
52    enum {
53        PREPARE_NOT_STARTED,
54        PREPARE_IN_PROGRESS,
55        PREPARE_COMPLETED_SUCCESSFULLY,
56        PREPARE_COMPLETED_UNSUCCESSFULLY
57    } mPlayerPrepared;
58};
59
60
61class MediaPlayerDeathNotifier : public IMediaDeathNotifier {
62public:
63    MediaPlayerDeathNotifier(const sp<MediaPlayerNotificationClient> playerClient) :
64        mPlayerClient(playerClient) {
65    }
66
67    void died() {
68        mPlayerClient->notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0, NULL);
69    }
70
71protected:
72    virtual ~MediaPlayerDeathNotifier() { }
73
74private:
75    const sp<MediaPlayerNotificationClient> mPlayerClient;
76};
77
78
79//--------------------------------------------------------------------------------------------------
80class GenericMediaPlayer : public GenericPlayer
81{
82public:
83
84    GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo);
85    virtual ~GenericMediaPlayer();
86
87    virtual void preDestroy();
88
89    // overridden from GenericPlayer
90    virtual void getPositionMsec(int* msec); // ANDROID_UNKNOWN_TIME if unknown
91
92    virtual void setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
93
94protected:
95    friend class MediaPlayerNotificationClient;
96
97    // Async event handlers (called from GenericPlayer's event loop)
98    virtual void onPrepare();
99    virtual void onPlay();
100    virtual void onPause();
101    virtual void onSeek(const sp<AMessage> &msg);
102    virtual void onLoop(const sp<AMessage> &msg);
103    virtual void onSeekComplete();
104    virtual void onVolumeUpdate();
105    virtual void onBufferingUpdate(const sp<AMessage> &msg);
106    virtual void onAttachAuxEffect(const sp<AMessage> &msg);
107    virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg);
108
109    const bool mHasVideo;   // const allows MediaPlayerNotificationClient::notify to safely access
110    int32_t mSeekTimeMsec;
111
112    sp<ISurfaceTexture> mVideoSurfaceTexture;
113
114    // only safe to access from within Realize and looper
115    sp<IMediaPlayer> mPlayer;
116    // Receives Android MediaPlayer events from mPlayer
117    const sp<MediaPlayerNotificationClient> mPlayerClient;
118
119    // Receives notifications about death of media.player service
120    const sp<MediaPlayerDeathNotifier> mPlayerDeathNotifier;
121
122    // Return a reference to the media player service, or ALOGE and return NULL after retries fail
123    static const sp<IMediaPlayerService> getMediaPlayerService() {
124        return IMediaDeathNotifier::getMediaPlayerService();
125    }
126
127private:
128    DISALLOW_EVIL_CONSTRUCTORS(GenericMediaPlayer);
129    void afterMediaPlayerPreparedSuccessfully();
130
131protected:  // FIXME temporary
132    Mutex mPreparedPlayerLock;          // protects mPreparedPlayer
133    sp<IMediaPlayer> mPreparedPlayer;   // non-NULL if MediaPlayer exists and prepared, write once
134private:
135    void getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer);   // safely read mPreparedPlayer
136
137};
138
139} // namespace android
140
141// is the specified URI a known distant protocol?
142bool isDistantProtocol(const char *uri);
143
144#endif /* __ANDROID_GENERICMEDIAPLAYER_H__ */
145