AwesomePlayer.h revision 5d2de4da54504836e4b772b3010ac28c19f667f0
1/*
2 * Copyright (C) 2009 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 AWESOME_PLAYER_H_
18
19#define AWESOME_PLAYER_H_
20
21#include "NuHTTPDataSource.h"
22#include "TimedEventQueue.h"
23
24#include <media/MediaPlayerInterface.h>
25#include <media/stagefright/DataSource.h>
26#include <media/stagefright/OMXClient.h>
27#include <media/stagefright/TimeSource.h>
28#include <utils/threads.h>
29
30namespace android {
31
32struct AudioPlayer;
33struct DataSource;
34struct MediaBuffer;
35struct MediaExtractor;
36struct MediaSource;
37struct NuCachedSource2;
38
39struct ALooper;
40struct ARTSPController;
41
42struct AwesomeRenderer : public RefBase {
43    AwesomeRenderer() {}
44
45    virtual void render(MediaBuffer *buffer) = 0;
46
47private:
48    AwesomeRenderer(const AwesomeRenderer &);
49    AwesomeRenderer &operator=(const AwesomeRenderer &);
50};
51
52struct AwesomePlayer {
53    AwesomePlayer();
54    ~AwesomePlayer();
55
56    void setListener(const wp<MediaPlayerBase> &listener);
57
58    status_t setDataSource(
59            const char *uri,
60            const KeyedVector<String8, String8> *headers = NULL);
61
62    status_t setDataSource(int fd, int64_t offset, int64_t length);
63
64    void reset();
65
66    status_t prepare();
67    status_t prepare_l();
68    status_t prepareAsync();
69    status_t prepareAsync_l();
70
71    status_t play();
72    status_t pause();
73
74    bool isPlaying() const;
75
76    void setISurface(const sp<ISurface> &isurface);
77    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
78    status_t setLooping(bool shouldLoop);
79
80    status_t getDuration(int64_t *durationUs);
81    status_t getPosition(int64_t *positionUs);
82
83    status_t seekTo(int64_t timeUs);
84
85    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
86
87    status_t suspend();
88    status_t resume();
89
90    // This is a mask of MediaExtractor::Flags.
91    uint32_t flags() const;
92
93private:
94    friend struct AwesomeEvent;
95
96    enum {
97        PLAYING             = 1,
98        LOOPING             = 2,
99        FIRST_FRAME         = 4,
100        PREPARING           = 8,
101        PREPARED            = 16,
102        AT_EOS              = 32,
103        PREPARE_CANCELLED   = 64,
104        CACHE_UNDERRUN      = 128,
105        AUDIO_AT_EOS        = 256,
106        VIDEO_AT_EOS        = 512,
107    };
108
109    mutable Mutex mLock;
110    Mutex mMiscStateLock;
111
112    OMXClient mClient;
113    TimedEventQueue mQueue;
114    bool mQueueStarted;
115    wp<MediaPlayerBase> mListener;
116
117    sp<ISurface> mISurface;
118    sp<MediaPlayerBase::AudioSink> mAudioSink;
119
120    SystemTimeSource mSystemTimeSource;
121    TimeSource *mTimeSource;
122
123    String8 mUri;
124    KeyedVector<String8, String8> mUriHeaders;
125
126    sp<DataSource> mFileSource;
127
128    sp<MediaSource> mVideoTrack;
129    sp<MediaSource> mVideoSource;
130    sp<AwesomeRenderer> mVideoRenderer;
131    bool mVideoRendererIsPreview;
132
133    sp<MediaSource> mAudioTrack;
134    sp<MediaSource> mAudioSource;
135    AudioPlayer *mAudioPlayer;
136    int64_t mDurationUs;
137
138    uint32_t mFlags;
139    uint32_t mExtractorFlags;
140
141    int32_t mVideoWidth, mVideoHeight;
142    int64_t mTimeSourceDeltaUs;
143    int64_t mVideoTimeUs;
144
145    bool mSeeking;
146    bool mSeekNotificationSent;
147    int64_t mSeekTimeUs;
148
149    bool mWatchForAudioSeekComplete;
150    bool mWatchForAudioEOS;
151
152    sp<TimedEventQueue::Event> mVideoEvent;
153    bool mVideoEventPending;
154    sp<TimedEventQueue::Event> mStreamDoneEvent;
155    bool mStreamDoneEventPending;
156    sp<TimedEventQueue::Event> mBufferingEvent;
157    bool mBufferingEventPending;
158    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
159    bool mAudioStatusEventPending;
160
161    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
162    Condition mPreparedCondition;
163    bool mIsAsyncPrepare;
164    status_t mPrepareResult;
165    status_t mStreamDoneStatus;
166
167    void postVideoEvent_l(int64_t delayUs = -1);
168    void postBufferingEvent_l();
169    void postStreamDoneEvent_l(status_t status);
170    void postCheckAudioStatusEvent_l();
171    status_t play_l();
172
173    MediaBuffer *mLastVideoBuffer;
174    MediaBuffer *mVideoBuffer;
175
176    sp<NuHTTPDataSource> mConnectingDataSource;
177    sp<NuCachedSource2> mCachedSource;
178
179    sp<ALooper> mLooper;
180    sp<ARTSPController> mRTSPController;
181
182    struct SuspensionState {
183        String8 mUri;
184        KeyedVector<String8, String8> mUriHeaders;
185        sp<DataSource> mFileSource;
186
187        uint32_t mFlags;
188        int64_t mPositionUs;
189
190        void *mLastVideoFrame;
191        size_t mLastVideoFrameSize;
192        int32_t mColorFormat;
193        int32_t mVideoWidth, mVideoHeight;
194        int32_t mDecodedWidth, mDecodedHeight;
195
196        SuspensionState()
197            : mLastVideoFrame(NULL) {
198        }
199
200        ~SuspensionState() {
201            if (mLastVideoFrame) {
202                free(mLastVideoFrame);
203                mLastVideoFrame = NULL;
204            }
205        }
206    } *mSuspensionState;
207
208    status_t setDataSource_l(
209            const char *uri,
210            const KeyedVector<String8, String8> *headers = NULL);
211
212    status_t setDataSource_l(const sp<DataSource> &dataSource);
213    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
214    void reset_l();
215    status_t seekTo_l(int64_t timeUs);
216    status_t pause_l();
217    void initRenderer_l();
218    void seekAudioIfNecessary_l();
219
220    void cancelPlayerEvents(bool keepBufferingGoing = false);
221
222    void setAudioSource(sp<MediaSource> source);
223    status_t initAudioDecoder();
224
225    void setVideoSource(sp<MediaSource> source);
226    status_t initVideoDecoder();
227
228    void onStreamDone();
229
230    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
231
232    void onVideoEvent();
233    void onBufferingUpdate();
234    void onCheckAudioStatus();
235    void onPrepareAsyncEvent();
236    void abortPrepare(status_t err);
237
238    status_t finishSetDataSource_l();
239
240    static bool ContinuePreparation(void *cookie);
241
242    AwesomePlayer(const AwesomePlayer &);
243    AwesomePlayer &operator=(const AwesomePlayer &);
244};
245
246}  // namespace android
247
248#endif  // AWESOME_PLAYER_H_
249
250