The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <utils/threads.h> |
| 23 | #include <utils/List.h> |
| 24 | #include <utils/Errors.h> |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 25 | #include <media/IMediaRecorderClient.h> |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 26 | #include <media/IMediaDeathNotifier.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | class Surface; |
| 31 | class IMediaRecorder; |
| 32 | class ICamera; |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 33 | class ICameraRecordingProxy; |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 34 | class ISurfaceTexture; |
| 35 | class SurfaceTextureClient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | typedef void (*media_completion_f)(status_t status, void *cookie); |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | enum video_source { |
| 40 | VIDEO_SOURCE_DEFAULT = 0, |
| 41 | VIDEO_SOURCE_CAMERA = 1, |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 42 | VIDEO_SOURCE_GRALLOC_BUFFER = 2, |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 43 | |
| 44 | VIDEO_SOURCE_LIST_END // must be last - used to validate audio source type |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 47 | //Please update media/java/android/media/MediaRecorder.java if the following is updated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | enum output_format { |
| 49 | OUTPUT_FORMAT_DEFAULT = 0, |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 50 | OUTPUT_FORMAT_THREE_GPP = 1, |
| 51 | OUTPUT_FORMAT_MPEG_4 = 2, |
| 52 | |
| 53 | |
| 54 | OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format. Should be the |
| 55 | // at the start of the audio only output formats. |
| 56 | |
| 57 | /* These are audio only file formats */ |
| 58 | OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible |
| 59 | OUTPUT_FORMAT_AMR_NB = 3, |
| 60 | OUTPUT_FORMAT_AMR_WB = 4, |
| 61 | OUTPUT_FORMAT_AAC_ADIF = 5, |
| 62 | OUTPUT_FORMAT_AAC_ADTS = 6, |
| 63 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 64 | /* Stream over a socket, limited to a single stream */ |
| 65 | OUTPUT_FORMAT_RTP_AVP = 7, |
| 66 | |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 67 | /* H.264/AAC data encapsulated in MPEG2/TS */ |
| 68 | OUTPUT_FORMAT_MPEG2TS = 8, |
| 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | OUTPUT_FORMAT_LIST_END // must be last - used to validate format type |
| 71 | }; |
| 72 | |
| 73 | enum audio_encoder { |
| 74 | AUDIO_ENCODER_DEFAULT = 0, |
| 75 | AUDIO_ENCODER_AMR_NB = 1, |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 76 | AUDIO_ENCODER_AMR_WB = 2, |
| 77 | AUDIO_ENCODER_AAC = 3, |
| 78 | AUDIO_ENCODER_AAC_PLUS = 4, |
| 79 | AUDIO_ENCODER_EAAC_PLUS = 5, |
| 80 | |
| 81 | AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | enum video_encoder { |
| 85 | VIDEO_ENCODER_DEFAULT = 0, |
| 86 | VIDEO_ENCODER_H263 = 1, |
| 87 | VIDEO_ENCODER_H264 = 2, |
| 88 | VIDEO_ENCODER_MPEG_4_SP = 3, |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 89 | |
| 90 | VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /* |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 94 | * The state machine of the media_recorder. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | */ |
| 96 | enum media_recorder_states { |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 97 | // Error state. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | MEDIA_RECORDER_ERROR = 0, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 99 | |
| 100 | // Recorder was just created. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | MEDIA_RECORDER_IDLE = 1 << 0, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 102 | |
| 103 | // Recorder has been initialized. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | MEDIA_RECORDER_INITIALIZED = 1 << 1, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 105 | |
| 106 | // Configuration of the recorder has been completed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 108 | |
| 109 | // Recorder is ready to start. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | MEDIA_RECORDER_PREPARED = 1 << 3, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 111 | |
| 112 | // Recording is in progress. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | MEDIA_RECORDER_RECORDING = 1 << 4, |
| 114 | }; |
| 115 | |
| 116 | // The "msg" code passed to the listener in notify. |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 117 | enum media_recorder_event_type { |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 118 | MEDIA_RECORDER_EVENT_LIST_START = 1, |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 119 | MEDIA_RECORDER_EVENT_ERROR = 1, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 120 | MEDIA_RECORDER_EVENT_INFO = 2, |
| 121 | MEDIA_RECORDER_EVENT_LIST_END = 99, |
| 122 | |
| 123 | // Track related event types |
| 124 | MEDIA_RECORDER_TRACK_EVENT_LIST_START = 100, |
| 125 | MEDIA_RECORDER_TRACK_EVENT_ERROR = 100, |
| 126 | MEDIA_RECORDER_TRACK_EVENT_INFO = 101, |
| 127 | MEDIA_RECORDER_TRACK_EVENT_LIST_END = 1000, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 130 | /* |
| 131 | * The (part of) "what" code passed to the listener in notify. |
| 132 | * When the error or info type is track specific, the what has |
| 133 | * the following layout: |
| 134 | * the left-most 16-bit is meant for error or info type. |
| 135 | * the right-most 4-bit is meant for track id. |
| 136 | * the rest is reserved. |
| 137 | * |
| 138 | * | track id | reserved | error or info type | |
| 139 | * 31 28 16 0 |
| 140 | * |
| 141 | */ |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 142 | enum media_recorder_error_type { |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 143 | MEDIA_RECORDER_ERROR_UNKNOWN = 1, |
| 144 | |
| 145 | // Track related error type |
| 146 | MEDIA_RECORDER_TRACK_ERROR_LIST_START = 100, |
| 147 | MEDIA_RECORDER_TRACK_ERROR_GENERAL = 100, |
| 148 | MEDIA_RECORDER_ERROR_VIDEO_NO_SYNC_FRAME = 200, |
| 149 | MEDIA_RECORDER_TRACK_ERROR_LIST_END = 1000, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 152 | // The codes are distributed as follow: |
| 153 | // 0xx: Reserved |
| 154 | // 8xx: General info/warning |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 155 | // |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 156 | enum media_recorder_info_type { |
| 157 | MEDIA_RECORDER_INFO_UNKNOWN = 1, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 158 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 159 | MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800, |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 160 | MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801, |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 161 | |
| 162 | // All track related informtional events start here |
| 163 | MEDIA_RECORDER_TRACK_INFO_LIST_START = 1000, |
| 164 | MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS = 1000, |
| 165 | MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME = 1001, |
James Dong | ed74230 | 2011-05-06 11:27:59 -0700 | [diff] [blame] | 166 | MEDIA_RECORDER_TRACK_INFO_TYPE = 1002, |
| 167 | MEDIA_RECORDER_TRACK_INFO_DURATION_MS = 1003, |
James Dong | 3aea037 | 2011-05-06 12:19:04 -0700 | [diff] [blame] | 168 | |
| 169 | // The time to measure the max chunk duration |
James Dong | ed74230 | 2011-05-06 11:27:59 -0700 | [diff] [blame] | 170 | MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS = 1004, |
James Dong | 3aea037 | 2011-05-06 12:19:04 -0700 | [diff] [blame] | 171 | |
James Dong | ed74230 | 2011-05-06 11:27:59 -0700 | [diff] [blame] | 172 | MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES = 1005, |
James Dong | 3aea037 | 2011-05-06 12:19:04 -0700 | [diff] [blame] | 173 | |
| 174 | // The time to measure how well the audio and video |
| 175 | // track data is interleaved. |
| 176 | MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS = 1006, |
| 177 | |
| 178 | // The time to measure system response. Note that |
| 179 | // the delay does not include the intentional delay |
| 180 | // we use to eliminate the recording sound. |
| 181 | MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS = 1007, |
| 182 | |
| 183 | // The time used to compensate for initial A/V sync. |
| 184 | MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS = 1008, |
| 185 | |
James Dong | 0f32fb3 | 2011-05-14 07:22:40 -0700 | [diff] [blame] | 186 | // Total number of bytes of the media data. |
| 187 | MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES = 1009, |
| 188 | |
James Dong | 7fc8b4f | 2011-03-18 11:25:41 -0700 | [diff] [blame] | 189 | MEDIA_RECORDER_TRACK_INFO_LIST_END = 2000, |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | // ---------------------------------------------------------------------------- |
| 193 | // ref-counted object for callbacks |
| 194 | class MediaRecorderListener: virtual public RefBase |
| 195 | { |
| 196 | public: |
| 197 | virtual void notify(int msg, int ext1, int ext2) = 0; |
| 198 | }; |
| 199 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 200 | class MediaRecorder : public BnMediaRecorderClient, |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 201 | public virtual IMediaDeathNotifier |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | { |
| 203 | public: |
| 204 | MediaRecorder(); |
| 205 | ~MediaRecorder(); |
| 206 | |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 207 | void died(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | status_t initCheck(); |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 209 | status_t setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | status_t setPreviewSurface(const sp<Surface>& surface); |
| 211 | status_t setVideoSource(int vs); |
| 212 | status_t setAudioSource(int as); |
| 213 | status_t setOutputFormat(int of); |
| 214 | status_t setVideoEncoder(int ve); |
| 215 | status_t setAudioEncoder(int ae); |
| 216 | status_t setOutputFile(const char* path); |
| 217 | status_t setOutputFile(int fd, int64_t offset, int64_t length); |
| 218 | status_t setVideoSize(int width, int height); |
| 219 | status_t setVideoFrameRate(int frames_per_second); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 220 | status_t setParameters(const String8& params); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | status_t setListener(const sp<MediaRecorderListener>& listener); |
| 222 | status_t prepare(); |
| 223 | status_t getMaxAmplitude(int* max); |
| 224 | status_t start(); |
| 225 | status_t stop(); |
| 226 | status_t reset(); |
| 227 | status_t init(); |
| 228 | status_t close(); |
| 229 | status_t release(); |
| 230 | void notify(int msg, int ext1, int ext2); |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 231 | sp<ISurfaceTexture> querySurfaceMediaSourceFromMediaServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
| 233 | private: |
| 234 | void doCleanUp(); |
| 235 | status_t doReset(); |
| 236 | |
| 237 | sp<IMediaRecorder> mMediaRecorder; |
| 238 | sp<MediaRecorderListener> mListener; |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 239 | |
| 240 | // Reference toISurfaceTexture |
| 241 | // for encoding GL Frames. That is useful only when the |
| 242 | // video source is set to VIDEO_SOURCE_GRALLOC_BUFFER |
| 243 | sp<ISurfaceTexture> mSurfaceMediaSource; |
| 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | media_recorder_states mCurrentState; |
| 246 | bool mIsAudioSourceSet; |
| 247 | bool mIsVideoSourceSet; |
| 248 | bool mIsAudioEncoderSet; |
| 249 | bool mIsVideoEncoderSet; |
| 250 | bool mIsOutputFileSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | Mutex mLock; |
| 252 | Mutex mNotifyLock; |
| 253 | }; |
| 254 | |
| 255 | }; // namespace android |
| 256 | |
| 257 | #endif // ANDROID_MEDIARECORDER_H |