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 ISurfaceTexture;
39class SurfaceMediaSource;
40
41struct StagefrightRecorder : public MediaRecorderBase {
42    StagefrightRecorder();
43    virtual ~StagefrightRecorder();
44
45    virtual status_t init();
46    virtual status_t setAudioSource(audio_source_t as);
47    virtual status_t setVideoSource(video_source vs);
48    virtual status_t setOutputFormat(output_format of);
49    virtual status_t setAudioEncoder(audio_encoder ae);
50    virtual status_t setVideoEncoder(video_encoder ve);
51    virtual status_t setVideoSize(int width, int height);
52    virtual status_t setVideoFrameRate(int frames_per_second);
53    virtual status_t setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
54    virtual status_t setPreviewSurface(const sp<Surface>& surface);
55    virtual status_t setOutputFile(const char *path);
56    virtual status_t setOutputFile(int fd, int64_t offset, int64_t length);
57    virtual status_t setParameters(const String8& params);
58    virtual status_t setListener(const sp<IMediaRecorderClient>& listener);
59    virtual status_t prepare();
60    virtual status_t start();
61    virtual status_t pause();
62    virtual status_t stop();
63    virtual status_t close();
64    virtual status_t reset();
65    virtual status_t getMaxAmplitude(int *max);
66    virtual status_t dump(int fd, const Vector<String16>& args) const;
67    // Querying a SurfaceMediaSourcer
68    virtual sp<ISurfaceTexture> querySurfaceMediaSource() const;
69
70private:
71    sp<ICamera> mCamera;
72    sp<ICameraRecordingProxy> mCameraProxy;
73    sp<Surface> mPreviewSurface;
74    sp<IMediaRecorderClient> mListener;
75    sp<MediaWriter> mWriter;
76    int mOutputFd;
77    sp<AudioSource> mAudioSourceNode;
78
79    audio_source_t mAudioSource;
80    video_source mVideoSource;
81    output_format mOutputFormat;
82    audio_encoder mAudioEncoder;
83    video_encoder mVideoEncoder;
84    bool mUse64BitFileOffset;
85    int32_t mVideoWidth, mVideoHeight;
86    int32_t mFrameRate;
87    int32_t mVideoBitRate;
88    int32_t mAudioBitRate;
89    int32_t mAudioChannels;
90    int32_t mSampleRate;
91    int32_t mInterleaveDurationUs;
92    int32_t mIFramesIntervalSec;
93    int32_t mCameraId;
94    int32_t mVideoEncoderProfile;
95    int32_t mVideoEncoderLevel;
96    int32_t mMovieTimeScale;
97    int32_t mVideoTimeScale;
98    int32_t mAudioTimeScale;
99    int64_t mMaxFileSizeBytes;
100    int64_t mMaxFileDurationUs;
101    int64_t mTrackEveryTimeDurationUs;
102    int32_t mRotationDegrees;  // Clockwise
103    int32_t mLatitudex10000;
104    int32_t mLongitudex10000;
105    int32_t mStartTimeOffsetMs;
106
107    bool mCaptureTimeLapse;
108    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
109    sp<CameraSourceTimeLapse> mCameraSourceTimeLapse;
110
111
112    String8 mParams;
113
114    bool mIsMetaDataStoredInVideoBuffers;
115    MediaProfiles *mEncoderProfiles;
116
117    bool mStarted;
118    // Needed when GLFrames are encoded.
119    // An <ISurfaceTexture> pointer
120    // will be sent to the client side using which the
121    // frame buffers will be queued and dequeued
122    sp<SurfaceMediaSource> mSurfaceMediaSource;
123
124    status_t setupMPEG4Recording(
125        int outputFd,
126        int32_t videoWidth, int32_t videoHeight,
127        int32_t videoBitRate,
128        int32_t *totalBitRate,
129        sp<MediaWriter> *mediaWriter);
130    void setupMPEG4MetaData(int64_t startTimeUs, int32_t totalBitRate,
131        sp<MetaData> *meta);
132    status_t startMPEG4Recording();
133    status_t startAMRRecording();
134    status_t startAACRecording();
135    status_t startRawAudioRecording();
136    status_t startRTPRecording();
137    status_t startMPEG2TSRecording();
138    sp<MediaSource> createAudioSource();
139    status_t checkVideoEncoderCapabilities();
140    status_t checkAudioEncoderCapabilities();
141    // Generic MediaSource set-up. Returns the appropriate
142    // source (CameraSource or SurfaceMediaSource)
143    // depending on the videosource type
144    status_t setupMediaSource(sp<MediaSource> *mediaSource);
145    status_t setupCameraSource(sp<CameraSource> *cameraSource);
146    // setup the surfacemediasource for the encoder
147    status_t setupSurfaceMediaSource();
148
149    status_t setupAudioEncoder(const sp<MediaWriter>& writer);
150    status_t setupVideoEncoder(
151            sp<MediaSource> cameraSource,
152            int32_t videoBitRate,
153            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 setParamTimeBetweenTimeLapseFrameCapture(int64_t timeUs);
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
187
188    StagefrightRecorder(const StagefrightRecorder &);
189    StagefrightRecorder &operator=(const StagefrightRecorder &);
190};
191
192}  // namespace android
193
194#endif  // STAGEFRIGHT_RECORDER_H_
195