android_GenericPlayer.h revision 4b0e0b2860ffd5e246b42c8a434833cca2f068b3
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_GENERICPLAYER_H__
18#define __ANDROID_GENERICPLAYER_H__
19
20#include <media/stagefright/foundation/AHandler.h>
21#include <media/stagefright/foundation/ALooper.h>
22#include <media/stagefright/foundation/AMessage.h>
23
24//--------------------------------------------------------------------------------------------------
25/**
26 * Message parameters for AHandler messages, see list in GenericPlayer::kWhatxxx
27 */
28#define WHATPARAM_SEEK_SEEKTIME_MS                  "seekTimeMs"
29#define WHATPARAM_LOOP_LOOPING                      "looping"
30#define WHATPARAM_BUFFERING_UPDATE                  "bufferingUpdate"
31#define WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT "buffUpdateThreshold"
32
33namespace android {
34
35class GenericPlayer : public AHandler
36{
37public:
38
39    enum {
40        kEventPrepared                = 'prep',
41        kEventHasVideoSize            = 'vsiz',
42        kEventPrefetchStatusChange    = 'pfsc',
43        kEventPrefetchFillLevelUpdate = 'pflu',
44        kEventEndOfStream             = 'eos'
45    };
46
47
48    GenericPlayer(const AudioPlayback_Parameters* params);
49    virtual ~GenericPlayer();
50
51    virtual void init(const notif_cbf_t cbf, void* notifUser);
52    virtual void preDestroy();
53
54    virtual void setDataSource(const char *uri);
55    virtual void setDataSource(int fd, int64_t offset, int64_t length);
56
57    virtual void prepare();
58    virtual void play();
59    virtual void pause();
60    virtual void stop();
61    virtual void seek(int64_t timeMsec);
62    virtual void loop(bool loop);
63    virtual void setBufferingUpdateThreshold(int16_t thresholdPercent);
64
65    virtual void getDurationMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
66    virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
67    virtual void getSampleRate(uint32_t* hz);// hz  != NULL, UNKNOWN_SAMPLERATE if unknown
68
69    void setVolume(bool mute, bool useStereoPos, XApermille stereoPos, XAmillibel volume);
70
71protected:
72    // mutex used for set vs use of volume and cache (fill, threshold) settings
73    Mutex mSettingsLock;
74
75    void resetDataLocator();
76    DataLocator2 mDataLocator;
77    int          mDataLocatorType;
78
79    // Constants used to identify the messages in this player's AHandler message loop
80    //   in onMessageReceived()
81    enum {
82        kWhatPrepare         = 'prep',
83        kWhatNotif           = 'noti',
84        kWhatPlay            = 'play',
85        kWhatPause           = 'paus',
86        kWhatSeek            = 'seek',
87        kWhatSeekComplete    = 'skcp',
88        kWhatLoop            = 'loop',
89        kWhatVolumeUpdate    = 'volu',
90        kWhatBufferingUpdate = 'bufu',
91        kWhatBuffUpdateThres = 'buut',
92        kWhatMediaPlayerInfo = 'mpin'
93    };
94
95    // Send a notification to one of the event listeners
96    virtual void notify(const char* event, int data1, bool async);
97    virtual void notify(const char* event, int data1, int data2, bool async);
98
99    // AHandler implementation
100    virtual void onMessageReceived(const sp<AMessage> &msg);
101
102    // Async event handlers (called from GenericPlayer's event loop)
103    virtual void onPrepare();
104    virtual void onNotify(const sp<AMessage> &msg);
105    virtual void onPlay();
106    virtual void onPause();
107    virtual void onSeek(const sp<AMessage> &msg);
108    virtual void onLoop(const sp<AMessage> &msg);
109    virtual void onVolumeUpdate();
110    virtual void onSeekComplete();
111    virtual void onBufferingUpdate(const sp<AMessage> &msg);
112    virtual void onSetBufferingUpdateThreshold(const sp<AMessage> &msg);
113
114    // Convenience methods
115    //   for async notifications of prefetch status and cache fill level, needs to be called
116    //     with mSettingsLock locked
117    void notifyStatus();
118    void notifyCacheFill();
119    //   for internal async notification to update state that the player is no longer seeking
120    void seekComplete();
121    void bufferingUpdate(int16_t fillLevelPerMille);
122
123    // Event notification from GenericPlayer to OpenSL ES / OpenMAX AL framework
124    notif_cbf_t mNotifyClient;
125    void*       mNotifyUser;
126    // lock to protect mNotifyClient and mNotifyUser updates
127    Mutex       mNotifyClientLock;
128
129    enum {
130        kFlagPrepared  = 1 <<0,
131        kFlagPreparing = 1 <<1,
132        kFlagPlaying   = 1 <<2,
133        kFlagBuffering = 1 <<3,
134        kFlagSeeking   = 1 <<4,
135        kFlagLooping   = 1 <<5,
136    };
137
138    uint32_t mStateFlags;
139
140    sp<ALooper> mLooper;
141    int32_t mLooperPriority;
142
143    AudioPlayback_Parameters mPlaybackParams;
144
145    AndroidAudioLevels mAndroidAudioLevels;
146    int mChannelCount; // this is used for the panning law, and is not exposed outside of the object
147    int32_t mDurationMsec;
148    // position is not protected by any lock in this generic class, may be different in subclasses
149    int32_t mPositionMsec;
150    uint32_t mSampleRateHz;
151
152    CacheStatus_t mCacheStatus;
153    int16_t mCacheFill; // cache fill level + played back level in permille
154    int16_t mLastNotifiedCacheFill; // last cache fill level communicated to the listener
155    int16_t mCacheFillNotifThreshold; // threshold in cache fill level for cache fill to be reported
156
157private:
158    DISALLOW_EVIL_CONSTRUCTORS(GenericPlayer);
159};
160
161} // namespace android
162
163#endif /* __ANDROID_GENERICPLAYER_H__ */
164