mediarecorder.h revision 114819633470ebd5b346c13c2a82a0025d2d39c0
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/*
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** Copyright (C) 2008 The Android Open Source Project
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** Licensed under the Apache License, Version 2.0 (the "License");
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** you may not use this file except in compliance with the License.
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** You may obtain a copy of the License at
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **     http://www.apache.org/licenses/LICENSE-2.0
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** Unless required by applicable law or agreed to in writing, software
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** distributed under the License is distributed on an "AS IS" BASIS,
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** See the License for the specific language governing permissions and
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ** limitations under the License.
165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu */
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef ANDROID_MEDIARECORDER_H
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define ANDROID_MEDIARECORDER_H
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/Log.h>
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/threads.h>
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/List.h>
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/Errors.h>
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <media/IMediaRecorderClient.h>
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <media/IMediaDeathNotifier.h>
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace android {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class Surface;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class IMediaRecorder;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ICamera;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ICameraRecordingProxy;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class IGraphicBufferProducer;
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class Surface;
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef void (*media_completion_f)(status_t status, void *cookie);
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)enum video_source {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VIDEO_SOURCE_DEFAULT = 0,
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VIDEO_SOURCE_CAMERA = 1,
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VIDEO_SOURCE_SURFACE = 2,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VIDEO_SOURCE_LIST_END  // must be last - used to validate audio source type
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//Please update media/java/android/media/MediaRecorder.java if the following is updated.
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)enum output_format {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_DEFAULT = 0,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_THREE_GPP = 1,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_MPEG_4 = 2,
525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format.  Should be the
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                        //  at the start of the audio only output formats.
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /* These are audio only file formats */
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_AMR_NB = 3,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_AMR_WB = 4,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_AAC_ADIF = 5,
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_AAC_ADTS = 6,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    OUTPUT_FORMAT_AUDIO_ONLY_END = 7, // Used in validating the output format.  Should be the
65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      //  at the end of the audio only output formats.
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /* Stream over a socket, limited to a single stream */
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_RTP_AVP = 7,
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /* H.264/AAC data encapsulated in MPEG2/TS */
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_MPEG2TS = 8,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)enum audio_encoder {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_DEFAULT = 0,
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_AMR_NB = 1,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_AMR_WB = 2,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_AAC = 3,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_HE_AAC = 4,
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_AAC_ELD = 5,
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
87enum video_encoder {
88    VIDEO_ENCODER_DEFAULT = 0,
89    VIDEO_ENCODER_H263 = 1,
90    VIDEO_ENCODER_H264 = 2,
91    VIDEO_ENCODER_MPEG_4_SP = 3,
92
93    VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type
94};
95
96/*
97 * The state machine of the media_recorder.
98 */
99enum media_recorder_states {
100    // Error state.
101    MEDIA_RECORDER_ERROR                 =      0,
102
103    // Recorder was just created.
104    MEDIA_RECORDER_IDLE                  = 1 << 0,
105
106    // Recorder has been initialized.
107    MEDIA_RECORDER_INITIALIZED           = 1 << 1,
108
109    // Configuration of the recorder has been completed.
110    MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
111
112    // Recorder is ready to start.
113    MEDIA_RECORDER_PREPARED              = 1 << 3,
114
115    // Recording is in progress.
116    MEDIA_RECORDER_RECORDING             = 1 << 4,
117};
118
119// The "msg" code passed to the listener in notify.
120enum media_recorder_event_type {
121    MEDIA_RECORDER_EVENT_LIST_START               = 1,
122    MEDIA_RECORDER_EVENT_ERROR                    = 1,
123    MEDIA_RECORDER_EVENT_INFO                     = 2,
124    MEDIA_RECORDER_EVENT_LIST_END                 = 99,
125
126    // Track related event types
127    MEDIA_RECORDER_TRACK_EVENT_LIST_START         = 100,
128    MEDIA_RECORDER_TRACK_EVENT_ERROR              = 100,
129    MEDIA_RECORDER_TRACK_EVENT_INFO               = 101,
130    MEDIA_RECORDER_TRACK_EVENT_LIST_END           = 1000,
131};
132
133/*
134 * The (part of) "what" code passed to the listener in notify.
135 * When the error or info type is track specific, the what has
136 * the following layout:
137 * the left-most 16-bit is meant for error or info type.
138 * the right-most 4-bit is meant for track id.
139 * the rest is reserved.
140 *
141 * | track id | reserved |     error or info type     |
142 * 31         28         16                           0
143 *
144 */
145enum media_recorder_error_type {
146    MEDIA_RECORDER_ERROR_UNKNOWN                   = 1,
147
148    // Track related error type
149    MEDIA_RECORDER_TRACK_ERROR_LIST_START          = 100,
150    MEDIA_RECORDER_TRACK_ERROR_GENERAL             = 100,
151    MEDIA_RECORDER_ERROR_VIDEO_NO_SYNC_FRAME       = 200,
152    MEDIA_RECORDER_TRACK_ERROR_LIST_END            = 1000,
153};
154
155// The codes are distributed as follow:
156//   0xx: Reserved
157//   8xx: General info/warning
158//
159enum media_recorder_info_type {
160    MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
161
162    MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
163    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
164
165    // All track related informtional events start here
166    MEDIA_RECORDER_TRACK_INFO_LIST_START           = 1000,
167    MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS    = 1000,
168    MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME     = 1001,
169    MEDIA_RECORDER_TRACK_INFO_TYPE                 = 1002,
170    MEDIA_RECORDER_TRACK_INFO_DURATION_MS          = 1003,
171
172    // The time to measure the max chunk duration
173    MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS     = 1004,
174
175    MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES       = 1005,
176
177    // The time to measure how well the audio and video
178    // track data is interleaved.
179    MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS       = 1006,
180
181    // The time to measure system response. Note that
182    // the delay does not include the intentional delay
183    // we use to eliminate the recording sound.
184    MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS     = 1007,
185
186    // The time used to compensate for initial A/V sync.
187    MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS      = 1008,
188
189    // Total number of bytes of the media data.
190    MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES          = 1009,
191
192    MEDIA_RECORDER_TRACK_INFO_LIST_END             = 2000,
193};
194
195// ----------------------------------------------------------------------------
196// ref-counted object for callbacks
197class MediaRecorderListener: virtual public RefBase
198{
199public:
200    virtual void notify(int msg, int ext1, int ext2) = 0;
201};
202
203class MediaRecorder : public BnMediaRecorderClient,
204                      public virtual IMediaDeathNotifier
205{
206public:
207    MediaRecorder();
208    ~MediaRecorder();
209
210    void        died();
211    status_t    initCheck();
212    status_t    setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
213    status_t    setPreviewSurface(const sp<IGraphicBufferProducer>& surface);
214    status_t    setVideoSource(int vs);
215    status_t    setAudioSource(int as);
216    status_t    setOutputFormat(int of);
217    status_t    setVideoEncoder(int ve);
218    status_t    setAudioEncoder(int ae);
219    status_t    setOutputFile(const char* path);
220    status_t    setOutputFile(int fd, int64_t offset, int64_t length);
221    status_t    setVideoSize(int width, int height);
222    status_t    setVideoFrameRate(int frames_per_second);
223    status_t    setParameters(const String8& params);
224    status_t    setListener(const sp<MediaRecorderListener>& listener);
225    status_t    setClientName(const String16& clientName);
226    status_t    prepare();
227    status_t    getMaxAmplitude(int* max);
228    status_t    start();
229    status_t    stop();
230    status_t    reset();
231    status_t    init();
232    status_t    close();
233    status_t    release();
234    void        notify(int msg, int ext1, int ext2);
235    sp<IGraphicBufferProducer>     querySurfaceMediaSourceFromMediaServer();
236
237private:
238    void                    doCleanUp();
239    status_t                doReset();
240
241    sp<IMediaRecorder>          mMediaRecorder;
242    sp<MediaRecorderListener>   mListener;
243
244    // Reference to IGraphicBufferProducer
245    // for encoding GL Frames. That is useful only when the
246    // video source is set to VIDEO_SOURCE_GRALLOC_BUFFER
247    sp<IGraphicBufferProducer>  mSurfaceMediaSource;
248
249    media_recorder_states       mCurrentState;
250    bool                        mIsAudioSourceSet;
251    bool                        mIsVideoSourceSet;
252    bool                        mIsAudioEncoderSet;
253    bool                        mIsVideoEncoderSet;
254    bool                        mIsOutputFileSet;
255    Mutex                       mLock;
256    Mutex                       mNotifyLock;
257};
258
259};  // namespace android
260
261#endif // ANDROID_MEDIARECORDER_H
262