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