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