• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  ** Copyright (C) 2008 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  **
15  ** limitations under the License.
16  */
17 
18 #ifndef ANDROID_MEDIARECORDER_H
19 #define ANDROID_MEDIARECORDER_H
20 
21 #include <utils/Log.h>
22 #include <utils/threads.h>
23 #include <utils/List.h>
24 #include <utils/Errors.h>
25 #include <media/AudioContainers.h>
26 #include <media/IMediaRecorderClient.h>
27 #include <media/IMediaDeathNotifier.h>
28 #include <android/media/MicrophoneInfoFw.h>
29 #include <android/content/AttributionSourceState.h>
30 
31 namespace android {
32 
33 class Surface;
34 class IMediaRecorder;
35 class ICameraRecordingProxy;
36 class IGraphicBufferProducer;
37 struct PersistentSurface;
38 class Surface;
39 
40 namespace hardware {
41 class ICamera;
42 }
43 
44 typedef void (*media_completion_f)(status_t status, void *cookie);
45 
46 enum video_source {
47     VIDEO_SOURCE_DEFAULT = 0,
48     VIDEO_SOURCE_CAMERA = 1,
49     VIDEO_SOURCE_SURFACE = 2,
50 
51     VIDEO_SOURCE_LIST_END  // must be last - used to validate audio source type
52 };
53 
54 //Please update media/java/android/media/MediaRecorder.java if the following is updated.
55 enum output_format {
56     OUTPUT_FORMAT_DEFAULT = 0,
57     OUTPUT_FORMAT_THREE_GPP = 1,
58     OUTPUT_FORMAT_MPEG_4 = 2,
59 
60 
61     OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format.  Should be the
62                                         //  at the start of the audio only output formats.
63 
64     /* These are audio only file formats */
65     OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible
66     OUTPUT_FORMAT_AMR_NB = 3,
67     OUTPUT_FORMAT_AMR_WB = 4,
68     OUTPUT_FORMAT_AAC_ADIF = 5,
69     OUTPUT_FORMAT_AAC_ADTS = 6,
70 
71     OUTPUT_FORMAT_AUDIO_ONLY_END = 7, // Used in validating the output format.  Should be the
72                                       // at the end of the audio only output formats.
73 
74     /* Stream over a socket, limited to a single stream */
75     OUTPUT_FORMAT_RTP_AVP = 7,
76 
77     /* H.264/AAC data encapsulated in MPEG2/TS */
78     OUTPUT_FORMAT_MPEG2TS = 8,
79 
80     /* VP8/VORBIS data in a WEBM container */
81     OUTPUT_FORMAT_WEBM = 9,
82 
83     /* HEIC data in a HEIF container */
84     OUTPUT_FORMAT_HEIF = 10,
85 
86     /* Opus data in a OGG container */
87     OUTPUT_FORMAT_OGG = 11,
88 
89     OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
90 };
91 
92 enum audio_encoder {
93     AUDIO_ENCODER_DEFAULT = 0,
94     AUDIO_ENCODER_AMR_NB = 1,
95     AUDIO_ENCODER_AMR_WB = 2,
96     AUDIO_ENCODER_AAC = 3,
97     AUDIO_ENCODER_HE_AAC = 4,
98     AUDIO_ENCODER_AAC_ELD = 5,
99     AUDIO_ENCODER_VORBIS = 6,
100     AUDIO_ENCODER_OPUS = 7,
101 
102     AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type
103 };
104 
105 enum video_encoder {
106     VIDEO_ENCODER_DEFAULT = 0,
107     VIDEO_ENCODER_H263 = 1,
108     VIDEO_ENCODER_H264 = 2,
109     VIDEO_ENCODER_MPEG_4_SP = 3,
110     VIDEO_ENCODER_VP8 = 4,
111     VIDEO_ENCODER_HEVC = 5,
112     VIDEO_ENCODER_VP9 = 6,
113     VIDEO_ENCODER_DOLBY_VISION = 7,
114     VIDEO_ENCODER_AV1 = 8,
115     VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type
116 };
117 
118 /*
119  * The state machine of the media_recorder.
120  */
121 enum media_recorder_states {
122     // Recorder was just created.
123     MEDIA_RECORDER_IDLE                  = 1 << 0,
124 
125     // Recorder has been initialized.
126     MEDIA_RECORDER_INITIALIZED           = 1 << 1,
127 
128     // Configuration of the recorder has been completed.
129     MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
130 
131     // Recorder is ready to start.
132     MEDIA_RECORDER_PREPARED              = 1 << 3,
133 
134     // Recording is in progress.
135     MEDIA_RECORDER_RECORDING             = 1 << 4,
136 
137     // Error state.
138     MEDIA_RECORDER_ERROR                 = 1 << 5,
139 };
140 
141 // The "msg" code passed to the listener in notify.
142 enum media_recorder_event_type {
143     MEDIA_RECORDER_EVENT_LIST_START               = 1,
144     MEDIA_RECORDER_EVENT_ERROR                    = 1,
145     MEDIA_RECORDER_EVENT_INFO                     = 2,
146     MEDIA_RECORDER_EVENT_LIST_END                 = 99,
147 
148     // Track related event types
149     MEDIA_RECORDER_TRACK_EVENT_LIST_START         = 100,
150     MEDIA_RECORDER_TRACK_EVENT_ERROR              = 100,
151     MEDIA_RECORDER_TRACK_EVENT_INFO               = 101,
152     MEDIA_RECORDER_TRACK_EVENT_LIST_END           = 1000,
153 
154     MEDIA_RECORDER_AUDIO_ROUTING_CHANGED          = 10000,
155 };
156 
157 /*
158  * The (part of) "what" code passed to the listener in notify.
159  * When the error or info type is track specific, the what has
160  * the following layout:
161  * the left-most 16-bit is meant for error or info type.
162  * the right-most 4-bit is meant for track id.
163  * the rest is reserved.
164  *
165  * | track id | reserved |     error or info type     |
166  * 31         28         16                           0
167  *
168  */
169 enum media_recorder_error_type {
170     MEDIA_RECORDER_ERROR_UNKNOWN                   = 1,
171 
172     // Track related error type
173     MEDIA_RECORDER_TRACK_ERROR_LIST_START          = 100,
174     MEDIA_RECORDER_TRACK_ERROR_GENERAL             = 100,
175     MEDIA_RECORDER_ERROR_VIDEO_NO_SYNC_FRAME       = 200,
176     MEDIA_RECORDER_TRACK_ERROR_LIST_END            = 1000,
177 };
178 
179 // The codes are distributed as follow:
180 //   0xx: Reserved
181 //   8xx: General info/warning
182 //
183 enum media_recorder_info_type {
184     MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
185 
186     MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
187     MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
188     MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING  = 802,
189     MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED  = 803,
190 
191     // All track related informtional events start here
192     MEDIA_RECORDER_TRACK_INFO_LIST_START           = 1000,
193     MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS    = 1000,
194     MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME     = 1001,
195     MEDIA_RECORDER_TRACK_INFO_TYPE                 = 1002,
196     MEDIA_RECORDER_TRACK_INFO_DURATION_MS          = 1003,
197 
198     // The time to measure the max chunk duration
199     MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS     = 1004,
200 
201     MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES       = 1005,
202 
203     // The time to measure how well the audio and video
204     // track data is interleaved.
205     MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS       = 1006,
206 
207     // The time to measure system response. Note that
208     // the delay does not include the intentional delay
209     // we use to eliminate the recording sound.
210     MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS     = 1007,
211 
212     // The time used to compensate for initial A/V sync.
213     MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS      = 1008,
214 
215     // Total number of bytes of the media data.
216     MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES          = 1009,
217 
218     MEDIA_RECORDER_TRACK_INFO_LIST_END             = 2000,
219 };
220 
221 // ----------------------------------------------------------------------------
222 // ref-counted object for callbacks
223 class MediaRecorderListener: virtual public RefBase
224 {
225 public:
226     virtual void notify(int msg, int ext1, int ext2) = 0;
227 };
228 
229 class MediaRecorder : public BnMediaRecorderClient,
230                       public virtual IMediaDeathNotifier
231 {
232 public:
233     explicit MediaRecorder(const android::content::AttributionSourceState& attributionSource);
234     ~MediaRecorder();
235 
236     void        died();
237     status_t    initCheck();
238     status_t    setCamera(const sp<hardware::ICamera>& camera,
239             const sp<ICameraRecordingProxy>& proxy);
240     status_t    setPreviewSurface(const sp<IGraphicBufferProducer>& surface);
241     status_t    setVideoSource(int vs);
242     status_t    setAudioSource(int as);
243     status_t    setPrivacySensitive(bool privacySensitive);
244     status_t    isPrivacySensitive(bool *privacySensitive) const;
245     status_t    setOutputFormat(int of);
246     status_t    setVideoEncoder(int ve);
247     status_t    setAudioEncoder(int ae);
248     status_t    setOutputFile(int fd);
249     status_t    setNextOutputFile(int fd);
250     status_t    setVideoSize(int width, int height);
251     status_t    setVideoFrameRate(int frames_per_second);
252     status_t    setParameters(const String8& params);
253     status_t    setListener(const sp<MediaRecorderListener>& listener);
254     status_t    setClientName(const String16& clientName);
255     status_t    prepare();
256     status_t    getMaxAmplitude(int* max);
257     status_t    start();
258     status_t    stop();
259     status_t    reset();
260     status_t    pause();
261     status_t    resume();
262     status_t    init();
263     status_t    close();
264     status_t    release();
265     void        notify(int msg, int ext1, int ext2);
266     status_t    setInputSurface(const sp<PersistentSurface>& surface);
267     sp<IGraphicBufferProducer>     querySurfaceMediaSourceFromMediaServer();
268     status_t    getMetrics(Parcel *reply);
269     status_t    setInputDevice(audio_port_handle_t deviceId);
270     status_t    getRoutedDeviceIds(DeviceIdVector& deviceIds);
271     status_t    enableAudioDeviceCallback(bool enabled);
272     status_t    getActiveMicrophones(std::vector<media::MicrophoneInfoFw>* activeMicrophones);
273     status_t    setPreferredMicrophoneDirection(audio_microphone_direction_t direction);
274     status_t    setPreferredMicrophoneFieldDimension(float zoom);
275 
276     status_t    getPortId(audio_port_handle_t *portId) const;
277     status_t    getRtpDataUsage(uint64_t *bytes);
278 
279 private:
280     void                    doCleanUp();
281     status_t                doReset();
282 
283     sp<IMediaRecorder>          mMediaRecorder;
284     sp<MediaRecorderListener>   mListener;
285 
286     // Reference to IGraphicBufferProducer
287     // for encoding GL Frames. That is useful only when the
288     // video source is set to VIDEO_SOURCE_GRALLOC_BUFFER
289     sp<IGraphicBufferProducer>  mSurfaceMediaSource;
290 
291     media_recorder_states       mCurrentState;
292     bool                        mIsAudioSourceSet;
293     bool                        mIsVideoSourceSet;
294     bool                        mIsAudioEncoderSet;
295     bool                        mIsVideoEncoderSet;
296     bool                        mIsOutputFileSet;
297     Mutex                       mLock;
298     Mutex                       mNotifyLock;
299 
300     output_format               mOutputFormat;
301 };
302 
303 };  // namespace android
304 
305 #endif // ANDROID_MEDIARECORDER_H
306