130ab66297501757d745b9ae10da61adcd891f497Andreas Huber/*
230ab66297501757d745b9ae10da61adcd891f497Andreas Huber * Copyright (C) 2009 The Android Open Source Project
330ab66297501757d745b9ae10da61adcd891f497Andreas Huber *
430ab66297501757d745b9ae10da61adcd891f497Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
530ab66297501757d745b9ae10da61adcd891f497Andreas Huber * you may not use this file except in compliance with the License.
630ab66297501757d745b9ae10da61adcd891f497Andreas Huber * You may obtain a copy of the License at
730ab66297501757d745b9ae10da61adcd891f497Andreas Huber *
830ab66297501757d745b9ae10da61adcd891f497Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
930ab66297501757d745b9ae10da61adcd891f497Andreas Huber *
1030ab66297501757d745b9ae10da61adcd891f497Andreas Huber * Unless required by applicable law or agreed to in writing, software
1130ab66297501757d745b9ae10da61adcd891f497Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1230ab66297501757d745b9ae10da61adcd891f497Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1330ab66297501757d745b9ae10da61adcd891f497Andreas Huber * See the License for the specific language governing permissions and
1430ab66297501757d745b9ae10da61adcd891f497Andreas Huber * limitations under the License.
1530ab66297501757d745b9ae10da61adcd891f497Andreas Huber */
1630ab66297501757d745b9ae10da61adcd891f497Andreas Huber
1730ab66297501757d745b9ae10da61adcd891f497Andreas Huber#ifndef STAGEFRIGHT_RECORDER_H_
1830ab66297501757d745b9ae10da61adcd891f497Andreas Huber
1930ab66297501757d745b9ae10da61adcd891f497Andreas Huber#define STAGEFRIGHT_RECORDER_H_
2030ab66297501757d745b9ae10da61adcd891f497Andreas Huber
21ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick#include <media/MediaAnalyticsItem.h>
2230ab66297501757d745b9ae10da61adcd891f497Andreas Huber#include <media/MediaRecorderBase.h>
235f4d93467a03b3a38320416f8cc2c2c2c94bf3fcJames Dong#include <camera/CameraParameters.h>
2430ab66297501757d745b9ae10da61adcd891f497Andreas Huber#include <utils/String8.h>
2530ab66297501757d745b9ae10da61adcd891f497Andreas Huber
2664760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
27fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
283e328782f1e1061d08ea0c45b855cc418a2d9ea6Lajos Molnar#include <MetadataBufferType.h>
293e328782f1e1061d08ea0c45b855cc418a2d9ea6Lajos Molnar
3030ab66297501757d745b9ae10da61adcd891f497Andreas Hubernamespace android {
3130ab66297501757d745b9ae10da61adcd891f497Andreas Huber
32c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dongclass Camera;
334ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Liclass ICameraRecordingProxy;
349c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatraclass CameraSource;
3578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatraclass CameraSourceTimeLapse;
36d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kimstruct MediaCodecSource;
372dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huberstruct MediaSource;
382dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huberstruct MediaWriter;
399c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatraclass MetaData;
40d3d4e5069e1af0437c4f5a7b4ba344bda5b937afJames Dongstruct AudioSource;
4199c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dongclass MediaProfiles;
426d339f1f764bbd32e3381dae7bfa7c6c575bb493Lajos Molnarstruct ALooper;
4330ab66297501757d745b9ae10da61adcd891f497Andreas Huber
4430ab66297501757d745b9ae10da61adcd891f497Andreas Huberstruct StagefrightRecorder : public MediaRecorderBase {
4540d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    explicit StagefrightRecorder(const String16 &opPackageName);
4630ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual ~StagefrightRecorder();
4730ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t init();
48fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    virtual status_t setAudioSource(audio_source_t as);
4930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoSource(video_source vs);
5030ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setOutputFormat(output_format of);
5130ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setAudioEncoder(audio_encoder ae);
5230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoEncoder(video_encoder ve);
5330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoSize(int width, int height);
5430ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoFrameRate(int frames_per_second);
55d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    virtual status_t setCamera(const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
5699617adda9bc46c43f511f0940bc735c73de61deMathias Agopian    virtual status_t setPreviewSurface(const sp<IGraphicBufferProducer>& surface);
57addf2cbb120346ae42e78fa739245a353db5edadChong Zhang    virtual status_t setInputSurface(const sp<PersistentSurface>& surface);
58fe44e4f74fe2582cbf012687059278dbcbdaa6f7Hangyu Kuang    virtual status_t setOutputFile(int fd);
59fe44e4f74fe2582cbf012687059278dbcbdaa6f7Hangyu Kuang    virtual status_t setNextOutputFile(int fd);
6040d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    virtual status_t setParameters(const String8 &params);
6140d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    virtual status_t setListener(const sp<IMediaRecorderClient> &listener);
6240d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    virtual status_t setClientName(const String16 &clientName);
6330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t prepare();
6430ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t start();
65a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong    virtual status_t pause();
66d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    virtual status_t resume();
6730ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t stop();
6830ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t close();
6930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t reset();
7030ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t getMaxAmplitude(int *max);
71ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick    virtual status_t getMetrics(Parcel *reply);
7240d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    virtual status_t dump(int fd, const Vector<String16> &args) const;
73b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Querying a SurfaceMediaSourcer
748ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    virtual sp<IGraphicBufferProducer> querySurfaceMediaSource() const;
7530ab66297501757d745b9ae10da61adcd891f497Andreas Huber
7630ab66297501757d745b9ae10da61adcd891f497Andreas Huberprivate:
77fc079f79d8d4f6ef9f138aa615486c10dda99fb7Hangyu Kuang    mutable Mutex mLock;
78d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    sp<hardware::ICamera> mCamera;
794ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li    sp<ICameraRecordingProxy> mCameraProxy;
8099617adda9bc46c43f511f0940bc735c73de61deMathias Agopian    sp<IGraphicBufferProducer> mPreviewSurface;
81addf2cbb120346ae42e78fa739245a353db5edadChong Zhang    sp<PersistentSurface> mPersistentSurface;
8237047fceba836f341d0108beed0991b0f8dfc543James Dong    sp<IMediaRecorderClient> mListener;
83ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    String16 mClientName;
84ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    uid_t mClientUid;
8598a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen    pid_t mClientPid;
863cecf640c4daf2df616b278bd9986018c8182908James Dong    sp<MediaWriter> mWriter;
873cecf640c4daf2df616b278bd9986018c8182908James Dong    int mOutputFd;
88d3d4e5069e1af0437c4f5a7b4ba344bda5b937afJames Dong    sp<AudioSource> mAudioSourceNode;
8930ab66297501757d745b9ae10da61adcd891f497Andreas Huber
90ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick    MediaAnalyticsItem *mAnalyticsItem;
91ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick    bool mAnalyticsDirty;
92ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick    void resetMetrics();
93ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick    void updateMetrics();
94ac4e4189db77dc0af7671b162d11be5ccec69339Ray Essick
95fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    audio_source_t mAudioSource;
9630ab66297501757d745b9ae10da61adcd891f497Andreas Huber    video_source mVideoSource;
9730ab66297501757d745b9ae10da61adcd891f497Andreas Huber    output_format mOutputFormat;
9830ab66297501757d745b9ae10da61adcd891f497Andreas Huber    audio_encoder mAudioEncoder;
9930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    video_encoder mVideoEncoder;
1002dec2b5be2056c6d9428897dc672185872d30d17James Dong    bool mUse64BitFileOffset;
101050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mVideoWidth, mVideoHeight;
102050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mFrameRate;
1033cecf640c4daf2df616b278bd9986018c8182908James Dong    int32_t mVideoBitRate;
104cfe88a20345dad981842b2c8092e4c704d3f98b4Nipun Kwatra    int32_t mAudioBitRate;
105050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mAudioChannels;
106050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mSampleRate;
10713aec890216948b0c364f8f92792129d0335f506James Dong    int32_t mInterleaveDurationUs;
1088f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mIFramesIntervalSec;
10993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    int32_t mCameraId;
110145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    int32_t mVideoEncoderProfile;
111145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    int32_t mVideoEncoderLevel;
1128f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mMovieTimeScale;
1138f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mVideoTimeScale;
1148f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mAudioTimeScale;
115ef9d0cd72e476a4b7556833fb09505a51b626797James Dong    int64_t mMaxFileSizeBytes;
116ef9d0cd72e476a4b7556833fb09505a51b626797James Dong    int64_t mMaxFileDurationUs;
11793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    int64_t mTrackEveryTimeDurationUs;
11813f6284305e4b27395a23db7882d670bdb1bcae1James Dong    int32_t mRotationDegrees;  // Clockwise
11907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong    int32_t mLatitudex10000;
12007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong    int32_t mLongitudex10000;
12186b7f47aa7482424cf8fd248f1315311919be3b0James Dong    int32_t mStartTimeOffsetMs;
12272cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int32_t mTotalBitRate;
123050b28a593350047845a45a14cc5026221ac1620James Dong
12446d26dd29195450db15704e84d65740628a821fbChong Zhang    bool mCaptureFpsEnable;
12573c3e6363b31fb27882c4666453ad8ef050b3cc1Wonsik Kim    double mCaptureFps;
12646d26dd29195450db15704e84d65740628a821fbChong Zhang    int64_t mTimeBetweenCaptureUs;
12778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    sp<CameraSourceTimeLapse> mCameraSourceTimeLapse;
128fc20aab463f527ab3b0664986f0381a86b375884Nipun Kwatra
12930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    String8 mParams;
13030ab66297501757d745b9ae10da61adcd891f497Andreas Huber
1313e328782f1e1061d08ea0c45b855cc418a2d9ea6Lajos Molnar    MetadataBufferType mMetaDataStoredInVideoBuffers;
13299c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    MediaProfiles *mEncoderProfiles;
13399c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong
134d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    int64_t mPauseStartTimeUs;
135d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    int64_t mTotalPausedDurationUs;
136d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    sp<MediaCodecSource> mAudioEncoderSource;
137d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    sp<MediaCodecSource> mVideoEncoderSource;
138d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim
13975b0b5473c7b43f8c5972db7e6e8930988ead751Gloria Wang    bool mStarted;
140b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Needed when GLFrames are encoded.
1418ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    // An <IGraphicBufferProducer> pointer
142b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // will be sent to the client side using which the
143b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // frame buffers will be queued and dequeued
14472cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    sp<IGraphicBufferProducer> mGraphicBufferProducer;
14572cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    sp<ALooper> mLooper;
14672cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang
147c9f122d8f32ba6928ac9087448025fa3bed1553dZhijun He    static const int kMaxHighSpeedFps = 1000;
148c9f122d8f32ba6928ac9087448025fa3bed1553dZhijun He
1491a5690652f3f6ee40f15c2f9f6c4b6badf4dbcf5Chong Zhang    status_t prepareInternal();
150114819633470ebd5b346c13c2a82a0025d2d39c0Robert Shih    status_t setupMPEG4orWEBMRecording();
151114819633470ebd5b346c13c2a82a0025d2d39c0Robert Shih    void setupMPEG4orWEBMMetaData(sp<MetaData> *meta);
15272cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    status_t setupAMRRecording();
15372cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    status_t setupAACRecording();
15472cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    status_t setupRawAudioRecording();
15572cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    status_t setupRTPRecording();
15672cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    status_t setupMPEG2TSRecording();
157d008275796ac4cccf85fefce53cef733a49bc1faWonsik Kim    sp<MediaCodecSource> createAudioSource();
158d46a6b9fd8b2a4f9098757384711e2cd03a91651Eino-Ville Talvala    status_t checkVideoEncoderCapabilities();
15942dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    status_t checkAudioEncoderCapabilities();
160b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Generic MediaSource set-up. Returns the appropriate
161b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // source (CameraSource or SurfaceMediaSource)
162b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // depending on the videosource type
163b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    status_t setupMediaSource(sp<MediaSource> *mediaSource);
1649c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra    status_t setupCameraSource(sp<CameraSource> *cameraSource);
165934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setupAudioEncoder(const sp<MediaWriter>& writer);
16640d8899f60c5212af9d727ba0ffaaecf676ebd1dChih-Hung Hsieh    status_t setupVideoEncoder(const sp<MediaSource>& cameraSource, sp<MediaCodecSource> *source);
167934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong
168934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    // Encoding parameter handling utilities
169050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParameter(const String8 &key, const String8 &value);
170050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioEncodingBitRate(int32_t bitRate);
171050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioNumberOfChannels(int32_t channles);
172050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioSamplingRate(int32_t sampleRate);
1738f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamAudioTimeScale(int32_t timeScale);
17446d26dd29195450db15704e84d65740628a821fbChong Zhang    status_t setParamCaptureFpsEnable(int32_t timeLapseEnable);
17573c3e6363b31fb27882c4666453ad8ef050b3cc1Wonsik Kim    status_t setParamCaptureFps(double fps);
17693d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamVideoEncodingBitRate(int32_t bitRate);
1778f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamVideoIFramesInterval(int32_t seconds);
178145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    status_t setParamVideoEncoderProfile(int32_t profile);
179145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    status_t setParamVideoEncoderLevel(int32_t level);
18093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamVideoCameraId(int32_t cameraId);
1818f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamVideoTimeScale(int32_t timeScale);
18213f6284305e4b27395a23db7882d670bdb1bcae1James Dong    status_t setParamVideoRotation(int32_t degrees);
18393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamTrackTimeStatus(int64_t timeDurationUs);
18413aec890216948b0c364f8f92792129d0335f506James Dong    status_t setParamInterleaveDuration(int32_t durationUs);
1852dec2b5be2056c6d9428897dc672185872d30d17James Dong    status_t setParam64BitFileOffset(bool use64BitFileOffset);
186934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setParamMaxFileDurationUs(int64_t timeUs);
187934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setParamMaxFileSizeBytes(int64_t bytes);
1888f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamMovieTimeScale(int32_t timeScale);
189b832a03d563dd000faa44f2577e92237525edc32James Dong    status_t setParamGeoDataLongitude(int64_t longitudex10000);
190b832a03d563dd000faa44f2577e92237525edc32James Dong    status_t setParamGeoDataLatitude(int64_t latitudex10000);
19199c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoBitRate();
19299c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameRate();
19399c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameWidth();
19499c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameHeight();
19542dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipAudioBitRate();
19642dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipAudioSampleRate();
19742dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipNumberOfAudioChannels();
198d552b88515c6ccd18695e5db5e6032a6425d8c63James Dong    void setDefaultProfileIfNecessary();
19954ef1bae010f12dfe6a40ff4452695b1b11ff449Chong Zhang    void setDefaultVideoEncoderIfNecessary();
2002dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber
201b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi
20230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    StagefrightRecorder(const StagefrightRecorder &);
20330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    StagefrightRecorder &operator=(const StagefrightRecorder &);
20430ab66297501757d745b9ae10da61adcd891f497Andreas Huber};
20530ab66297501757d745b9ae10da61adcd891f497Andreas Huber
20630ab66297501757d745b9ae10da61adcd891f497Andreas Huber}  // namespace android
20730ab66297501757d745b9ae10da61adcd891f497Andreas Huber
20830ab66297501757d745b9ae10da61adcd891f497Andreas Huber#endif  // STAGEFRIGHT_RECORDER_H_
209