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