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
2130ab66297501757d745b9ae10da61adcd891f497Andreas Huber#include <media/MediaRecorderBase.h>
225f4d93467a03b3a38320416f8cc2c2c2c94bf3fcJames Dong#include <camera/CameraParameters.h>
2330ab66297501757d745b9ae10da61adcd891f497Andreas Huber#include <utils/String8.h>
2430ab66297501757d745b9ae10da61adcd891f497Andreas Huber
2564760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
26fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
2730ab66297501757d745b9ae10da61adcd891f497Andreas Hubernamespace android {
2830ab66297501757d745b9ae10da61adcd891f497Andreas Huber
29c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dongclass Camera;
304ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Liclass ICameraRecordingProxy;
319c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatraclass CameraSource;
3278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatraclass CameraSourceTimeLapse;
332dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huberstruct MediaSource;
342dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huberstruct MediaWriter;
359c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatraclass MetaData;
36d3d4e5069e1af0437c4f5a7b4ba344bda5b937afJames Dongstruct AudioSource;
3799c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dongclass MediaProfiles;
38b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketiclass ISurfaceTexture;
39b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketiclass SurfaceMediaSource;
4030ab66297501757d745b9ae10da61adcd891f497Andreas Huber
4130ab66297501757d745b9ae10da61adcd891f497Andreas Huberstruct StagefrightRecorder : public MediaRecorderBase {
4230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    StagefrightRecorder();
4330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual ~StagefrightRecorder();
4430ab66297501757d745b9ae10da61adcd891f497Andreas Huber
4530ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t init();
46fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    virtual status_t setAudioSource(audio_source_t as);
4730ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoSource(video_source vs);
4830ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setOutputFormat(output_format of);
4930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setAudioEncoder(audio_encoder ae);
5030ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoEncoder(video_encoder ve);
5130ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoSize(int width, int height);
5230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setVideoFrameRate(int frames_per_second);
534ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li    virtual status_t setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy);
544b79168835965cf0fc41ebe2a367e22b4cb20d08Jamie Gennis    virtual status_t setPreviewSurface(const sp<Surface>& surface);
5530ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setOutputFile(const char *path);
5630ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setOutputFile(int fd, int64_t offset, int64_t length);
5730ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t setParameters(const String8& params);
5837047fceba836f341d0108beed0991b0f8dfc543James Dong    virtual status_t setListener(const sp<IMediaRecorderClient>& listener);
5930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t prepare();
6030ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t start();
61a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong    virtual status_t pause();
6230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t stop();
6330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t close();
6430ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t reset();
6530ab66297501757d745b9ae10da61adcd891f497Andreas Huber    virtual status_t getMaxAmplitude(int *max);
66b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong    virtual status_t dump(int fd, const Vector<String16>& args) const;
67b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Querying a SurfaceMediaSourcer
68b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    virtual sp<ISurfaceTexture> querySurfaceMediaSource() const;
6930ab66297501757d745b9ae10da61adcd891f497Andreas Huber
7030ab66297501757d745b9ae10da61adcd891f497Andreas Huberprivate:
7154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong    sp<ICamera> mCamera;
724ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li    sp<ICameraRecordingProxy> mCameraProxy;
734b79168835965cf0fc41ebe2a367e22b4cb20d08Jamie Gennis    sp<Surface> mPreviewSurface;
7437047fceba836f341d0108beed0991b0f8dfc543James Dong    sp<IMediaRecorderClient> mListener;
753cecf640c4daf2df616b278bd9986018c8182908James Dong    sp<MediaWriter> mWriter;
763cecf640c4daf2df616b278bd9986018c8182908James Dong    int mOutputFd;
77d3d4e5069e1af0437c4f5a7b4ba344bda5b937afJames Dong    sp<AudioSource> mAudioSourceNode;
7830ab66297501757d745b9ae10da61adcd891f497Andreas Huber
79fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    audio_source_t mAudioSource;
8030ab66297501757d745b9ae10da61adcd891f497Andreas Huber    video_source mVideoSource;
8130ab66297501757d745b9ae10da61adcd891f497Andreas Huber    output_format mOutputFormat;
8230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    audio_encoder mAudioEncoder;
8330ab66297501757d745b9ae10da61adcd891f497Andreas Huber    video_encoder mVideoEncoder;
842dec2b5be2056c6d9428897dc672185872d30d17James Dong    bool mUse64BitFileOffset;
85050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mVideoWidth, mVideoHeight;
86050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mFrameRate;
873cecf640c4daf2df616b278bd9986018c8182908James Dong    int32_t mVideoBitRate;
88cfe88a20345dad981842b2c8092e4c704d3f98b4Nipun Kwatra    int32_t mAudioBitRate;
89050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mAudioChannels;
90050b28a593350047845a45a14cc5026221ac1620James Dong    int32_t mSampleRate;
9113aec890216948b0c364f8f92792129d0335f506James Dong    int32_t mInterleaveDurationUs;
928f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mIFramesIntervalSec;
9393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    int32_t mCameraId;
94145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    int32_t mVideoEncoderProfile;
95145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    int32_t mVideoEncoderLevel;
968f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mMovieTimeScale;
978f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mVideoTimeScale;
988f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    int32_t mAudioTimeScale;
99ef9d0cd72e476a4b7556833fb09505a51b626797James Dong    int64_t mMaxFileSizeBytes;
100ef9d0cd72e476a4b7556833fb09505a51b626797James Dong    int64_t mMaxFileDurationUs;
10193d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    int64_t mTrackEveryTimeDurationUs;
10213f6284305e4b27395a23db7882d670bdb1bcae1James Dong    int32_t mRotationDegrees;  // Clockwise
10307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong    int32_t mLatitudex10000;
10407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong    int32_t mLongitudex10000;
10586b7f47aa7482424cf8fd248f1315311919be3b0James Dong    int32_t mStartTimeOffsetMs;
106050b28a593350047845a45a14cc5026221ac1620James Dong
107fc20aab463f527ab3b0664986f0381a86b375884Nipun Kwatra    bool mCaptureTimeLapse;
108e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
10978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    sp<CameraSourceTimeLapse> mCameraSourceTimeLapse;
110fc20aab463f527ab3b0664986f0381a86b375884Nipun Kwatra
111b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi
11230ab66297501757d745b9ae10da61adcd891f497Andreas Huber    String8 mParams;
11330ab66297501757d745b9ae10da61adcd891f497Andreas Huber
1148480835b4bc1350646376aa7f3ae33742a7adeb1James Dong    bool mIsMetaDataStoredInVideoBuffers;
11599c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    MediaProfiles *mEncoderProfiles;
11699c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong
11775b0b5473c7b43f8c5972db7e6e8930988ead751Gloria Wang    bool mStarted;
118b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Needed when GLFrames are encoded.
119b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // An <ISurfaceTexture> pointer
120b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // will be sent to the client side using which the
121b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // frame buffers will be queued and dequeued
122b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    sp<SurfaceMediaSource> mSurfaceMediaSource;
12375b0b5473c7b43f8c5972db7e6e8930988ead751Gloria Wang
1249c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra    status_t setupMPEG4Recording(
125cfe88a20345dad981842b2c8092e4c704d3f98b4Nipun Kwatra        int outputFd,
126cfe88a20345dad981842b2c8092e4c704d3f98b4Nipun Kwatra        int32_t videoWidth, int32_t videoHeight,
127cfe88a20345dad981842b2c8092e4c704d3f98b4Nipun Kwatra        int32_t videoBitRate,
1289c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra        int32_t *totalBitRate,
1299c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra        sp<MediaWriter> *mediaWriter);
1309c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra    void setupMPEG4MetaData(int64_t startTimeUs, int32_t totalBitRate,
1319c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra        sp<MetaData> *meta);
1322dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber    status_t startMPEG4Recording();
1332dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber    status_t startAMRRecording();
134ef9d0cd72e476a4b7556833fb09505a51b626797James Dong    status_t startAACRecording();
135760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    status_t startRawAudioRecording();
13639ddf8e0f18766f7ba1e3246b774aa6ebd93eea8Andreas Huber    status_t startRTPRecording();
13759b7dc39ea8332d3418a599e51447d7edb612ac4Andreas Huber    status_t startMPEG2TSRecording();
138050b28a593350047845a45a14cc5026221ac1620James Dong    sp<MediaSource> createAudioSource();
13954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong    status_t checkVideoEncoderCapabilities();
14042dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    status_t checkAudioEncoderCapabilities();
141b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // Generic MediaSource set-up. Returns the appropriate
142b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // source (CameraSource or SurfaceMediaSource)
143b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // depending on the videosource type
144b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    status_t setupMediaSource(sp<MediaSource> *mediaSource);
1459c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra    status_t setupCameraSource(sp<CameraSource> *cameraSource);
146b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // setup the surfacemediasource for the encoder
147b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    status_t setupSurfaceMediaSource();
148b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi
149934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setupAudioEncoder(const sp<MediaWriter>& writer);
1509c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra    status_t setupVideoEncoder(
1519c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra            sp<MediaSource> cameraSource,
1529c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra            int32_t videoBitRate,
1539c075bca0b75093ca0514a3c8f74d73c8e9e83fdNipun Kwatra            sp<MediaSource> *source);
154934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong
155934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    // Encoding parameter handling utilities
156050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParameter(const String8 &key, const String8 &value);
157050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioEncodingBitRate(int32_t bitRate);
158050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioNumberOfChannels(int32_t channles);
159050b28a593350047845a45a14cc5026221ac1620James Dong    status_t setParamAudioSamplingRate(int32_t sampleRate);
1608f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamAudioTimeScale(int32_t timeScale);
161e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra    status_t setParamTimeLapseEnable(int32_t timeLapseEnable);
162e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra    status_t setParamTimeBetweenTimeLapseFrameCapture(int64_t timeUs);
16393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamVideoEncodingBitRate(int32_t bitRate);
1648f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamVideoIFramesInterval(int32_t seconds);
165145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    status_t setParamVideoEncoderProfile(int32_t profile);
166145bfe5eb3e08c9689c28f6bf3287a979438b04bJames Dong    status_t setParamVideoEncoderLevel(int32_t level);
16793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamVideoCameraId(int32_t cameraId);
1688f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamVideoTimeScale(int32_t timeScale);
16913f6284305e4b27395a23db7882d670bdb1bcae1James Dong    status_t setParamVideoRotation(int32_t degrees);
17093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong    status_t setParamTrackTimeStatus(int64_t timeDurationUs);
17113aec890216948b0c364f8f92792129d0335f506James Dong    status_t setParamInterleaveDuration(int32_t durationUs);
1722dec2b5be2056c6d9428897dc672185872d30d17James Dong    status_t setParam64BitFileOffset(bool use64BitFileOffset);
173934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setParamMaxFileDurationUs(int64_t timeUs);
174934da11c4cc598d3dd3b53726c362bcdcc0fd37fJames Dong    status_t setParamMaxFileSizeBytes(int64_t bytes);
1758f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong    status_t setParamMovieTimeScale(int32_t timeScale);
176b832a03d563dd000faa44f2577e92237525edc32James Dong    status_t setParamGeoDataLongitude(int64_t longitudex10000);
177b832a03d563dd000faa44f2577e92237525edc32James Dong    status_t setParamGeoDataLatitude(int64_t latitudex10000);
17899c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoBitRate();
17999c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameRate();
18099c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameWidth();
18199c2a076b4a46762a22bbb4dfbd51d107e0532d9James Dong    void clipVideoFrameHeight();
18242dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipAudioBitRate();
18342dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipAudioSampleRate();
18442dd1d5f186252a7f09f8fb1a46ea82e3877b2d3James Dong    void clipNumberOfAudioChannels();
185d552b88515c6ccd18695e5db5e6032a6425d8c63James Dong    void setDefaultProfileIfNecessary();
1862dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber
187b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi
18830ab66297501757d745b9ae10da61adcd891f497Andreas Huber    StagefrightRecorder(const StagefrightRecorder &);
18930ab66297501757d745b9ae10da61adcd891f497Andreas Huber    StagefrightRecorder &operator=(const StagefrightRecorder &);
19030ab66297501757d745b9ae10da61adcd891f497Andreas Huber};
19130ab66297501757d745b9ae10da61adcd891f497Andreas Huber
19230ab66297501757d745b9ae10da61adcd891f497Andreas Huber}  // namespace android
19330ab66297501757d745b9ae10da61adcd891f497Andreas Huber
19430ab66297501757d745b9ae10da61adcd891f497Andreas Huber#endif  // STAGEFRIGHT_RECORDER_H_
195