AwesomePlayer.h revision ab7a2e544643edcb2e09ed5f204580afd763edc1
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/*
220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project
320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License.
620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at
720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software
1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and
1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License.
1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */
1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#ifndef AWESOME_PLAYER_H_
1820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#define AWESOME_PLAYER_H_
2089e69da4d86348409994c9dafbbb2634ccd7c196Andreas Huber
2189e69da4d86348409994c9dafbbb2634ccd7c196Andreas Huber#include "HTTPBase.h"
2220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include "TimedEventQueue.h"
2389e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber
240c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber#include <media/MediaPlayerInterface.h>
2520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/DataSource.h>
2620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/OMXClient.h>
2720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/TimeSource.h>
2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/threads.h>
2920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <drm/DrmManagerClient.h>
3010f75b8c71beb7f327e50bbac8e528af4e40fa24Andreas Huber
3120111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android {
3220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstruct AudioPlayer;
3410f75b8c71beb7f327e50bbac8e528af4e40fa24Andreas Huberstruct DataSource;
352a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huberstruct MediaBuffer;
3610f75b8c71beb7f327e50bbac8e528af4e40fa24Andreas Huberstruct MediaExtractor;
3720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstruct MediaSource;
3820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstruct NuCachedSource2;
3920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstruct ISurfaceTexture;
4020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
4120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass DrmManagerClinet;
422a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huberclass DecryptHandle;
4389e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber
4489e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huberclass TimedTextDriver;
4589e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huberstruct WVMExtractor;
4689e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber
4789e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huberstruct AwesomeRenderer : public RefBase {
4889e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber    AwesomeRenderer() {}
4989e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber
5089e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber    virtual void render(MediaBuffer *buffer) = 0;
5189e7fff6a5d7410815f42b4a55958a59d4463180Andreas Huber
520c89199745bc1bf05b997fc7c342017807676b6fAndreas Huberprivate:
530c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber    AwesomeRenderer(const AwesomeRenderer &);
540c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber    AwesomeRenderer &operator=(const AwesomeRenderer &);
550c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber};
562a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huber
5720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstruct AwesomePlayer {
5820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    AwesomePlayer();
5920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    ~AwesomePlayer();
6020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    void setListener(const wp<MediaPlayerBase> &listener);
6220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    void setUID(uid_t uid);
6320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    status_t setDataSource(
650c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber            const char *uri,
6620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber            const KeyedVector<String8, String8> *headers = NULL);
6720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    status_t setDataSource(int fd, int64_t offset, int64_t length);
6920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
7020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    status_t setDataSource(const sp<IStreamSource> &source);
7120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
7220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    void reset();
7320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
744265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    status_t prepare();
754265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    status_t prepare_l();
764265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    status_t prepareAsync();
772a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huber    status_t prepareAsync_l();
782a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huber
792a4a7d5af053a17586a262a1267ba993e31790f1Andreas Huber    status_t play();
804265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    status_t pause();
814265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber
824265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    bool isPlaying() const;
834265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber
844265a834394bfe14a008a2ac58fbbe388566ad07Andreas Huber    status_t setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
8520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
86    status_t setLooping(bool shouldLoop);
87
88    status_t getDuration(int64_t *durationUs);
89    status_t getPosition(int64_t *positionUs);
90
91    status_t setParameter(int key, const Parcel &request);
92    status_t getParameter(int key, Parcel *reply);
93    status_t invoke(const Parcel &request, Parcel *reply);
94    status_t setCacheStatCollectFreq(const Parcel &request);
95
96    status_t seekTo(int64_t timeUs);
97
98    // This is a mask of MediaExtractor::Flags.
99    uint32_t flags() const;
100
101    void postAudioEOS(int64_t delayUs = 0ll);
102    void postAudioSeekComplete();
103
104    status_t dump(int fd, const Vector<String16> &args) const;
105
106private:
107    friend struct AwesomeEvent;
108    friend struct PreviewPlayer;
109
110    enum {
111        PLAYING             = 0x01,
112        LOOPING             = 0x02,
113        FIRST_FRAME         = 0x04,
114        PREPARING           = 0x08,
115        PREPARED            = 0x10,
116        AT_EOS              = 0x20,
117        PREPARE_CANCELLED   = 0x40,
118        CACHE_UNDERRUN      = 0x80,
119        AUDIO_AT_EOS        = 0x0100,
120        VIDEO_AT_EOS        = 0x0200,
121        AUTO_LOOPING        = 0x0400,
122
123        // We are basically done preparing but are currently buffering
124        // sufficient data to begin playback and finish the preparation phase
125        // for good.
126        PREPARING_CONNECTED = 0x0800,
127
128        // We're triggering a single video event to display the first frame
129        // after the seekpoint.
130        SEEK_PREVIEW        = 0x1000,
131
132        AUDIO_RUNNING       = 0x2000,
133        AUDIOPLAYER_STARTED = 0x4000,
134
135        INCOGNITO           = 0x8000,
136
137        TEXT_RUNNING        = 0x10000,
138        TEXTPLAYER_INITIALIZED  = 0x20000,
139
140        SLOW_DECODER_HACK   = 0x40000,
141    };
142
143    mutable Mutex mLock;
144    Mutex mMiscStateLock;
145    mutable Mutex mStatsLock;
146    Mutex mAudioLock;
147
148    OMXClient mClient;
149    TimedEventQueue mQueue;
150    bool mQueueStarted;
151    wp<MediaPlayerBase> mListener;
152    bool mUIDValid;
153    uid_t mUID;
154
155    sp<ANativeWindow> mNativeWindow;
156    sp<MediaPlayerBase::AudioSink> mAudioSink;
157
158    SystemTimeSource mSystemTimeSource;
159    TimeSource *mTimeSource;
160
161    String8 mUri;
162    KeyedVector<String8, String8> mUriHeaders;
163
164    sp<DataSource> mFileSource;
165
166    sp<MediaSource> mVideoTrack;
167    sp<MediaSource> mVideoSource;
168    sp<AwesomeRenderer> mVideoRenderer;
169    bool mVideoRendererIsPreview;
170
171    ssize_t mActiveAudioTrackIndex;
172    sp<MediaSource> mAudioTrack;
173    sp<MediaSource> mAudioSource;
174    AudioPlayer *mAudioPlayer;
175    int64_t mDurationUs;
176
177    int32_t mDisplayWidth;
178    int32_t mDisplayHeight;
179    int32_t mVideoScalingMode;
180
181    uint32_t mFlags;
182    uint32_t mExtractorFlags;
183    uint32_t mSinceLastDropped;
184
185    int64_t mTimeSourceDeltaUs;
186    int64_t mVideoTimeUs;
187
188    enum SeekType {
189        NO_SEEK,
190        SEEK,
191        SEEK_VIDEO_ONLY
192    };
193    SeekType mSeeking;
194
195    bool mSeekNotificationSent;
196    int64_t mSeekTimeUs;
197
198    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
199
200    bool mWatchForAudioSeekComplete;
201    bool mWatchForAudioEOS;
202
203    sp<TimedEventQueue::Event> mVideoEvent;
204    bool mVideoEventPending;
205    sp<TimedEventQueue::Event> mStreamDoneEvent;
206    bool mStreamDoneEventPending;
207    sp<TimedEventQueue::Event> mBufferingEvent;
208    bool mBufferingEventPending;
209    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
210    bool mAudioStatusEventPending;
211    sp<TimedEventQueue::Event> mVideoLagEvent;
212    bool mVideoLagEventPending;
213
214    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
215    Condition mPreparedCondition;
216    bool mIsAsyncPrepare;
217    status_t mPrepareResult;
218    status_t mStreamDoneStatus;
219
220    void postVideoEvent_l(int64_t delayUs = -1);
221    void postBufferingEvent_l();
222    void postStreamDoneEvent_l(status_t status);
223    void postCheckAudioStatusEvent(int64_t delayUs);
224    void postVideoLagEvent_l();
225    status_t play_l();
226
227    MediaBuffer *mVideoBuffer;
228
229    sp<HTTPBase> mConnectingDataSource;
230    sp<NuCachedSource2> mCachedSource;
231
232    DrmManagerClient *mDrmManagerClient;
233    sp<DecryptHandle> mDecryptHandle;
234
235    int64_t mLastVideoTimeUs;
236    TimedTextDriver *mTextDriver;
237
238    sp<WVMExtractor> mWVMExtractor;
239    sp<MediaExtractor> mExtractor;
240
241    status_t setDataSource_l(
242            const char *uri,
243            const KeyedVector<String8, String8> *headers = NULL);
244
245    status_t setDataSource_l(const sp<DataSource> &dataSource);
246    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
247    void reset_l();
248    status_t seekTo_l(int64_t timeUs);
249    status_t pause_l(bool at_eos = false);
250    void initRenderer_l();
251    void notifyVideoSize_l();
252    void seekAudioIfNecessary_l();
253
254    void cancelPlayerEvents(bool keepNotifications = false);
255
256    void setAudioSource(sp<MediaSource> source);
257    status_t initAudioDecoder();
258
259    void setVideoSource(sp<MediaSource> source);
260    status_t initVideoDecoder(uint32_t flags = 0);
261
262    void addTextSource_l(size_t trackIndex, const sp<MediaSource>& source);
263
264    void onStreamDone();
265
266    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
267
268    void onVideoEvent();
269    void onBufferingUpdate();
270    void onCheckAudioStatus();
271    void onPrepareAsyncEvent();
272    void abortPrepare(status_t err);
273    void finishAsyncPrepare_l();
274    void onVideoLagUpdate();
275
276    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
277
278    status_t finishSetDataSource_l();
279
280    static bool ContinuePreparation(void *cookie);
281
282    bool getBitrate(int64_t *bitrate);
283
284    void finishSeekIfNecessary(int64_t videoTimeUs);
285    void ensureCacheIsFetching_l();
286
287    status_t startAudioPlayer_l(bool sendErrorNotification = true);
288
289    void shutdownVideoDecoder_l();
290    status_t setNativeWindow_l(const sp<ANativeWindow> &native);
291
292    bool isStreamingHTTP() const;
293    void sendCacheStats();
294    void checkDrmStatus(const sp<DataSource>& dataSource);
295
296    enum FlagMode {
297        SET,
298        CLEAR,
299        ASSIGN
300    };
301    void modifyFlags(unsigned value, FlagMode mode);
302
303    struct TrackStat {
304        String8 mMIME;
305        String8 mDecoderName;
306    };
307
308    // protected by mStatsLock
309    struct Stats {
310        int mFd;
311        String8 mURI;
312        int64_t mBitrate;
313
314        // FIXME:
315        // These two indices are just 0 or 1 for now
316        // They are not representing the actual track
317        // indices in the stream.
318        ssize_t mAudioTrackIndex;
319        ssize_t mVideoTrackIndex;
320
321        int64_t mNumVideoFramesDecoded;
322        int64_t mNumVideoFramesDropped;
323        int32_t mVideoWidth;
324        int32_t mVideoHeight;
325        uint32_t mFlags;
326        Vector<TrackStat> mTracks;
327    } mStats;
328
329    status_t setVideoScalingMode(int32_t mode);
330    status_t setVideoScalingMode_l(int32_t mode);
331    status_t getTrackInfo(Parcel* reply) const;
332
333    status_t selectAudioTrack_l(const sp<MediaSource>& source, size_t trackIndex);
334
335    // when select is true, the given track is selected.
336    // otherwise, the given track is unselected.
337    status_t selectTrack(size_t trackIndex, bool select);
338
339    size_t countTracks() const;
340
341    AwesomePlayer(const AwesomePlayer &);
342    AwesomePlayer &operator=(const AwesomePlayer &);
343};
344
345}  // namespace android
346
347#endif  // AWESOME_PLAYER_H_
348