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