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