1/*
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_H_
18
19#define PREVIEW_PLAYER_H_
20
21#include "TimedEventQueue.h"
22#include "VideoEditorAudioPlayer.h"
23
24#include <media/MediaPlayerInterface.h>
25#include <media/stagefright/OMXClient.h>
26#include <media/stagefright/TimeSource.h>
27#include <utils/threads.h>
28#include "NativeWindowRenderer.h"
29
30namespace android {
31
32struct VideoEditorAudioPlayer;
33struct MediaExtractor;
34
35struct PreviewPlayer {
36    PreviewPlayer(NativeWindowRenderer* renderer);
37    ~PreviewPlayer();
38
39    void setListener(const wp<MediaPlayerBase> &listener);
40    void reset();
41
42    status_t play();
43    status_t pause();
44
45    bool isPlaying() const;
46    void setSurface(const sp<Surface> &surface);
47    void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
48    status_t seekTo(int64_t timeUs);
49
50    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
51
52
53    // FIXME: Sync between ...
54    void acquireLock();
55    void releaseLock();
56
57    status_t prepare();
58    status_t prepareAsync();
59    status_t setDataSource(const char *path);
60    status_t setDataSource(const sp<IStreamSource> &source);
61
62    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
63    status_t setLooping(bool shouldLoop);
64    status_t getDuration(int64_t *durationUs);
65    status_t getPosition(int64_t *positionUs);
66
67    uint32_t getSourceSeekFlags() const;
68
69    void postAudioEOS(int64_t delayUs = 0ll);
70    void postAudioSeekComplete();
71
72    status_t loadEffectsSettings(M4VSS3GPP_EffectSettings* pEffectSettings,
73                                 int nEffects);
74    status_t loadAudioMixSettings(M4xVSS_AudioMixingSettings* pAudioMixSettings);
75    status_t setAudioMixPCMFileHandle(M4OSA_Context pAudioMixPCMFileHandle);
76    status_t setAudioMixStoryBoardParam(M4OSA_UInt32 audioMixStoryBoardTS,
77                            M4OSA_UInt32 currentMediaBeginCutTime,
78                            M4OSA_UInt32 currentMediaVolumeVol);
79
80    status_t setPlaybackBeginTime(uint32_t msec);
81    status_t setPlaybackEndTime(uint32_t msec);
82    status_t setStoryboardStartTime(uint32_t msec);
83    status_t setProgressCallbackInterval(uint32_t cbInterval);
84    status_t setMediaRenderingMode(M4xVSS_MediaRendering mode,
85                            M4VIDEOEDITING_VideoFrameSize outputVideoSize);
86
87    status_t resetJniCallbackTimeStamp();
88    status_t setImageClipProperties(uint32_t width, uint32_t height);
89    status_t readFirstVideoFrame();
90    status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
91    status_t setAudioPlayer(VideoEditorAudioPlayer *audioPlayer);
92
93private:
94    enum {
95        PLAYING             = 1,
96        LOOPING             = 2,
97        FIRST_FRAME         = 4,
98        PREPARING           = 8,
99        PREPARED            = 16,
100        AT_EOS              = 32,
101        PREPARE_CANCELLED   = 64,
102        CACHE_UNDERRUN      = 128,
103        AUDIO_AT_EOS        = 256,
104        VIDEO_AT_EOS        = 512,
105        AUTO_LOOPING        = 1024,
106        INFORMED_AV_EOS     = 2048,
107
108        // We are basically done preparing but are currently buffering
109        // sufficient data to begin playback and finish the preparation phase
110        // for good.
111        PREPARING_CONNECTED = 2048,
112
113        // We're triggering a single video event to display the first frame
114        // after the seekpoint.
115        SEEK_PREVIEW        = 4096,
116
117        AUDIO_RUNNING       = 8192,
118        AUDIOPLAYER_STARTED = 16384,
119
120        INCOGNITO           = 32768,
121    };
122
123    mutable Mutex mLock;
124
125    OMXClient mClient;
126    TimedEventQueue mQueue;
127    bool mQueueStarted;
128    wp<MediaPlayerBase> mListener;
129
130    sp<Surface> mSurface;
131    sp<ANativeWindow> mNativeWindow;
132    sp<MediaPlayerBase::AudioSink> mAudioSink;
133
134    SystemTimeSource mSystemTimeSource;
135    TimeSource *mTimeSource;
136
137    String8 mUri;
138
139    sp<MediaSource> mVideoTrack;
140    sp<MediaSource> mVideoSource;
141    bool mVideoRendererIsPreview;
142
143    sp<MediaSource> mAudioTrack;
144    sp<MediaSource> mAudioSource;
145    VideoEditorAudioPlayer *mAudioPlayer;
146    int64_t mDurationUs;
147
148    int32_t mDisplayWidth;
149    int32_t mDisplayHeight;
150
151    uint32_t mFlags;
152    uint32_t mExtractorFlags;
153
154    int64_t mTimeSourceDeltaUs;
155    int64_t mVideoTimeUs;
156
157    enum SeekType {
158        NO_SEEK,
159        SEEK,
160        SEEK_VIDEO_ONLY
161    };
162    SeekType mSeeking;
163
164    bool mSeekNotificationSent;
165    int64_t mSeekTimeUs;
166
167    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
168
169    bool mWatchForAudioSeekComplete;
170    bool mWatchForAudioEOS;
171
172    sp<TimedEventQueue::Event> mVideoEvent;
173    bool mVideoEventPending;
174    sp<TimedEventQueue::Event> mStreamDoneEvent;
175    bool mStreamDoneEventPending;
176    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
177    bool mAudioStatusEventPending;
178    sp<TimedEventQueue::Event> mVideoLagEvent;
179    bool mVideoLagEventPending;
180
181    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
182    Condition mPreparedCondition;
183    bool mIsAsyncPrepare;
184    status_t mPrepareResult;
185    status_t mStreamDoneStatus;
186
187    MediaBuffer *mVideoBuffer;
188    int64_t mLastVideoTimeUs;
189    ARect mCropRect;
190    int32_t mGivenWidth, mGivenHeight;
191
192
193    bool mIsChangeSourceRequired;
194
195    NativeWindowRenderer *mNativeWindowRenderer;
196    RenderInput *mVideoRenderer;
197
198    int32_t mVideoWidth, mVideoHeight;
199
200    //Data structures used for audio and video effects
201    M4VSS3GPP_EffectSettings* mEffectsSettings;
202    M4xVSS_AudioMixingSettings* mPreviewPlayerAudioMixSettings;
203    M4OSA_Context mAudioMixPCMFileHandle;
204    M4OSA_UInt32 mAudioMixStoryBoardTS;
205    M4OSA_UInt32 mCurrentMediaBeginCutTime;
206    M4OSA_UInt32 mCurrentMediaVolumeValue;
207    M4OSA_UInt32 mCurrFramingEffectIndex;
208
209    uint32_t mNumberEffects;
210    uint32_t mPlayBeginTimeMsec;
211    uint32_t mPlayEndTimeMsec;
212    uint64_t mDecodedVideoTs; // timestamp of current decoded video frame buffer
213    uint64_t mDecVideoTsStoryBoard; // timestamp of frame relative to storyboard
214    uint32_t mCurrentVideoEffect;
215    uint32_t mProgressCbInterval;
216    uint32_t mNumberDecVideoFrames; // Counter of number of video frames decoded
217    sp<TimedEventQueue::Event> mProgressCbEvent;
218    bool mProgressCbEventPending;
219    sp<TimedEventQueue::Event> mOverlayUpdateEvent;
220    bool mOverlayUpdateEventPending;
221    bool mOverlayUpdateEventPosted;
222
223    M4xVSS_MediaRendering mRenderingMode;
224    uint32_t mOutputVideoWidth;
225    uint32_t mOutputVideoHeight;
226
227    uint32_t mStoryboardStartTimeMsec;
228
229    bool mIsVideoSourceJpg;
230    bool mIsFiftiesEffectStarted;
231    int64_t mImageFrameTimeUs;
232    bool mStartNextPlayer;
233    mutable Mutex mLockControl;
234
235    M4VIFI_UInt8*  mFrameRGBBuffer;
236    M4VIFI_UInt8*  mFrameYUVBuffer;
237
238    void cancelPlayerEvents_l(bool updateProgressCb = false);
239    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
240    status_t setDataSource_l(const char *path);
241    void setNativeWindow_l(const sp<ANativeWindow> &native);
242    void reset_l();
243    void clear_l();
244    status_t play_l();
245    status_t pause_l(bool at_eos = false);
246    status_t initRenderer_l();
247    status_t initAudioDecoder_l();
248    status_t initVideoDecoder_l(uint32_t flags = 0);
249    void notifyVideoSize_l();
250    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
251    void onVideoEvent();
252    void onVideoLagUpdate();
253    void onStreamDone();
254    void onCheckAudioStatus();
255    void onPrepareAsyncEvent();
256
257    void finishAsyncPrepare_l();
258    void abortPrepare(status_t err);
259
260    status_t startAudioPlayer_l();
261    void setVideoSource(const sp<MediaSource>& source);
262    status_t finishSetDataSource_l();
263    void setAudioSource(const sp<MediaSource>& source);
264
265    status_t seekTo_l(int64_t timeUs);
266    void seekAudioIfNecessary_l();
267    void finishSeekIfNecessary(int64_t videoTimeUs);
268
269    void postCheckAudioStatusEvent_l(int64_t delayUs);
270    void postVideoLagEvent_l();
271    void postStreamDoneEvent_l(status_t status);
272    void postVideoEvent_l(int64_t delayUs = -1);
273    void setVideoPostProcessingNode(
274                    M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable);
275    void postProgressCallbackEvent_l();
276    void shutdownVideoDecoder_l();
277    void onProgressCbEvent();
278
279    void postOverlayUpdateEvent_l();
280    void onUpdateOverlayEvent();
281
282    status_t setDataSource_l_jpg();
283    status_t prepare_l();
284    status_t prepareAsync_l();
285    void updateBatteryUsage_l();
286    void updateSizeToRender(sp<MetaData> meta);
287
288    void setDuration_l(int64_t durationUs);
289    void setPosition_l(int64_t timeUs);
290
291    PreviewPlayer(const PreviewPlayer &);
292    PreviewPlayer &operator=(const PreviewPlayer &);
293};
294
295}  // namespace android
296
297#endif  // PREVIEW_PLAYER_H_
298
299