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/MediaAnalyticsItem.h>
22 #include <media/MediaRecorderBase.h>
23 #include <camera/CameraParameters.h>
24 #include <utils/String8.h>
25 
26 #include <system/audio.h>
27 
28 #include <media/hardware/MetadataBufferType.h>
29 
30 namespace android {
31 
32 class Camera;
33 class ICameraRecordingProxy;
34 class CameraSource;
35 class CameraSourceTimeLapse;
36 struct MediaCodecSource;
37 struct MediaSource;
38 struct MediaWriter;
39 class MetaData;
40 struct AudioSource;
41 class MediaProfiles;
42 struct ALooper;
43 
44 struct StagefrightRecorder : public MediaRecorderBase {
45     explicit StagefrightRecorder(const String16 &opPackageName);
46     virtual ~StagefrightRecorder();
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<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
56     virtual status_t setPreviewSurface(const sp<IGraphicBufferProducer>& surface);
57     virtual status_t setInputSurface(const sp<PersistentSurface>& surface);
58     virtual status_t setOutputFile(int fd);
59     virtual status_t setNextOutputFile(int fd);
60     virtual status_t setParameters(const String8 &params);
61     virtual status_t setListener(const sp<IMediaRecorderClient> &listener);
62     virtual status_t setClientName(const String16 &clientName);
63     virtual status_t prepare();
64     virtual status_t start();
65     virtual status_t pause();
66     virtual status_t resume();
67     virtual status_t stop();
68     virtual status_t close();
69     virtual status_t reset();
70     virtual status_t getMaxAmplitude(int *max);
71     virtual status_t getMetrics(Parcel *reply);
72     virtual status_t dump(int fd, const Vector<String16> &args) const;
73     // Querying a SurfaceMediaSourcer
74     virtual sp<IGraphicBufferProducer> querySurfaceMediaSource() const;
75     virtual status_t setInputDevice(audio_port_handle_t deviceId);
76     virtual status_t getRoutedDeviceId(audio_port_handle_t* deviceId);
77     virtual void setAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback);
78     virtual status_t enableAudioDeviceCallback(bool enabled);
79     virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
80     virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction);
81     virtual status_t setPreferredMicrophoneFieldDimension(float zoom);
82             status_t getPortId(audio_port_handle_t *portId) const override;
83 
84 private:
85     mutable Mutex mLock;
86     sp<hardware::ICamera> mCamera;
87     sp<ICameraRecordingProxy> mCameraProxy;
88     sp<IGraphicBufferProducer> mPreviewSurface;
89     sp<PersistentSurface> mPersistentSurface;
90     sp<IMediaRecorderClient> mListener;
91     String16 mClientName;
92     uid_t mClientUid;
93     pid_t mClientPid;
94     sp<MediaWriter> mWriter;
95     int mOutputFd;
96     sp<AudioSource> mAudioSourceNode;
97 
98     MediaAnalyticsItem *mAnalyticsItem;
99     bool mAnalyticsDirty;
100     void flushAndResetMetrics(bool reinitialize);
101     void updateMetrics();
102 
103     audio_source_t mAudioSource;
104     video_source mVideoSource;
105     output_format mOutputFormat;
106     audio_encoder mAudioEncoder;
107     video_encoder mVideoEncoder;
108     bool mUse64BitFileOffset;
109     int32_t mVideoWidth, mVideoHeight;
110     int32_t mFrameRate;
111     int32_t mVideoBitRate;
112     int32_t mAudioBitRate;
113     int32_t mAudioChannels;
114     int32_t mSampleRate;
115     int32_t mInterleaveDurationUs;
116     int32_t mIFramesIntervalSec;
117     int32_t mCameraId;
118     int32_t mVideoEncoderProfile;
119     int32_t mVideoEncoderLevel;
120     int32_t mMovieTimeScale;
121     int32_t mVideoTimeScale;
122     int32_t mAudioTimeScale;
123     int64_t mMaxFileSizeBytes;
124     int64_t mMaxFileDurationUs;
125     int64_t mTrackEveryTimeDurationUs;
126     int32_t mRotationDegrees;  // Clockwise
127     int32_t mLatitudex10000;
128     int32_t mLongitudex10000;
129     int32_t mStartTimeOffsetMs;
130     int32_t mTotalBitRate;
131 
132     int64_t mDurationRecordedUs;
133     int64_t mStartedRecordingUs;
134     int64_t mDurationPausedUs;
135     int32_t mNPauses;
136 
137     bool mCaptureFpsEnable;
138     double mCaptureFps;
139     int64_t mTimeBetweenCaptureUs;
140     sp<CameraSourceTimeLapse> mCameraSourceTimeLapse;
141 
142     String8 mParams;
143 
144     MetadataBufferType mMetaDataStoredInVideoBuffers;
145     MediaProfiles *mEncoderProfiles;
146 
147     int64_t mPauseStartTimeUs;
148     int64_t mTotalPausedDurationUs;
149     sp<MediaCodecSource> mAudioEncoderSource;
150     sp<MediaCodecSource> mVideoEncoderSource;
151 
152     bool mStarted;
153     // Needed when GLFrames are encoded.
154     // An <IGraphicBufferProducer> pointer
155     // will be sent to the client side using which the
156     // frame buffers will be queued and dequeued
157     sp<IGraphicBufferProducer> mGraphicBufferProducer;
158     sp<ALooper> mLooper;
159 
160     audio_port_handle_t mSelectedDeviceId;
161     bool mDeviceCallbackEnabled;
162     wp<AudioSystem::AudioDeviceCallback> mAudioDeviceCallback;
163 
164     audio_microphone_direction_t mSelectedMicDirection;
165     float mSelectedMicFieldDimension;
166 
167     static const int kMaxHighSpeedFps = 1000;
168 
169     status_t prepareInternal();
170     status_t setupMPEG4orWEBMRecording();
171     void setupMPEG4orWEBMMetaData(sp<MetaData> *meta);
172     status_t setupAMRRecording();
173     status_t setupAACRecording();
174     status_t setupOggRecording();
175     status_t setupRawAudioRecording();
176     status_t setupRTPRecording();
177     status_t setupMPEG2TSRecording();
178     sp<MediaCodecSource> createAudioSource();
179     status_t checkVideoEncoderCapabilities();
180     status_t checkAudioEncoderCapabilities();
181     // Generic MediaSource set-up. Returns the appropriate
182     // source (CameraSource or SurfaceMediaSource)
183     // depending on the videosource type
184     status_t setupMediaSource(sp<MediaSource> *mediaSource);
185     status_t setupCameraSource(sp<CameraSource> *cameraSource);
186     status_t setupAudioEncoder(const sp<MediaWriter>& writer);
187     status_t setupVideoEncoder(const sp<MediaSource>& cameraSource, sp<MediaCodecSource> *source);
188 
189     // Encoding parameter handling utilities
190     status_t setParameter(const String8 &key, const String8 &value);
191     status_t setParamAudioEncodingBitRate(int32_t bitRate);
192     status_t setParamAudioNumberOfChannels(int32_t channles);
193     status_t setParamAudioSamplingRate(int32_t sampleRate);
194     status_t setParamAudioTimeScale(int32_t timeScale);
195     status_t setParamCaptureFpsEnable(int32_t timeLapseEnable);
196     status_t setParamCaptureFps(double fps);
197     status_t setParamVideoEncodingBitRate(int32_t bitRate);
198     status_t setParamVideoIFramesInterval(int32_t seconds);
199     status_t setParamVideoEncoderProfile(int32_t profile);
200     status_t setParamVideoEncoderLevel(int32_t level);
201     status_t setParamVideoCameraId(int32_t cameraId);
202     status_t setParamVideoTimeScale(int32_t timeScale);
203     status_t setParamVideoRotation(int32_t degrees);
204     status_t setParamTrackTimeStatus(int64_t timeDurationUs);
205     status_t setParamInterleaveDuration(int32_t durationUs);
206     status_t setParam64BitFileOffset(bool use64BitFileOffset);
207     status_t setParamMaxFileDurationUs(int64_t timeUs);
208     status_t setParamMaxFileSizeBytes(int64_t bytes);
209     status_t setParamMovieTimeScale(int32_t timeScale);
210     status_t setParamGeoDataLongitude(int64_t longitudex10000);
211     status_t setParamGeoDataLatitude(int64_t latitudex10000);
212     void clipVideoBitRate();
213     void clipVideoFrameRate();
214     void clipVideoFrameWidth();
215     void clipVideoFrameHeight();
216     void clipAudioBitRate();
217     void clipAudioSampleRate();
218     void clipNumberOfAudioChannels();
219     void setDefaultProfileIfNecessary();
220     void setDefaultVideoEncoderIfNecessary();
221 
222 
223     StagefrightRecorder(const StagefrightRecorder &);
224     StagefrightRecorder &operator=(const StagefrightRecorder &);
225 };
226 
227 }  // namespace android
228 
229 #endif  // STAGEFRIGHT_RECORDER_H_
230