android_GenericPlayer.h revision e6ded5c61944a87fa9e472dec3a6929855d42aeb
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); // ANDROID_UNKNOWN_TIME if unknown
66    virtual void getPositionMsec(int* msec); // ANDROID_UNKNOWN_TIME if unknown
67    virtual void getSampleRate(uint32_t* hz);// ANDROID_UNKNOWN_SAMPLERATE if unknown
68
69    void setVolume(bool mute, bool useStereoPos, XApermille stereoPos, XAmillibel volume);
70
71protected:
72    Mutex mSettingsLock;
73
74    void resetDataLocator();
75    DataLocator2 mDataLocator;
76    int          mDataLocatorType;
77
78    // Constants used to identify the messages in this player's AHandler message loop
79    //   in onMessageReceived()
80    enum {
81        kWhatPrepare         = 'prep',
82        kWhatNotif           = 'noti',
83        kWhatPlay            = 'play',
84        kWhatPause           = 'paus',
85        kWhatSeek            = 'seek',
86        kWhatSeekComplete    = 'skcp',
87        kWhatLoop            = 'loop',
88        kWhatVolumeUpdate    = 'volu',
89        kWhatBufferingUpdate = 'bufu',
90        kWhatBuffUpdateThres = 'buut',
91    };
92
93    // Send a notification to one of the event listeners
94    virtual void notify(const char* event, int data1, bool async);
95    virtual void notify(const char* event, int data1, int data2, bool async);
96
97    // AHandler implementation
98    virtual void onMessageReceived(const sp<AMessage> &msg);
99
100    // Async event handlers (called from GenericPlayer's event loop)
101    virtual void onPrepare();
102    virtual void onNotify(const sp<AMessage> &msg);
103    virtual void onPlay();
104    virtual void onPause();
105    virtual void onSeek(const sp<AMessage> &msg);
106    virtual void onLoop(const sp<AMessage> &msg);
107    virtual void onVolumeUpdate();
108    virtual void onSeekComplete();
109    virtual void onBufferingUpdate(const sp<AMessage> &msg);
110    virtual void onSetBufferingUpdateThreshold(const sp<AMessage> &msg);
111
112    // Convenience methods
113    //   for async notifications of prefetch status and cache fill level, needs to be called
114    //     with mSettingsLock locked
115    void notifyStatus();
116    void notifyCacheFill();
117    //   for internal async notification to update state that the player is no longer seeking
118    void seekComplete();
119    void bufferingUpdate(int16_t fillLevelPerMille);
120
121    // Event notification from GenericPlayer to OpenSL ES / OpenMAX AL framework
122    notif_cbf_t mNotifyClient;
123    void*       mNotifyUser;
124    // lock to protect mNotifyClient and mNotifyUser updates
125    Mutex       mNotifyClientLock;
126
127    enum {
128        kFlagPrepared  = 1 <<0,
129        kFlagPreparing = 1 <<1,
130        kFlagPlaying   = 1 <<2,
131        kFlagBuffering = 1 <<3,
132        kFlagSeeking   = 1 <<4,
133        kFlagLooping   = 1 <<5,
134    };
135
136    uint32_t mStateFlags;
137
138    sp<ALooper> mLooper;
139    int32_t mLooperPriority;
140
141    AudioPlayback_Parameters mPlaybackParams;
142
143    AndroidAudioLevels mAndroidAudioLevels;
144    int mChannelCount; // this is used for the panning law, and is not exposed outside of the object
145    int32_t mDurationMsec;
146    int32_t mPositionMsec;
147    uint32_t mSampleRateHz;
148
149    CacheStatus_t mCacheStatus;
150    int16_t mCacheFill; // cache fill level + played back level in permille
151    int16_t mLastNotifiedCacheFill; // last cache fill level communicated to the listener
152    int16_t mCacheFillNotifThreshold; // threshold in cache fill level for cache fill to be reported
153
154private:
155    DISALLOW_EVIL_CONSTRUCTORS(GenericPlayer);
156};
157
158} // namespace android
159
160#endif /* __ANDROID_GENERICPLAYER_H__ */
161