AwesomePlayer.h revision f5ab57c2d5e02af7483c94eddb177e4f5c9e9892
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 "NuHTTPDataSource.h"
22#include "TimedEventQueue.h"
23
24#include <media/MediaPlayerInterface.h>
25#include <media/stagefright/DataSource.h>
26#include <media/stagefright/OMXClient.h>
27#include <media/stagefright/TimeSource.h>
28#include <utils/threads.h>
29#include <drm/DrmManagerClient.h>
30
31namespace android {
32
33struct AudioPlayer;
34struct DataSource;
35struct MediaBuffer;
36struct MediaExtractor;
37struct MediaSource;
38struct NuCachedSource2;
39
40struct ALooper;
41struct ARTSPController;
42struct ARTPSession;
43struct UDPPusher;
44
45class DrmManagerClinet;
46class DecryptHandle;
47
48struct AwesomeRenderer : public RefBase {
49    AwesomeRenderer() {}
50
51    virtual void render(MediaBuffer *buffer) = 0;
52
53private:
54    AwesomeRenderer(const AwesomeRenderer &);
55    AwesomeRenderer &operator=(const AwesomeRenderer &);
56};
57
58struct AwesomePlayer {
59    AwesomePlayer();
60    ~AwesomePlayer();
61
62    void setListener(const wp<MediaPlayerBase> &listener);
63
64    status_t setDataSource(
65            const char *uri,
66            const KeyedVector<String8, String8> *headers = NULL);
67
68    status_t setDataSource(int fd, int64_t offset, int64_t length);
69
70    void reset();
71
72    status_t prepare();
73    status_t prepare_l();
74    status_t prepareAsync();
75    status_t prepareAsync_l();
76
77    status_t play();
78    status_t pause();
79
80    bool isPlaying() const;
81
82    void setSurface(const sp<Surface> &surface);
83    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
84    status_t setLooping(bool shouldLoop);
85
86    status_t getDuration(int64_t *durationUs);
87    status_t getPosition(int64_t *positionUs);
88
89    status_t seekTo(int64_t timeUs);
90
91    // This is a mask of MediaExtractor::Flags.
92    uint32_t flags() const;
93
94    void postAudioEOS();
95    void postAudioSeekComplete();
96
97private:
98    friend struct AwesomeEvent;
99
100    enum {
101        PLAYING             = 1,
102        LOOPING             = 2,
103        FIRST_FRAME         = 4,
104        PREPARING           = 8,
105        PREPARED            = 16,
106        AT_EOS              = 32,
107        PREPARE_CANCELLED   = 64,
108        CACHE_UNDERRUN      = 128,
109        AUDIO_AT_EOS        = 256,
110        VIDEO_AT_EOS        = 512,
111        AUTO_LOOPING        = 1024,
112
113        // We are basically done preparing but are currently buffering
114        // sufficient data to begin playback and finish the preparation phase
115        // for good.
116        PREPARING_CONNECTED = 2048,
117    };
118
119    mutable Mutex mLock;
120    Mutex mMiscStateLock;
121
122    OMXClient mClient;
123    TimedEventQueue mQueue;
124    bool mQueueStarted;
125    wp<MediaPlayerBase> mListener;
126
127    sp<Surface> mSurface;
128    sp<MediaPlayerBase::AudioSink> mAudioSink;
129
130    SystemTimeSource mSystemTimeSource;
131    TimeSource *mTimeSource;
132
133    String8 mUri;
134    KeyedVector<String8, String8> mUriHeaders;
135
136    sp<DataSource> mFileSource;
137
138    sp<MediaSource> mVideoTrack;
139    sp<MediaSource> mVideoSource;
140    sp<AwesomeRenderer> mVideoRenderer;
141    bool mVideoRendererIsPreview;
142
143    sp<MediaSource> mAudioTrack;
144    sp<MediaSource> mAudioSource;
145    AudioPlayer *mAudioPlayer;
146    int64_t mDurationUs;
147
148    uint32_t mFlags;
149    uint32_t mExtractorFlags;
150
151    int64_t mTimeSourceDeltaUs;
152    int64_t mVideoTimeUs;
153
154    bool mSeeking;
155    bool mSeekNotificationSent;
156    int64_t mSeekTimeUs;
157
158    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
159
160    bool mWatchForAudioSeekComplete;
161    bool mWatchForAudioEOS;
162
163    sp<TimedEventQueue::Event> mVideoEvent;
164    bool mVideoEventPending;
165    sp<TimedEventQueue::Event> mStreamDoneEvent;
166    bool mStreamDoneEventPending;
167    sp<TimedEventQueue::Event> mBufferingEvent;
168    bool mBufferingEventPending;
169    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
170    bool mAudioStatusEventPending;
171
172    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
173    Condition mPreparedCondition;
174    bool mIsAsyncPrepare;
175    status_t mPrepareResult;
176    status_t mStreamDoneStatus;
177
178    void postVideoEvent_l(int64_t delayUs = -1);
179    void postBufferingEvent_l();
180    void postStreamDoneEvent_l(status_t status);
181    void postCheckAudioStatusEvent_l();
182    status_t play_l();
183
184    MediaBuffer *mVideoBuffer;
185
186    sp<NuHTTPDataSource> mConnectingDataSource;
187    sp<NuCachedSource2> mCachedSource;
188
189    sp<ALooper> mLooper;
190    sp<ARTSPController> mRTSPController;
191    sp<ARTPSession> mRTPSession;
192    sp<UDPPusher> mRTPPusher, mRTCPPusher;
193
194    DrmManagerClient *mDrmManagerClient;
195    DecryptHandle *mDecryptHandle;
196
197    status_t setDataSource_l(
198            const char *uri,
199            const KeyedVector<String8, String8> *headers = NULL);
200
201    status_t setDataSource_l(const sp<DataSource> &dataSource);
202    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
203    void reset_l();
204    void partial_reset_l();
205    status_t seekTo_l(int64_t timeUs);
206    status_t pause_l(bool at_eos = false);
207    void initRenderer_l();
208    void notifyVideoSize_l();
209    void seekAudioIfNecessary_l();
210
211    void cancelPlayerEvents(bool keepBufferingGoing = false);
212
213    void setAudioSource(sp<MediaSource> source);
214    status_t initAudioDecoder();
215
216    void setVideoSource(sp<MediaSource> source);
217    status_t initVideoDecoder(uint32_t flags = 0);
218
219    void onStreamDone();
220
221    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
222
223    void onVideoEvent();
224    void onBufferingUpdate();
225    void onCheckAudioStatus();
226    void onPrepareAsyncEvent();
227    void abortPrepare(status_t err);
228    void finishAsyncPrepare_l();
229
230    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
231
232    status_t finishSetDataSource_l();
233
234    static bool ContinuePreparation(void *cookie);
235
236    static void OnRTSPSeekDoneWrapper(void *cookie);
237    void onRTSPSeekDone();
238
239    bool getBitrate(int64_t *bitrate);
240
241    void finishSeekIfNecessary(int64_t videoTimeUs);
242    void ensureCacheIsFetching_l();
243
244    AwesomePlayer(const AwesomePlayer &);
245    AwesomePlayer &operator=(const AwesomePlayer &);
246};
247
248}  // namespace android
249
250#endif  // AWESOME_PLAYER_H_
251
252