mediarecorder.h revision 273d098cc72a88c797d01c7dff0c1350f315f05f
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/IMediaPlayerClient.h>
26
27namespace android {
28
29class Surface;
30class IMediaRecorder;
31class ICamera;
32
33typedef void (*media_completion_f)(status_t status, void *cookie);
34
35/* Do not change these values without updating their counterparts
36 * in java/android/android/media/MediaRecorder.java!
37 */
38enum audio_source {
39    AUDIO_SOURCE_DEFAULT = 0,
40    AUDIO_SOURCE_MIC = 1,
41    AUDIO_SOURCE_VOICE_UPLINK = 2,
42    AUDIO_SOURCE_VOICE_DOWNLINK = 3,
43    AUDIO_SOURCE_VOICE_CALL = 4,
44    AUDIO_SOURCE_MAX = AUDIO_SOURCE_VOICE_CALL
45};
46
47enum video_source {
48    VIDEO_SOURCE_DEFAULT = 0,
49    VIDEO_SOURCE_CAMERA = 1,
50};
51
52//Please update java/android/android/media/MediaRecorder.java if the following is updated.
53enum output_format {
54    OUTPUT_FORMAT_DEFAULT = 0,
55    OUTPUT_FORMAT_THREE_GPP,
56    OUTPUT_FORMAT_MPEG_4,
57    OUTPUT_FORMAT_RAW_AMR,
58    OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
59};
60
61enum audio_encoder {
62    AUDIO_ENCODER_DEFAULT = 0,
63    AUDIO_ENCODER_AMR_NB = 1,
64};
65
66enum video_encoder {
67    VIDEO_ENCODER_DEFAULT = 0,
68    VIDEO_ENCODER_H263 = 1,
69    VIDEO_ENCODER_H264 = 2,
70    VIDEO_ENCODER_MPEG_4_SP = 3,
71};
72
73// Maximum frames per second is 24
74#define MEDIA_RECORDER_MAX_FRAME_RATE         24
75
76/*
77 * The state machine of the media_recorder uses a set of different state names.
78 * The mapping between the media_recorder and the pvauthorengine is shown below:
79 *
80 *    mediarecorder                        pvauthorengine
81 * ----------------------------------------------------------------
82 *    MEDIA_RECORDER_ERROR                 ERROR
83 *    MEDIA_RECORDER_IDLE                  IDLE
84 *    MEDIA_RECORDER_INITIALIZED           OPENED
85 *    MEDIA_RECORDER_DATASOURCE_CONFIGURED
86 *    MEDIA_RECORDER_PREPARED              INITIALIZED
87 *    MEDIA_RECORDER_RECORDING             RECORDING
88 */
89enum media_recorder_states {
90    MEDIA_RECORDER_ERROR                 =      0,
91    MEDIA_RECORDER_IDLE                  = 1 << 0,
92    MEDIA_RECORDER_INITIALIZED           = 1 << 1,
93    MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
94    MEDIA_RECORDER_PREPARED              = 1 << 3,
95    MEDIA_RECORDER_RECORDING             = 1 << 4,
96};
97
98// The "msg" code passed to the listener in notify.
99enum media_recorder_event_type {
100    MEDIA_RECORDER_EVENT_ERROR                    = 1,
101    MEDIA_RECORDER_EVENT_INFO                     = 2
102};
103
104enum media_recorder_error_type {
105    MEDIA_RECORDER_ERROR_UNKNOWN                  = 1
106};
107
108// The codes are distributed as follow:
109//   0xx: Reserved
110//   8xx: General info/warning
111//
112enum media_recorder_info_type {
113    MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
114    MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
115    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801
116};
117
118// ----------------------------------------------------------------------------
119// ref-counted object for callbacks
120class MediaRecorderListener: virtual public RefBase
121{
122public:
123    virtual void notify(int msg, int ext1, int ext2) = 0;
124};
125
126class MediaRecorder : public BnMediaPlayerClient
127{
128public:
129    MediaRecorder();
130    ~MediaRecorder();
131
132    status_t    initCheck();
133    status_t    setCamera(const sp<ICamera>& camera);
134    status_t    setPreviewSurface(const sp<Surface>& surface);
135    status_t    setVideoSource(int vs);
136    status_t    setAudioSource(int as);
137    status_t    setOutputFormat(int of);
138    status_t    setVideoEncoder(int ve);
139    status_t    setAudioEncoder(int ae);
140    status_t    setOutputFile(const char* path);
141    status_t    setOutputFile(int fd, int64_t offset, int64_t length);
142    status_t    setVideoSize(int width, int height);
143    status_t    setVideoFrameRate(int frames_per_second);
144    status_t    setParameters(const String8& params);
145    status_t    setListener(const sp<MediaRecorderListener>& listener);
146    status_t    prepare();
147    status_t    getMaxAmplitude(int* max);
148    status_t    start();
149    status_t    stop();
150    status_t    reset();
151    status_t    init();
152    status_t    close();
153    status_t    release();
154    void        notify(int msg, int ext1, int ext2);
155
156private:
157    void                    doCleanUp();
158    status_t                doReset();
159
160    sp<IMediaRecorder>          mMediaRecorder;
161    sp<MediaRecorderListener>   mListener;
162    media_recorder_states       mCurrentState;
163    bool                        mIsAudioSourceSet;
164    bool                        mIsVideoSourceSet;
165    bool                        mIsAudioEncoderSet;
166    bool                        mIsVideoEncoderSet;
167    bool                        mIsOutputFileSet;
168    Mutex                       mLock;
169    Mutex                       mNotifyLock;
170};
171
172};  // namespace android
173
174#endif // ANDROID_MEDIARECORDER_H
175