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