mediarecorder.h revision c643d77a099b0e86ad12de9b498f6f780e03184e
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
36/* Do not change these values without updating their counterparts
37 * in media/java/android/media/MediaRecorder.java!
38 */
39enum audio_source {
40    AUDIO_SOURCE_DEFAULT = 0,
41    AUDIO_SOURCE_MIC = 1,
42    AUDIO_SOURCE_VOICE_UPLINK = 2,
43    AUDIO_SOURCE_VOICE_DOWNLINK = 3,
44    AUDIO_SOURCE_VOICE_CALL = 4,
45    AUDIO_SOURCE_CAMCORDER = 5,
46    AUDIO_SOURCE_VOICE_RECOGNITION = 6,
47    AUDIO_SOURCE_VOICE_COMMUNICATION = 7,
48    AUDIO_SOURCE_MAX = AUDIO_SOURCE_VOICE_COMMUNICATION,
49
50    AUDIO_SOURCE_LIST_END  // must be last - used to validate audio source type
51};
52
53enum video_source {
54    VIDEO_SOURCE_DEFAULT = 0,
55    VIDEO_SOURCE_CAMERA = 1,
56
57    VIDEO_SOURCE_LIST_END  // must be last - used to validate audio source type
58};
59
60//Please update media/java/android/media/MediaRecorder.java if the following is updated.
61enum output_format {
62    OUTPUT_FORMAT_DEFAULT = 0,
63    OUTPUT_FORMAT_THREE_GPP = 1,
64    OUTPUT_FORMAT_MPEG_4 = 2,
65
66
67    OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format.  Should be the
68                                        //  at the start of the audio only output formats.
69
70    /* These are audio only file formats */
71    OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible
72    OUTPUT_FORMAT_AMR_NB = 3,
73    OUTPUT_FORMAT_AMR_WB = 4,
74    OUTPUT_FORMAT_AAC_ADIF = 5,
75    OUTPUT_FORMAT_AAC_ADTS = 6,
76
77    /* Stream over a socket, limited to a single stream */
78    OUTPUT_FORMAT_RTP_AVP = 7,
79
80    /* H.264/AAC data encapsulated in MPEG2/TS */
81    OUTPUT_FORMAT_MPEG2TS = 8,
82
83    OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
84};
85
86enum audio_encoder {
87    AUDIO_ENCODER_DEFAULT = 0,
88    AUDIO_ENCODER_AMR_NB = 1,
89    AUDIO_ENCODER_AMR_WB = 2,
90    AUDIO_ENCODER_AAC = 3,
91    AUDIO_ENCODER_AAC_PLUS = 4,
92    AUDIO_ENCODER_EAAC_PLUS = 5,
93
94    AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type
95};
96
97enum video_encoder {
98    VIDEO_ENCODER_DEFAULT = 0,
99    VIDEO_ENCODER_H263 = 1,
100    VIDEO_ENCODER_H264 = 2,
101    VIDEO_ENCODER_MPEG_4_SP = 3,
102
103    VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type
104};
105
106/*
107 * The state machine of the media_recorder uses a set of different state names.
108 * The mapping between the media_recorder and the pvauthorengine is shown below:
109 *
110 *    mediarecorder                        pvauthorengine
111 * ----------------------------------------------------------------
112 *    MEDIA_RECORDER_ERROR                 ERROR
113 *    MEDIA_RECORDER_IDLE                  IDLE
114 *    MEDIA_RECORDER_INITIALIZED           OPENED
115 *    MEDIA_RECORDER_DATASOURCE_CONFIGURED
116 *    MEDIA_RECORDER_PREPARED              INITIALIZED
117 *    MEDIA_RECORDER_RECORDING             RECORDING
118 */
119enum media_recorder_states {
120    MEDIA_RECORDER_ERROR                 =      0,
121    MEDIA_RECORDER_IDLE                  = 1 << 0,
122    MEDIA_RECORDER_INITIALIZED           = 1 << 1,
123    MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
124    MEDIA_RECORDER_PREPARED              = 1 << 3,
125    MEDIA_RECORDER_RECORDING             = 1 << 4,
126};
127
128// The "msg" code passed to the listener in notify.
129enum media_recorder_event_type {
130    MEDIA_RECORDER_EVENT_ERROR                    = 1,
131    MEDIA_RECORDER_EVENT_INFO                     = 2
132};
133
134enum media_recorder_error_type {
135    MEDIA_RECORDER_ERROR_UNKNOWN                  = 1
136};
137
138// The codes are distributed as follow:
139//   0xx: Reserved
140//   8xx: General info/warning
141//
142enum media_recorder_info_type {
143    MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
144    MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
145    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
146    MEDIA_RECORDER_INFO_COMPLETION_STATUS         = 802,
147    MEDIA_RECORDER_INFO_PROGRESS_FRAME_STATUS     = 803,
148    MEDIA_RECORDER_INFO_PROGRESS_TIME_STATUS      = 804,
149};
150
151// ----------------------------------------------------------------------------
152// ref-counted object for callbacks
153class MediaRecorderListener: virtual public RefBase
154{
155public:
156    virtual void notify(int msg, int ext1, int ext2) = 0;
157};
158
159class MediaRecorder : public BnMediaRecorderClient,
160                      public virtual IMediaDeathNotifier
161{
162public:
163    MediaRecorder();
164    ~MediaRecorder();
165
166    void        died();
167    status_t    initCheck();
168    status_t    setCamera(const sp<ICamera>& camera);
169    status_t    setPreviewSurface(const sp<Surface>& surface);
170    status_t    setVideoSource(int vs);
171    status_t    setAudioSource(int as);
172    status_t    setOutputFormat(int of);
173    status_t    setVideoEncoder(int ve);
174    status_t    setAudioEncoder(int ae);
175    status_t    setOutputFile(const char* path);
176    status_t    setOutputFile(int fd, int64_t offset, int64_t length);
177    status_t    setOutputFileAuxiliary(int fd);
178    status_t    setVideoSize(int width, int height);
179    status_t    setVideoFrameRate(int frames_per_second);
180    status_t    setParameters(const String8& params);
181    status_t    setListener(const sp<MediaRecorderListener>& listener);
182    status_t    prepare();
183    status_t    getMaxAmplitude(int* max);
184    status_t    start();
185    status_t    stop();
186    status_t    reset();
187    status_t    init();
188    status_t    close();
189    status_t    release();
190    void        notify(int msg, int ext1, int ext2);
191
192private:
193    void                    doCleanUp();
194    status_t                doReset();
195
196    sp<IMediaRecorder>          mMediaRecorder;
197    sp<MediaRecorderListener>   mListener;
198    media_recorder_states       mCurrentState;
199    bool                        mIsAudioSourceSet;
200    bool                        mIsVideoSourceSet;
201    bool                        mIsAudioEncoderSet;
202    bool                        mIsVideoEncoderSet;
203    bool                        mIsOutputFileSet;
204    bool                        mIsAuxiliaryOutputFileSet;
205    Mutex                       mLock;
206    Mutex                       mNotifyLock;
207};
208
209};  // namespace android
210
211#endif // ANDROID_MEDIARECORDER_H
212