xref: /aosp_15_r20/frameworks/av/media/libmediaplayerservice/StagefrightRecorder.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2009 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  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "StagefrightRecorder"
19 #define ATRACE_TAG ATRACE_TAG_VIDEO
20 #include <utils/Trace.h>
21 #include <inttypes.h>
22 // TODO/workaround: including base logging now as it conflicts with ADebug.h
23 // and it must be included first.
24 #include <android-base/logging.h>
25 #include <utils/Log.h>
26 
27 #include <webm/WebmWriter.h>
28 
29 #include "StagefrightRecorder.h"
30 
31 #include <algorithm>
32 
33 #include <android-base/properties.h>
34 #include <android/hardware/ICamera.h>
35 
36 #include <binder/IPCThreadState.h>
37 #include <binder/IServiceManager.h>
38 
39 #include <media/AidlConversion.h>
40 #include <media/IMediaPlayerService.h>
41 #include <media/MediaMetricsItem.h>
42 #include <media/stagefright/foundation/ABuffer.h>
43 #include <media/stagefright/foundation/ADebug.h>
44 #include <media/stagefright/foundation/AMessage.h>
45 #include <media/stagefright/foundation/ALooper.h>
46 #include <media/stagefright/ACodec.h>
47 #include <media/stagefright/AudioSource.h>
48 #include <media/stagefright/AMRWriter.h>
49 #include <media/stagefright/AACWriter.h>
50 #include <media/stagefright/CameraSource.h>
51 #include <media/stagefright/CameraSourceTimeLapse.h>
52 #include <media/stagefright/MPEG2TSWriter.h>
53 #include <media/stagefright/MPEG4Writer.h>
54 #include <media/stagefright/MediaCodecConstants.h>
55 #include <media/stagefright/MediaDefs.h>
56 #include <media/stagefright/MetaData.h>
57 #include <media/stagefright/MediaCodecSource.h>
58 #include <media/stagefright/OggWriter.h>
59 #include <media/stagefright/PersistentSurface.h>
60 #include <media/MediaProfiles.h>
61 #include <camera/CameraParameters.h>
62 #include <gui/Flags.h>
63 
64 #include <utils/Errors.h>
65 #include <sys/types.h>
66 #include <ctype.h>
67 #include <unistd.h>
68 
69 #include <system/audio.h>
70 
71 #include <media/stagefright/rtsp/ARTPWriter.h>
72 #include <com_android_media_editing_flags.h>
73 
74 namespace android {
75 
76 static const float kTypicalDisplayRefreshingRate = 60.f;
77 // display refresh rate drops on battery saver
78 static const float kMinTypicalDisplayRefreshingRate = kTypicalDisplayRefreshingRate / 2;
79 static const int kMaxNumVideoTemporalLayers = 8;
80 
81 // key for media statistics
82 static const char *kKeyRecorder = "recorder";
83 // attrs for media statistics
84 // NB: these are matched with public Java API constants defined
85 // in frameworks/base/media/java/android/media/MediaRecorder.java
86 // These must be kept synchronized with the constants there.
87 static const char *kRecorderLogSessionId = "android.media.mediarecorder.log-session-id";
88 static const char *kRecorderAudioBitrate = "android.media.mediarecorder.audio-bitrate";
89 static const char *kRecorderAudioChannels = "android.media.mediarecorder.audio-channels";
90 static const char *kRecorderAudioSampleRate = "android.media.mediarecorder.audio-samplerate";
91 static const char *kRecorderAudioTimescale = "android.media.mediarecorder.audio-timescale";
92 static const char *kRecorderCaptureFps = "android.media.mediarecorder.capture-fps";
93 static const char *kRecorderCaptureFpsEnable = "android.media.mediarecorder.capture-fpsenable";
94 static const char *kRecorderFrameRate = "android.media.mediarecorder.frame-rate";
95 static const char *kRecorderHeight = "android.media.mediarecorder.height";
96 static const char *kRecorderMovieTimescale = "android.media.mediarecorder.movie-timescale";
97 static const char *kRecorderRotation = "android.media.mediarecorder.rotation";
98 static const char *kRecorderVideoBitrate = "android.media.mediarecorder.video-bitrate";
99 static const char *kRecorderVideoIframeInterval = "android.media.mediarecorder.video-iframe-interval";
100 static const char *kRecorderVideoLevel = "android.media.mediarecorder.video-encoder-level";
101 static const char *kRecorderVideoProfile = "android.media.mediarecorder.video-encoder-profile";
102 static const char *kRecorderVideoTimescale = "android.media.mediarecorder.video-timescale";
103 static const char *kRecorderWidth = "android.media.mediarecorder.width";
104 
105 // new fields, not yet frozen in the public Java API definitions
106 static const char *kRecorderAudioMime = "android.media.mediarecorder.audio.mime";
107 static const char *kRecorderVideoMime = "android.media.mediarecorder.video.mime";
108 static const char *kRecorderDurationMs = "android.media.mediarecorder.durationMs";
109 static const char *kRecorderPaused = "android.media.mediarecorder.pausedMs";
110 static const char *kRecorderNumPauses = "android.media.mediarecorder.NPauses";
111 
112 
113 // To collect the encoder usage for the battery app
addBatteryData(uint32_t params)114 static void addBatteryData(uint32_t params) {
115     sp<IBinder> binder =
116         defaultServiceManager()->waitForService(String16("media.player"));
117     sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
118     if (service.get() == nullptr) {
119         ALOGE("%s: Failed to get media.player service", __func__);
120         return;
121     }
122 
123     service->addBatteryData(params);
124 }
125 
126 
StagefrightRecorder(const AttributionSourceState & client)127 StagefrightRecorder::StagefrightRecorder(const AttributionSourceState& client)
128     : MediaRecorderBase(client),
129       mWriter(NULL),
130       mOutputFd(-1),
131       mAudioSource((audio_source_t)AUDIO_SOURCE_CNT), // initialize with invalid value
132       mPrivacySensitive(PRIVACY_SENSITIVE_DEFAULT),
133       mVideoSource(VIDEO_SOURCE_LIST_END),
134       mRTPCVOExtMap(-1),
135       mRTPCVODegrees(0),
136       mRTPSockDscp(0),
137       mRTPSockOptEcn(0),
138       mRTPSockNetwork(0),
139       mLastSeqNo(0),
140       mStarted(false),
141       mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
142       mDeviceCallbackEnabled(false),
143       mSelectedMicDirection(MIC_DIRECTION_UNSPECIFIED),
144       mSelectedMicFieldDimension(MIC_FIELD_DIMENSION_NORMAL) {
145 
146     ALOGV("Constructor");
147 
148     mMetricsItem = NULL;
149     mAnalyticsDirty = false;
150     reset();
151 }
152 
~StagefrightRecorder()153 StagefrightRecorder::~StagefrightRecorder() {
154     ALOGV("Destructor");
155     stop();
156 
157     if (mLooper != NULL) {
158         mLooper->stop();
159     }
160 
161     // log the current record, provided it has some information worth recording
162     // NB: this also reclaims & clears mMetricsItem.
163     flushAndResetMetrics(false);
164 }
165 
updateMetrics()166 void StagefrightRecorder::updateMetrics() {
167     ALOGV("updateMetrics");
168 
169     // we run as part of the media player service; what we really want to
170     // know is the app which requested the recording.
171     mMetricsItem->setUid(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(mAttributionSource.uid)));
172 
173     mMetricsItem->setCString(kRecorderLogSessionId, mLogSessionId.c_str());
174 
175     // populate the values from the raw fields.
176 
177     // TBD mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
178     // TBD mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
179     // TBD mVideoEncoder  = VIDEO_ENCODER_DEFAULT;
180     mMetricsItem->setInt32(kRecorderHeight, mVideoHeight);
181     mMetricsItem->setInt32(kRecorderWidth, mVideoWidth);
182     mMetricsItem->setInt32(kRecorderFrameRate, mFrameRate);
183     mMetricsItem->setInt32(kRecorderVideoBitrate, mVideoBitRate);
184     mMetricsItem->setInt32(kRecorderAudioSampleRate, mSampleRate);
185     mMetricsItem->setInt32(kRecorderAudioChannels, mAudioChannels);
186     mMetricsItem->setInt32(kRecorderAudioBitrate, mAudioBitRate);
187     // TBD mInterleaveDurationUs = 0;
188     mMetricsItem->setInt32(kRecorderVideoIframeInterval, mIFramesIntervalSec);
189     // TBD mAudioSourceNode = 0;
190     // TBD mUse64BitFileOffset = false;
191     if (mMovieTimeScale != -1)
192         mMetricsItem->setInt32(kRecorderMovieTimescale, mMovieTimeScale);
193     if (mAudioTimeScale != -1)
194         mMetricsItem->setInt32(kRecorderAudioTimescale, mAudioTimeScale);
195     if (mVideoTimeScale != -1)
196         mMetricsItem->setInt32(kRecorderVideoTimescale, mVideoTimeScale);
197     // TBD mCameraId        = 0;
198     // TBD mStartTimeOffsetMs = -1;
199     mMetricsItem->setInt32(kRecorderVideoProfile, mVideoEncoderProfile);
200     mMetricsItem->setInt32(kRecorderVideoLevel, mVideoEncoderLevel);
201     // TBD mMaxFileDurationUs = 0;
202     // TBD mMaxFileSizeBytes = 0;
203     // TBD mTrackEveryTimeDurationUs = 0;
204     mMetricsItem->setInt32(kRecorderCaptureFpsEnable, mCaptureFpsEnable);
205     mMetricsItem->setDouble(kRecorderCaptureFps, mCaptureFps);
206     // TBD mCameraSourceTimeLapse = NULL;
207     // TBD mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid;
208     // TBD mEncoderProfiles = MediaProfiles::getInstance();
209     mMetricsItem->setInt32(kRecorderRotation, mRotationDegrees);
210     // PII mLatitudex10000 = -3600000;
211     // PII mLongitudex10000 = -3600000;
212     // TBD mTotalBitRate = 0;
213 
214     // duration information (recorded, paused, # of pauses)
215     mMetricsItem->setInt64(kRecorderDurationMs, (mDurationRecordedUs+500)/1000 );
216     if (mNPauses != 0) {
217         mMetricsItem->setInt64(kRecorderPaused, (mDurationPausedUs+500)/1000 );
218         mMetricsItem->setInt32(kRecorderNumPauses, mNPauses);
219     }
220 }
221 
flushAndResetMetrics(bool reinitialize)222 void StagefrightRecorder::flushAndResetMetrics(bool reinitialize) {
223     ALOGV("flushAndResetMetrics");
224     // flush anything we have, maybe setup a new record
225     if (mMetricsItem != NULL) {
226         if (mAnalyticsDirty) {
227             updateMetrics();
228             if (mMetricsItem->count() > 0) {
229                 mMetricsItem->selfrecord();
230             }
231         }
232         delete mMetricsItem;
233         mMetricsItem = NULL;
234     }
235     mAnalyticsDirty = false;
236     if (reinitialize) {
237         mMetricsItem = mediametrics::Item::create(kKeyRecorder);
238     }
239 }
240 
init()241 status_t StagefrightRecorder::init() {
242     ALOGV("init");
243 
244     mLooper = new ALooper;
245     mLooper->setName("recorder_looper");
246     mLooper->start();
247 
248     return OK;
249 }
250 
251 // The client side of mediaserver asks it to create a SurfaceMediaSource
252 // and return a interface reference. The client side will use that
253 // while encoding GL Frames
querySurfaceMediaSource() const254 sp<IGraphicBufferProducer> StagefrightRecorder::querySurfaceMediaSource() const {
255     ALOGV("Get SurfaceMediaSource");
256     return mGraphicBufferProducer;
257 }
258 
setAudioSource(audio_source_t as)259 status_t StagefrightRecorder::setAudioSource(audio_source_t as) {
260     ALOGV("setAudioSource: %d", as);
261 
262     if (as == AUDIO_SOURCE_DEFAULT) {
263         mAudioSource = AUDIO_SOURCE_MIC;
264     } else {
265         mAudioSource = as;
266     }
267     // Reset privacy sensitive in case this is the second time audio source is set
268     mPrivacySensitive = PRIVACY_SENSITIVE_DEFAULT;
269     return OK;
270 }
271 
setPrivacySensitive(bool privacySensitive)272 status_t StagefrightRecorder::setPrivacySensitive(bool privacySensitive) {
273     // privacy sensitive cannot be set before audio source is set
274     if (mAudioSource == AUDIO_SOURCE_CNT) {
275         return INVALID_OPERATION;
276     }
277     mPrivacySensitive = privacySensitive ? PRIVACY_SENSITIVE_ENABLED : PRIVACY_SENSITIVE_DISABLED;
278     return OK;
279 }
280 
isPrivacySensitive(bool * privacySensitive) const281 status_t StagefrightRecorder::isPrivacySensitive(bool *privacySensitive) const {
282     *privacySensitive = false;
283     if (mAudioSource == AUDIO_SOURCE_CNT) {
284         return INVALID_OPERATION;
285     }
286     if (mPrivacySensitive == PRIVACY_SENSITIVE_DEFAULT) {
287          *privacySensitive = mAudioSource == AUDIO_SOURCE_VOICE_COMMUNICATION
288                 || mAudioSource == AUDIO_SOURCE_CAMCORDER;
289     } else {
290         *privacySensitive = mPrivacySensitive == PRIVACY_SENSITIVE_ENABLED;
291     }
292     return OK;
293 }
294 
setVideoSource(video_source vs)295 status_t StagefrightRecorder::setVideoSource(video_source vs) {
296     ALOGV("setVideoSource: %d", vs);
297     if (vs < VIDEO_SOURCE_DEFAULT ||
298         vs >= VIDEO_SOURCE_LIST_END) {
299         ALOGE("Invalid video source: %d", vs);
300         return BAD_VALUE;
301     }
302 
303     if (vs == VIDEO_SOURCE_DEFAULT) {
304         mVideoSource = VIDEO_SOURCE_CAMERA;
305     } else {
306         mVideoSource = vs;
307     }
308 
309     return OK;
310 }
311 
setOutputFormat(output_format of)312 status_t StagefrightRecorder::setOutputFormat(output_format of) {
313     ALOGV("setOutputFormat: %d", of);
314     if (of < OUTPUT_FORMAT_DEFAULT ||
315         of >= OUTPUT_FORMAT_LIST_END) {
316         ALOGE("Invalid output format: %d", of);
317         return BAD_VALUE;
318     }
319 
320     if (of == OUTPUT_FORMAT_DEFAULT) {
321         mOutputFormat = OUTPUT_FORMAT_THREE_GPP;
322     } else {
323         mOutputFormat = of;
324     }
325 
326     return OK;
327 }
328 
setAudioEncoder(audio_encoder ae)329 status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) {
330     ALOGV("setAudioEncoder: %d", ae);
331     if (ae < AUDIO_ENCODER_DEFAULT ||
332         ae >= AUDIO_ENCODER_LIST_END) {
333         ALOGE("Invalid audio encoder: %d", ae);
334         return BAD_VALUE;
335     }
336 
337     if (ae == AUDIO_ENCODER_DEFAULT) {
338         mAudioEncoder = AUDIO_ENCODER_AMR_NB;
339     } else {
340         mAudioEncoder = ae;
341     }
342 
343     return OK;
344 }
345 
setVideoEncoder(video_encoder ve)346 status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) {
347     ALOGV("setVideoEncoder: %d", ve);
348     if (ve < VIDEO_ENCODER_DEFAULT ||
349         ve >= VIDEO_ENCODER_LIST_END) {
350         ALOGE("Invalid video encoder: %d", ve);
351         return BAD_VALUE;
352     }
353 
354     mVideoEncoder = ve;
355 
356     return OK;
357 }
358 
setVideoSize(int width,int height)359 status_t StagefrightRecorder::setVideoSize(int width, int height) {
360     ALOGV("setVideoSize: %dx%d", width, height);
361     if (width <= 0 || height <= 0) {
362         ALOGE("Invalid video size: %dx%d", width, height);
363         return BAD_VALUE;
364     }
365 
366     // Additional check on the dimension will be performed later
367     mVideoWidth = width;
368     mVideoHeight = height;
369 
370     return OK;
371 }
372 
setVideoFrameRate(int frames_per_second)373 status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) {
374     ALOGV("setVideoFrameRate: %d", frames_per_second);
375     if ((frames_per_second <= 0 && frames_per_second != -1) ||
376         frames_per_second > kMaxHighSpeedFps) {
377         ALOGE("Invalid video frame rate: %d", frames_per_second);
378         return BAD_VALUE;
379     }
380 
381     // Additional check on the frame rate will be performed later
382     mFrameRate = frames_per_second;
383 
384     return OK;
385 }
386 
setCamera(const sp<hardware::ICamera> & camera,const sp<ICameraRecordingProxy> & proxy)387 status_t StagefrightRecorder::setCamera(const sp<hardware::ICamera> &camera,
388                                         const sp<ICameraRecordingProxy> &proxy) {
389     ALOGV("setCamera");
390     if (camera == 0) {
391         ALOGE("camera is NULL");
392         return BAD_VALUE;
393     }
394     if (proxy == 0) {
395         ALOGE("camera proxy is NULL");
396         return BAD_VALUE;
397     }
398 
399     mCamera = camera;
400     mCameraProxy = proxy;
401     return OK;
402 }
403 
setPreviewSurface(const sp<IGraphicBufferProducer> & surface)404 status_t StagefrightRecorder::setPreviewSurface(const sp<IGraphicBufferProducer> &surface) {
405     ALOGV("setPreviewSurface: %p", surface.get());
406     mPreviewSurface = surface;
407 
408     return OK;
409 }
410 
setInputSurface(const sp<PersistentSurface> & surface)411 status_t StagefrightRecorder::setInputSurface(
412         const sp<PersistentSurface>& surface) {
413     mPersistentSurface = surface;
414 
415     return OK;
416 }
417 
setOutputFile(int fd)418 status_t StagefrightRecorder::setOutputFile(int fd) {
419     ALOGV("setOutputFile: %d", fd);
420 
421     if (fd < 0) {
422         ALOGE("Invalid file descriptor: %d", fd);
423         return -EBADF;
424     }
425 
426     // start with a clean, empty file
427     ftruncate(fd, 0);
428 
429     if (mOutputFd >= 0) {
430         ::close(mOutputFd);
431     }
432     mOutputFd = dup(fd);
433 
434     return OK;
435 }
436 
setNextOutputFile(int fd)437 status_t StagefrightRecorder::setNextOutputFile(int fd) {
438     Mutex::Autolock autolock(mLock);
439     // Only support MPEG4
440     if (mOutputFormat != OUTPUT_FORMAT_MPEG_4) {
441         ALOGE("Only MP4 file format supports setting next output file");
442         return INVALID_OPERATION;
443     }
444     ALOGV("setNextOutputFile: %d", fd);
445 
446     if (fd < 0) {
447         ALOGE("Invalid file descriptor: %d", fd);
448         return -EBADF;
449     }
450 
451     if (mWriter == nullptr) {
452         ALOGE("setNextOutputFile failed. Writer has been freed");
453         return INVALID_OPERATION;
454     }
455 
456     // start with a clean, empty file
457     ftruncate(fd, 0);
458 
459     return mWriter->setNextFd(fd);
460 }
461 
462 // Attempt to parse an float literal optionally surrounded by whitespace,
463 // returns true on success, false otherwise.
safe_strtod(const char * s,double * val)464 static bool safe_strtod(const char *s, double *val) {
465     char *end;
466 
467     // It is lame, but according to man page, we have to set errno to 0
468     // before calling strtod().
469     errno = 0;
470     *val = strtod(s, &end);
471 
472     if (end == s || errno == ERANGE) {
473         return false;
474     }
475 
476     // Skip trailing whitespace
477     while (isspace(*end)) {
478         ++end;
479     }
480 
481     // For a successful return, the string must contain nothing but a valid
482     // float literal optionally surrounded by whitespace.
483 
484     return *end == '\0';
485 }
486 
487 // Attempt to parse an int64 literal optionally surrounded by whitespace,
488 // returns true on success, false otherwise.
safe_strtoi64(const char * s,int64_t * val)489 static bool safe_strtoi64(const char *s, int64_t *val) {
490     char *end;
491 
492     // It is lame, but according to man page, we have to set errno to 0
493     // before calling strtoll().
494     errno = 0;
495     *val = strtoll(s, &end, 10);
496 
497     if (end == s || errno == ERANGE) {
498         return false;
499     }
500 
501     // Skip trailing whitespace
502     while (isspace(*end)) {
503         ++end;
504     }
505 
506     // For a successful return, the string must contain nothing but a valid
507     // int64 literal optionally surrounded by whitespace.
508 
509     return *end == '\0';
510 }
511 
512 // Return true if the value is in [0, 0x007FFFFFFF]
safe_strtoi32(const char * s,int32_t * val)513 static bool safe_strtoi32(const char *s, int32_t *val) {
514     int64_t temp;
515     if (safe_strtoi64(s, &temp)) {
516         if (temp >= 0 && temp <= 0x007FFFFFFF) {
517             *val = static_cast<int32_t>(temp);
518             return true;
519         }
520     }
521     return false;
522 }
523 
524 // Trim both leading and trailing whitespace from the given string.
TrimString(String8 * s)525 static void TrimString(String8 *s) {
526     size_t num_bytes = s->bytes();
527     const char *data = s->c_str();
528 
529     size_t leading_space = 0;
530     while (leading_space < num_bytes && isspace(data[leading_space])) {
531         ++leading_space;
532     }
533 
534     size_t i = num_bytes;
535     while (i > leading_space && isspace(data[i - 1])) {
536         --i;
537     }
538 
539     *s = String8(&data[leading_space], i - leading_space);
540 }
541 
setParamAudioSamplingRate(int32_t sampleRate)542 status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) {
543     ALOGV("setParamAudioSamplingRate: %d", sampleRate);
544     if (sampleRate <= 0) {
545         ALOGE("Invalid audio sampling rate: %d", sampleRate);
546         return BAD_VALUE;
547     }
548 
549     // Additional check on the sample rate will be performed later.
550     mSampleRate = sampleRate;
551 
552     return OK;
553 }
554 
setParamAudioNumberOfChannels(int32_t channels)555 status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) {
556     ALOGV("setParamAudioNumberOfChannels: %d", channels);
557     if (channels <= 0 || channels >= 3) {
558         ALOGE("Invalid number of audio channels: %d", channels);
559         return BAD_VALUE;
560     }
561 
562     // Additional check on the number of channels will be performed later.
563     mAudioChannels = channels;
564 
565     return OK;
566 }
567 
setParamAudioEncodingBitRate(int32_t bitRate)568 status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) {
569     ALOGV("setParamAudioEncodingBitRate: %d", bitRate);
570     if (bitRate <= 0) {
571         ALOGE("Invalid audio encoding bit rate: %d", bitRate);
572         return BAD_VALUE;
573     }
574 
575     // The target bit rate may not be exactly the same as the requested.
576     // It depends on many factors, such as rate control, and the bit rate
577     // range that a specific encoder supports. The mismatch between the
578     // the target and requested bit rate will NOT be treated as an error.
579     mAudioBitRate = bitRate;
580     return OK;
581 }
582 
setParamVideoEncodingBitRate(int32_t bitRate)583 status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) {
584     ALOGV("setParamVideoEncodingBitRate: %d", bitRate);
585     if (bitRate <= 0) {
586         ALOGE("Invalid video encoding bit rate: %d", bitRate);
587         return BAD_VALUE;
588     }
589 
590     // The target bit rate may not be exactly the same as the requested.
591     // It depends on many factors, such as rate control, and the bit rate
592     // range that a specific encoder supports. The mismatch between the
593     // the target and requested bit rate will NOT be treated as an error.
594     mVideoBitRate = bitRate;
595 
596     // A new bitrate(TMMBR) should be applied on runtime as well if OutputFormat is RTP_AVP
597     if (mOutputFormat == OUTPUT_FORMAT_RTP_AVP) {
598         // Regular I frames may overload the network so we reduce the bitrate to allow
599         // margins for the I frame overruns.
600         // Still send requested bitrate (TMMBR) in the reply (TMMBN).
601         const float coefficient = 0.8f;
602         mVideoBitRate = (bitRate * coefficient) / 1000 * 1000;
603     }
604     if (mOutputFormat == OUTPUT_FORMAT_RTP_AVP && mStarted && mPauseStartTimeUs == 0) {
605         mVideoEncoderSource->setEncodingBitrate(mVideoBitRate);
606         ARTPWriter* rtpWriter  = static_cast<ARTPWriter*>(mWriter.get());
607         rtpWriter->setTMMBNInfo(mOpponentID, bitRate);
608     }
609 
610     return OK;
611 }
612 
setParamVideoBitRateMode(int32_t bitRateMode)613 status_t StagefrightRecorder::setParamVideoBitRateMode(int32_t bitRateMode) {
614     ALOGV("setParamVideoBitRateMode: %d", bitRateMode);
615     // TODO: clarify what bitrate mode of -1 is as these start from 0
616     if (bitRateMode < -1) {
617         ALOGE("Unsupported video bitrate mode: %d", bitRateMode);
618         return BAD_VALUE;
619     }
620     mVideoBitRateMode = bitRateMode;
621     return OK;
622 }
623 
624 // Always rotate clockwise, and only support 0, 90, 180 and 270 for now.
setParamVideoRotation(int32_t degrees)625 status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) {
626     ALOGV("setParamVideoRotation: %d", degrees);
627     if (degrees < 0 || degrees % 90 != 0) {
628         ALOGE("Unsupported video rotation angle: %d", degrees);
629         return BAD_VALUE;
630     }
631     mRotationDegrees = degrees % 360;
632     return OK;
633 }
634 
setParamMaxFileDurationUs(int64_t timeUs)635 status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) {
636     ALOGV("setParamMaxFileDurationUs: %lld us", (long long)timeUs);
637 
638     // This is meant for backward compatibility for MediaRecorder.java
639     if (timeUs <= 0) {
640         ALOGW("Max file duration is not positive: %lld us. Disabling duration limit.",
641                 (long long)timeUs);
642         timeUs = 0; // Disable the duration limit for zero or negative values.
643     } else if (timeUs <= 100000LL) {  // XXX: 100 milli-seconds
644         ALOGE("Max file duration is too short: %lld us", (long long)timeUs);
645         return BAD_VALUE;
646     }
647 
648     if (timeUs <= 15 * 1000000LL) {
649         ALOGW("Target duration (%lld us) too short to be respected", (long long)timeUs);
650     }
651     mMaxFileDurationUs = timeUs;
652     return OK;
653 }
654 
setParamMaxFileSizeBytes(int64_t bytes)655 status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) {
656     ALOGV("setParamMaxFileSizeBytes: %lld bytes", (long long)bytes);
657 
658     // This is meant for backward compatibility for MediaRecorder.java
659     if (bytes <= 0) {
660         ALOGW("Max file size is not positive: %lld bytes. "
661              "Disabling file size limit.", (long long)bytes);
662         bytes = 0; // Disable the file size limit for zero or negative values.
663     } else if (bytes <= 1024) {  // XXX: 1 kB
664         ALOGE("Max file size is too small: %lld bytes", (long long)bytes);
665         return BAD_VALUE;
666     }
667 
668     if (bytes <= 100 * 1024) {
669         ALOGW("Target file size (%lld bytes) is too small to be respected", (long long)bytes);
670     }
671 
672     mMaxFileSizeBytes = bytes;
673     return OK;
674 }
675 
setParamInterleaveDuration(int32_t durationUs)676 status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) {
677     ALOGV("setParamInterleaveDuration: %d", durationUs);
678     if (durationUs <= 500000) {           //  500 ms
679         // If interleave duration is too small, it is very inefficient to do
680         // interleaving since the metadata overhead will count for a significant
681         // portion of the saved contents
682         ALOGE("Audio/video interleave duration is too small: %d us", durationUs);
683         return BAD_VALUE;
684     } else if (durationUs >= 10000000) {  // 10 seconds
685         // If interleaving duration is too large, it can cause the recording
686         // session to use too much memory since we have to save the output
687         // data before we write them out
688         ALOGE("Audio/video interleave duration is too large: %d us", durationUs);
689         return BAD_VALUE;
690     }
691     mInterleaveDurationUs = durationUs;
692     return OK;
693 }
694 
695 // If seconds <  0, only the first frame is I frame, and rest are all P frames
696 // If seconds == 0, all frames are encoded as I frames. No P frames
697 // If seconds >  0, it is the time spacing (seconds) between 2 neighboring I frames
setParamVideoIFramesInterval(int32_t seconds)698 status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) {
699     ALOGV("setParamVideoIFramesInterval: %d seconds", seconds);
700     mIFramesIntervalSec = seconds;
701     return OK;
702 }
703 
setParam64BitFileOffset(bool use64Bit)704 status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) {
705     ALOGV("setParam64BitFileOffset: %s",
706         use64Bit? "use 64 bit file offset": "use 32 bit file offset");
707     mUse64BitFileOffset = use64Bit;
708     return OK;
709 }
710 
setParamVideoCameraId(int32_t cameraId)711 status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) {
712     ALOGV("setParamVideoCameraId: %d", cameraId);
713     if (cameraId < 0) {
714         return BAD_VALUE;
715     }
716     mCameraId = cameraId;
717     return OK;
718 }
719 
setParamTrackTimeStatus(int64_t timeDurationUs)720 status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) {
721     ALOGV("setParamTrackTimeStatus: %lld", (long long)timeDurationUs);
722     if (timeDurationUs < 20000) {  // Infeasible if shorter than 20 ms?
723         ALOGE("Tracking time duration too short: %lld us", (long long)timeDurationUs);
724         return BAD_VALUE;
725     }
726     mTrackEveryTimeDurationUs = timeDurationUs;
727     return OK;
728 }
729 
setParamVideoEncoderProfile(int32_t profile)730 status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) {
731     ALOGV("setParamVideoEncoderProfile: %d", profile);
732 
733     // Additional check will be done later when we load the encoder.
734     // For now, we are accepting values defined in OpenMAX IL.
735     mVideoEncoderProfile = profile;
736     return OK;
737 }
738 
setParamVideoEncoderLevel(int32_t level)739 status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) {
740     ALOGV("setParamVideoEncoderLevel: %d", level);
741 
742     // Additional check will be done later when we load the encoder.
743     // For now, we are accepting values defined in OpenMAX IL.
744     mVideoEncoderLevel = level;
745     return OK;
746 }
747 
setParamMovieTimeScale(int32_t timeScale)748 status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) {
749     ALOGV("setParamMovieTimeScale: %d", timeScale);
750 
751     // The range is set to be the same as the audio's time scale range
752     // since audio's time scale has a wider range.
753     if (timeScale < 600 || timeScale > 96000) {
754         ALOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale);
755         return BAD_VALUE;
756     }
757     mMovieTimeScale = timeScale;
758     return OK;
759 }
760 
setParamVideoTimeScale(int32_t timeScale)761 status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) {
762     ALOGV("setParamVideoTimeScale: %d", timeScale);
763 
764     // 60000 is chosen to make sure that each video frame from a 60-fps
765     // video has 1000 ticks.
766     if (timeScale < 600 || timeScale > 60000) {
767         ALOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale);
768         return BAD_VALUE;
769     }
770     mVideoTimeScale = timeScale;
771     return OK;
772 }
773 
setParamAudioTimeScale(int32_t timeScale)774 status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) {
775     ALOGV("setParamAudioTimeScale: %d", timeScale);
776 
777     // 96000 Hz is the highest sampling rate support in AAC.
778     if (timeScale < 600 || timeScale > 96000) {
779         ALOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale);
780         return BAD_VALUE;
781     }
782     mAudioTimeScale = timeScale;
783     return OK;
784 }
785 
setParamCaptureFpsEnable(int32_t captureFpsEnable)786 status_t StagefrightRecorder::setParamCaptureFpsEnable(int32_t captureFpsEnable) {
787     ALOGV("setParamCaptureFpsEnable: %d", captureFpsEnable);
788 
789     if(captureFpsEnable == 0) {
790         mCaptureFpsEnable = false;
791     } else if (captureFpsEnable == 1) {
792         mCaptureFpsEnable = true;
793     } else {
794         return BAD_VALUE;
795     }
796     return OK;
797 }
798 
setParamCaptureFps(double fps)799 status_t StagefrightRecorder::setParamCaptureFps(double fps) {
800     ALOGV("setParamCaptureFps: %.2f", fps);
801 
802     if (!(fps >= 1.0 / 86400)) {
803         ALOGE("FPS is too small");
804         return BAD_VALUE;
805     }
806     mCaptureFps = fps;
807     return OK;
808 }
809 
setParamGeoDataLongitude(int64_t longitudex10000)810 status_t StagefrightRecorder::setParamGeoDataLongitude(
811     int64_t longitudex10000) {
812 
813     if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
814         return BAD_VALUE;
815     }
816     mLongitudex10000 = longitudex10000;
817     return OK;
818 }
819 
setParamGeoDataLatitude(int64_t latitudex10000)820 status_t StagefrightRecorder::setParamGeoDataLatitude(
821     int64_t latitudex10000) {
822 
823     if (latitudex10000 > 900000 || latitudex10000 < -900000) {
824         return BAD_VALUE;
825     }
826     mLatitudex10000 = latitudex10000;
827     return OK;
828 }
829 
setParamRtpLocalIp(const String8 & localIp)830 status_t StagefrightRecorder::setParamRtpLocalIp(const String8 &localIp) {
831     ALOGV("setParamVideoLocalIp: %s", localIp.c_str());
832 
833     mLocalIp = localIp.c_str();
834     return OK;
835 }
836 
setParamRtpLocalPort(int32_t localPort)837 status_t StagefrightRecorder::setParamRtpLocalPort(int32_t localPort) {
838     ALOGV("setParamVideoLocalPort: %d", localPort);
839 
840     mLocalPort = localPort;
841     return OK;
842 }
843 
setParamRtpRemoteIp(const String8 & remoteIp)844 status_t StagefrightRecorder::setParamRtpRemoteIp(const String8 &remoteIp) {
845     ALOGV("setParamVideoRemoteIp: %s", remoteIp.c_str());
846 
847     mRemoteIp = remoteIp.c_str();
848     return OK;
849 }
850 
setParamRtpRemotePort(int32_t remotePort)851 status_t StagefrightRecorder::setParamRtpRemotePort(int32_t remotePort) {
852     ALOGV("setParamVideoRemotePort: %d", remotePort);
853 
854     mRemotePort = remotePort;
855     return OK;
856 }
857 
setParamSelfID(int32_t selfID)858 status_t StagefrightRecorder::setParamSelfID(int32_t selfID) {
859     ALOGV("setParamSelfID: %x", selfID);
860 
861     mSelfID = selfID;
862     return OK;
863 }
864 
setParamVideoOpponentID(int32_t opponentID)865 status_t StagefrightRecorder::setParamVideoOpponentID(int32_t opponentID) {
866     mOpponentID = opponentID;
867     return OK;
868 }
869 
setParamPayloadType(int32_t payloadType)870 status_t StagefrightRecorder::setParamPayloadType(int32_t payloadType) {
871     ALOGV("setParamPayloadType: %d", payloadType);
872 
873     mPayloadType = payloadType;
874 
875     if (mStarted && mOutputFormat == OUTPUT_FORMAT_RTP_AVP) {
876         mWriter->updatePayloadType(mPayloadType);
877     }
878 
879     return OK;
880 }
881 
setRTPCVOExtMap(int32_t extmap)882 status_t StagefrightRecorder::setRTPCVOExtMap(int32_t extmap) {
883     ALOGV("setRtpCvoExtMap: %d", extmap);
884 
885     mRTPCVOExtMap = extmap;
886     return OK;
887 }
888 
setRTPCVODegrees(int32_t cvoDegrees)889 status_t StagefrightRecorder::setRTPCVODegrees(int32_t cvoDegrees) {
890     Mutex::Autolock autolock(mLock);
891     ALOGV("setRtpCvoDegrees: %d", cvoDegrees);
892 
893     mRTPCVODegrees = cvoDegrees;
894 
895     if (mStarted && mOutputFormat == OUTPUT_FORMAT_RTP_AVP) {
896         mWriter->updateCVODegrees(mRTPCVODegrees);
897     }
898 
899     return OK;
900 }
901 
setParamRtpDscp(int32_t dscp)902 status_t StagefrightRecorder::setParamRtpDscp(int32_t dscp) {
903     ALOGV("setParamRtpDscp: %d", dscp);
904 
905     mRTPSockDscp = dscp;
906     return OK;
907 }
908 
setSocketNetwork(int64_t networkHandle)909 status_t StagefrightRecorder::setSocketNetwork(int64_t networkHandle) {
910     ALOGV("setSocketNetwork: %llu", (unsigned long long) networkHandle);
911 
912     mRTPSockNetwork = networkHandle;
913     if (mStarted && mOutputFormat == OUTPUT_FORMAT_RTP_AVP) {
914         mWriter->updateSocketNetwork(mRTPSockNetwork);
915     }
916     return OK;
917 }
918 
setParamRtpEcn(int32_t ecn)919 status_t StagefrightRecorder::setParamRtpEcn(int32_t ecn) {
920     ALOGV("setParamRtpEcn: %d", ecn);
921 
922     mRTPSockOptEcn = ecn;
923     return OK;
924 }
925 
requestIDRFrame()926 status_t StagefrightRecorder::requestIDRFrame() {
927     status_t ret = BAD_VALUE;
928     if (mVideoEncoderSource != NULL) {
929         ret = mVideoEncoderSource->requestIDRFrame();
930     } else {
931         ALOGV("requestIDRFrame: Encoder not ready");
932     }
933     return ret;
934 }
935 
setLogSessionId(const String8 & log_session_id)936 status_t StagefrightRecorder::setLogSessionId(const String8 &log_session_id) {
937     ALOGV("setLogSessionId: %s", log_session_id.c_str());
938 
939     // TODO: validity check that log_session_id is a 32-byte hex digit.
940     mLogSessionId = log_session_id.c_str();
941     return OK;
942 }
943 
setParameter(const String8 & key,const String8 & value)944 status_t StagefrightRecorder::setParameter(
945         const String8 &key, const String8 &value) {
946     ALOGV("setParameter: key (%s) => value (%s)", key.c_str(), value.c_str());
947     if (key == "max-duration") {
948         int64_t max_duration_ms;
949         if (safe_strtoi64(value.c_str(), &max_duration_ms)) {
950             return setParamMaxFileDurationUs(1000LL * max_duration_ms);
951         }
952     } else if (key == "max-filesize") {
953         int64_t max_filesize_bytes;
954         if (safe_strtoi64(value.c_str(), &max_filesize_bytes)) {
955             return setParamMaxFileSizeBytes(max_filesize_bytes);
956         }
957     } else if (key == "interleave-duration-us") {
958         int32_t durationUs;
959         if (safe_strtoi32(value.c_str(), &durationUs)) {
960             return setParamInterleaveDuration(durationUs);
961         }
962     } else if (key == "param-movie-time-scale") {
963         int32_t timeScale;
964         if (safe_strtoi32(value.c_str(), &timeScale)) {
965             return setParamMovieTimeScale(timeScale);
966         }
967     } else if (key == "param-use-64bit-offset") {
968         int32_t use64BitOffset;
969         if (safe_strtoi32(value.c_str(), &use64BitOffset)) {
970             return setParam64BitFileOffset(use64BitOffset != 0);
971         }
972     } else if (key == "param-geotag-longitude") {
973         int64_t longitudex10000;
974         if (safe_strtoi64(value.c_str(), &longitudex10000)) {
975             return setParamGeoDataLongitude(longitudex10000);
976         }
977     } else if (key == "param-geotag-latitude") {
978         int64_t latitudex10000;
979         if (safe_strtoi64(value.c_str(), &latitudex10000)) {
980             return setParamGeoDataLatitude(latitudex10000);
981         }
982     } else if (key == "param-track-time-status") {
983         int64_t timeDurationUs;
984         if (safe_strtoi64(value.c_str(), &timeDurationUs)) {
985             return setParamTrackTimeStatus(timeDurationUs);
986         }
987     } else if (key == "audio-param-sampling-rate") {
988         int32_t sampling_rate;
989         if (safe_strtoi32(value.c_str(), &sampling_rate)) {
990             return setParamAudioSamplingRate(sampling_rate);
991         }
992     } else if (key == "audio-param-number-of-channels") {
993         int32_t number_of_channels;
994         if (safe_strtoi32(value.c_str(), &number_of_channels)) {
995             return setParamAudioNumberOfChannels(number_of_channels);
996         }
997     } else if (key == "audio-param-encoding-bitrate") {
998         int32_t audio_bitrate;
999         if (safe_strtoi32(value.c_str(), &audio_bitrate)) {
1000             return setParamAudioEncodingBitRate(audio_bitrate);
1001         }
1002     } else if (key == "audio-param-time-scale") {
1003         int32_t timeScale;
1004         if (safe_strtoi32(value.c_str(), &timeScale)) {
1005             return setParamAudioTimeScale(timeScale);
1006         }
1007     } else if (key == "video-param-encoding-bitrate") {
1008         int32_t video_bitrate;
1009         if (safe_strtoi32(value.c_str(), &video_bitrate)) {
1010             return setParamVideoEncodingBitRate(video_bitrate);
1011         }
1012     } else if (key == "video-param-bitrate-mode") {
1013         int32_t video_bitrate_mode;
1014         if (safe_strtoi32(value.c_str(), &video_bitrate_mode)) {
1015             return setParamVideoBitRateMode(video_bitrate_mode);
1016         }
1017     } else if (key == "video-param-rotation-angle-degrees") {
1018         int32_t degrees;
1019         if (safe_strtoi32(value.c_str(), &degrees)) {
1020             return setParamVideoRotation(degrees);
1021         }
1022     } else if (key == "video-param-i-frames-interval") {
1023         int32_t seconds;
1024         if (safe_strtoi32(value.c_str(), &seconds)) {
1025             return setParamVideoIFramesInterval(seconds);
1026         }
1027     } else if (key == "video-param-encoder-profile") {
1028         int32_t profile;
1029         if (safe_strtoi32(value.c_str(), &profile)) {
1030             return setParamVideoEncoderProfile(profile);
1031         }
1032     } else if (key == "video-param-encoder-level") {
1033         int32_t level;
1034         if (safe_strtoi32(value.c_str(), &level)) {
1035             return setParamVideoEncoderLevel(level);
1036         }
1037     } else if (key == "video-param-camera-id") {
1038         int32_t cameraId;
1039         if (safe_strtoi32(value.c_str(), &cameraId)) {
1040             return setParamVideoCameraId(cameraId);
1041         }
1042     } else if (key == "video-param-time-scale") {
1043         int32_t timeScale;
1044         if (safe_strtoi32(value.c_str(), &timeScale)) {
1045             return setParamVideoTimeScale(timeScale);
1046         }
1047     } else if (key == "time-lapse-enable") {
1048         int32_t captureFpsEnable;
1049         if (safe_strtoi32(value.c_str(), &captureFpsEnable)) {
1050             return setParamCaptureFpsEnable(captureFpsEnable);
1051         }
1052     } else if (key == "time-lapse-fps") {
1053         double fps;
1054         if (safe_strtod(value.c_str(), &fps)) {
1055             return setParamCaptureFps(fps);
1056         }
1057     } else if (key == "rtp-param-local-ip") {
1058         return setParamRtpLocalIp(value);
1059     } else if (key == "rtp-param-local-port") {
1060         int32_t localPort;
1061         if (safe_strtoi32(value.c_str(), &localPort)) {
1062             return setParamRtpLocalPort(localPort);
1063         }
1064     } else if (key == "rtp-param-remote-ip") {
1065         return setParamRtpRemoteIp(value);
1066     } else if (key == "rtp-param-remote-port") {
1067         int32_t remotePort;
1068         if (safe_strtoi32(value.c_str(), &remotePort)) {
1069             return setParamRtpRemotePort(remotePort);
1070         }
1071     } else if (key == "rtp-param-self-id") {
1072         int32_t selfID;
1073         int64_t temp;
1074         if (safe_strtoi64(value.c_str(), &temp)) {
1075             selfID = static_cast<int32_t>(temp);
1076             return setParamSelfID(selfID);
1077         }
1078     } else if (key == "rtp-param-opponent-id") {
1079         int32_t opnId;
1080         int64_t temp;
1081         if (safe_strtoi64(value.c_str(), &temp)) {
1082             opnId = static_cast<int32_t>(temp);
1083             return setParamVideoOpponentID(opnId);
1084         }
1085     } else if (key == "rtp-param-payload-type") {
1086         int32_t payloadType;
1087         if (safe_strtoi32(value.c_str(), &payloadType)) {
1088             return setParamPayloadType(payloadType);
1089         }
1090     } else if (key == "rtp-param-ext-cvo-extmap") {
1091         int32_t extmap;
1092         if (safe_strtoi32(value.c_str(), &extmap)) {
1093             return setRTPCVOExtMap(extmap);
1094         }
1095     } else if (key == "rtp-param-ext-cvo-degrees") {
1096         int32_t degrees;
1097         if (safe_strtoi32(value.c_str(), &degrees)) {
1098             return setRTPCVODegrees(degrees);
1099         }
1100     } else if (key == "video-param-request-i-frame") {
1101         return requestIDRFrame();
1102     } else if (key == "rtp-param-set-socket-dscp") {
1103         int32_t dscp;
1104         if (safe_strtoi32(value.c_str(), &dscp)) {
1105             return setParamRtpDscp(dscp);
1106         }
1107     } else if (key == "rtp-param-set-socket-ecn") {
1108         int32_t targetEcn;
1109         if (safe_strtoi32(value.c_str(), &targetEcn)) {
1110             return setParamRtpEcn(targetEcn);
1111         }
1112     } else if (key == "rtp-param-set-socket-network") {
1113         int64_t networkHandle;
1114         if (safe_strtoi64(value.c_str(), &networkHandle)) {
1115             return setSocketNetwork(networkHandle);
1116         }
1117     } else if (key == "log-session-id") {
1118         return setLogSessionId(value);
1119     } else {
1120         ALOGE("setParameter: failed to find key %s", key.c_str());
1121     }
1122     return BAD_VALUE;
1123 }
1124 
setParameters(const String8 & params)1125 status_t StagefrightRecorder::setParameters(const String8 &params) {
1126     ALOGV("setParameters: %s", params.c_str());
1127     const char *cparams = params.c_str();
1128     const char *key_start = cparams;
1129     for (;;) {
1130         const char *equal_pos = strchr(key_start, '=');
1131         if (equal_pos == NULL) {
1132             ALOGE("Parameters %s miss a value", cparams);
1133             return BAD_VALUE;
1134         }
1135         String8 key(key_start, equal_pos - key_start);
1136         TrimString(&key);
1137         if (key.length() == 0) {
1138             ALOGE("Parameters %s contains an empty key", cparams);
1139             return BAD_VALUE;
1140         }
1141         const char *value_start = equal_pos + 1;
1142         const char *semicolon_pos = strchr(value_start, ';');
1143         String8 value;
1144         if (semicolon_pos == NULL) {
1145             value = value_start;
1146         } else {
1147             value = String8(value_start, semicolon_pos - value_start);
1148         }
1149         if (setParameter(key, value) != OK) {
1150             return BAD_VALUE;
1151         }
1152         if (semicolon_pos == NULL) {
1153             break;  // Reaches the end
1154         }
1155         key_start = semicolon_pos + 1;
1156     }
1157     return OK;
1158 }
1159 
setListener(const sp<IMediaRecorderClient> & listener)1160 status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) {
1161     mListener = listener;
1162 
1163     return OK;
1164 }
1165 
setClientName(const String16 & clientName)1166 status_t StagefrightRecorder::setClientName(const String16& clientName) {
1167 
1168     mAttributionSource.packageName = VALUE_OR_RETURN_STATUS(
1169             legacy2aidl_String16_string(clientName));
1170 
1171     return OK;
1172 }
1173 
prepareInternal()1174 status_t StagefrightRecorder::prepareInternal() {
1175     ALOGV("prepare");
1176     if (mOutputFd < 0) {
1177         ALOGE("Output file descriptor is invalid");
1178         return INVALID_OPERATION;
1179     }
1180 
1181     status_t status = OK;
1182 
1183     switch (mOutputFormat) {
1184         case OUTPUT_FORMAT_DEFAULT:
1185         case OUTPUT_FORMAT_THREE_GPP:
1186         case OUTPUT_FORMAT_MPEG_4:
1187         case OUTPUT_FORMAT_WEBM:
1188             status = setupMPEG4orWEBMRecording();
1189             break;
1190 
1191         case OUTPUT_FORMAT_AMR_NB:
1192         case OUTPUT_FORMAT_AMR_WB:
1193             status = setupAMRRecording();
1194             break;
1195 
1196         case OUTPUT_FORMAT_AAC_ADIF:
1197         case OUTPUT_FORMAT_AAC_ADTS:
1198             status = setupAACRecording();
1199             break;
1200 
1201         case OUTPUT_FORMAT_RTP_AVP:
1202             status = setupRTPRecording();
1203             break;
1204 
1205         case OUTPUT_FORMAT_MPEG2TS:
1206             status = setupMPEG2TSRecording();
1207             break;
1208 
1209         case OUTPUT_FORMAT_OGG:
1210             status = setupOggRecording();
1211             break;
1212 
1213         default:
1214             ALOGE("Unsupported output file format: %d", mOutputFormat);
1215             status = UNKNOWN_ERROR;
1216             break;
1217     }
1218 
1219     ALOGV("Recording frameRate: %d captureFps: %f",
1220             mFrameRate, mCaptureFps);
1221 
1222     return status;
1223 }
1224 
prepare()1225 status_t StagefrightRecorder::prepare() {
1226     ALOGV("prepare");
1227     Mutex::Autolock autolock(mLock);
1228     if (mVideoSource == VIDEO_SOURCE_SURFACE) {
1229         return prepareInternal();
1230     }
1231     return OK;
1232 }
1233 
start()1234 status_t StagefrightRecorder::start() {
1235     ALOGV("start");
1236     Mutex::Autolock autolock(mLock);
1237     if (mOutputFd < 0) {
1238         ALOGE("Output file descriptor is invalid");
1239         return INVALID_OPERATION;
1240     }
1241 
1242     status_t status = OK;
1243 
1244     if (mVideoSource != VIDEO_SOURCE_SURFACE) {
1245         status = prepareInternal();
1246         if (status != OK) {
1247             return status;
1248         }
1249     }
1250 
1251     if (mWriter == NULL) {
1252         ALOGE("File writer is not avaialble");
1253         return UNKNOWN_ERROR;
1254     }
1255 
1256     switch (mOutputFormat) {
1257         case OUTPUT_FORMAT_DEFAULT:
1258         case OUTPUT_FORMAT_THREE_GPP:
1259         case OUTPUT_FORMAT_MPEG_4:
1260         case OUTPUT_FORMAT_WEBM:
1261         {
1262             sp<MetaData> meta = new MetaData;
1263             setupMPEG4orWEBMMetaData(&meta);
1264             status = mWriter->start(meta.get());
1265             break;
1266         }
1267 
1268         case OUTPUT_FORMAT_AMR_NB:
1269         case OUTPUT_FORMAT_AMR_WB:
1270         case OUTPUT_FORMAT_AAC_ADIF:
1271         case OUTPUT_FORMAT_AAC_ADTS:
1272         case OUTPUT_FORMAT_RTP_AVP:
1273         case OUTPUT_FORMAT_MPEG2TS:
1274         case OUTPUT_FORMAT_OGG:
1275         {
1276             sp<MetaData> meta = new MetaData;
1277             int64_t startTimeUs = systemTime() / 1000;
1278             meta->setInt64(kKeyTime, startTimeUs);
1279             meta->setInt32(kKeySelfID, mSelfID);
1280             meta->setInt32(kKeyPayloadType, mPayloadType);
1281             meta->setInt64(kKeySocketNetwork, mRTPSockNetwork);
1282             if (mRTPCVOExtMap > 0) {
1283                 meta->setInt32(kKeyRtpExtMap, mRTPCVOExtMap);
1284                 meta->setInt32(kKeyRtpCvoDegrees, mRTPCVODegrees);
1285             }
1286             if (mRTPSockDscp > 0) {
1287                 meta->setInt32(kKeyRtpDscp, mRTPSockDscp);
1288             }
1289             if (mRTPSockOptEcn > 0) {
1290                 meta->setInt32(kKeyRtpEcn, mRTPSockOptEcn);
1291             }
1292 
1293             status = mWriter->start(meta.get());
1294             break;
1295         }
1296 
1297         default:
1298         {
1299             ALOGE("Unsupported output file format: %d", mOutputFormat);
1300             status = UNKNOWN_ERROR;
1301             break;
1302         }
1303     }
1304 
1305     if (status != OK) {
1306         mWriter.clear();
1307         mWriter = NULL;
1308     }
1309 
1310     if ((status == OK) && (!mStarted)) {
1311         mAnalyticsDirty = true;
1312         mStarted = true;
1313 
1314         mStartedRecordingUs = systemTime() / 1000;
1315 
1316         uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted;
1317         if (mAudioSource != AUDIO_SOURCE_CNT) {
1318             params |= IMediaPlayerService::kBatteryDataTrackAudio;
1319         }
1320         if (mVideoSource != VIDEO_SOURCE_LIST_END) {
1321             params |= IMediaPlayerService::kBatteryDataTrackVideo;
1322         }
1323 
1324         addBatteryData(params);
1325     }
1326 
1327     return status;
1328 }
1329 
createAudioSource()1330 sp<MediaCodecSource> StagefrightRecorder::createAudioSource() {
1331     int32_t sourceSampleRate = mSampleRate;
1332 
1333     if (mCaptureFpsEnable && mCaptureFps >= mFrameRate) {
1334         // Upscale the sample rate for slow motion recording.
1335         // Fail audio source creation if source sample rate is too high, as it could
1336         // cause out-of-memory due to large input buffer size. And audio recording
1337         // probably doesn't make sense in the scenario, since the slow-down factor
1338         // is probably huge (eg. mSampleRate=48K, mCaptureFps=240, mFrameRate=1).
1339         const static int32_t kSampleRateHzMax = 192000;
1340         sourceSampleRate =
1341                 (mSampleRate * mCaptureFps + mFrameRate / 2) / mFrameRate;
1342         if (sourceSampleRate < mSampleRate || sourceSampleRate > kSampleRateHzMax) {
1343             ALOGE("source sample rate out of range! "
1344                     "(mSampleRate %d, mCaptureFps %.2f, mFrameRate %d",
1345                     mSampleRate, mCaptureFps, mFrameRate);
1346             return NULL;
1347         }
1348     }
1349 
1350     audio_attributes_t attr = AUDIO_ATTRIBUTES_INITIALIZER;
1351     attr.source = mAudioSource;
1352     // attr.flags AUDIO_FLAG_CAPTURE_PRIVATE is cleared by default
1353     if (mPrivacySensitive == PRIVACY_SENSITIVE_DEFAULT) {
1354         if (attr.source == AUDIO_SOURCE_VOICE_COMMUNICATION
1355                 || attr.source == AUDIO_SOURCE_CAMCORDER) {
1356             attr.flags = static_cast<audio_flags_mask_t>(attr.flags | AUDIO_FLAG_CAPTURE_PRIVATE);
1357             mPrivacySensitive = PRIVACY_SENSITIVE_ENABLED;
1358         } else {
1359             mPrivacySensitive = PRIVACY_SENSITIVE_DISABLED;
1360         }
1361     } else {
1362         if (mAudioSource == AUDIO_SOURCE_REMOTE_SUBMIX
1363                 || mAudioSource == AUDIO_SOURCE_FM_TUNER
1364                 || mAudioSource == AUDIO_SOURCE_VOICE_DOWNLINK
1365                 || mAudioSource == AUDIO_SOURCE_VOICE_UPLINK
1366                 || mAudioSource == AUDIO_SOURCE_VOICE_CALL
1367                 || mAudioSource == AUDIO_SOURCE_ECHO_REFERENCE) {
1368             ALOGE("Cannot request private capture with source: %d", mAudioSource);
1369             return NULL;
1370         }
1371         if (mPrivacySensitive == PRIVACY_SENSITIVE_ENABLED) {
1372             attr.flags = static_cast<audio_flags_mask_t>(attr.flags | AUDIO_FLAG_CAPTURE_PRIVATE);
1373         }
1374     }
1375 
1376     sp<AudioSource> audioSource =
1377         new AudioSource(
1378                 &attr,
1379                 mAttributionSource,
1380                 sourceSampleRate,
1381                 mAudioChannels,
1382                 mSampleRate,
1383                 mSelectedDeviceId,
1384                 mSelectedMicDirection,
1385                 mSelectedMicFieldDimension);
1386 
1387     status_t err = audioSource->initCheck();
1388 
1389     if (err != OK) {
1390         ALOGE("audio source is not initialized");
1391         return NULL;
1392     }
1393 
1394     sp<AMessage> format = new AMessage;
1395     switch (mAudioEncoder) {
1396         case AUDIO_ENCODER_AMR_NB:
1397         case AUDIO_ENCODER_DEFAULT:
1398             format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_NB);
1399             break;
1400         case AUDIO_ENCODER_AMR_WB:
1401             format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_WB);
1402             break;
1403         case AUDIO_ENCODER_AAC:
1404             format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1405             format->setInt32("aac-profile", OMX_AUDIO_AACObjectLC);
1406             break;
1407         case AUDIO_ENCODER_HE_AAC:
1408             format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1409             format->setInt32("aac-profile", OMX_AUDIO_AACObjectHE);
1410             break;
1411         case AUDIO_ENCODER_AAC_ELD:
1412             format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1413             format->setInt32("aac-profile", OMX_AUDIO_AACObjectELD);
1414             break;
1415         case AUDIO_ENCODER_OPUS:
1416             format->setString("mime", MEDIA_MIMETYPE_AUDIO_OPUS);
1417             break;
1418 
1419         default:
1420             ALOGE("Unknown audio encoder: %d", mAudioEncoder);
1421             return NULL;
1422     }
1423 
1424     // log audio mime type for media metrics
1425     if (mMetricsItem != NULL) {
1426         AString audiomime;
1427         if (format->findString("mime", &audiomime)) {
1428             mMetricsItem->setCString(kRecorderAudioMime, audiomime.c_str());
1429         }
1430     }
1431 
1432     int32_t maxInputSize;
1433     CHECK(audioSource->getFormat()->findInt32(
1434                 kKeyMaxInputSize, &maxInputSize));
1435 
1436     format->setInt32("max-input-size", maxInputSize);
1437     format->setInt32("channel-count", mAudioChannels);
1438     format->setInt32("sample-rate", mSampleRate);
1439     format->setInt32("bitrate", mAudioBitRate);
1440     if (mAudioTimeScale > 0) {
1441         format->setInt32("time-scale", mAudioTimeScale);
1442     }
1443     format->setInt32("priority", 0 /* realtime */);
1444 
1445     sp<MediaCodecSource> audioEncoder =
1446             MediaCodecSource::Create(mLooper, format, audioSource);
1447     sp<AudioSystem::AudioDeviceCallback> callback = mAudioDeviceCallback.promote();
1448     if (mDeviceCallbackEnabled && callback != 0) {
1449         audioSource->addAudioDeviceCallback(callback);
1450     }
1451     mAudioSourceNode = audioSource;
1452 
1453     if (audioEncoder == NULL) {
1454         ALOGE("Failed to create audio encoder");
1455     }
1456 
1457     return audioEncoder;
1458 }
1459 
setupAACRecording()1460 status_t StagefrightRecorder::setupAACRecording() {
1461     // TODO(b/324512842): Add support for OUTPUT_FORMAT_AAC_ADIF
1462     if (mOutputFormat != OUTPUT_FORMAT_AAC_ADTS) {
1463         ALOGE("Invalid output format %d used for AAC recording", mOutputFormat);
1464         return BAD_VALUE;
1465     }
1466 
1467     if (mAudioEncoder != AUDIO_ENCODER_AAC
1468             && mAudioEncoder != AUDIO_ENCODER_HE_AAC
1469             && mAudioEncoder != AUDIO_ENCODER_AAC_ELD) {
1470         ALOGE("Invalid encoder %d used for AAC recording", mAudioEncoder);
1471         return BAD_VALUE;
1472     }
1473 
1474     if (mAudioSource == AUDIO_SOURCE_CNT) {
1475         ALOGE("Audio source hasn't been set correctly");
1476         return BAD_VALUE;
1477     }
1478 
1479     mWriter = new AACWriter(mOutputFd);
1480     return setupRawAudioRecording();
1481 }
1482 
setupOggRecording()1483 status_t StagefrightRecorder::setupOggRecording() {
1484     if (mOutputFormat != OUTPUT_FORMAT_OGG) {
1485         ALOGE("Invalid output format %d used for OGG recording", mOutputFormat);
1486         return BAD_VALUE;
1487     }
1488 
1489     mWriter = new OggWriter(mOutputFd);
1490     return setupRawAudioRecording();
1491 }
1492 
setupAMRRecording()1493 status_t StagefrightRecorder::setupAMRRecording() {
1494     if (mOutputFormat != OUTPUT_FORMAT_AMR_NB
1495             && mOutputFormat != OUTPUT_FORMAT_AMR_WB) {
1496         ALOGE("Invalid output format %d used for AMR recording", mOutputFormat);
1497         return BAD_VALUE;
1498     }
1499 
1500     if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) {
1501         if (mAudioEncoder != AUDIO_ENCODER_DEFAULT &&
1502             mAudioEncoder != AUDIO_ENCODER_AMR_NB) {
1503             ALOGE("Invalid encoder %d used for AMRNB recording",
1504                     mAudioEncoder);
1505             return BAD_VALUE;
1506         }
1507     } else {  // mOutputFormat must be OUTPUT_FORMAT_AMR_WB
1508         if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) {
1509             ALOGE("Invlaid encoder %d used for AMRWB recording",
1510                     mAudioEncoder);
1511             return BAD_VALUE;
1512         }
1513     }
1514 
1515     mWriter = new AMRWriter(mOutputFd);
1516     return setupRawAudioRecording();
1517 }
1518 
setupRawAudioRecording()1519 status_t StagefrightRecorder::setupRawAudioRecording() {
1520     if (mAudioSource >= AUDIO_SOURCE_CNT && mAudioSource != AUDIO_SOURCE_FM_TUNER) {
1521         ALOGE("Invalid audio source: %d", mAudioSource);
1522         return BAD_VALUE;
1523     }
1524 
1525     status_t status = BAD_VALUE;
1526     if (OK != (status = checkAudioEncoderCapabilities())) {
1527         return status;
1528     }
1529 
1530     sp<MediaCodecSource> audioEncoder = createAudioSource();
1531     if (audioEncoder == NULL) {
1532         return UNKNOWN_ERROR;
1533     }
1534 
1535     CHECK(mWriter != 0);
1536     mWriter->addSource(audioEncoder);
1537     mAudioEncoderSource = audioEncoder;
1538 
1539     if (mMaxFileDurationUs != 0) {
1540         mWriter->setMaxFileDuration(mMaxFileDurationUs);
1541     }
1542     if (mMaxFileSizeBytes != 0) {
1543         mWriter->setMaxFileSize(mMaxFileSizeBytes);
1544     }
1545     mWriter->setListener(mListener);
1546 
1547     return OK;
1548 }
1549 
setupRTPRecording()1550 status_t StagefrightRecorder::setupRTPRecording() {
1551     if (mOutputFormat != OUTPUT_FORMAT_RTP_AVP) {
1552         ALOGE("Invalid output format %d used for RTP recording", mOutputFormat);
1553         return BAD_VALUE;
1554     }
1555 
1556     if ((mAudioSource != AUDIO_SOURCE_CNT
1557                 && mVideoSource != VIDEO_SOURCE_LIST_END)
1558             || (mAudioSource == AUDIO_SOURCE_CNT
1559                 && mVideoSource == VIDEO_SOURCE_LIST_END)) {
1560         // Must have exactly one source.
1561         return BAD_VALUE;
1562     }
1563 
1564     if (mOutputFd < 0) {
1565         return BAD_VALUE;
1566     }
1567 
1568     sp<MediaCodecSource> source;
1569 
1570     if (mAudioSource != AUDIO_SOURCE_CNT) {
1571         source = createAudioSource();
1572         mAudioEncoderSource = source;
1573     } else {
1574         setDefaultVideoEncoderIfNecessary();
1575 
1576         sp<MediaSource> mediaSource;
1577         status_t err = setupMediaSource(&mediaSource);
1578         if (err != OK) {
1579             return err;
1580         }
1581 
1582         err = setupVideoEncoder(mediaSource, &source);
1583         if (err != OK) {
1584             return err;
1585         }
1586         mVideoEncoderSource = source;
1587     }
1588 
1589     mWriter = new ARTPWriter(mOutputFd, mLocalIp, mLocalPort, mRemoteIp, mRemotePort, mLastSeqNo);
1590     mWriter->addSource(source);
1591     mWriter->setListener(mListener);
1592 
1593     return OK;
1594 }
1595 
setupMPEG2TSRecording()1596 status_t StagefrightRecorder::setupMPEG2TSRecording() {
1597     if (mOutputFormat != OUTPUT_FORMAT_MPEG2TS) {
1598         ALOGE("Invalid output format %d used for MPEG2TS recording", mOutputFormat);
1599         return BAD_VALUE;
1600     }
1601 
1602     sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd);
1603 
1604     if (mAudioSource != AUDIO_SOURCE_CNT) {
1605         if (mAudioEncoder != AUDIO_ENCODER_AAC &&
1606             mAudioEncoder != AUDIO_ENCODER_HE_AAC &&
1607             mAudioEncoder != AUDIO_ENCODER_AAC_ELD) {
1608             return ERROR_UNSUPPORTED;
1609         }
1610 
1611         status_t err = setupAudioEncoder(writer);
1612 
1613         if (err != OK) {
1614             return err;
1615         }
1616     }
1617 
1618     if (mVideoSource < VIDEO_SOURCE_LIST_END) {
1619         if (mVideoEncoder != VIDEO_ENCODER_H264) {
1620             ALOGE("MPEG2TS recording only supports H.264 encoding!");
1621             return ERROR_UNSUPPORTED;
1622         }
1623 
1624         sp<MediaSource> mediaSource;
1625         status_t err = setupMediaSource(&mediaSource);
1626         if (err != OK) {
1627             return err;
1628         }
1629 
1630         sp<MediaCodecSource> encoder;
1631         err = setupVideoEncoder(mediaSource, &encoder);
1632 
1633         if (err != OK) {
1634             return err;
1635         }
1636 
1637         writer->addSource(encoder);
1638         mVideoEncoderSource = encoder;
1639     }
1640 
1641     if (mMaxFileDurationUs != 0) {
1642         writer->setMaxFileDuration(mMaxFileDurationUs);
1643     }
1644 
1645     if (mMaxFileSizeBytes != 0) {
1646         writer->setMaxFileSize(mMaxFileSizeBytes);
1647     }
1648 
1649     mWriter = writer;
1650 
1651     return OK;
1652 }
1653 
clipVideoFrameRate()1654 void StagefrightRecorder::clipVideoFrameRate() {
1655     ALOGV("clipVideoFrameRate: encoder %d", mVideoEncoder);
1656     if (mFrameRate == -1) {
1657         mFrameRate = mEncoderProfiles->getCamcorderProfileParamByName(
1658                 "vid.fps", mCameraId, CAMCORDER_QUALITY_LOW);
1659         ALOGW("Using default video fps %d", mFrameRate);
1660     }
1661 
1662     int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
1663                         "enc.vid.fps.min", mVideoEncoder);
1664     int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
1665                         "enc.vid.fps.max", mVideoEncoder);
1666     if (mFrameRate < minFrameRate && minFrameRate != -1) {
1667         ALOGW("Intended video encoding frame rate (%d fps) is too small"
1668              " and will be set to (%d fps)", mFrameRate, minFrameRate);
1669         mFrameRate = minFrameRate;
1670     } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) {
1671         ALOGW("Intended video encoding frame rate (%d fps) is too large"
1672              " and will be set to (%d fps)", mFrameRate, maxFrameRate);
1673         mFrameRate = maxFrameRate;
1674     }
1675 }
1676 
clipVideoBitRate()1677 void StagefrightRecorder::clipVideoBitRate() {
1678     ALOGV("clipVideoBitRate: encoder %d", mVideoEncoder);
1679     int minBitRate = mEncoderProfiles->getVideoEncoderParamByName(
1680                         "enc.vid.bps.min", mVideoEncoder);
1681     int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName(
1682                         "enc.vid.bps.max", mVideoEncoder);
1683     if (mVideoBitRate < minBitRate && minBitRate != -1) {
1684         ALOGW("Intended video encoding bit rate (%d bps) is too small"
1685              " and will be set to (%d bps)", mVideoBitRate, minBitRate);
1686         mVideoBitRate = minBitRate;
1687     } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) {
1688         ALOGW("Intended video encoding bit rate (%d bps) is too large"
1689              " and will be set to (%d bps)", mVideoBitRate, maxBitRate);
1690         mVideoBitRate = maxBitRate;
1691     }
1692 }
1693 
clipVideoFrameWidth()1694 void StagefrightRecorder::clipVideoFrameWidth() {
1695     ALOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder);
1696     int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
1697                         "enc.vid.width.min", mVideoEncoder);
1698     int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
1699                         "enc.vid.width.max", mVideoEncoder);
1700     if (mVideoWidth < minFrameWidth && minFrameWidth != -1) {
1701         ALOGW("Intended video encoding frame width (%d) is too small"
1702              " and will be set to (%d)", mVideoWidth, minFrameWidth);
1703         mVideoWidth = minFrameWidth;
1704     } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) {
1705         ALOGW("Intended video encoding frame width (%d) is too large"
1706              " and will be set to (%d)", mVideoWidth, maxFrameWidth);
1707         mVideoWidth = maxFrameWidth;
1708     }
1709 }
1710 
checkVideoEncoderCapabilities()1711 status_t StagefrightRecorder::checkVideoEncoderCapabilities() {
1712     if (!mCaptureFpsEnable) {
1713         // Dont clip for time lapse capture as encoder will have enough
1714         // time to encode because of slow capture rate of time lapse.
1715         clipVideoBitRate();
1716         clipVideoFrameRate();
1717         clipVideoFrameWidth();
1718         clipVideoFrameHeight();
1719         setDefaultProfileIfNecessary();
1720     }
1721     return OK;
1722 }
1723 
1724 // Set to use AVC baseline profile if the encoding parameters matches
1725 // CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service.
setDefaultProfileIfNecessary()1726 void StagefrightRecorder::setDefaultProfileIfNecessary() {
1727     ALOGV("setDefaultProfileIfNecessary");
1728 
1729     camcorder_quality quality = CAMCORDER_QUALITY_LOW;
1730 
1731     int64_t durationUs   = mEncoderProfiles->getCamcorderProfileParamByName(
1732                                 "duration", mCameraId, quality) * 1000000LL;
1733 
1734     int fileFormat       = mEncoderProfiles->getCamcorderProfileParamByName(
1735                                 "file.format", mCameraId, quality);
1736 
1737     int videoCodec       = mEncoderProfiles->getCamcorderProfileParamByName(
1738                                 "vid.codec", mCameraId, quality);
1739 
1740     int videoBitRate     = mEncoderProfiles->getCamcorderProfileParamByName(
1741                                 "vid.bps", mCameraId, quality);
1742 
1743     int videoFrameRate   = mEncoderProfiles->getCamcorderProfileParamByName(
1744                                 "vid.fps", mCameraId, quality);
1745 
1746     int videoFrameWidth  = mEncoderProfiles->getCamcorderProfileParamByName(
1747                                 "vid.width", mCameraId, quality);
1748 
1749     int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName(
1750                                 "vid.height", mCameraId, quality);
1751 
1752     int audioCodec       = mEncoderProfiles->getCamcorderProfileParamByName(
1753                                 "aud.codec", mCameraId, quality);
1754 
1755     int audioBitRate     = mEncoderProfiles->getCamcorderProfileParamByName(
1756                                 "aud.bps", mCameraId, quality);
1757 
1758     int audioSampleRate  = mEncoderProfiles->getCamcorderProfileParamByName(
1759                                 "aud.hz", mCameraId, quality);
1760 
1761     int audioChannels    = mEncoderProfiles->getCamcorderProfileParamByName(
1762                                 "aud.ch", mCameraId, quality);
1763 
1764     if (durationUs == mMaxFileDurationUs &&
1765         fileFormat == mOutputFormat &&
1766         videoCodec == mVideoEncoder &&
1767         videoBitRate == mVideoBitRate &&
1768         videoFrameRate == mFrameRate &&
1769         videoFrameWidth == mVideoWidth &&
1770         videoFrameHeight == mVideoHeight &&
1771         audioCodec == mAudioEncoder &&
1772         audioBitRate == mAudioBitRate &&
1773         audioSampleRate == mSampleRate &&
1774         audioChannels == mAudioChannels) {
1775         if (videoCodec == VIDEO_ENCODER_H264) {
1776             ALOGI("Force to use AVC baseline profile");
1777             setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline);
1778             // set 0 for invalid levels - this will be rejected by the
1779             // codec if it cannot handle it during configure
1780             setParamVideoEncoderLevel(ACodec::getAVCLevelFor(
1781                     videoFrameWidth, videoFrameHeight, videoFrameRate, videoBitRate));
1782         }
1783     }
1784 }
1785 
setDefaultVideoEncoderIfNecessary()1786 void StagefrightRecorder::setDefaultVideoEncoderIfNecessary() {
1787     if (mVideoEncoder == VIDEO_ENCODER_DEFAULT) {
1788         if (mOutputFormat == OUTPUT_FORMAT_WEBM) {
1789             // default to VP8 for WEBM recording
1790             mVideoEncoder = VIDEO_ENCODER_VP8;
1791         } else {
1792             // pick the default encoder for CAMCORDER_QUALITY_LOW
1793             int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName(
1794                     "vid.codec", mCameraId, CAMCORDER_QUALITY_LOW);
1795 
1796             if (videoCodec > VIDEO_ENCODER_DEFAULT &&
1797                 videoCodec < VIDEO_ENCODER_LIST_END) {
1798                 mVideoEncoder = (video_encoder)videoCodec;
1799             } else {
1800                 // default to H.264 if camcorder profile not available
1801                 mVideoEncoder = VIDEO_ENCODER_H264;
1802             }
1803         }
1804     }
1805 }
1806 
checkAudioEncoderCapabilities()1807 status_t StagefrightRecorder::checkAudioEncoderCapabilities() {
1808     clipAudioBitRate();
1809     clipAudioSampleRate();
1810     clipNumberOfAudioChannels();
1811     return OK;
1812 }
1813 
clipAudioBitRate()1814 void StagefrightRecorder::clipAudioBitRate() {
1815     ALOGV("clipAudioBitRate: encoder %d", mAudioEncoder);
1816 
1817     int minAudioBitRate =
1818             mEncoderProfiles->getAudioEncoderParamByName(
1819                 "enc.aud.bps.min", mAudioEncoder);
1820     if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) {
1821         ALOGW("Intended audio encoding bit rate (%d) is too small"
1822             " and will be set to (%d)", mAudioBitRate, minAudioBitRate);
1823         mAudioBitRate = minAudioBitRate;
1824     }
1825 
1826     int maxAudioBitRate =
1827             mEncoderProfiles->getAudioEncoderParamByName(
1828                 "enc.aud.bps.max", mAudioEncoder);
1829     if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) {
1830         ALOGW("Intended audio encoding bit rate (%d) is too large"
1831             " and will be set to (%d)", mAudioBitRate, maxAudioBitRate);
1832         mAudioBitRate = maxAudioBitRate;
1833     }
1834 }
1835 
clipAudioSampleRate()1836 void StagefrightRecorder::clipAudioSampleRate() {
1837     ALOGV("clipAudioSampleRate: encoder %d", mAudioEncoder);
1838 
1839     int minSampleRate =
1840             mEncoderProfiles->getAudioEncoderParamByName(
1841                 "enc.aud.hz.min", mAudioEncoder);
1842     if (minSampleRate != -1 && mSampleRate < minSampleRate) {
1843         ALOGW("Intended audio sample rate (%d) is too small"
1844             " and will be set to (%d)", mSampleRate, minSampleRate);
1845         mSampleRate = minSampleRate;
1846     }
1847 
1848     int maxSampleRate =
1849             mEncoderProfiles->getAudioEncoderParamByName(
1850                 "enc.aud.hz.max", mAudioEncoder);
1851     if (maxSampleRate != -1 && mSampleRate > maxSampleRate) {
1852         ALOGW("Intended audio sample rate (%d) is too large"
1853             " and will be set to (%d)", mSampleRate, maxSampleRate);
1854         mSampleRate = maxSampleRate;
1855     }
1856 }
1857 
clipNumberOfAudioChannels()1858 void StagefrightRecorder::clipNumberOfAudioChannels() {
1859     ALOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder);
1860 
1861     int minChannels =
1862             mEncoderProfiles->getAudioEncoderParamByName(
1863                 "enc.aud.ch.min", mAudioEncoder);
1864     if (minChannels != -1 && mAudioChannels < minChannels) {
1865         ALOGW("Intended number of audio channels (%d) is too small"
1866             " and will be set to (%d)", mAudioChannels, minChannels);
1867         mAudioChannels = minChannels;
1868     }
1869 
1870     int maxChannels =
1871             mEncoderProfiles->getAudioEncoderParamByName(
1872                 "enc.aud.ch.max", mAudioEncoder);
1873     if (maxChannels != -1 && mAudioChannels > maxChannels) {
1874         ALOGW("Intended number of audio channels (%d) is too large"
1875             " and will be set to (%d)", mAudioChannels, maxChannels);
1876         mAudioChannels = maxChannels;
1877     }
1878 }
1879 
clipVideoFrameHeight()1880 void StagefrightRecorder::clipVideoFrameHeight() {
1881     ALOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder);
1882     int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
1883                         "enc.vid.height.min", mVideoEncoder);
1884     int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
1885                         "enc.vid.height.max", mVideoEncoder);
1886     if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) {
1887         ALOGW("Intended video encoding frame height (%d) is too small"
1888              " and will be set to (%d)", mVideoHeight, minFrameHeight);
1889         mVideoHeight = minFrameHeight;
1890     } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) {
1891         ALOGW("Intended video encoding frame height (%d) is too large"
1892              " and will be set to (%d)", mVideoHeight, maxFrameHeight);
1893         mVideoHeight = maxFrameHeight;
1894     }
1895 }
1896 
1897 // Set up the appropriate MediaSource depending on the chosen option
setupMediaSource(sp<MediaSource> * mediaSource)1898 status_t StagefrightRecorder::setupMediaSource(
1899                       sp<MediaSource> *mediaSource) {
1900     ATRACE_CALL();
1901     if (mVideoSource == VIDEO_SOURCE_DEFAULT
1902             || mVideoSource == VIDEO_SOURCE_CAMERA) {
1903         sp<CameraSource> cameraSource;
1904         status_t err = setupCameraSource(&cameraSource);
1905         if (err != OK) {
1906             return err;
1907         }
1908         *mediaSource = cameraSource;
1909     } else if (mVideoSource == VIDEO_SOURCE_SURFACE) {
1910         *mediaSource = NULL;
1911     } else {
1912         return INVALID_OPERATION;
1913     }
1914     return OK;
1915 }
1916 
setupCameraSource(sp<CameraSource> * cameraSource)1917 status_t StagefrightRecorder::setupCameraSource(
1918         sp<CameraSource> *cameraSource) {
1919     status_t err = OK;
1920     if ((err = checkVideoEncoderCapabilities()) != OK) {
1921         return err;
1922     }
1923     Size videoSize;
1924     videoSize.width = mVideoWidth;
1925     videoSize.height = mVideoHeight;
1926     uid_t uid = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_uid_t(mAttributionSource.uid));
1927     pid_t pid = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_pid_t(mAttributionSource.pid));
1928     String16 clientName = VALUE_OR_RETURN_STATUS(
1929         aidl2legacy_string_view_String16(mAttributionSource.packageName.value_or("")));
1930     if (mCaptureFpsEnable) {
1931         if (!(mCaptureFps > 0.)) {
1932             ALOGE("Invalid mCaptureFps value: %lf", mCaptureFps);
1933             return BAD_VALUE;
1934         }
1935 
1936 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
1937         sp<Surface> surface = new Surface(mPreviewSurface);
1938         mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera(
1939                 mCamera, mCameraProxy, mCameraId, clientName, uid, pid,
1940                 videoSize, mFrameRate, surface,
1941                 std::llround(1e6 / mCaptureFps));
1942 #else
1943         mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera(
1944                 mCamera, mCameraProxy, mCameraId, clientName, uid, pid,
1945                 videoSize, mFrameRate, mPreviewSurface,
1946                 std::llround(1e6 / mCaptureFps));
1947 #endif
1948         *cameraSource = mCameraSourceTimeLapse;
1949     } else {
1950 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
1951         sp<Surface> surface = new Surface(mPreviewSurface);
1952         *cameraSource = CameraSource::CreateFromCamera(
1953                 mCamera, mCameraProxy, mCameraId, clientName, uid, pid,
1954                 videoSize, mFrameRate,
1955                 surface);
1956 #else
1957         *cameraSource = CameraSource::CreateFromCamera(
1958                 mCamera, mCameraProxy, mCameraId, clientName, uid, pid,
1959                 videoSize, mFrameRate,
1960                 mPreviewSurface);
1961 #endif
1962     }
1963     mCamera.clear();
1964     mCameraProxy.clear();
1965     if (*cameraSource == NULL) {
1966         return UNKNOWN_ERROR;
1967     }
1968 
1969     if ((*cameraSource)->initCheck() != OK) {
1970         (*cameraSource).clear();
1971         *cameraSource = NULL;
1972         return NO_INIT;
1973     }
1974 
1975     // When frame rate is not set, the actual frame rate will be set to
1976     // the current frame rate being used.
1977     if (mFrameRate == -1) {
1978         int32_t frameRate = 0;
1979         CHECK ((*cameraSource)->getFormat()->findInt32(
1980                     kKeyFrameRate, &frameRate));
1981         ALOGI("Frame rate is not explicitly set. Use the current frame "
1982              "rate (%d fps)", frameRate);
1983         mFrameRate = frameRate;
1984     }
1985 
1986     CHECK(mFrameRate != -1);
1987 
1988     mMetaDataStoredInVideoBuffers =
1989         (*cameraSource)->metaDataStoredInVideoBuffers();
1990 
1991     return OK;
1992 }
1993 
setupVideoEncoder(const sp<MediaSource> & cameraSource,sp<MediaCodecSource> * source)1994 status_t StagefrightRecorder::setupVideoEncoder(
1995         const sp<MediaSource> &cameraSource,
1996         sp<MediaCodecSource> *source) {
1997     ATRACE_CALL();
1998     source->clear();
1999 
2000     sp<AMessage> format = new AMessage();
2001 
2002     switch (mVideoEncoder) {
2003         case VIDEO_ENCODER_H263:
2004             format->setString("mime", MEDIA_MIMETYPE_VIDEO_H263);
2005             break;
2006 
2007         case VIDEO_ENCODER_MPEG_4_SP:
2008             format->setString("mime", MEDIA_MIMETYPE_VIDEO_MPEG4);
2009             break;
2010 
2011         case VIDEO_ENCODER_H264:
2012             format->setString("mime", MEDIA_MIMETYPE_VIDEO_AVC);
2013             break;
2014 
2015         case VIDEO_ENCODER_VP8:
2016             format->setString("mime", MEDIA_MIMETYPE_VIDEO_VP8);
2017             break;
2018 
2019         case VIDEO_ENCODER_HEVC:
2020             format->setString("mime", MEDIA_MIMETYPE_VIDEO_HEVC);
2021             break;
2022 
2023         case VIDEO_ENCODER_DOLBY_VISION:
2024             format->setString("mime", MEDIA_MIMETYPE_VIDEO_DOLBY_VISION);
2025             break;
2026 
2027         case VIDEO_ENCODER_AV1:
2028             format->setString("mime", MEDIA_MIMETYPE_VIDEO_AV1);
2029             break;
2030 
2031         default:
2032             CHECK(!"Should not be here, unsupported video encoding.");
2033             break;
2034     }
2035 
2036     // log video mime type for media metrics
2037     if (mMetricsItem != NULL) {
2038         AString videomime;
2039         if (format->findString("mime", &videomime)) {
2040             mMetricsItem->setCString(kRecorderVideoMime, videomime.c_str());
2041         }
2042     }
2043 
2044     if (cameraSource != NULL) {
2045         sp<MetaData> meta = cameraSource->getFormat();
2046 
2047         int32_t width, height, stride, sliceHeight, colorFormat;
2048         CHECK(meta->findInt32(kKeyWidth, &width));
2049         CHECK(meta->findInt32(kKeyHeight, &height));
2050         CHECK(meta->findInt32(kKeyStride, &stride));
2051         CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight));
2052         CHECK(meta->findInt32(kKeyColorFormat, &colorFormat));
2053 
2054         format->setInt32("width", width);
2055         format->setInt32("height", height);
2056         format->setInt32("stride", stride);
2057         format->setInt32("slice-height", sliceHeight);
2058         format->setInt32("color-format", colorFormat);
2059     } else {
2060         format->setInt32("width", mVideoWidth);
2061         format->setInt32("height", mVideoHeight);
2062         format->setInt32("stride", mVideoWidth);
2063         format->setInt32("slice-height", mVideoHeight);
2064         format->setInt32("color-format", OMX_COLOR_FormatAndroidOpaque);
2065 
2066         // set up time lapse/slow motion for surface source
2067         if (mCaptureFpsEnable) {
2068             if (!(mCaptureFps > 0.)) {
2069                 ALOGE("Invalid mCaptureFps value: %lf", mCaptureFps);
2070                 return BAD_VALUE;
2071             }
2072             format->setDouble("time-lapse-fps", mCaptureFps);
2073         }
2074     }
2075 
2076     if (mOutputFormat == OUTPUT_FORMAT_RTP_AVP) {
2077         // This indicates that a raw image provided to encoder needs to be rotated.
2078         format->setInt32("rotation-degrees", mRotationDegrees);
2079     }
2080 
2081     format->setInt32("bitrate", mVideoBitRate);
2082     format->setInt32("bitrate-mode", mVideoBitRateMode);
2083     format->setInt32("frame-rate", mFrameRate);
2084     format->setInt32("i-frame-interval", mIFramesIntervalSec);
2085 
2086     if (mVideoTimeScale > 0) {
2087         format->setInt32("time-scale", mVideoTimeScale);
2088     }
2089     if (mVideoEncoderProfile != -1) {
2090         format->setInt32("profile", mVideoEncoderProfile);
2091     }
2092     if (mVideoEncoderLevel != -1) {
2093         format->setInt32("level", mVideoEncoderLevel);
2094     }
2095 
2096     uint32_t tsLayers = 1;
2097     bool preferBFrames = true; // we like B-frames as it produces better quality per bitrate
2098     format->setInt32("priority", 0 /* realtime */);
2099     float maxPlaybackFps = mFrameRate; // assume video is only played back at normal speed
2100 
2101     if (mCaptureFpsEnable) {
2102         format->setFloat("operating-rate", mCaptureFps);
2103 
2104         // enable layering for all time lapse and high frame rate recordings
2105         if (mFrameRate / mCaptureFps >= 1.9) { // time lapse
2106             preferBFrames = false;
2107             tsLayers = 2; // use at least two layers as resulting video will likely be sped up
2108         } else if (mCaptureFps > maxPlaybackFps) { // slow-mo
2109             maxPlaybackFps = mCaptureFps; // assume video will be played back at full capture speed
2110             preferBFrames = false;
2111         }
2112     }
2113 
2114     // Enable temporal layering if the expected (max) playback frame rate is greater than ~11% of
2115     // the minimum display refresh rate on a typical device. Add layers until the base layer falls
2116     // under this limit. Allow device manufacturers to override this limit.
2117 
2118     // TODO: make this configurable by the application
2119     std::string maxBaseLayerFpsProperty =
2120         ::android::base::GetProperty("ro.media.recorder-max-base-layer-fps", "");
2121     float maxBaseLayerFps = (float)::atof(maxBaseLayerFpsProperty.c_str());
2122     // TRICKY: use !> to fix up any NaN values
2123     if (!(maxBaseLayerFps >= kMinTypicalDisplayRefreshingRate / 0.9)) {
2124         maxBaseLayerFps = kMinTypicalDisplayRefreshingRate / 0.9;
2125     }
2126 
2127     for (uint32_t tryLayers = 1; tryLayers <= kMaxNumVideoTemporalLayers; ++tryLayers) {
2128         if (tryLayers > tsLayers) {
2129             tsLayers = tryLayers;
2130         }
2131         // keep going until the base layer fps falls below the typical display refresh rate
2132         float baseLayerFps = maxPlaybackFps / (1 << (tryLayers - 1));
2133         if (baseLayerFps < maxBaseLayerFps) {
2134             break;
2135         }
2136     }
2137 
2138     if (tsLayers > 1) {
2139         uint32_t bLayers = std::min(2u, tsLayers - 1); // use up-to 2 B-layers
2140         // TODO(b/341121900): Remove this once B frames are handled correctly in screen recorder
2141         // use case in case of mic only
2142         if (!com::android::media::editing::flags::stagefrightrecorder_enable_b_frames()
2143                 && mAudioSource == AUDIO_SOURCE_MIC && mVideoSource == VIDEO_SOURCE_SURFACE) {
2144             bLayers = 0;
2145         }
2146         uint32_t pLayers = tsLayers - bLayers;
2147         format->setString(
2148                 "ts-schema", AStringPrintf("android.generic.%u+%u", pLayers, bLayers));
2149 
2150         // TODO: some encoders do not support B-frames with temporal layering, and we have a
2151         // different preference based on use-case. We could move this into camera profiles.
2152         format->setInt32("android._prefer-b-frames", preferBFrames);
2153     }
2154 
2155     if (mMetaDataStoredInVideoBuffers != kMetadataBufferTypeInvalid) {
2156         format->setInt32("android._input-metadata-buffer-type", mMetaDataStoredInVideoBuffers);
2157     }
2158 
2159     uint32_t flags = 0;
2160     if (cameraSource == NULL) {
2161         flags |= MediaCodecSource::FLAG_USE_SURFACE_INPUT;
2162     } else {
2163         // require dataspace setup even if not using surface input
2164         format->setInt32("android._using-recorder", 1);
2165     }
2166 
2167     sp<MediaCodecSource> encoder = MediaCodecSource::Create(
2168             mLooper, format, cameraSource, mPersistentSurface, flags);
2169     if (encoder == NULL) {
2170         ALOGE("Failed to create video encoder");
2171         // When the encoder fails to be created, we need
2172         // release the camera source due to the camera's lock
2173         // and unlock mechanism.
2174         if (cameraSource != NULL) {
2175             cameraSource->stop();
2176         }
2177         return UNKNOWN_ERROR;
2178     }
2179 
2180     if (cameraSource == NULL) {
2181         mGraphicBufferProducer = encoder->getGraphicBufferProducer();
2182     }
2183 
2184     *source = encoder;
2185 
2186     return OK;
2187 }
2188 
setupAudioEncoder(const sp<MediaWriter> & writer)2189 status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) {
2190     ATRACE_CALL();
2191     status_t status = BAD_VALUE;
2192     if (OK != (status = checkAudioEncoderCapabilities())) {
2193         return status;
2194     }
2195 
2196     switch(mAudioEncoder) {
2197         case AUDIO_ENCODER_AMR_NB:
2198         case AUDIO_ENCODER_AMR_WB:
2199         case AUDIO_ENCODER_AAC:
2200         case AUDIO_ENCODER_HE_AAC:
2201         case AUDIO_ENCODER_AAC_ELD:
2202         case AUDIO_ENCODER_OPUS:
2203             break;
2204 
2205         default:
2206             ALOGE("Unsupported audio encoder: %d", mAudioEncoder);
2207             return UNKNOWN_ERROR;
2208     }
2209 
2210     sp<MediaCodecSource> audioEncoder = createAudioSource();
2211     if (audioEncoder == NULL) {
2212         return UNKNOWN_ERROR;
2213     }
2214 
2215     writer->addSource(audioEncoder);
2216     mAudioEncoderSource = audioEncoder;
2217     return OK;
2218 }
2219 
setupMPEG4orWEBMRecording()2220 status_t StagefrightRecorder::setupMPEG4orWEBMRecording() {
2221     mWriter.clear();
2222     mTotalBitRate = 0;
2223 
2224     status_t err = OK;
2225     sp<MediaWriter> writer;
2226     sp<MPEG4Writer> mp4writer;
2227     if (mOutputFormat == OUTPUT_FORMAT_WEBM) {
2228         writer = new WebmWriter(mOutputFd);
2229     } else {
2230         writer = mp4writer = new MPEG4Writer(mOutputFd);
2231     }
2232 
2233     if (mVideoSource < VIDEO_SOURCE_LIST_END) {
2234         setDefaultVideoEncoderIfNecessary();
2235 
2236         sp<MediaSource> mediaSource;
2237         err = setupMediaSource(&mediaSource);
2238         if (err != OK) {
2239             return err;
2240         }
2241 
2242         sp<MediaCodecSource> encoder;
2243         err = setupVideoEncoder(mediaSource, &encoder);
2244         if (err != OK) {
2245             return err;
2246         }
2247 
2248         writer->addSource(encoder);
2249         mVideoEncoderSource = encoder;
2250         mTotalBitRate += mVideoBitRate;
2251     }
2252 
2253     // Audio source is added at the end if it exists.
2254     // This help make sure that the "recoding" sound is suppressed for
2255     // camcorder applications in the recorded files.
2256     // disable audio for time lapse recording
2257     const bool disableAudio = mCaptureFpsEnable && mCaptureFps < mFrameRate;
2258     if (!disableAudio && mAudioSource != AUDIO_SOURCE_CNT) {
2259         err = setupAudioEncoder(writer);
2260         if (err != OK) return err;
2261         mTotalBitRate += mAudioBitRate;
2262     }
2263 
2264     if (mOutputFormat != OUTPUT_FORMAT_WEBM) {
2265         if (mCaptureFpsEnable) {
2266             mp4writer->setCaptureRate(mCaptureFps);
2267         }
2268 
2269         if (mInterleaveDurationUs > 0) {
2270             mp4writer->setInterleaveDuration(mInterleaveDurationUs);
2271         }
2272         if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) {
2273             mp4writer->setGeoData(mLatitudex10000, mLongitudex10000);
2274         }
2275     }
2276     if (mMaxFileDurationUs != 0) {
2277         writer->setMaxFileDuration(mMaxFileDurationUs);
2278     }
2279     if (mMaxFileSizeBytes != 0) {
2280         writer->setMaxFileSize(mMaxFileSizeBytes);
2281     }
2282     if (mVideoSource == VIDEO_SOURCE_DEFAULT
2283             || mVideoSource == VIDEO_SOURCE_CAMERA) {
2284         mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId);
2285     } else if (mVideoSource == VIDEO_SOURCE_SURFACE) {
2286         // surface source doesn't need large initial delay
2287         mStartTimeOffsetMs = 100;
2288     }
2289     if (mStartTimeOffsetMs > 0) {
2290         writer->setStartTimeOffsetMs(mStartTimeOffsetMs);
2291     }
2292 
2293     writer->setListener(mListener);
2294     mWriter = writer;
2295     return OK;
2296 }
2297 
setupMPEG4orWEBMMetaData(sp<MetaData> * meta)2298 void StagefrightRecorder::setupMPEG4orWEBMMetaData(sp<MetaData> *meta) {
2299     int64_t startTimeUs = systemTime() / 1000;
2300     (*meta)->setInt64(kKeyTime, startTimeUs);
2301     (*meta)->setInt32(kKeyFileType, mOutputFormat);
2302     (*meta)->setInt32(kKeyBitRate, mTotalBitRate);
2303     if (mMovieTimeScale > 0) {
2304         (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale);
2305     }
2306     if (mOutputFormat != OUTPUT_FORMAT_WEBM) {
2307         if (mTrackEveryTimeDurationUs > 0) {
2308             (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs);
2309         }
2310         if (mRotationDegrees != 0) {
2311             (*meta)->setInt32(kKeyRotation, mRotationDegrees);
2312         }
2313     }
2314     if (mOutputFormat == OUTPUT_FORMAT_MPEG_4 || mOutputFormat == OUTPUT_FORMAT_THREE_GPP) {
2315         (*meta)->setInt32(kKeyEmptyTrackMalFormed, true);
2316         (*meta)->setInt32(kKey4BitTrackIds, true);
2317     }
2318 }
2319 
pause()2320 status_t StagefrightRecorder::pause() {
2321     ALOGV("pause");
2322     if (!mStarted) {
2323         return INVALID_OPERATION;
2324     }
2325 
2326     // Already paused --- no-op.
2327     if (mPauseStartTimeUs != 0) {
2328         return OK;
2329     }
2330 
2331     mPauseStartTimeUs = systemTime() / 1000;
2332     sp<MetaData> meta = new MetaData;
2333     meta->setInt64(kKeyTime, mPauseStartTimeUs);
2334 
2335     if (mStartedRecordingUs != 0) {
2336         // should always be true
2337         int64_t recordingUs = mPauseStartTimeUs - mStartedRecordingUs;
2338         mDurationRecordedUs += recordingUs;
2339         mStartedRecordingUs = 0;
2340     }
2341 
2342     if (mAudioEncoderSource != NULL) {
2343         mAudioEncoderSource->pause();
2344     }
2345     if (mVideoEncoderSource != NULL) {
2346         mVideoEncoderSource->pause(meta.get());
2347     }
2348 
2349     return OK;
2350 }
2351 
resume()2352 status_t StagefrightRecorder::resume() {
2353     ALOGV("resume");
2354     if (!mStarted) {
2355         return INVALID_OPERATION;
2356     }
2357 
2358     // Not paused --- no-op.
2359     if (mPauseStartTimeUs == 0) {
2360         return OK;
2361     }
2362 
2363     int64_t resumeStartTimeUs = systemTime() / 1000;
2364 
2365     int64_t bufferStartTimeUs = 0;
2366     bool allSourcesStarted = true;
2367     for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
2368         if (source == nullptr) {
2369             continue;
2370         }
2371         int64_t timeUs = source->getFirstSampleSystemTimeUs();
2372         if (timeUs < 0) {
2373             allSourcesStarted = false;
2374         }
2375         if (bufferStartTimeUs < timeUs) {
2376             bufferStartTimeUs = timeUs;
2377         }
2378     }
2379 
2380     if (allSourcesStarted) {
2381         if (mPauseStartTimeUs < bufferStartTimeUs) {
2382             mPauseStartTimeUs = bufferStartTimeUs;
2383         }
2384         // 30 ms buffer to avoid timestamp overlap
2385         mTotalPausedDurationUs += resumeStartTimeUs - mPauseStartTimeUs - 30000;
2386     }
2387     double timeOffset = -mTotalPausedDurationUs;
2388     if (mCaptureFpsEnable && (mVideoSource == VIDEO_SOURCE_CAMERA)) {
2389         timeOffset *= mCaptureFps / mFrameRate;
2390     }
2391     sp<MetaData> meta = new MetaData;
2392     meta->setInt64(kKeyTime, resumeStartTimeUs);
2393     for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
2394         if (source == nullptr) {
2395             continue;
2396         }
2397         source->setInputBufferTimeOffset((int64_t)timeOffset);
2398         source->start(meta.get());
2399     }
2400 
2401 
2402     // sum info on pause duration
2403     // (ignore the 30msec of overlap adjustment factored into mTotalPausedDurationUs)
2404     int64_t pausedUs = resumeStartTimeUs - mPauseStartTimeUs;
2405     mDurationPausedUs += pausedUs;
2406     mNPauses++;
2407     // and a timestamp marking that we're back to recording....
2408     mStartedRecordingUs = resumeStartTimeUs;
2409 
2410     mPauseStartTimeUs = 0;
2411 
2412     return OK;
2413 }
2414 
stop()2415 status_t StagefrightRecorder::stop() {
2416     ALOGV("stop");
2417     Mutex::Autolock autolock(mLock);
2418     status_t err = OK;
2419 
2420     if (mCaptureFpsEnable && mCameraSourceTimeLapse != NULL) {
2421         mCameraSourceTimeLapse->startQuickReadReturns();
2422         mCameraSourceTimeLapse = NULL;
2423     }
2424 
2425     int64_t stopTimeUs = systemTime() / 1000;
2426     for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
2427         if (source != nullptr && OK != source->setStopTimeUs(stopTimeUs)) {
2428             ALOGW("Failed to set stopTime %lld us for %s",
2429                     (long long)stopTimeUs, source->isVideo() ? "Video" : "Audio");
2430         }
2431     }
2432 
2433     if (mWriter != NULL) {
2434         err = mWriter->stop();
2435         mLastSeqNo = mWriter->getSequenceNum();
2436         mWriter.clear();
2437     }
2438 
2439     // account for the last 'segment' -- whether paused or recording
2440     if (mPauseStartTimeUs != 0) {
2441         // we were paused
2442         int64_t additive = stopTimeUs - mPauseStartTimeUs;
2443         mDurationPausedUs += additive;
2444         mNPauses++;
2445     } else if (mStartedRecordingUs != 0) {
2446         // we were recording
2447         int64_t additive = stopTimeUs - mStartedRecordingUs;
2448         mDurationRecordedUs += additive;
2449     } else {
2450         ALOGW("stop while neither recording nor paused");
2451     }
2452 
2453     flushAndResetMetrics(true);
2454 
2455     mDurationRecordedUs = 0;
2456     mDurationPausedUs = 0;
2457     mNPauses = 0;
2458     mTotalPausedDurationUs = 0;
2459     mPauseStartTimeUs = 0;
2460     mStartedRecordingUs = 0;
2461 
2462     mGraphicBufferProducer.clear();
2463     mPersistentSurface.clear();
2464     mAudioEncoderSource.clear();
2465     mVideoEncoderSource.clear();
2466 
2467     if (mOutputFd >= 0) {
2468         ::close(mOutputFd);
2469         mOutputFd = -1;
2470     }
2471 
2472     if (mStarted) {
2473         mStarted = false;
2474 
2475         uint32_t params = 0;
2476         if (mAudioSource != AUDIO_SOURCE_CNT) {
2477             params |= IMediaPlayerService::kBatteryDataTrackAudio;
2478         }
2479         if (mVideoSource != VIDEO_SOURCE_LIST_END) {
2480             params |= IMediaPlayerService::kBatteryDataTrackVideo;
2481         }
2482 
2483         addBatteryData(params);
2484     }
2485 
2486     return err;
2487 }
2488 
close()2489 status_t StagefrightRecorder::close() {
2490     ALOGV("close");
2491     stop();
2492 
2493     return OK;
2494 }
2495 
reset()2496 status_t StagefrightRecorder::reset() {
2497     ALOGV("reset");
2498     stop();
2499 
2500     // No audio or video source by default
2501     mAudioSource = (audio_source_t)AUDIO_SOURCE_CNT; // reset to invalid value
2502     mVideoSource = VIDEO_SOURCE_LIST_END;
2503 
2504     // Default parameters
2505     mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
2506     mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
2507     mVideoEncoder  = VIDEO_ENCODER_DEFAULT;
2508     mVideoWidth    = 176;
2509     mVideoHeight   = 144;
2510     mFrameRate     = -1;
2511     mVideoBitRate  = 192000;
2512     // Following MediaCodec's default
2513     mVideoBitRateMode = BITRATE_MODE_VBR;
2514     mSampleRate    = 8000;
2515     mAudioChannels = 1;
2516     mAudioBitRate  = 12200;
2517     mInterleaveDurationUs = 0;
2518     mIFramesIntervalSec = 1;
2519     mAudioSourceNode = 0;
2520     mUse64BitFileOffset = false;
2521     mMovieTimeScale  = -1;
2522     mAudioTimeScale  = -1;
2523     mVideoTimeScale  = -1;
2524     mCameraId        = 0;
2525     mStartTimeOffsetMs = -1;
2526     mVideoEncoderProfile = -1;
2527     mVideoEncoderLevel   = -1;
2528     mMaxFileDurationUs = 0;
2529     mMaxFileSizeBytes = 0;
2530     mTrackEveryTimeDurationUs = 0;
2531     mCaptureFpsEnable = false;
2532     mCaptureFps = -1.0;
2533     mCameraSourceTimeLapse = NULL;
2534     mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid;
2535     mEncoderProfiles = MediaProfiles::getInstance();
2536     mRotationDegrees = 0;
2537     mLatitudex10000 = -3600000;
2538     mLongitudex10000 = -3600000;
2539     mTotalBitRate = 0;
2540 
2541     // tracking how long we recorded.
2542     mDurationRecordedUs = 0;
2543     mStartedRecordingUs = 0;
2544     mDurationPausedUs = 0;
2545     mNPauses = 0;
2546 
2547     mOutputFd = -1;
2548 
2549     return OK;
2550 }
2551 
getMaxAmplitude(int * max)2552 status_t StagefrightRecorder::getMaxAmplitude(int *max) {
2553     ALOGV("getMaxAmplitude");
2554 
2555     if (max == NULL) {
2556         ALOGE("Null pointer argument");
2557         return BAD_VALUE;
2558     }
2559 
2560     if (mAudioSourceNode != 0) {
2561         *max = mAudioSourceNode->getMaxAmplitude();
2562     } else {
2563         *max = 0;
2564     }
2565 
2566     return OK;
2567 }
2568 
getMetrics(Parcel * reply)2569 status_t StagefrightRecorder::getMetrics(Parcel *reply) {
2570     ALOGV("StagefrightRecorder::getMetrics");
2571 
2572     if (reply == NULL) {
2573         ALOGE("Null pointer argument");
2574         return BAD_VALUE;
2575     }
2576 
2577     if (mMetricsItem == NULL) {
2578         return UNKNOWN_ERROR;
2579     }
2580 
2581     updateMetrics();
2582     mMetricsItem->writeToParcel(reply);
2583     return OK;
2584 }
2585 
setInputDevice(audio_port_handle_t deviceId)2586 status_t StagefrightRecorder::setInputDevice(audio_port_handle_t deviceId) {
2587     ALOGV("setInputDevice");
2588 
2589     if (mSelectedDeviceId != deviceId) {
2590         mSelectedDeviceId = deviceId;
2591         if (mAudioSourceNode != 0) {
2592             return mAudioSourceNode->setInputDevice(deviceId);
2593         }
2594     }
2595     return NO_ERROR;
2596 }
2597 
getRoutedDeviceIds(DeviceIdVector & deviceIds)2598 status_t StagefrightRecorder::getRoutedDeviceIds(DeviceIdVector& deviceIds) {
2599     ALOGV("getRoutedDeviceIds");
2600 
2601     if (mAudioSourceNode != 0) {
2602         status_t status = mAudioSourceNode->getRoutedDeviceIds(deviceIds);
2603         return status;
2604     }
2605     return NO_INIT;
2606 }
2607 
setAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback> & callback)2608 void StagefrightRecorder::setAudioDeviceCallback(
2609         const sp<AudioSystem::AudioDeviceCallback>& callback) {
2610     mAudioDeviceCallback = callback;
2611 }
2612 
enableAudioDeviceCallback(bool enabled)2613 status_t StagefrightRecorder::enableAudioDeviceCallback(bool enabled) {
2614     mDeviceCallbackEnabled = enabled;
2615     sp<AudioSystem::AudioDeviceCallback> callback = mAudioDeviceCallback.promote();
2616     if (mAudioSourceNode != 0 && callback != 0) {
2617         if (enabled) {
2618             return mAudioSourceNode->addAudioDeviceCallback(callback);
2619         } else {
2620             return mAudioSourceNode->removeAudioDeviceCallback(callback);
2621         }
2622     }
2623     return NO_ERROR;
2624 }
2625 
getActiveMicrophones(std::vector<media::MicrophoneInfoFw> * activeMicrophones)2626 status_t StagefrightRecorder::getActiveMicrophones(
2627         std::vector<media::MicrophoneInfoFw>* activeMicrophones) {
2628     if (mAudioSourceNode != 0) {
2629         return mAudioSourceNode->getActiveMicrophones(activeMicrophones);
2630     }
2631     return NO_INIT;
2632 }
2633 
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)2634 status_t StagefrightRecorder::setPreferredMicrophoneDirection(audio_microphone_direction_t direction) {
2635     ALOGV("setPreferredMicrophoneDirection(%d)", direction);
2636     mSelectedMicDirection = direction;
2637     if (mAudioSourceNode != 0) {
2638         return mAudioSourceNode->setPreferredMicrophoneDirection(direction);
2639     }
2640     return NO_INIT;
2641 }
2642 
setPreferredMicrophoneFieldDimension(float zoom)2643 status_t StagefrightRecorder::setPreferredMicrophoneFieldDimension(float zoom) {
2644     ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
2645     mSelectedMicFieldDimension = zoom;
2646     if (mAudioSourceNode != 0) {
2647         return mAudioSourceNode->setPreferredMicrophoneFieldDimension(zoom);
2648     }
2649     return NO_INIT;
2650 }
2651 
getPortId(audio_port_handle_t * portId) const2652 status_t StagefrightRecorder::getPortId(audio_port_handle_t *portId) const {
2653     if (mAudioSourceNode != 0) {
2654         return mAudioSourceNode->getPortId(portId);
2655     }
2656     return NO_INIT;
2657 }
2658 
getRtpDataUsage(uint64_t * bytes)2659 status_t StagefrightRecorder::getRtpDataUsage(uint64_t *bytes) {
2660     if (mWriter != 0) {
2661         *bytes = mWriter->getAccumulativeBytes();
2662         return OK;
2663     }
2664     return NO_INIT;
2665 }
2666 
dump(int fd,const Vector<String16> & args) const2667 status_t StagefrightRecorder::dump(
2668         int fd, const Vector<String16>& args) const {
2669     ALOGV("dump");
2670     Mutex::Autolock autolock(mLock);
2671     const size_t SIZE = 256;
2672     char buffer[SIZE];
2673     String8 result;
2674     if (mWriter != 0) {
2675         mWriter->dump(fd, args);
2676     } else {
2677         snprintf(buffer, SIZE, "   No file writer\n");
2678         result.append(buffer);
2679     }
2680     snprintf(buffer, SIZE, "   Recorder: %p\n", this);
2681     snprintf(buffer, SIZE, "   Output file (fd %d):\n", mOutputFd);
2682     result.append(buffer);
2683     snprintf(buffer, SIZE, "     File format: %d\n", mOutputFormat);
2684     result.append(buffer);
2685     snprintf(buffer, SIZE, "     Max file size (bytes): %" PRId64 "\n", mMaxFileSizeBytes);
2686     result.append(buffer);
2687     snprintf(buffer, SIZE, "     Max file duration (us): %" PRId64 "\n", mMaxFileDurationUs);
2688     result.append(buffer);
2689     snprintf(buffer, SIZE, "     File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32);
2690     result.append(buffer);
2691     snprintf(buffer, SIZE, "     Interleave duration (us): %d\n", mInterleaveDurationUs);
2692     result.append(buffer);
2693     snprintf(buffer, SIZE, "     Progress notification: %" PRId64 " us\n", mTrackEveryTimeDurationUs);
2694     result.append(buffer);
2695     snprintf(buffer, SIZE, "   Audio\n");
2696     result.append(buffer);
2697     snprintf(buffer, SIZE, "     Source: %d\n", mAudioSource);
2698     result.append(buffer);
2699     snprintf(buffer, SIZE, "     Encoder: %d\n", mAudioEncoder);
2700     result.append(buffer);
2701     snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mAudioBitRate);
2702     result.append(buffer);
2703     snprintf(buffer, SIZE, "     Sampling rate (hz): %d\n", mSampleRate);
2704     result.append(buffer);
2705     snprintf(buffer, SIZE, "     Number of channels: %d\n", mAudioChannels);
2706     result.append(buffer);
2707     snprintf(buffer, SIZE, "     Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude());
2708     result.append(buffer);
2709     snprintf(buffer, SIZE, "   Video\n");
2710     result.append(buffer);
2711     snprintf(buffer, SIZE, "     Source: %d\n", mVideoSource);
2712     result.append(buffer);
2713     snprintf(buffer, SIZE, "     Camera Id: %d\n", mCameraId);
2714     result.append(buffer);
2715     snprintf(buffer, SIZE, "     Start time offset (ms): %d\n", mStartTimeOffsetMs);
2716     result.append(buffer);
2717     snprintf(buffer, SIZE, "     Encoder: %d\n", mVideoEncoder);
2718     result.append(buffer);
2719     snprintf(buffer, SIZE, "     Encoder profile: %d\n", mVideoEncoderProfile);
2720     result.append(buffer);
2721     snprintf(buffer, SIZE, "     Encoder level: %d\n", mVideoEncoderLevel);
2722     result.append(buffer);
2723     snprintf(buffer, SIZE, "     I frames interval (s): %d\n", mIFramesIntervalSec);
2724     result.append(buffer);
2725     snprintf(buffer, SIZE, "     Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight);
2726     result.append(buffer);
2727     snprintf(buffer, SIZE, "     Frame rate (fps): %d\n", mFrameRate);
2728     result.append(buffer);
2729     snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mVideoBitRate);
2730     result.append(buffer);
2731     ::write(fd, result.c_str(), result.size());
2732     return OK;
2733 }
2734 }  // namespace android
2735