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 
     28 namespace android {
     29 
     30 class Surface;
     31 class IMediaRecorder;
     32 class ICamera;
     33 
     34 typedef void (*media_completion_f)(status_t status, void *cookie);
     35 
     36 /* Do not change these values without updating their counterparts
     37  * in media/java/android/media/MediaRecorder.java!
     38  */
     39 enum audio_source {
     40     AUDIO_SOURCE_DEFAULT = 0,
     41     AUDIO_SOURCE_MIC = 1,
     42     AUDIO_SOURCE_VOICE_UPLINK = 2,
     43     AUDIO_SOURCE_VOICE_DOWNLINK = 3,
     44     AUDIO_SOURCE_VOICE_CALL = 4,
     45     AUDIO_SOURCE_CAMCORDER = 5,
     46     AUDIO_SOURCE_VOICE_RECOGNITION = 6,
     47     AUDIO_SOURCE_MAX = AUDIO_SOURCE_VOICE_RECOGNITION,
     48 
     49     AUDIO_SOURCE_LIST_END  // must be last - used to validate audio source type
     50 };
     51 
     52 enum video_source {
     53     VIDEO_SOURCE_DEFAULT = 0,
     54     VIDEO_SOURCE_CAMERA = 1,
     55 
     56     VIDEO_SOURCE_LIST_END  // must be last - used to validate audio source type
     57 };
     58 
     59 //Please update media/java/android/media/MediaRecorder.java if the following is updated.
     60 enum output_format {
     61     OUTPUT_FORMAT_DEFAULT = 0,
     62     OUTPUT_FORMAT_THREE_GPP = 1,
     63     OUTPUT_FORMAT_MPEG_4 = 2,
     64 
     65 
     66     OUTPUT_FORMAT_AUDIO_ONLY_START = 3, // Used in validating the output format.  Should be the
     67                                         //  at the start of the audio only output formats.
     68 
     69     /* These are audio only file formats */
     70     OUTPUT_FORMAT_RAW_AMR = 3, //to be backward compatible
     71     OUTPUT_FORMAT_AMR_NB = 3,
     72     OUTPUT_FORMAT_AMR_WB = 4,
     73     OUTPUT_FORMAT_AAC_ADIF = 5,
     74     OUTPUT_FORMAT_AAC_ADTS = 6,
     75 
     76     /* Stream over a socket, limited to a single stream */
     77     OUTPUT_FORMAT_RTP_AVP = 7,
     78 
     79     /* H.264/AAC data encapsulated in MPEG2/TS */
     80     OUTPUT_FORMAT_MPEG2TS = 8,
     81 
     82     OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
     83 };
     84 
     85 enum audio_encoder {
     86     AUDIO_ENCODER_DEFAULT = 0,
     87     AUDIO_ENCODER_AMR_NB = 1,
     88     AUDIO_ENCODER_AMR_WB = 2,
     89     AUDIO_ENCODER_AAC = 3,
     90     AUDIO_ENCODER_AAC_PLUS = 4,
     91     AUDIO_ENCODER_EAAC_PLUS = 5,
     92 
     93     AUDIO_ENCODER_LIST_END // must be the last - used to validate the audio encoder type
     94 };
     95 
     96 enum video_encoder {
     97     VIDEO_ENCODER_DEFAULT = 0,
     98     VIDEO_ENCODER_H263 = 1,
     99     VIDEO_ENCODER_H264 = 2,
    100     VIDEO_ENCODER_MPEG_4_SP = 3,
    101 
    102     VIDEO_ENCODER_LIST_END // must be the last - used to validate the video encoder type
    103 };
    104 
    105 /*
    106  * The state machine of the media_recorder uses a set of different state names.
    107  * The mapping between the media_recorder and the pvauthorengine is shown below:
    108  *
    109  *    mediarecorder                        pvauthorengine
    110  * ----------------------------------------------------------------
    111  *    MEDIA_RECORDER_ERROR                 ERROR
    112  *    MEDIA_RECORDER_IDLE                  IDLE
    113  *    MEDIA_RECORDER_INITIALIZED           OPENED
    114  *    MEDIA_RECORDER_DATASOURCE_CONFIGURED
    115  *    MEDIA_RECORDER_PREPARED              INITIALIZED
    116  *    MEDIA_RECORDER_RECORDING             RECORDING
    117  */
    118 enum media_recorder_states {
    119     MEDIA_RECORDER_ERROR                 =      0,
    120     MEDIA_RECORDER_IDLE                  = 1 << 0,
    121     MEDIA_RECORDER_INITIALIZED           = 1 << 1,
    122     MEDIA_RECORDER_DATASOURCE_CONFIGURED = 1 << 2,
    123     MEDIA_RECORDER_PREPARED              = 1 << 3,
    124     MEDIA_RECORDER_RECORDING             = 1 << 4,
    125 };
    126 
    127 // The "msg" code passed to the listener in notify.
    128 enum media_recorder_event_type {
    129     MEDIA_RECORDER_EVENT_ERROR                    = 1,
    130     MEDIA_RECORDER_EVENT_INFO                     = 2
    131 };
    132 
    133 enum media_recorder_error_type {
    134     MEDIA_RECORDER_ERROR_UNKNOWN                  = 1
    135 };
    136 
    137 // The codes are distributed as follow:
    138 //   0xx: Reserved
    139 //   8xx: General info/warning
    140 //
    141 enum media_recorder_info_type {
    142     MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
    143     MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
    144     MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
    145     MEDIA_RECORDER_INFO_COMPLETION_STATUS         = 802,
    146     MEDIA_RECORDER_INFO_PROGRESS_FRAME_STATUS     = 803,
    147     MEDIA_RECORDER_INFO_PROGRESS_TIME_STATUS      = 804,
    148 };
    149 
    150 // ----------------------------------------------------------------------------
    151 // ref-counted object for callbacks
    152 class MediaRecorderListener: virtual public RefBase
    153 {
    154 public:
    155     virtual void notify(int msg, int ext1, int ext2) = 0;
    156 };
    157 
    158 class MediaRecorder : public BnMediaRecorderClient,
    159                       public virtual IMediaDeathNotifier
    160 {
    161 public:
    162     MediaRecorder();
    163     ~MediaRecorder();
    164 
    165     void        died();
    166     status_t    initCheck();
    167     status_t    setCamera(const sp<ICamera>& camera);
    168     status_t    setPreviewSurface(const sp<Surface>& surface);
    169     status_t    setVideoSource(int vs);
    170     status_t    setAudioSource(int as);
    171     status_t    setOutputFormat(int of);
    172     status_t    setVideoEncoder(int ve);
    173     status_t    setAudioEncoder(int ae);
    174     status_t    setOutputFile(const char* path);
    175     status_t    setOutputFile(int fd, int64_t offset, int64_t length);
    176     status_t    setVideoSize(int width, int height);
    177     status_t    setVideoFrameRate(int frames_per_second);
    178     status_t    setParameters(const String8& params);
    179     status_t    setListener(const sp<MediaRecorderListener>& listener);
    180     status_t    prepare();
    181     status_t    getMaxAmplitude(int* max);
    182     status_t    start();
    183     status_t    stop();
    184     status_t    reset();
    185     status_t    init();
    186     status_t    close();
    187     status_t    release();
    188     void        notify(int msg, int ext1, int ext2);
    189 
    190 private:
    191     void                    doCleanUp();
    192     status_t                doReset();
    193 
    194     sp<IMediaRecorder>          mMediaRecorder;
    195     sp<MediaRecorderListener>   mListener;
    196     media_recorder_states       mCurrentState;
    197     bool                        mIsAudioSourceSet;
    198     bool                        mIsVideoSourceSet;
    199     bool                        mIsAudioEncoderSet;
    200     bool                        mIsVideoEncoderSet;
    201     bool                        mIsOutputFileSet;
    202     Mutex                       mLock;
    203     Mutex                       mNotifyLock;
    204 };
    205 
    206 };  // namespace android
    207 
    208 #endif // ANDROID_MEDIARECORDER_H
    209