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