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