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