blob: 750df494b534c06c2c343bf61a6f590e8a0d9519 [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
James Dongc9dedc42011-05-01 12:36:22 -070021#include "TimedEventQueue.h"
22
23#include <media/MediaPlayerInterface.h>
Chih-Chung Chang7efb8ef2011-07-22 09:01:36 +080024#include <media/stagefright/MediaSource.h>
James Dongc9dedc42011-05-01 12:36:22 -070025#include <media/stagefright/OMXClient.h>
26#include <media/stagefright/TimeSource.h>
27#include <utils/threads.h>
James Dongc9dedc42011-05-01 12:36:22 -070028
29namespace android {
30
31struct AudioPlayerBase;
James Dongc9dedc42011-05-01 12:36:22 -070032struct MediaBuffer;
33struct MediaExtractor;
34struct MediaSource;
James Dongc9dedc42011-05-01 12:36:22 -070035struct ISurfaceTexture;
36
James Dongc9dedc42011-05-01 12:36:22 -070037struct AwesomeRenderer : public RefBase {
38 AwesomeRenderer() {}
39
40 virtual void render(MediaBuffer *buffer) = 0;
41
42private:
43 AwesomeRenderer(const AwesomeRenderer &);
44 AwesomeRenderer &operator=(const AwesomeRenderer &);
45};
46
47struct PreviewPlayerBase {
48 PreviewPlayerBase();
49 ~PreviewPlayerBase();
50
51 void setListener(const wp<MediaPlayerBase> &listener);
52
James Dongdaeb5b32012-01-12 12:12:40 -080053 status_t setDataSource(const char *path);
James Dongc9dedc42011-05-01 12:36:22 -070054 status_t setDataSource(const sp<IStreamSource> &source);
55
56 void reset();
57
58 status_t prepare();
59 status_t prepare_l();
60 status_t prepareAsync();
61 status_t prepareAsync_l();
62
63 status_t play();
64 status_t pause();
65
66 bool isPlaying() const;
67
68 void setSurface(const sp<Surface> &surface);
69 void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
70 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
71 status_t setLooping(bool shouldLoop);
72
73 status_t getDuration(int64_t *durationUs);
74 status_t getPosition(int64_t *positionUs);
75
James Dongc9dedc42011-05-01 12:36:22 -070076 status_t seekTo(int64_t timeUs);
77
78 // This is a mask of MediaExtractor::Flags.
79 uint32_t flags() const;
80
81 void postAudioEOS(int64_t delayUs = 0ll);
82 void postAudioSeekComplete();
83
84private:
85 friend struct AwesomeEvent;
86 friend struct PreviewPlayer;
87
88 enum {
89 PLAYING = 1,
90 LOOPING = 2,
91 FIRST_FRAME = 4,
92 PREPARING = 8,
93 PREPARED = 16,
94 AT_EOS = 32,
95 PREPARE_CANCELLED = 64,
96 CACHE_UNDERRUN = 128,
97 AUDIO_AT_EOS = 256,
98 VIDEO_AT_EOS = 512,
99 AUTO_LOOPING = 1024,
100
101 // We are basically done preparing but are currently buffering
102 // sufficient data to begin playback and finish the preparation phase
103 // for good.
104 PREPARING_CONNECTED = 2048,
105
106 // We're triggering a single video event to display the first frame
107 // after the seekpoint.
108 SEEK_PREVIEW = 4096,
109
110 AUDIO_RUNNING = 8192,
111 AUDIOPLAYER_STARTED = 16384,
112
113 INCOGNITO = 32768,
114 };
115
116 mutable Mutex mLock;
117 Mutex mMiscStateLock;
118
119 OMXClient mClient;
120 TimedEventQueue mQueue;
121 bool mQueueStarted;
122 wp<MediaPlayerBase> mListener;
123
124 sp<Surface> mSurface;
125 sp<ANativeWindow> mNativeWindow;
126 sp<MediaPlayerBase::AudioSink> mAudioSink;
127
128 SystemTimeSource mSystemTimeSource;
129 TimeSource *mTimeSource;
130
131 String8 mUri;
132 KeyedVector<String8, String8> mUriHeaders;
133
James Dongc9dedc42011-05-01 12:36:22 -0700134 sp<MediaSource> mVideoTrack;
135 sp<MediaSource> mVideoSource;
136 sp<AwesomeRenderer> mVideoRenderer;
137 bool mVideoRendererIsPreview;
138
139 sp<MediaSource> mAudioTrack;
140 sp<MediaSource> mAudioSource;
141 AudioPlayerBase *mAudioPlayer;
142 int64_t mDurationUs;
143
144 int32_t mDisplayWidth;
145 int32_t mDisplayHeight;
146
147 uint32_t mFlags;
148 uint32_t mExtractorFlags;
149
150 int64_t mTimeSourceDeltaUs;
151 int64_t mVideoTimeUs;
152
153 enum SeekType {
154 NO_SEEK,
155 SEEK,
156 SEEK_VIDEO_ONLY
157 };
158 SeekType mSeeking;
159
160 bool mSeekNotificationSent;
161 int64_t mSeekTimeUs;
162
163 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown.
164
165 bool mWatchForAudioSeekComplete;
166 bool mWatchForAudioEOS;
167
168 sp<TimedEventQueue::Event> mVideoEvent;
169 bool mVideoEventPending;
170 sp<TimedEventQueue::Event> mStreamDoneEvent;
171 bool mStreamDoneEventPending;
James Dongc9dedc42011-05-01 12:36:22 -0700172 sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
173 bool mAudioStatusEventPending;
174 sp<TimedEventQueue::Event> mVideoLagEvent;
175 bool mVideoLagEventPending;
176
177 sp<TimedEventQueue::Event> mAsyncPrepareEvent;
178 Condition mPreparedCondition;
179 bool mIsAsyncPrepare;
180 status_t mPrepareResult;
181 status_t mStreamDoneStatus;
182
183 void postVideoEvent_l(int64_t delayUs = -1);
184 void postBufferingEvent_l();
185 void postStreamDoneEvent_l(status_t status);
186 void postCheckAudioStatusEvent_l(int64_t delayUs);
187 void postVideoLagEvent_l();
188 status_t play_l();
189
190 MediaBuffer *mVideoBuffer;
191
James Dongc9dedc42011-05-01 12:36:22 -0700192 int64_t mLastVideoTimeUs;
193
Chih-Chung Chang7efb8ef2011-07-22 09:01:36 +0800194 ARect mCropRect;
195 int32_t mGivenWidth, mGivenHeight;
Chih-Chung Chang7efb8ef2011-07-22 09:01:36 +0800196
James Dongdaeb5b32012-01-12 12:12:40 -0800197 status_t setDataSource_l(const char *path);
James Dongc9dedc42011-05-01 12:36:22 -0700198 status_t setDataSource_l(const sp<MediaExtractor> &extractor);
199 void reset_l();
200 status_t seekTo_l(int64_t timeUs);
201 status_t pause_l(bool at_eos = false);
202 void initRenderer_l();
203 void notifyVideoSize_l();
204 void seekAudioIfNecessary_l();
205
James Dongdaeb5b32012-01-12 12:12:40 -0800206 void cancelPlayerEvents();
James Dongc9dedc42011-05-01 12:36:22 -0700207
208 void setAudioSource(sp<MediaSource> source);
209 status_t initAudioDecoder();
210
211 void setVideoSource(sp<MediaSource> source);
212 status_t initVideoDecoder(uint32_t flags = 0);
213
214 void onStreamDone();
215
216 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
217
218 void onVideoEvent();
219 void onBufferingUpdate();
220 void onCheckAudioStatus();
221 void onPrepareAsyncEvent();
222 void abortPrepare(status_t err);
223 void finishAsyncPrepare_l();
224 void onVideoLagUpdate();
225
James Dongc9dedc42011-05-01 12:36:22 -0700226 status_t finishSetDataSource_l();
227
228 static bool ContinuePreparation(void *cookie);
229
James Dongc9dedc42011-05-01 12:36:22 -0700230 void finishSeekIfNecessary(int64_t videoTimeUs);
James Dongc9dedc42011-05-01 12:36:22 -0700231
232 status_t startAudioPlayer_l();
233
234 void shutdownVideoDecoder_l();
235 void setNativeWindow_l(const sp<ANativeWindow> &native);
236
237 PreviewPlayerBase(const PreviewPlayerBase &);
238 PreviewPlayerBase &operator=(const PreviewPlayerBase &);
239};
240
241} // namespace android
242
243#endif // PREVIEW_PLAYER_BASE_H_
244