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