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