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 ANDROID_VIDEOEDITOR_PLAYER_H
18#define ANDROID_VIDEOEDITOR_PLAYER_H
19
20#include <media/MediaPlayerInterface.h>
21#include <media/AudioTrack.h>
22#include "M4xVSS_API.h"
23#include "VideoEditorMain.h"
24#include "VideoEditorTools.h"
25#include "VideoEditorAudioPlayer.h"
26#include "NativeWindowRenderer.h"
27
28namespace android {
29
30struct PreviewPlayer;
31
32class VideoEditorPlayer : public MediaPlayerInterface {
33    public:
34    class VeAudioOutput: public MediaPlayerBase::AudioSink
35    {
36    public:
37                                VeAudioOutput();
38        virtual                 ~VeAudioOutput();
39
40        virtual bool            ready() const { return mTrack != NULL; }
41        virtual bool            realtime() const { return true; }
42        virtual ssize_t         bufferSize() const;
43        virtual ssize_t         frameCount() const;
44        virtual ssize_t         channelCount() const;
45        virtual ssize_t         frameSize() const;
46        virtual uint32_t        latency() const;
47        virtual float           msecsPerFrame() const;
48        virtual status_t        getPosition(uint32_t *position) const;
49        virtual status_t        getFramesWritten(uint32_t*) const;
50        virtual int             getSessionId() const;
51
52        virtual status_t        open(
53                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
54                audio_format_t format, int bufferCount,
55                AudioCallback cb, void *cookie, audio_output_flags_t flags);
56
57        virtual void            start();
58        virtual ssize_t         write(const void* buffer, size_t size);
59        virtual void            stop();
60        virtual void            flush();
61        virtual void            pause();
62        virtual void            close();
63        void setAudioStreamType(audio_stream_type_t streamType) { mStreamType = streamType; }
64                void            setVolume(float left, float right);
65        virtual status_t        dump(int fd,const Vector<String16>& args) const;
66
67        static bool             isOnEmulator();
68        static int              getMinBufferCount();
69    private:
70        static void             setMinBufferCount();
71        static void             CallbackWrapper(
72                int event, void *me, void *info);
73
74        AudioTrack*             mTrack;
75        AudioCallback           mCallback;
76        void *                  mCallbackCookie;
77        audio_stream_type_t     mStreamType;
78        float                   mLeftVolume;
79        float                   mRightVolume;
80        float                   mMsecsPerFrame;
81        uint32_t                mLatency;
82        int                     mSessionId;
83        static bool             mIsOnEmulator;
84        static int              mMinBufferCount; // 12 for emulator; otherwise 4
85
86        public:
87        uint32_t                mNumFramesWritten;
88        void                    snoopWrite(const void*, size_t);
89    };
90
91public:
92    VideoEditorPlayer(NativeWindowRenderer* renderer);
93    virtual ~VideoEditorPlayer();
94
95    virtual status_t initCheck();
96
97    virtual status_t setDataSource(
98            const char *url, const KeyedVector<String8, String8> *headers);
99
100    virtual status_t setDataSource(int fd, int64_t offset, int64_t length);
101    virtual status_t setVideoSurface(const sp<Surface> &surface);
102    virtual status_t setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
103    virtual status_t prepare();
104    virtual status_t prepareAsync();
105    virtual status_t start();
106    virtual status_t stop();
107    virtual status_t pause();
108    virtual bool isPlaying();
109    virtual status_t seekTo(int msec);
110    virtual status_t getCurrentPosition(int *msec);
111    virtual status_t getDuration(int *msec);
112    virtual status_t reset();
113    virtual status_t setLooping(int loop);
114    virtual player_type playerType();
115    virtual status_t invoke(const Parcel &request, Parcel *reply);
116    virtual void setAudioSink(const sp<AudioSink> &audioSink);
117    virtual void acquireLock();
118    virtual void releaseLock();
119    virtual status_t setParameter(int key, const Parcel &request);
120    virtual status_t getParameter(int key, Parcel *reply);
121
122    virtual status_t getMetadata(
123                        const media::Metadata::Filter& ids, Parcel *records);
124
125    virtual status_t loadEffectsSettings(
126                         M4VSS3GPP_EffectSettings* pEffectSettings, int nEffects);
127
128    virtual status_t loadAudioMixSettings(
129                         M4xVSS_AudioMixingSettings* pAudioMixSettings);
130
131    virtual status_t setAudioMixPCMFileHandle(
132                         M4OSA_Context pAudioMixPCMFileHandle);
133
134    virtual status_t setAudioMixStoryBoardParam(
135                         M4OSA_UInt32 x, M4OSA_UInt32 y, M4OSA_UInt32 z);
136
137    virtual status_t setPlaybackBeginTime(uint32_t msec);
138    virtual status_t setPlaybackEndTime(uint32_t msec);
139    virtual status_t setStoryboardStartTime(uint32_t msec);
140    virtual status_t setProgressCallbackInterval(uint32_t cbInterval);
141
142    virtual status_t setMediaRenderingMode(M4xVSS_MediaRendering mode,
143                          M4VIDEOEDITING_VideoFrameSize outputVideoSize);
144
145    virtual status_t resetJniCallbackTimeStamp();
146    virtual status_t setImageClipProperties(uint32_t width, uint32_t height);
147    virtual status_t readFirstVideoFrame();
148    virtual status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
149
150    status_t setAudioPlayer(VideoEditorAudioPlayer *audioPlayer);
151private:
152    PreviewPlayer       *mPlayer;
153    sp<VeAudioOutput>    mVeAudioSink;
154
155    VideoEditorPlayer(const VideoEditorPlayer &);
156    VideoEditorPlayer &operator=(const VideoEditorPlayer &);
157};
158
159}  // namespace android
160
161#endif  // ANDROID_VIDEOEDITOR_PLAYER_H
162