AwesomePlayer.h revision 2e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9
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
149    void postVideoEvent_l(int64_t delayUs = -1);
150    void postBufferingEvent_l();
151    void postStreamDoneEvent_l();
152    void postCheckAudioStatusEvent_l();
153    status_t getPosition_l(int64_t *positionUs);
154    status_t play_l();
155
156    MediaBuffer *mLastVideoBuffer;
157    MediaBuffer *mVideoBuffer;
158
159    sp<Prefetcher> mPrefetcher;
160
161    struct SuspensionState {
162        String8 mUri;
163        KeyedVector<String8, String8> mUriHeaders;
164        sp<DataSource> mFileSource;
165
166        uint32_t mFlags;
167        int64_t mPositionUs;
168
169        void *mLastVideoFrame;
170        size_t mLastVideoFrameSize;
171        int32_t mColorFormat;
172        int32_t mVideoWidth, mVideoHeight;
173        int32_t mDecodedWidth, mDecodedHeight;
174
175        SuspensionState()
176            : mLastVideoFrame(NULL) {
177        }
178
179        ~SuspensionState() {
180            if (mLastVideoFrame) {
181                free(mLastVideoFrame);
182                mLastVideoFrame = NULL;
183            }
184        }
185    } *mSuspensionState;
186
187    status_t setDataSource_l(
188            const char *uri,
189            const KeyedVector<String8, String8> *headers = NULL);
190
191    status_t setDataSource_l(const sp<DataSource> &dataSource);
192    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
193    void reset_l();
194    status_t seekTo_l(int64_t timeUs);
195    status_t pause_l();
196    void initRenderer_l();
197    void seekAudioIfNecessary_l();
198
199    void cancelPlayerEvents(bool keepBufferingGoing = false);
200
201    status_t setAudioSource(sp<MediaSource> source);
202    status_t setVideoSource(sp<MediaSource> source);
203
204    void onStreamDone();
205
206    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
207
208    void onVideoEvent();
209    void onBufferingUpdate();
210    void onCheckAudioStatus();
211    void onPrepareAsyncEvent();
212    status_t finishSetDataSource_l();
213
214    AwesomePlayer(const AwesomePlayer &);
215    AwesomePlayer &operator=(const AwesomePlayer &);
216};
217
218}  // namespace android
219
220#endif  // AWESOME_PLAYER_H_
221
222