PreviewPlayer.h revision eb69601111ca9b14e58c4d1f6738bfde2db422af
1/*
2 * Copyright (C) 2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef PREVIEW_PLAYER_H_
19
20#define PREVIEW_PLAYER_H_
21
22#include "NuHTTPDataSource.h"
23#include "TimedEventQueue.h"
24#include "VideoEditorAudioPlayer.h"
25
26#include <media/MediaPlayerInterface.h>
27#include <media/stagefright/DataSource.h>
28#include <media/stagefright/OMXClient.h>
29#include <media/stagefright/TimeSource.h>
30#include <utils/threads.h>
31#include <AwesomePlayer.h>
32#include "VideoEditorPreviewController.h"
33
34namespace android {
35
36struct AudioPlayer;
37struct DataSource;
38struct MediaBuffer;
39struct MediaExtractor;
40struct MediaSource;
41
42struct PreviewPlayerRenderer : public RefBase {
43    PreviewPlayerRenderer() {}
44
45    virtual void render(MediaBuffer *buffer) = 0;
46    virtual void render() = 0;
47    virtual void getBuffer(uint8_t **data, size_t *stride) = 0;
48
49private:
50    PreviewPlayerRenderer(const PreviewPlayerRenderer &);
51    PreviewPlayerRenderer &operator=(const PreviewPlayerRenderer &);
52};
53
54struct PreviewPlayer : public AwesomePlayer {
55    PreviewPlayer();
56    ~PreviewPlayer();
57
58    //Override baseclass methods
59    void reset();
60
61    status_t play();
62
63    void setISurface(const sp<ISurface> &isurface);
64
65    status_t seekTo(int64_t timeUs);
66
67    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
68
69    status_t suspend();
70    status_t resume();
71
72    status_t prepare();
73    status_t setDataSource(
74        const char *uri, const KeyedVector<String8, String8> *headers);
75
76    //Added methods
77    status_t loadEffectsSettings(M4VSS3GPP_EffectSettings* pEffectSettings,
78                                 int nEffects);
79    status_t loadAudioMixSettings(M4xVSS_AudioMixingSettings* pAudioMixSettings);
80    status_t setAudioMixPCMFileHandle(M4OSA_Context pAudioMixPCMFileHandle);
81    status_t setAudioMixStoryBoardParam(M4OSA_UInt32 audioMixStoryBoardTS,
82                            M4OSA_UInt32 currentMediaBeginCutTime,
83                            M4OSA_UInt32 currentMediaVolumeVol);
84
85    status_t setPlaybackBeginTime(uint32_t msec);
86    status_t setPlaybackEndTime(uint32_t msec);
87    status_t setStoryboardStartTime(uint32_t msec);
88    status_t setProgressCallbackInterval(uint32_t cbInterval);
89    status_t setMediaRenderingMode(M4xVSS_MediaRendering mode,
90                            M4VIDEOEDITING_VideoFrameSize outputVideoSize);
91
92    status_t resetJniCallbackTimeStamp();
93    status_t setImageClipProperties(uint32_t width, uint32_t height);
94    status_t readFirstVideoFrame();
95
96
97private:
98    friend struct PreviewPlayerEvent;
99
100    enum {
101        PLAYING             = 1,
102        LOOPING             = 2,
103        FIRST_FRAME         = 4,
104        PREPARING           = 8,
105        PREPARED            = 16,
106        AT_EOS              = 32,
107        PREPARE_CANCELLED   = 64,
108        CACHE_UNDERRUN      = 128,
109        AUDIO_AT_EOS        = 256,
110        VIDEO_AT_EOS        = 512,
111        AUTO_LOOPING        = 1024,
112    };
113
114    sp<ISurface> mISurface;
115
116    void cancelPlayerEvents(bool keepBufferingGoing = false);
117    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
118    status_t setDataSource_l(
119        const char *uri, const KeyedVector<String8, String8> *headers);
120    void reset_l();
121    void partial_reset_l();
122    status_t play_l();
123    void initRenderer_l();
124    status_t initAudioDecoder();
125    status_t initVideoDecoder(uint32_t flags = 0);
126    void onVideoEvent();
127    status_t finishSetDataSource_l();
128    static bool ContinuePreparation(void *cookie);
129    void onPrepareAsyncEvent();
130    void finishAsyncPrepare_l();
131
132    sp<PreviewPlayerRenderer> mVideoRenderer;
133
134    int32_t mVideoWidth, mVideoHeight;
135
136    MediaBuffer *mLastVideoBuffer;
137
138    struct SuspensionState {
139        String8 mUri;
140        KeyedVector<String8, String8> mUriHeaders;
141        sp<DataSource> mFileSource;
142
143        uint32_t mFlags;
144        int64_t mPositionUs;
145
146        void *mLastVideoFrame;
147        size_t mLastVideoFrameSize;
148        int32_t mColorFormat;
149        int32_t mVideoWidth, mVideoHeight;
150        int32_t mDecodedWidth, mDecodedHeight;
151
152        SuspensionState()
153            : mLastVideoFrame(NULL) {
154        }
155
156        ~SuspensionState() {
157            if (mLastVideoFrame) {
158                free(mLastVideoFrame);
159                mLastVideoFrame = NULL;
160            }
161        }
162    } *mSuspensionState;
163
164    //Data structures used for audio and video effects
165    M4VSS3GPP_EffectSettings* mEffectsSettings;
166    M4xVSS_AudioMixingSettings* mPreviewPlayerAudioMixSettings;
167    M4OSA_Context mAudioMixPCMFileHandle;
168    M4OSA_UInt32 mAudioMixStoryBoardTS;
169    M4OSA_UInt32 mCurrentMediaBeginCutTime;
170    M4OSA_UInt32 mCurrentMediaVolumeValue;
171
172    uint32_t mNumberEffects;
173    uint32_t mPlayBeginTimeMsec;
174    uint32_t mPlayEndTimeMsec;
175    uint64_t mDecodedVideoTs; // timestamp of current decoded video frame buffer
176    uint64_t mDecVideoTsStoryBoard; // timestamp of frame relative to storyboard
177    uint32_t mCurrentVideoEffect;
178    uint32_t mProgressCbInterval;
179    uint32_t mNumberDecVideoFrames; // Counter of number of video frames decoded
180    sp<TimedEventQueue::Event> mProgressCbEvent;
181    bool mProgressCbEventPending;
182    MediaBuffer *mResizedVideoBuffer;
183    bool mVideoResizedOrCropped;
184    M4xVSS_MediaRendering mRenderingMode;
185    uint32_t mOutputVideoWidth;
186    uint32_t mOutputVideoHeight;
187
188    int32_t mReportedWidth;  //docoder reported width
189    int32_t mReportedHeight; //docoder reported height
190
191    uint32_t mStoryboardStartTimeMsec;
192
193    bool mIsVideoSourceJpg;
194    bool mIsFiftiesEffectStarted;
195    int64_t mImageFrameTimeUs;
196    bool mStartNextPlayer;
197
198    M4VIFI_UInt8*  mFrameRGBBuffer;
199    M4VIFI_UInt8*  mFrameYUVBuffer;
200
201    void setVideoPostProcessingNode(
202                    M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable);
203    M4OSA_ERR doVideoPostProcessing();
204    M4OSA_ERR doMediaRendering();
205    void postProgressCallbackEvent_l();
206    void onProgressCbEvent();
207
208    status_t setDataSource_l_jpg();
209
210    status_t prepare_l();
211    status_t prepareAsync_l();
212
213    PreviewPlayer(const PreviewPlayer &);
214    PreviewPlayer &operator=(const PreviewPlayer &);
215};
216
217}  // namespace android
218
219#endif  // PREVIEW_PLAYER_H_
220
221