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/IGraphicBufferProducer.h>
24 
25 
26 //--------------------------------------------------------------------------------------------------
27 namespace android {
28 
29 class GenericMediaPlayer;
30 class MediaPlayerNotificationClient : public BnMediaPlayerClient
31 {
32 public:
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 
45 protected:
46     virtual ~MediaPlayerNotificationClient();
47 
48 private:
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 
61 class MediaPlayerDeathNotifier : public IMediaDeathNotifier {
62 public:
MediaPlayerDeathNotifier(const sp<MediaPlayerNotificationClient> playerClient)63     MediaPlayerDeathNotifier(const sp<MediaPlayerNotificationClient> playerClient) :
64         mPlayerClient(playerClient) {
65     }
66 
died()67     void died() {
68         mPlayerClient->notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0, NULL);
69     }
70 
71 protected:
~MediaPlayerDeathNotifier()72     virtual ~MediaPlayerDeathNotifier() { }
73 
74 private:
75     const sp<MediaPlayerNotificationClient> mPlayerClient;
76 };
77 
78 
79 //--------------------------------------------------------------------------------------------------
80 class GenericMediaPlayer : public GenericPlayer
81 {
82 public:
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<IGraphicBufferProducer> &bufferProducer);
93 
94     virtual void setPlaybackRate(int32_t ratePermille);
95 
96 protected:
97     friend class MediaPlayerNotificationClient;
98 
99     // Async event handlers (called from GenericPlayer's event loop)
100     virtual void onPrepare();
101     virtual void onPlay();
102     virtual void onPause();
103     virtual void onSeek(const sp<AMessage> &msg);
104     virtual void onLoop(const sp<AMessage> &msg);
105     virtual void onSeekComplete();
106     virtual void onVolumeUpdate();
107     virtual void onBufferingUpdate(const sp<AMessage> &msg);
108     virtual void onAttachAuxEffect(const sp<AMessage> &msg);
109     virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg);
110 
111     const bool mHasVideo;   // const allows MediaPlayerNotificationClient::notify to safely access
112     int32_t mSeekTimeMsec;
113 
114     sp<IGraphicBufferProducer> mVideoSurfaceTexture;
115 
116     // only safe to access from within Realize and looper
117     sp<IMediaPlayer> mPlayer;
118     // Receives Android MediaPlayer events from mPlayer
119     const sp<MediaPlayerNotificationClient> mPlayerClient;
120 
121     // Receives notifications about death of media.player service
122     const sp<MediaPlayerDeathNotifier> mPlayerDeathNotifier;
123 
124     // Return a reference to the media player service, or ALOGE and return NULL after retries fail
getMediaPlayerService()125     static const sp<IMediaPlayerService> getMediaPlayerService() {
126         return IMediaDeathNotifier::getMediaPlayerService();
127     }
128 
129 private:
130     DISALLOW_EVIL_CONSTRUCTORS(GenericMediaPlayer);
131     void afterMediaPlayerPreparedSuccessfully();
132 
133 protected:  // FIXME temporary
134     Mutex mPreparedPlayerLock;          // protects mPreparedPlayer
135     sp<IMediaPlayer> mPreparedPlayer;   // non-NULL if MediaPlayer exists and prepared, write once
136 private:
137     void getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer);   // safely read mPreparedPlayer
138 
139 };
140 
141 } // namespace android
142 
143 // is the specified URI a known distant protocol?
144 bool isDistantProtocol(const char *uri);
145 
146 #endif /* __ANDROID_GENERICMEDIAPLAYER_H__ */
147