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