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 <media/MicrophoneInfo.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 112 VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type 113 }; 114 115 /* 116 * The state machine of the media_recorder. 117 */ 118 enum media_recorder_states { 119 // Recorder was just created. 120 MEDIA_RECORDER_IDLE = 1 << 0, 121 122 // Recorder has been initialized. 123 MEDIA_RECORDER_INITIALIZED = 1 << 1, 124 125 // Configuration of the recorder has been completed. 126 MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2, 127 128 // Recorder is ready to start. 129 MEDIA_RECORDER_PREPARED = 1 << 3, 130 131 // Recording is in progress. 132 MEDIA_RECORDER_RECORDING = 1 << 4, 133 134 // Error state. 135 MEDIA_RECORDER_ERROR = 1 << 5, 136 }; 137 138 // The "msg" code passed to the listener in notify. 139 enum media_recorder_event_type { 140 MEDIA_RECORDER_EVENT_LIST_START = 1, 141 MEDIA_RECORDER_EVENT_ERROR = 1, 142 MEDIA_RECORDER_EVENT_INFO = 2, 143 MEDIA_RECORDER_EVENT_LIST_END = 99, 144 145 // Track related event types 146 MEDIA_RECORDER_TRACK_EVENT_LIST_START = 100, 147 MEDIA_RECORDER_TRACK_EVENT_ERROR = 100, 148 MEDIA_RECORDER_TRACK_EVENT_INFO = 101, 149 MEDIA_RECORDER_TRACK_EVENT_LIST_END = 1000, 150 151 MEDIA_RECORDER_AUDIO_ROUTING_CHANGED = 10000, 152 }; 153 154 /* 155 * The (part of) "what" code passed to the listener in notify. 156 * When the error or info type is track specific, the what has 157 * the following layout: 158 * the left-most 16-bit is meant for error or info type. 159 * the right-most 4-bit is meant for track id. 160 * the rest is reserved. 161 * 162 * | track id | reserved | error or info type | 163 * 31 28 16 0 164 * 165 */ 166 enum media_recorder_error_type { 167 MEDIA_RECORDER_ERROR_UNKNOWN = 1, 168 169 // Track related error type 170 MEDIA_RECORDER_TRACK_ERROR_LIST_START = 100, 171 MEDIA_RECORDER_TRACK_ERROR_GENERAL = 100, 172 MEDIA_RECORDER_ERROR_VIDEO_NO_SYNC_FRAME = 200, 173 MEDIA_RECORDER_TRACK_ERROR_LIST_END = 1000, 174 }; 175 176 // The codes are distributed as follow: 177 // 0xx: Reserved 178 // 8xx: General info/warning 179 // 180 enum media_recorder_info_type { 181 MEDIA_RECORDER_INFO_UNKNOWN = 1, 182 183 MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800, 184 MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801, 185 MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING = 802, 186 MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED = 803, 187 188 // All track related informtional events start here 189 MEDIA_RECORDER_TRACK_INFO_LIST_START = 1000, 190 MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS = 1000, 191 MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME = 1001, 192 MEDIA_RECORDER_TRACK_INFO_TYPE = 1002, 193 MEDIA_RECORDER_TRACK_INFO_DURATION_MS = 1003, 194 195 // The time to measure the max chunk duration 196 MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS = 1004, 197 198 MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES = 1005, 199 200 // The time to measure how well the audio and video 201 // track data is interleaved. 202 MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS = 1006, 203 204 // The time to measure system response. Note that 205 // the delay does not include the intentional delay 206 // we use to eliminate the recording sound. 207 MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS = 1007, 208 209 // The time used to compensate for initial A/V sync. 210 MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS = 1008, 211 212 // Total number of bytes of the media data. 213 MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES = 1009, 214 215 MEDIA_RECORDER_TRACK_INFO_LIST_END = 2000, 216 }; 217 218 // ---------------------------------------------------------------------------- 219 // ref-counted object for callbacks 220 class MediaRecorderListener: virtual public RefBase 221 { 222 public: 223 virtual void notify(int msg, int ext1, int ext2) = 0; 224 }; 225 226 class MediaRecorder : public BnMediaRecorderClient, 227 public virtual IMediaDeathNotifier 228 { 229 public: 230 explicit MediaRecorder(const android::content::AttributionSourceState& attributionSource); 231 ~MediaRecorder(); 232 233 void died(); 234 status_t initCheck(); 235 status_t setCamera(const sp<hardware::ICamera>& camera, 236 const sp<ICameraRecordingProxy>& proxy); 237 status_t setPreviewSurface(const sp<IGraphicBufferProducer>& surface); 238 status_t setVideoSource(int vs); 239 status_t setAudioSource(int as); 240 status_t setPrivacySensitive(bool privacySensitive); 241 status_t isPrivacySensitive(bool *privacySensitive) const; 242 status_t setOutputFormat(int of); 243 status_t setVideoEncoder(int ve); 244 status_t setAudioEncoder(int ae); 245 status_t setOutputFile(int fd); 246 status_t setNextOutputFile(int fd); 247 status_t setVideoSize(int width, int height); 248 status_t setVideoFrameRate(int frames_per_second); 249 status_t setParameters(const String8& params); 250 status_t setListener(const sp<MediaRecorderListener>& listener); 251 status_t setClientName(const String16& clientName); 252 status_t prepare(); 253 status_t getMaxAmplitude(int* max); 254 status_t start(); 255 status_t stop(); 256 status_t reset(); 257 status_t pause(); 258 status_t resume(); 259 status_t init(); 260 status_t close(); 261 status_t release(); 262 void notify(int msg, int ext1, int ext2); 263 status_t setInputSurface(const sp<PersistentSurface>& surface); 264 sp<IGraphicBufferProducer> querySurfaceMediaSourceFromMediaServer(); 265 status_t getMetrics(Parcel *reply); 266 status_t setInputDevice(audio_port_handle_t deviceId); 267 status_t getRoutedDeviceId(audio_port_handle_t *deviceId); 268 status_t enableAudioDeviceCallback(bool enabled); 269 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones); 270 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction); 271 status_t setPreferredMicrophoneFieldDimension(float zoom); 272 273 status_t getPortId(audio_port_handle_t *portId) const; 274 status_t getRtpDataUsage(uint64_t *bytes); 275 276 private: 277 void doCleanUp(); 278 status_t doReset(); 279 280 sp<IMediaRecorder> mMediaRecorder; 281 sp<MediaRecorderListener> mListener; 282 283 // Reference to IGraphicBufferProducer 284 // for encoding GL Frames. That is useful only when the 285 // video source is set to VIDEO_SOURCE_GRALLOC_BUFFER 286 sp<IGraphicBufferProducer> mSurfaceMediaSource; 287 288 media_recorder_states mCurrentState; 289 bool mIsAudioSourceSet; 290 bool mIsVideoSourceSet; 291 bool mIsAudioEncoderSet; 292 bool mIsVideoEncoderSet; 293 bool mIsOutputFileSet; 294 Mutex mLock; 295 Mutex mNotifyLock; 296 297 output_format mOutputFormat; 298 }; 299 300 }; // namespace android 301 302 #endif // ANDROID_MEDIARECORDER_H 303