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