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