AwesomePlayer.h revision acdd9d0f59c3d8bdc3de80664a609527ec3d21e2
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
87    // This is a mask of MediaExtractor::Flags.
88    uint32_t flags() const;
89
90private:
91    friend struct AwesomeEvent;
92
93    enum {
94        PLAYING             = 1,
95        LOOPING             = 2,
96        FIRST_FRAME         = 4,
97        PREPARING           = 8,
98        PREPARED            = 16,
99        AT_EOS              = 32,
100        PREPARE_CANCELLED   = 64,
101    };
102
103    mutable Mutex mLock;
104    Mutex mMiscStateLock;
105
106    OMXClient mClient;
107    TimedEventQueue mQueue;
108    bool mQueueStarted;
109    wp<MediaPlayerBase> mListener;
110
111    sp<ISurface> mISurface;
112    sp<MediaPlayerBase::AudioSink> mAudioSink;
113
114    TimeSource *mTimeSource;
115
116    String8 mUri;
117    KeyedVector<String8, String8> mUriHeaders;
118
119    sp<DataSource> mFileSource;
120
121    sp<MediaSource> mVideoTrack;
122    sp<MediaSource> mVideoSource;
123    sp<AwesomeRenderer> mVideoRenderer;
124    bool mVideoRendererIsPreview;
125
126    sp<MediaSource> mAudioTrack;
127    sp<MediaSource> mAudioSource;
128    AudioPlayer *mAudioPlayer;
129    int64_t mDurationUs;
130
131    uint32_t mFlags;
132    uint32_t mExtractorFlags;
133
134    int32_t mVideoWidth, mVideoHeight;
135    int64_t mTimeSourceDeltaUs;
136    int64_t mVideoTimeUs;
137
138    bool mSeeking;
139    bool mSeekNotificationSent;
140    int64_t mSeekTimeUs;
141
142    bool mWatchForAudioSeekComplete;
143    bool mWatchForAudioEOS;
144
145    sp<TimedEventQueue::Event> mVideoEvent;
146    bool mVideoEventPending;
147    sp<TimedEventQueue::Event> mStreamDoneEvent;
148    bool mStreamDoneEventPending;
149    sp<TimedEventQueue::Event> mBufferingEvent;
150    bool mBufferingEventPending;
151    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
152    bool mAudioStatusEventPending;
153
154    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
155    Condition mPreparedCondition;
156    bool mIsAsyncPrepare;
157    status_t mPrepareResult;
158    status_t mStreamDoneStatus;
159
160    void postVideoEvent_l(int64_t delayUs = -1);
161    void postBufferingEvent_l();
162    void postStreamDoneEvent_l(status_t status);
163    void postCheckAudioStatusEvent_l();
164    status_t play_l();
165
166    MediaBuffer *mLastVideoBuffer;
167    MediaBuffer *mVideoBuffer;
168
169    sp<Prefetcher> mPrefetcher;
170    sp<HTTPDataSource> mConnectingDataSource;
171
172    struct SuspensionState {
173        String8 mUri;
174        KeyedVector<String8, String8> mUriHeaders;
175        sp<DataSource> mFileSource;
176
177        uint32_t mFlags;
178        int64_t mPositionUs;
179
180        void *mLastVideoFrame;
181        size_t mLastVideoFrameSize;
182        int32_t mColorFormat;
183        int32_t mVideoWidth, mVideoHeight;
184        int32_t mDecodedWidth, mDecodedHeight;
185
186        SuspensionState()
187            : mLastVideoFrame(NULL) {
188        }
189
190        ~SuspensionState() {
191            if (mLastVideoFrame) {
192                free(mLastVideoFrame);
193                mLastVideoFrame = NULL;
194            }
195        }
196    } *mSuspensionState;
197
198    status_t setDataSource_l(
199            const char *uri,
200            const KeyedVector<String8, String8> *headers = NULL);
201
202    status_t setDataSource_l(const sp<DataSource> &dataSource);
203    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
204    void reset_l();
205    status_t seekTo_l(int64_t timeUs);
206    status_t pause_l();
207    void initRenderer_l();
208    void seekAudioIfNecessary_l();
209
210    void cancelPlayerEvents(bool keepBufferingGoing = false);
211
212    void setAudioSource(sp<MediaSource> source);
213    status_t initAudioDecoder();
214
215    void setVideoSource(sp<MediaSource> source);
216    status_t initVideoDecoder();
217
218    void onStreamDone();
219
220    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
221
222    void onVideoEvent();
223    void onBufferingUpdate();
224    void onCheckAudioStatus();
225    void onPrepareAsyncEvent();
226    void abortPrepare(status_t err);
227
228    status_t finishSetDataSource_l();
229
230    static bool ContinuePreparation(void *cookie);
231
232    AwesomePlayer(const AwesomePlayer &);
233    AwesomePlayer &operator=(const AwesomePlayer &);
234};
235
236}  // namespace android
237
238#endif  // AWESOME_PLAYER_H_
239
240