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