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