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