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