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