blob: a68d53cb3ee418344be1b74a06cee88b559bab1b [file] [log] [blame]
James Dongc9dedc42011-05-01 12:36:22 -07001/*
2 * Copyright (C) 2011 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 PREVIEW_PLAYER_BASE_H_
18
19#define PREVIEW_PLAYER_BASE_H_
20
21#include "HTTPBase.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 AudioPlayerBase;
34struct DataSource;
35struct MediaBuffer;
36struct MediaExtractor;
37struct MediaSource;
38struct NuCachedSource2;
39struct ISurfaceTexture;
40
41struct ALooper;
42struct ARTSPController;
43
44class DrmManagerClinet;
45class DecryptHandle;
46
47struct AwesomeRenderer : public RefBase {
48 AwesomeRenderer() {}
49
50 virtual void render(MediaBuffer *buffer) = 0;
51
52private:
53 AwesomeRenderer(const AwesomeRenderer &);
54 AwesomeRenderer &operator=(const AwesomeRenderer &);
55};
56
57struct PreviewPlayerBase {
58 PreviewPlayerBase();
59 ~PreviewPlayerBase();
60
61 void setListener(const wp<MediaPlayerBase> &listener);
62
63 status_t setDataSource(
64 const char *uri,
65 const KeyedVector<String8, String8> *headers = NULL);
66
67 status_t setDataSource(int fd, int64_t offset, int64_t length);
68
69 status_t setDataSource(const sp<IStreamSource> &source);
70
71 void reset();
72
73 status_t prepare();
74 status_t prepare_l();
75 status_t prepareAsync();
76 status_t prepareAsync_l();
77
78 status_t play();
79 status_t pause();
80
81 bool isPlaying() const;
82
83 void setSurface(const sp<Surface> &surface);
84 void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
85 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
86 status_t setLooping(bool shouldLoop);
87
88 status_t getDuration(int64_t *durationUs);
89 status_t getPosition(int64_t *positionUs);
90
91 status_t setParameter(int key, const Parcel &request);
92 status_t getParameter(int key, Parcel *reply);
93
94 status_t seekTo(int64_t timeUs);
95
96 // This is a mask of MediaExtractor::Flags.
97 uint32_t flags() const;
98
99 void postAudioEOS(int64_t delayUs = 0ll);
100 void postAudioSeekComplete();
101
102private:
103 friend struct AwesomeEvent;
104 friend struct PreviewPlayer;
105
106 enum {
107 PLAYING = 1,
108 LOOPING = 2,
109 FIRST_FRAME = 4,
110 PREPARING = 8,
111 PREPARED = 16,
112 AT_EOS = 32,
113 PREPARE_CANCELLED = 64,
114 CACHE_UNDERRUN = 128,
115 AUDIO_AT_EOS = 256,
116 VIDEO_AT_EOS = 512,
117 AUTO_LOOPING = 1024,
118
119 // We are basically done preparing but are currently buffering
120 // sufficient data to begin playback and finish the preparation phase
121 // for good.
122 PREPARING_CONNECTED = 2048,
123
124 // We're triggering a single video event to display the first frame
125 // after the seekpoint.
126 SEEK_PREVIEW = 4096,
127
128 AUDIO_RUNNING = 8192,
129 AUDIOPLAYER_STARTED = 16384,
130
131 INCOGNITO = 32768,
132 };
133
134 mutable Mutex mLock;
135 Mutex mMiscStateLock;
136
137 OMXClient mClient;
138 TimedEventQueue mQueue;
139 bool mQueueStarted;
140 wp<MediaPlayerBase> mListener;
141
142 sp<Surface> mSurface;
143 sp<ANativeWindow> mNativeWindow;
144 sp<MediaPlayerBase::AudioSink> mAudioSink;
145
146 SystemTimeSource mSystemTimeSource;
147 TimeSource *mTimeSource;
148
149 String8 mUri;
150 KeyedVector<String8, String8> mUriHeaders;
151
152 sp<DataSource> mFileSource;
153
154 sp<MediaSource> mVideoTrack;
155 sp<MediaSource> mVideoSource;
156 sp<AwesomeRenderer> mVideoRenderer;
157 bool mVideoRendererIsPreview;
158
159 sp<MediaSource> mAudioTrack;
160 sp<MediaSource> mAudioSource;
161 AudioPlayerBase *mAudioPlayer;
162 int64_t mDurationUs;
163
164 int32_t mDisplayWidth;
165 int32_t mDisplayHeight;
166
167 uint32_t mFlags;
168 uint32_t mExtractorFlags;
169
170 int64_t mTimeSourceDeltaUs;
171 int64_t mVideoTimeUs;
172
173 enum SeekType {
174 NO_SEEK,
175 SEEK,
176 SEEK_VIDEO_ONLY
177 };
178 SeekType mSeeking;
179
180 bool mSeekNotificationSent;
181 int64_t mSeekTimeUs;
182
183 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown.
184
185 bool mWatchForAudioSeekComplete;
186 bool mWatchForAudioEOS;
187
188 sp<TimedEventQueue::Event> mVideoEvent;
189 bool mVideoEventPending;
190 sp<TimedEventQueue::Event> mStreamDoneEvent;
191 bool mStreamDoneEventPending;
192 sp<TimedEventQueue::Event> mBufferingEvent;
193 bool mBufferingEventPending;
194 sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
195 bool mAudioStatusEventPending;
196 sp<TimedEventQueue::Event> mVideoLagEvent;
197 bool mVideoLagEventPending;
198
199 sp<TimedEventQueue::Event> mAsyncPrepareEvent;
200 Condition mPreparedCondition;
201 bool mIsAsyncPrepare;
202 status_t mPrepareResult;
203 status_t mStreamDoneStatus;
204
205 void postVideoEvent_l(int64_t delayUs = -1);
206 void postBufferingEvent_l();
207 void postStreamDoneEvent_l(status_t status);
208 void postCheckAudioStatusEvent_l(int64_t delayUs);
209 void postVideoLagEvent_l();
210 status_t play_l();
211
212 MediaBuffer *mVideoBuffer;
213
214 sp<HTTPBase> mConnectingDataSource;
215 sp<NuCachedSource2> mCachedSource;
216
217 sp<ALooper> mLooper;
218 sp<ARTSPController> mRTSPController;
219 sp<ARTSPController> mConnectingRTSPController;
220
221 DrmManagerClient *mDrmManagerClient;
222 sp<DecryptHandle> mDecryptHandle;
223
224 int64_t mLastVideoTimeUs;
225
226 status_t setDataSource_l(
227 const char *uri,
228 const KeyedVector<String8, String8> *headers = NULL);
229
230 status_t setDataSource_l(const sp<DataSource> &dataSource);
231 status_t setDataSource_l(const sp<MediaExtractor> &extractor);
232 void reset_l();
233 status_t seekTo_l(int64_t timeUs);
234 status_t pause_l(bool at_eos = false);
235 void initRenderer_l();
236 void notifyVideoSize_l();
237 void seekAudioIfNecessary_l();
238
239 void cancelPlayerEvents(bool keepBufferingGoing = false);
240
241 void setAudioSource(sp<MediaSource> source);
242 status_t initAudioDecoder();
243
244 void setVideoSource(sp<MediaSource> source);
245 status_t initVideoDecoder(uint32_t flags = 0);
246
247 void onStreamDone();
248
249 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
250
251 void onVideoEvent();
252 void onBufferingUpdate();
253 void onCheckAudioStatus();
254 void onPrepareAsyncEvent();
255 void abortPrepare(status_t err);
256 void finishAsyncPrepare_l();
257 void onVideoLagUpdate();
258
259 bool getCachedDuration_l(int64_t *durationUs, bool *eos);
260
261 status_t finishSetDataSource_l();
262
263 static bool ContinuePreparation(void *cookie);
264
265 static void OnRTSPSeekDoneWrapper(void *cookie);
266 void onRTSPSeekDone();
267
268 bool getBitrate(int64_t *bitrate);
269
270 void finishSeekIfNecessary(int64_t videoTimeUs);
271 void ensureCacheIsFetching_l();
272
273 status_t startAudioPlayer_l();
274
275 void shutdownVideoDecoder_l();
276 void setNativeWindow_l(const sp<ANativeWindow> &native);
277
278 PreviewPlayerBase(const PreviewPlayerBase &);
279 PreviewPlayerBase &operator=(const PreviewPlayerBase &);
280};
281
282} // namespace android
283
284#endif // PREVIEW_PLAYER_BASE_H_
285