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