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