StagefrightRecorder.h revision e2a2dfcbf0c9d6bb7139263ecf0d8e53b4ca1049
1/*
2 * Copyright (C) 2009 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 STAGEFRIGHT_RECORDER_H_
18
19#define STAGEFRIGHT_RECORDER_H_
20
21#include <media/MediaRecorderBase.h>
22#include <camera/CameraParameters.h>
23#include <utils/String8.h>
24
25#include <system/audio.h>
26
27namespace android {
28
29class Camera;
30class ICameraRecordingProxy;
31class CameraSource;
32class CameraSourceTimeLapse;
33struct MediaSource;
34struct MediaWriter;
35class MetaData;
36struct AudioSource;
37class MediaProfiles;
38class IGraphicBufferConsumer;
39class IGraphicBufferProducer;
40class SurfaceMediaSource;
41struct ALooper;
42
43struct StagefrightRecorder : public MediaRecorderBase {
44    StagefrightRecorder(const String16 &opPackageName);
45    virtual ~StagefrightRecorder();
46
47    virtual status_t init();
48    virtual status_t setAudioSource(audio_source_t as);
49    virtual status_t setVideoSource(video_source vs);
50    virtual status_t setOutputFormat(output_format of);
51    virtual status_t setAudioEncoder(audio_encoder ae);
52    virtual status_t setVideoEncoder(video_encoder ve);
53    virtual status_t setVideoSize(int width, int height);
54    virtual status_t setVideoFrameRate(int frames_per_second);
55    virtual status_t setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
56    virtual status_t setPreviewSurface(const sp<IGraphicBufferProducer>& surface);
57    virtual status_t usePersistentSurface(const sp<IGraphicBufferConsumer>& surface);
58    virtual status_t setOutputFile(int fd, int64_t offset, int64_t length);
59    virtual status_t setParameters(const String8& params);
60    virtual status_t setListener(const sp<IMediaRecorderClient>& listener);
61    virtual status_t setClientName(const String16& clientName);
62    virtual status_t prepare();
63    virtual status_t start();
64    virtual status_t pause();
65    virtual status_t stop();
66    virtual status_t close();
67    virtual status_t reset();
68    virtual status_t getMaxAmplitude(int *max);
69    virtual status_t dump(int fd, const Vector<String16>& args) const;
70    // Querying a SurfaceMediaSourcer
71    virtual sp<IGraphicBufferProducer> querySurfaceMediaSource() const;
72
73private:
74    sp<ICamera> mCamera;
75    sp<ICameraRecordingProxy> mCameraProxy;
76    sp<IGraphicBufferProducer> mPreviewSurface;
77    sp<IGraphicBufferConsumer> mPersistentSurface;
78    sp<IMediaRecorderClient> mListener;
79    String16 mClientName;
80    uid_t mClientUid;
81    sp<MediaWriter> mWriter;
82    int mOutputFd;
83    sp<AudioSource> mAudioSourceNode;
84
85    audio_source_t mAudioSource;
86    video_source mVideoSource;
87    output_format mOutputFormat;
88    audio_encoder mAudioEncoder;
89    video_encoder mVideoEncoder;
90    bool mUse64BitFileOffset;
91    int32_t mVideoWidth, mVideoHeight;
92    int32_t mFrameRate;
93    int32_t mVideoBitRate;
94    int32_t mAudioBitRate;
95    int32_t mAudioChannels;
96    int32_t mSampleRate;
97    int32_t mInterleaveDurationUs;
98    int32_t mIFramesIntervalSec;
99    int32_t mCameraId;
100    int32_t mVideoEncoderProfile;
101    int32_t mVideoEncoderLevel;
102    int32_t mMovieTimeScale;
103    int32_t mVideoTimeScale;
104    int32_t mAudioTimeScale;
105    int64_t mMaxFileSizeBytes;
106    int64_t mMaxFileDurationUs;
107    int64_t mTrackEveryTimeDurationUs;
108    int32_t mRotationDegrees;  // Clockwise
109    int32_t mLatitudex10000;
110    int32_t mLongitudex10000;
111    int32_t mStartTimeOffsetMs;
112    int32_t mTotalBitRate;
113
114    bool mCaptureTimeLapse;
115    float mCaptureFps;
116    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
117    sp<CameraSourceTimeLapse> mCameraSourceTimeLapse;
118
119
120    String8 mParams;
121
122    bool mIsMetaDataStoredInVideoBuffers;
123    MediaProfiles *mEncoderProfiles;
124
125    bool mStarted;
126    // Needed when GLFrames are encoded.
127    // An <IGraphicBufferProducer> pointer
128    // will be sent to the client side using which the
129    // frame buffers will be queued and dequeued
130    sp<IGraphicBufferProducer> mGraphicBufferProducer;
131    sp<ALooper> mLooper;
132
133    static const int kMaxHighSpeedFps = 1000;
134
135    status_t prepareInternal();
136    status_t setupMPEG4orWEBMRecording();
137    void setupMPEG4orWEBMMetaData(sp<MetaData> *meta);
138    status_t setupAMRRecording();
139    status_t setupAACRecording();
140    status_t setupRawAudioRecording();
141    status_t setupRTPRecording();
142    status_t setupMPEG2TSRecording();
143    sp<MediaSource> createAudioSource();
144    status_t checkVideoEncoderCapabilities(
145            bool *supportsCameraSourceMetaDataMode);
146    status_t checkAudioEncoderCapabilities();
147    // Generic MediaSource set-up. Returns the appropriate
148    // source (CameraSource or SurfaceMediaSource)
149    // depending on the videosource type
150    status_t setupMediaSource(sp<MediaSource> *mediaSource);
151    status_t setupCameraSource(sp<CameraSource> *cameraSource);
152    status_t setupAudioEncoder(const sp<MediaWriter>& writer);
153    status_t setupVideoEncoder(sp<MediaSource> cameraSource, sp<MediaSource> *source);
154
155    // Encoding parameter handling utilities
156    status_t setParameter(const String8 &key, const String8 &value);
157    status_t setParamAudioEncodingBitRate(int32_t bitRate);
158    status_t setParamAudioNumberOfChannels(int32_t channles);
159    status_t setParamAudioSamplingRate(int32_t sampleRate);
160    status_t setParamAudioTimeScale(int32_t timeScale);
161    status_t setParamTimeLapseEnable(int32_t timeLapseEnable);
162    status_t setParamTimeLapseFps(float fps);
163    status_t setParamVideoEncodingBitRate(int32_t bitRate);
164    status_t setParamVideoIFramesInterval(int32_t seconds);
165    status_t setParamVideoEncoderProfile(int32_t profile);
166    status_t setParamVideoEncoderLevel(int32_t level);
167    status_t setParamVideoCameraId(int32_t cameraId);
168    status_t setParamVideoTimeScale(int32_t timeScale);
169    status_t setParamVideoRotation(int32_t degrees);
170    status_t setParamTrackTimeStatus(int64_t timeDurationUs);
171    status_t setParamInterleaveDuration(int32_t durationUs);
172    status_t setParam64BitFileOffset(bool use64BitFileOffset);
173    status_t setParamMaxFileDurationUs(int64_t timeUs);
174    status_t setParamMaxFileSizeBytes(int64_t bytes);
175    status_t setParamMovieTimeScale(int32_t timeScale);
176    status_t setParamGeoDataLongitude(int64_t longitudex10000);
177    status_t setParamGeoDataLatitude(int64_t latitudex10000);
178    void clipVideoBitRate();
179    void clipVideoFrameRate();
180    void clipVideoFrameWidth();
181    void clipVideoFrameHeight();
182    void clipAudioBitRate();
183    void clipAudioSampleRate();
184    void clipNumberOfAudioChannels();
185    void setDefaultProfileIfNecessary();
186    void setDefaultVideoEncoderIfNecessary();
187
188
189    StagefrightRecorder(const StagefrightRecorder &);
190    StagefrightRecorder &operator=(const StagefrightRecorder &);
191};
192
193}  // namespace android
194
195#endif  // STAGEFRIGHT_RECORDER_H_
196