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