mediarecorder.h revision ed742306b3d24ba1c2ca3fea0cc2ada8534a18b0
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;
33
34typedef void (*media_completion_f)(status_t status, void *cookie);
35
36enum video_source {
37    VIDEO_SOURCE_DEFAULT = 0,
38    VIDEO_SOURCE_CAMERA = 1,
39
40    VIDEO_SOURCE_LIST_END  // must be last - used to validate audio source type
41};
42
43//Please update media/java/android/media/MediaRecorder.java if the following is updated.
44enum output_format {
45    OUTPUT_FORMAT_DEFAULT = 0,
46    OUTPUT_FORMAT_THREE_GPP = 1,
47    OUTPUT_FORMAT_MPEG_4 = 2,
48
49
50    OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format.  Should be the
51                                        //  at the start of the audio only output formats.
52
53    /* These are audio only file formats */
54    OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible
55    OUTPUT_FORMAT_AMR_NB = 3,
56    OUTPUT_FORMAT_AMR_WB = 4,
57    OUTPUT_FORMAT_AAC_ADIF = 5,
58    OUTPUT_FORMAT_AAC_ADTS = 6,
59
60    /* Stream over a socket, limited to a single stream */
61    OUTPUT_FORMAT_RTP_AVP = 7,
62
63    /* H.264/AAC data encapsulated in MPEG2/TS */
64    OUTPUT_FORMAT_MPEG2TS = 8,
65
66    OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
67};
68
69enum audio_encoder {
70    AUDIO_ENCODER_DEFAULT = 0,
71    AUDIO_ENCODER_AMR_NB = 1,
72    AUDIO_ENCODER_AMR_WB = 2,
73    AUDIO_ENCODER_AAC = 3,
74    AUDIO_ENCODER_AAC_PLUS = 4,
75    AUDIO_ENCODER_EAAC_PLUS = 5,
76
77    AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type
78};
79
80enum video_encoder {
81    VIDEO_ENCODER_DEFAULT = 0,
82    VIDEO_ENCODER_H263 = 1,
83    VIDEO_ENCODER_H264 = 2,
84    VIDEO_ENCODER_MPEG_4_SP = 3,
85
86    VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type
87};
88
89/*
90 * The state machine of the media_recorder.
91 */
92enum media_recorder_states {
93    // Error state.
94    MEDIA_RECORDER_ERROR                 =      0,
95
96    // Recorder was just created.
97    MEDIA_RECORDER_IDLE                  = 1 << 0,
98
99    // Recorder has been initialized.
100    MEDIA_RECORDER_INITIALIZED           = 1 << 1,
101
102    // Configuration of the recorder has been completed.
103    MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
104
105    // Recorder is ready to start.
106    MEDIA_RECORDER_PREPARED              = 1 << 3,
107
108    // Recording is in progress.
109    MEDIA_RECORDER_RECORDING             = 1 << 4,
110};
111
112// The "msg" code passed to the listener in notify.
113enum media_recorder_event_type {
114    MEDIA_RECORDER_EVENT_LIST_START               = 1,
115    MEDIA_RECORDER_EVENT_ERROR                    = 1,
116    MEDIA_RECORDER_EVENT_INFO                     = 2,
117    MEDIA_RECORDER_EVENT_LIST_END                 = 99,
118
119    // Track related event types
120    MEDIA_RECORDER_TRACK_EVENT_LIST_START         = 100,
121    MEDIA_RECORDER_TRACK_EVENT_ERROR              = 100,
122    MEDIA_RECORDER_TRACK_EVENT_INFO               = 101,
123    MEDIA_RECORDER_TRACK_EVENT_LIST_END           = 1000,
124};
125
126/*
127 * The (part of) "what" code passed to the listener in notify.
128 * When the error or info type is track specific, the what has
129 * the following layout:
130 * the left-most 16-bit is meant for error or info type.
131 * the right-most 4-bit is meant for track id.
132 * the rest is reserved.
133 *
134 * | track id | reserved |     error or info type     |
135 * 31         28         16                           0
136 *
137 */
138enum media_recorder_error_type {
139    MEDIA_RECORDER_ERROR_UNKNOWN                   = 1,
140
141    // Track related error type
142    MEDIA_RECORDER_TRACK_ERROR_LIST_START          = 100,
143    MEDIA_RECORDER_TRACK_ERROR_GENERAL             = 100,
144    MEDIA_RECORDER_ERROR_VIDEO_NO_SYNC_FRAME       = 200,
145    MEDIA_RECORDER_TRACK_ERROR_LIST_END            = 1000,
146};
147
148// The codes are distributed as follow:
149//   0xx: Reserved
150//   8xx: General info/warning
151//
152enum media_recorder_info_type {
153    MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
154
155    MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
156    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
157
158    // All track related informtional events start here
159    MEDIA_RECORDER_TRACK_INFO_LIST_START           = 1000,
160    MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS    = 1000,
161    MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME     = 1001,
162    MEDIA_RECORDER_TRACK_INFO_TYPE                 = 1002,
163    MEDIA_RECORDER_TRACK_INFO_DURATION_MS          = 1003,
164    MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS     = 1004,
165    MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES       = 1005,
166    MEDIA_RECORDER_TRACK_INFO_LIST_END             = 2000,
167};
168
169// ----------------------------------------------------------------------------
170// ref-counted object for callbacks
171class MediaRecorderListener: virtual public RefBase
172{
173public:
174    virtual void notify(int msg, int ext1, int ext2) = 0;
175};
176
177class MediaRecorder : public BnMediaRecorderClient,
178                      public virtual IMediaDeathNotifier
179{
180public:
181    MediaRecorder();
182    ~MediaRecorder();
183
184    void        died();
185    status_t    initCheck();
186    status_t    setCamera(const sp<ICamera>& camera);
187    status_t    setPreviewSurface(const sp<Surface>& surface);
188    status_t    setVideoSource(int vs);
189    status_t    setAudioSource(int as);
190    status_t    setOutputFormat(int of);
191    status_t    setVideoEncoder(int ve);
192    status_t    setAudioEncoder(int ae);
193    status_t    setOutputFile(const char* path);
194    status_t    setOutputFile(int fd, int64_t offset, int64_t length);
195    status_t    setOutputFileAuxiliary(int fd);
196    status_t    setVideoSize(int width, int height);
197    status_t    setVideoFrameRate(int frames_per_second);
198    status_t    setParameters(const String8& params);
199    status_t    setListener(const sp<MediaRecorderListener>& listener);
200    status_t    prepare();
201    status_t    getMaxAmplitude(int* max);
202    status_t    start();
203    status_t    stop();
204    status_t    reset();
205    status_t    init();
206    status_t    close();
207    status_t    release();
208    void        notify(int msg, int ext1, int ext2);
209
210private:
211    void                    doCleanUp();
212    status_t                doReset();
213
214    sp<IMediaRecorder>          mMediaRecorder;
215    sp<MediaRecorderListener>   mListener;
216    media_recorder_states       mCurrentState;
217    bool                        mIsAudioSourceSet;
218    bool                        mIsVideoSourceSet;
219    bool                        mIsAudioEncoderSet;
220    bool                        mIsVideoEncoderSet;
221    bool                        mIsOutputFileSet;
222    bool                        mIsAuxiliaryOutputFileSet;
223    Mutex                       mLock;
224    Mutex                       mNotifyLock;
225};
226
227};  // namespace android
228
229#endif // ANDROID_MEDIARECORDER_H
230