PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/frameworks/av/media/libmediaplayerservice/StagefrightRecorder.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C++ | 1479 lines | 1195 code | 203 blank | 81 comment | 420 complexity | ac9087a76c48045b7a0d2e3f88890ec6 MD5 | raw file
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "StagefrightRecorder"
  18. #include <utils/Log.h>
  19. #include "StagefrightRecorder.h"
  20. #include <binder/IPCThreadState.h>
  21. #include <binder/IServiceManager.h>
  22. #include <media/IMediaPlayerService.h>
  23. #include <media/openmax/OMX_Audio.h>
  24. #include <media/stagefright/foundation/ADebug.h>
  25. #include <media/stagefright/AudioSource.h>
  26. #include <media/stagefright/AMRWriter.h>
  27. #include <media/stagefright/AACWriter.h>
  28. #include <media/stagefright/CameraSource.h>
  29. #include <media/stagefright/CameraSourceTimeLapse.h>
  30. #include <media/stagefright/MPEG2TSWriter.h>
  31. #include <media/stagefright/MIRRORINGWriter.h>
  32. #include <media/stagefright/MPEG4Writer.h>
  33. #include <media/stagefright/MediaDefs.h>
  34. #include <media/stagefright/MetaData.h>
  35. #include <media/stagefright/OMXClient.h>
  36. #include <media/stagefright/OMXCodec.h>
  37. #include <media/stagefright/SurfaceMediaSource.h>
  38. #include <media/MediaProfiles.h>
  39. #include <camera/ICamera.h>
  40. #include <camera/CameraParameters.h>
  41. #include <gui/Surface.h>
  42. #include <utils/Errors.h>
  43. #include <sys/types.h>
  44. #include <ctype.h>
  45. #include <unistd.h>
  46. #include <media/stagefright/Video_Mirror_Source.h>
  47. #include <media/stagefright/Audio_Mirror_Source.h>
  48. #include <dlfcn.h>
  49. #include <system/audio.h>
  50. #include "ARTPWriter.h"
  51. namespace android {
  52. // To collect the encoder usage for the battery app
  53. static void addBatteryData(uint32_t params) {
  54. sp<IBinder> binder =
  55. defaultServiceManager()->getService(String16("media.player"));
  56. sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
  57. CHECK(service.get() != NULL);
  58. service->addBatteryData(params);
  59. }
  60. StagefrightRecorder::StagefrightRecorder()
  61. : mWriter(NULL),
  62. mOutputFd(-1),
  63. mAudioSource(AUDIO_SOURCE_CNT),
  64. mVideoSource(VIDEO_SOURCE_LIST_END),
  65. m_handle(NULL),
  66. mStarted(false), mSurfaceMediaSource(NULL) {
  67. ALOGV("Constructor");
  68. reset();
  69. }
  70. StagefrightRecorder::~StagefrightRecorder() {
  71. ALOGV("Destructor");
  72. stop();
  73. }
  74. status_t StagefrightRecorder::init() {
  75. ALOGV("init");
  76. return OK;
  77. }
  78. // The client side of mediaserver asks it to creat a SurfaceMediaSource
  79. // and return a interface reference. The client side will use that
  80. // while encoding GL Frames
  81. sp<ISurfaceTexture> StagefrightRecorder::querySurfaceMediaSource() const {
  82. ALOGV("Get SurfaceMediaSource");
  83. return mSurfaceMediaSource->getBufferQueue();
  84. }
  85. status_t StagefrightRecorder::setAudioSource(audio_source_t as) {
  86. ALOGV("setAudioSource: %d", as);
  87. if (as < AUDIO_SOURCE_DEFAULT ||
  88. as >= AUDIO_SOURCE_CNT) {
  89. ALOGE("Invalid audio source: %d", as);
  90. return BAD_VALUE;
  91. }
  92. if (as == AUDIO_SOURCE_DEFAULT) {
  93. mAudioSource = AUDIO_SOURCE_MIC;
  94. } else {
  95. mAudioSource = as;
  96. }
  97. return OK;
  98. }
  99. status_t StagefrightRecorder::setVideoSource(video_source vs) {
  100. ALOGV("setVideoSource: %d", vs);
  101. if (vs < VIDEO_SOURCE_DEFAULT ||
  102. vs >= VIDEO_SOURCE_LIST_END) {
  103. ALOGE("Invalid video source: %d", vs);
  104. return BAD_VALUE;
  105. }
  106. if (vs == VIDEO_SOURCE_DEFAULT) {
  107. mVideoSource = VIDEO_SOURCE_CAMERA;
  108. } else {
  109. mVideoSource = vs;
  110. }
  111. return OK;
  112. }
  113. status_t StagefrightRecorder::setOutputFormat(output_format of) {
  114. ALOGV("setOutputFormat: %d", of);
  115. if (of < OUTPUT_FORMAT_DEFAULT ||
  116. of >= OUTPUT_FORMAT_LIST_END) {
  117. ALOGE("Invalid output format: %d", of);
  118. return BAD_VALUE;
  119. }
  120. if (of == OUTPUT_FORMAT_DEFAULT) {
  121. mOutputFormat = OUTPUT_FORMAT_THREE_GPP;
  122. } else {
  123. mOutputFormat = of;
  124. }
  125. return OK;
  126. }
  127. status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) {
  128. ALOGV("setAudioEncoder: %d", ae);
  129. if (ae < AUDIO_ENCODER_DEFAULT ||
  130. ae >= AUDIO_ENCODER_LIST_END) {
  131. ALOGE("Invalid audio encoder: %d", ae);
  132. return BAD_VALUE;
  133. }
  134. if (ae == AUDIO_ENCODER_DEFAULT) {
  135. mAudioEncoder = AUDIO_ENCODER_AMR_NB;
  136. } else {
  137. mAudioEncoder = ae;
  138. }
  139. return OK;
  140. }
  141. status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) {
  142. ALOGV("setVideoEncoder: %d", ve);
  143. if (ve < VIDEO_ENCODER_DEFAULT ||
  144. ve >= VIDEO_ENCODER_LIST_END) {
  145. ALOGE("Invalid video encoder: %d", ve);
  146. return BAD_VALUE;
  147. }
  148. if (ve == VIDEO_ENCODER_DEFAULT) {
  149. mVideoEncoder = VIDEO_ENCODER_H264;
  150. } else {
  151. mVideoEncoder = ve;
  152. }
  153. return OK;
  154. }
  155. status_t StagefrightRecorder::setVideoSize(int width, int height) {
  156. int low_quality_w,low_quality_h,high_quality_w,high_quality_h, framerate=-1,minFrameRate;
  157. camcorder_quality quality=CAMCORDER_QUALITY_LOW;
  158. ALOGV("setVideoSize: %dx%d", width, height);
  159. if (width <= 0 || height <= 0) {
  160. ALOGE("Invalid video size: %dx%d", width, height);
  161. return BAD_VALUE;
  162. }
  163. // Additional check on the dimension will be performed later
  164. mVideoWidth = width;
  165. mVideoHeight = height;//&0xfffe;
  166. minFrameRate = mEncoderProfiles->getVideoEncoderParamByName("enc.vid.fps.min", mVideoEncoder);
  167. if ((mFrameRate == -1) || (mFrameRate == minFrameRate)) { /* ddl@rock-chips.com */
  168. low_quality_w = mEncoderProfiles->getCamcorderProfileParamByName
  169. ("vid.width",mCameraId,CAMCORDER_QUALITY_LOW);
  170. low_quality_h = mEncoderProfiles->getCamcorderProfileParamByName
  171. ("vid.height",mCameraId,CAMCORDER_QUALITY_LOW);
  172. high_quality_w = mEncoderProfiles->getCamcorderProfileParamByName
  173. ("vid.width",mCameraId,CAMCORDER_QUALITY_HIGH);
  174. high_quality_h = mEncoderProfiles->getCamcorderProfileParamByName
  175. ("vid.height",mCameraId,CAMCORDER_QUALITY_HIGH);
  176. if ((width == low_quality_w) && (height == low_quality_h)) {
  177. quality = CAMCORDER_QUALITY_LOW;
  178. } else if ((width == high_quality_w) && (height == high_quality_h)) {
  179. quality = CAMCORDER_QUALITY_HIGH;
  180. } else if ((width == 176) && (height == 144)) {
  181. quality = CAMCORDER_QUALITY_QCIF;
  182. } else if ((width == 352) && (height == 288)) {
  183. quality = CAMCORDER_QUALITY_CIF;
  184. } else if ((width == 320) && (height == 240)) {
  185. quality = CAMCORDER_QUALITY_QVGA;
  186. } else if (height == 480) {
  187. quality = CAMCORDER_QUALITY_480P;
  188. } else if ((width == 1280) && (height == 720)) {
  189. quality = CAMCORDER_QUALITY_720P;
  190. } else if ((width == 1920) && (height == 1080)) {
  191. quality = CAMCORDER_QUALITY_1080P;
  192. }
  193. framerate = mEncoderProfiles->getCamcorderProfileParamByName
  194. ("vid.fps",mCameraId,quality);
  195. setVideoFrameRate(framerate);
  196. ALOGW("Intended video encoding frame rate (%d fps) is too small"
  197. " and will be set to (%d fps quality: %d)", mFrameRate, framerate,quality);
  198. }
  199. return OK;
  200. }
  201. status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) {
  202. ALOGV("setVideoFrameRate: %d", frames_per_second);
  203. if ((frames_per_second <= 0 && frames_per_second != -1) ||
  204. frames_per_second > 120) {
  205. ALOGE("Invalid video frame rate: %d", frames_per_second);
  206. return BAD_VALUE;
  207. }
  208. // Additional check on the frame rate will be performed later
  209. mFrameRate = frames_per_second;
  210. return OK;
  211. }
  212. status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera,
  213. const sp<ICameraRecordingProxy> &proxy) {
  214. ALOGV("setCamera");
  215. if (camera == 0) {
  216. ALOGE("camera is NULL");
  217. return BAD_VALUE;
  218. }
  219. if (proxy == 0) {
  220. ALOGE("camera proxy is NULL");
  221. return BAD_VALUE;
  222. }
  223. mCamera = camera;
  224. mCameraProxy = proxy;
  225. return OK;
  226. }
  227. status_t StagefrightRecorder::setPreviewSurface(const sp<Surface> &surface) {
  228. ALOGV("setPreviewSurface: %p", surface.get());
  229. mPreviewSurface = surface;
  230. return OK;
  231. }
  232. status_t StagefrightRecorder::setOutputFile(const char *path) {
  233. ALOGE("setOutputFile(const char*) must not be called");
  234. // We don't actually support this at all, as the media_server process
  235. // no longer has permissions to create files.
  236. return -EPERM;
  237. }
  238. status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) {
  239. ALOGV("setOutputFile: %d, %lld, %lld", fd, offset, length);
  240. // These don't make any sense, do they?
  241. CHECK_EQ(offset, 0ll);
  242. CHECK_EQ(length, 0ll);
  243. if (fd < 0) {
  244. ALOGE("Invalid file descriptor: %d", fd);
  245. return -EBADF;
  246. }
  247. if (mOutputFd >= 0) {
  248. ::close(mOutputFd);
  249. }
  250. mOutputFd = dup(fd);
  251. return OK;
  252. }
  253. // Attempt to parse an int64 literal optionally surrounded by whitespace,
  254. // returns true on success, false otherwise.
  255. static bool safe_strtoi64(const char *s, int64_t *val) {
  256. char *end;
  257. // It is lame, but according to man page, we have to set errno to 0
  258. // before calling strtoll().
  259. errno = 0;
  260. *val = strtoll(s, &end, 10);
  261. if (end == s || errno == ERANGE) {
  262. return false;
  263. }
  264. // Skip trailing whitespace
  265. while (isspace(*end)) {
  266. ++end;
  267. }
  268. // For a successful return, the string must contain nothing but a valid
  269. // int64 literal optionally surrounded by whitespace.
  270. return *end == '\0';
  271. }
  272. // Return true if the value is in [0, 0x007FFFFFFF]
  273. static bool safe_strtoi32(const char *s, int32_t *val) {
  274. int64_t temp;
  275. if (safe_strtoi64(s, &temp)) {
  276. if (temp >= 0 && temp <= 0x007FFFFFFF) {
  277. *val = static_cast<int32_t>(temp);
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. // Trim both leading and trailing whitespace from the given string.
  284. static void TrimString(String8 *s) {
  285. size_t num_bytes = s->bytes();
  286. const char *data = s->string();
  287. size_t leading_space = 0;
  288. while (leading_space < num_bytes && isspace(data[leading_space])) {
  289. ++leading_space;
  290. }
  291. size_t i = num_bytes;
  292. while (i > leading_space && isspace(data[i - 1])) {
  293. --i;
  294. }
  295. s->setTo(String8(&data[leading_space], i - leading_space));
  296. }
  297. status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) {
  298. ALOGV("setParamAudioSamplingRate: %d", sampleRate);
  299. if (sampleRate <= 0) {
  300. ALOGE("Invalid audio sampling rate: %d", sampleRate);
  301. return BAD_VALUE;
  302. }
  303. // Additional check on the sample rate will be performed later.
  304. mSampleRate = sampleRate;
  305. return OK;
  306. }
  307. status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) {
  308. ALOGV("setParamAudioNumberOfChannels: %d", channels);
  309. if (channels <= 0 || channels >= 3) {
  310. ALOGE("Invalid number of audio channels: %d", channels);
  311. return BAD_VALUE;
  312. }
  313. // Additional check on the number of channels will be performed later.
  314. mAudioChannels = channels;
  315. return OK;
  316. }
  317. status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) {
  318. ALOGV("setParamAudioEncodingBitRate: %d", bitRate);
  319. if (bitRate <= 0) {
  320. ALOGE("Invalid audio encoding bit rate: %d", bitRate);
  321. return BAD_VALUE;
  322. }
  323. // The target bit rate may not be exactly the same as the requested.
  324. // It depends on many factors, such as rate control, and the bit rate
  325. // range that a specific encoder supports. The mismatch between the
  326. // the target and requested bit rate will NOT be treated as an error.
  327. mAudioBitRate = bitRate;
  328. return OK;
  329. }
  330. status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) {
  331. ALOGV("setParamVideoEncodingBitRate: %d", bitRate);
  332. if (bitRate <= 0) {
  333. ALOGE("Invalid video encoding bit rate: %d", bitRate);
  334. return BAD_VALUE;
  335. }
  336. // The target bit rate may not be exactly the same as the requested.
  337. // It depends on many factors, such as rate control, and the bit rate
  338. // range that a specific encoder supports. The mismatch between the
  339. // the target and requested bit rate will NOT be treated as an error.
  340. mVideoBitRate = bitRate;
  341. return OK;
  342. }
  343. // Always rotate clockwise, and only support 0, 90, 180 and 270 for now.
  344. status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) {
  345. ALOGV("setParamVideoRotation: %d", degrees);
  346. if (degrees < 0 || degrees % 90 != 0) {
  347. ALOGE("Unsupported video rotation angle: %d", degrees);
  348. return BAD_VALUE;
  349. }
  350. mRotationDegrees = degrees % 360;
  351. return OK;
  352. }
  353. status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) {
  354. ALOGV("setParamMaxFileDurationUs: %lld us", timeUs);
  355. // This is meant for backward compatibility for MediaRecorder.java
  356. if (timeUs <= 0) {
  357. ALOGW("Max file duration is not positive: %lld us. Disabling duration limit.", timeUs);
  358. timeUs = 0; // Disable the duration limit for zero or negative values.
  359. } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds
  360. ALOGE("Max file duration is too short: %lld us", timeUs);
  361. return BAD_VALUE;
  362. }
  363. if (timeUs <= 15 * 1000000LL) {
  364. ALOGW("Target duration (%lld us) too short to be respected", timeUs);
  365. }
  366. mMaxFileDurationUs = timeUs;
  367. return OK;
  368. }
  369. status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) {
  370. ALOGV("setParamMaxFileSizeBytes: %lld bytes", bytes);
  371. // This is meant for backward compatibility for MediaRecorder.java
  372. if (bytes <= 0) {
  373. ALOGW("Max file size is not positive: %lld bytes. "
  374. "Disabling file size limit.", bytes);
  375. bytes = 0; // Disable the file size limit for zero or negative values.
  376. } else if (bytes <= 1024) { // XXX: 1 kB
  377. ALOGE("Max file size is too small: %lld bytes", bytes);
  378. return BAD_VALUE;
  379. }
  380. if (bytes <= 100 * 1024) {
  381. ALOGW("Target file size (%lld bytes) is too small to be respected", bytes);
  382. }
  383. mMaxFileSizeBytes = bytes;
  384. return OK;
  385. }
  386. status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) {
  387. ALOGV("setParamInterleaveDuration: %d", durationUs);
  388. if (durationUs <= 500000) { // 500 ms
  389. // If interleave duration is too small, it is very inefficient to do
  390. // interleaving since the metadata overhead will count for a significant
  391. // portion of the saved contents
  392. ALOGE("Audio/video interleave duration is too small: %d us", durationUs);
  393. return BAD_VALUE;
  394. } else if (durationUs >= 10000000) { // 10 seconds
  395. // If interleaving duration is too large, it can cause the recording
  396. // session to use too much memory since we have to save the output
  397. // data before we write them out
  398. ALOGE("Audio/video interleave duration is too large: %d us", durationUs);
  399. return BAD_VALUE;
  400. }
  401. mInterleaveDurationUs = durationUs;
  402. return OK;
  403. }
  404. // If seconds < 0, only the first frame is I frame, and rest are all P frames
  405. // If seconds == 0, all frames are encoded as I frames. No P frames
  406. // If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames
  407. status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) {
  408. ALOGV("setParamVideoIFramesInterval: %d seconds", seconds);
  409. mIFramesIntervalSec = seconds;
  410. return OK;
  411. }
  412. status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) {
  413. ALOGV("setParam64BitFileOffset: %s",
  414. use64Bit? "use 64 bit file offset": "use 32 bit file offset");
  415. mUse64BitFileOffset = use64Bit;
  416. return OK;
  417. }
  418. status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) {
  419. ALOGV("setParamVideoCameraId: %d", cameraId);
  420. if (cameraId < 0) {
  421. return BAD_VALUE;
  422. }
  423. mCameraId = cameraId;
  424. return OK;
  425. }
  426. status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) {
  427. ALOGV("setParamTrackTimeStatus: %lld", timeDurationUs);
  428. if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms?
  429. ALOGE("Tracking time duration too short: %lld us", timeDurationUs);
  430. return BAD_VALUE;
  431. }
  432. mTrackEveryTimeDurationUs = timeDurationUs;
  433. return OK;
  434. }
  435. status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) {
  436. ALOGV("setParamVideoEncoderProfile: %d", profile);
  437. // Additional check will be done later when we load the encoder.
  438. // For now, we are accepting values defined in OpenMAX IL.
  439. mVideoEncoderProfile = profile;
  440. return OK;
  441. }
  442. status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) {
  443. ALOGV("setParamVideoEncoderLevel: %d", level);
  444. // Additional check will be done later when we load the encoder.
  445. // For now, we are accepting values defined in OpenMAX IL.
  446. mVideoEncoderLevel = level;
  447. return OK;
  448. }
  449. status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) {
  450. ALOGV("setParamMovieTimeScale: %d", timeScale);
  451. // The range is set to be the same as the audio's time scale range
  452. // since audio's time scale has a wider range.
  453. if (timeScale < 600 || timeScale > 96000) {
  454. ALOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale);
  455. return BAD_VALUE;
  456. }
  457. mMovieTimeScale = timeScale;
  458. return OK;
  459. }
  460. status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) {
  461. ALOGV("setParamVideoTimeScale: %d", timeScale);
  462. // 60000 is chosen to make sure that each video frame from a 60-fps
  463. // video has 1000 ticks.
  464. if (timeScale < 600 || timeScale > 60000) {
  465. ALOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale);
  466. return BAD_VALUE;
  467. }
  468. mVideoTimeScale = timeScale;
  469. return OK;
  470. }
  471. status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) {
  472. ALOGV("setParamAudioTimeScale: %d", timeScale);
  473. // 96000 Hz is the highest sampling rate support in AAC.
  474. if (timeScale < 600 || timeScale > 96000) {
  475. ALOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale);
  476. return BAD_VALUE;
  477. }
  478. mAudioTimeScale = timeScale;
  479. return OK;
  480. }
  481. status_t StagefrightRecorder::setParamTimeLapseEnable(int32_t timeLapseEnable) {
  482. ALOGV("setParamTimeLapseEnable: %d", timeLapseEnable);
  483. if(timeLapseEnable == 0) {
  484. mCaptureTimeLapse = false;
  485. } else if (timeLapseEnable == 1) {
  486. mCaptureTimeLapse = true;
  487. } else {
  488. return BAD_VALUE;
  489. }
  490. return OK;
  491. }
  492. status_t StagefrightRecorder::setParamTimeBetweenTimeLapseFrameCapture(int64_t timeUs) {
  493. ALOGV("setParamTimeBetweenTimeLapseFrameCapture: %lld us", timeUs);
  494. // Not allowing time more than a day
  495. if (timeUs <= 0 || timeUs > 86400*1E6) {
  496. ALOGE("Time between time lapse frame capture (%lld) is out of range [0, 1 Day]", timeUs);
  497. return BAD_VALUE;
  498. }
  499. mTimeBetweenTimeLapseFrameCaptureUs = timeUs;
  500. return OK;
  501. }
  502. status_t StagefrightRecorder::setParamGeoDataLongitude(
  503. int64_t longitudex10000) {
  504. if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
  505. return BAD_VALUE;
  506. }
  507. mLongitudex10000 = longitudex10000;
  508. return OK;
  509. }
  510. status_t StagefrightRecorder::setParamGeoDataLatitude(
  511. int64_t latitudex10000) {
  512. if (latitudex10000 > 900000 || latitudex10000 < -900000) {
  513. return BAD_VALUE;
  514. }
  515. mLatitudex10000 = latitudex10000;
  516. return OK;
  517. }
  518. status_t StagefrightRecorder::setParameter(
  519. const String8 &key, const String8 &value) {
  520. ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
  521. if (key == "max-duration") {
  522. int64_t max_duration_ms;
  523. if (safe_strtoi64(value.string(), &max_duration_ms)) {
  524. return setParamMaxFileDurationUs(1000LL * max_duration_ms);
  525. }
  526. } else if (key == "max-filesize") {
  527. int64_t max_filesize_bytes;
  528. if (safe_strtoi64(value.string(), &max_filesize_bytes)) {
  529. return setParamMaxFileSizeBytes(max_filesize_bytes);
  530. }
  531. } else if (key == "interleave-duration-us") {
  532. int32_t durationUs;
  533. if (safe_strtoi32(value.string(), &durationUs)) {
  534. return setParamInterleaveDuration(durationUs);
  535. }
  536. } else if (key == "param-movie-time-scale") {
  537. int32_t timeScale;
  538. if (safe_strtoi32(value.string(), &timeScale)) {
  539. return setParamMovieTimeScale(timeScale);
  540. }
  541. } else if (key == "param-use-64bit-offset") {
  542. int32_t use64BitOffset;
  543. if (safe_strtoi32(value.string(), &use64BitOffset)) {
  544. return setParam64BitFileOffset(use64BitOffset != 0);
  545. }
  546. } else if (key == "param-geotag-longitude") {
  547. int64_t longitudex10000;
  548. if (safe_strtoi64(value.string(), &longitudex10000)) {
  549. return setParamGeoDataLongitude(longitudex10000);
  550. }
  551. } else if (key == "param-geotag-latitude") {
  552. int64_t latitudex10000;
  553. if (safe_strtoi64(value.string(), &latitudex10000)) {
  554. return setParamGeoDataLatitude(latitudex10000);
  555. }
  556. } else if (key == "param-track-time-status") {
  557. int64_t timeDurationUs;
  558. if (safe_strtoi64(value.string(), &timeDurationUs)) {
  559. return setParamTrackTimeStatus(timeDurationUs);
  560. }
  561. } else if (key == "audio-param-sampling-rate") {
  562. int32_t sampling_rate;
  563. if (safe_strtoi32(value.string(), &sampling_rate)) {
  564. return setParamAudioSamplingRate(sampling_rate);
  565. }
  566. } else if (key == "audio-param-number-of-channels") {
  567. int32_t number_of_channels;
  568. if (safe_strtoi32(value.string(), &number_of_channels)) {
  569. return setParamAudioNumberOfChannels(number_of_channels);
  570. }
  571. } else if (key == "audio-param-encoding-bitrate") {
  572. int32_t audio_bitrate;
  573. if (safe_strtoi32(value.string(), &audio_bitrate)) {
  574. return setParamAudioEncodingBitRate(audio_bitrate);
  575. }
  576. } else if (key == "audio-param-time-scale") {
  577. int32_t timeScale;
  578. if (safe_strtoi32(value.string(), &timeScale)) {
  579. return setParamAudioTimeScale(timeScale);
  580. }
  581. } else if (key == "video-param-encoding-bitrate") {
  582. int32_t video_bitrate;
  583. if (safe_strtoi32(value.string(), &video_bitrate)) {
  584. return setParamVideoEncodingBitRate(video_bitrate);
  585. }
  586. } else if (key == "video-param-rotation-angle-degrees") {
  587. int32_t degrees;
  588. if (safe_strtoi32(value.string(), &degrees)) {
  589. return setParamVideoRotation(degrees);
  590. }
  591. } else if (key == "video-param-i-frames-interval") {
  592. int32_t seconds;
  593. if (safe_strtoi32(value.string(), &seconds)) {
  594. return setParamVideoIFramesInterval(seconds);
  595. }
  596. } else if (key == "video-param-encoder-profile") {
  597. int32_t profile;
  598. if (safe_strtoi32(value.string(), &profile)) {
  599. return setParamVideoEncoderProfile(profile);
  600. }
  601. } else if (key == "video-param-encoder-level") {
  602. int32_t level;
  603. if (safe_strtoi32(value.string(), &level)) {
  604. return setParamVideoEncoderLevel(level);
  605. }
  606. } else if (key == "video-param-camera-id") {
  607. int32_t cameraId;
  608. if (safe_strtoi32(value.string(), &cameraId)) {
  609. return setParamVideoCameraId(cameraId);
  610. }
  611. } else if (key == "video-param-time-scale") {
  612. int32_t timeScale;
  613. if (safe_strtoi32(value.string(), &timeScale)) {
  614. return setParamVideoTimeScale(timeScale);
  615. }
  616. } else if (key == "time-lapse-enable") {
  617. int32_t timeLapseEnable;
  618. if (safe_strtoi32(value.string(), &timeLapseEnable)) {
  619. return setParamTimeLapseEnable(timeLapseEnable);
  620. }
  621. } else if (key == "time-between-time-lapse-frame-capture") {
  622. int64_t timeBetweenTimeLapseFrameCaptureMs;
  623. if (safe_strtoi64(value.string(), &timeBetweenTimeLapseFrameCaptureMs)) {
  624. return setParamTimeBetweenTimeLapseFrameCapture(
  625. 1000LL * timeBetweenTimeLapseFrameCaptureMs);
  626. }
  627. } else {
  628. ALOGE("setParameter: failed to find key %s", key.string());
  629. }
  630. return BAD_VALUE;
  631. }
  632. status_t StagefrightRecorder::setParameters(const String8 &params) {
  633. ALOGV("setParameters: %s", params.string());
  634. const char *cparams = params.string();
  635. const char *key_start = cparams;
  636. for (;;) {
  637. const char *equal_pos = strchr(key_start, '=');
  638. if (equal_pos == NULL) {
  639. ALOGE("Parameters %s miss a value", cparams);
  640. return BAD_VALUE;
  641. }
  642. String8 key(key_start, equal_pos - key_start);
  643. TrimString(&key);
  644. if (key.length() == 0) {
  645. ALOGE("Parameters %s contains an empty key", cparams);
  646. return BAD_VALUE;
  647. }
  648. const char *value_start = equal_pos + 1;
  649. const char *semicolon_pos = strchr(value_start, ';');
  650. String8 value;
  651. if (semicolon_pos == NULL) {
  652. value.setTo(value_start);
  653. } else {
  654. value.setTo(value_start, semicolon_pos - value_start);
  655. }
  656. if (setParameter(key, value) != OK) {
  657. return BAD_VALUE;
  658. }
  659. if (semicolon_pos == NULL) {
  660. break; // Reaches the end
  661. }
  662. key_start = semicolon_pos + 1;
  663. }
  664. return OK;
  665. }
  666. status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) {
  667. mListener = listener;
  668. return OK;
  669. }
  670. status_t StagefrightRecorder::prepare() {
  671. return OK;
  672. }
  673. status_t StagefrightRecorder::start() {
  674. CHECK_GE(mOutputFd, 0);
  675. if (mWriter != NULL) {
  676. ALOGE("File writer is not avaialble");
  677. return UNKNOWN_ERROR;
  678. }
  679. status_t status = OK;
  680. switch (mOutputFormat) {
  681. case OUTPUT_FORMAT_DEFAULT:
  682. case OUTPUT_FORMAT_THREE_GPP:
  683. case OUTPUT_FORMAT_MPEG_4:
  684. status = startMPEG4Recording();
  685. break;
  686. case OUTPUT_FORMAT_AMR_NB:
  687. case OUTPUT_FORMAT_AMR_WB:
  688. status = startAMRRecording();
  689. break;
  690. case OUTPUT_FORMAT_AAC_ADIF:
  691. case OUTPUT_FORMAT_AAC_ADTS:
  692. status = startAACRecording();
  693. break;
  694. case OUTPUT_FORMAT_RTP_AVP:
  695. status = startRTPRecording();
  696. break;
  697. case OUTPUT_FORMAT_MPEG2TS:
  698. status = startMPEG2TSRecording();
  699. break;
  700. case OUTPUT_FORMAT_MIRRORINGTS:
  701. return startMIRRORINGTSRecording();
  702. default:
  703. ALOGE("Unsupported output file format: %d", mOutputFormat);
  704. status = UNKNOWN_ERROR;
  705. break;
  706. }
  707. if ((status == OK) && (!mStarted)) {
  708. mStarted = true;
  709. uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted;
  710. if (mAudioSource != AUDIO_SOURCE_CNT) {
  711. params |= IMediaPlayerService::kBatteryDataTrackAudio;
  712. }
  713. if (mVideoSource != VIDEO_SOURCE_LIST_END) {
  714. params |= IMediaPlayerService::kBatteryDataTrackVideo;
  715. }
  716. addBatteryData(params);
  717. }
  718. return status;
  719. }
  720. sp<MediaSource> StagefrightRecorder::createAudioSource() {
  721. if(mOutputFormat != OUTPUT_FORMAT_MIRRORINGTS)
  722. {
  723. sp<AudioSource> audioSource =
  724. new AudioSource(
  725. mAudioSource,
  726. mSampleRate,
  727. mAudioChannels);
  728. status_t err = audioSource->initCheck();
  729. if (err != OK) {
  730. ALOGE("audio source is not initialized");
  731. return NULL;
  732. }
  733. sp<MetaData> encMeta = new MetaData;
  734. const char *mime;
  735. switch (mAudioEncoder) {
  736. case AUDIO_ENCODER_AMR_NB:
  737. case AUDIO_ENCODER_DEFAULT:
  738. mime = MEDIA_MIMETYPE_AUDIO_AMR_NB;
  739. break;
  740. case AUDIO_ENCODER_AMR_WB:
  741. mime = MEDIA_MIMETYPE_AUDIO_AMR_WB;
  742. break;
  743. case AUDIO_ENCODER_AAC:
  744. mime = MEDIA_MIMETYPE_AUDIO_AAC;
  745. encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectLC);
  746. break;
  747. case AUDIO_ENCODER_HE_AAC:
  748. mime = MEDIA_MIMETYPE_AUDIO_AAC;
  749. encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectHE);
  750. break;
  751. case AUDIO_ENCODER_AAC_ELD:
  752. mime = MEDIA_MIMETYPE_AUDIO_AAC;
  753. encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectELD);
  754. break;
  755. default:
  756. ALOGE("Unknown audio encoder: %d", mAudioEncoder);
  757. return NULL;
  758. }
  759. encMeta->setCString(kKeyMIMEType, mime);
  760. int32_t maxInputSize;
  761. CHECK(audioSource->getFormat()->findInt32(
  762. kKeyMaxInputSize, &maxInputSize));
  763. encMeta->setInt32(kKeyMaxInputSize, maxInputSize);
  764. encMeta->setInt32(kKeyChannelCount, mAudioChannels);
  765. encMeta->setInt32(kKeySampleRate, mSampleRate);
  766. encMeta->setInt32(kKeyBitRate, mAudioBitRate);
  767. if (mAudioTimeScale > 0) {
  768. encMeta->setInt32(kKeyTimeScale, mAudioTimeScale);
  769. }
  770. OMXClient client;
  771. CHECK_EQ(client.connect(), (status_t)OK);
  772. sp<MediaSource> audioEncoder =
  773. OMXCodec::Create(client.interface(), encMeta,
  774. true /* createEncoder */, audioSource);
  775. mAudioSourceNode = audioSource;
  776. return audioEncoder;
  777. }
  778. else
  779. {
  780. MediaSource* (*mAudio_Mirror_Source)(int32_t sampleRate, int32_t numChannels);
  781. if(m_handle==NULL)
  782. {
  783. ALOGE("lib sf can't be loaded");
  784. return NULL;
  785. }
  786. mAudio_Mirror_Source = (MediaSource* (*)(int32_t sampleRate, int32_t numChannels))::dlsym(m_handle, "openAudio_Mirror_Source");
  787. if(mAudio_Mirror_Source==NULL)
  788. {
  789. ALOGE("Audio_Mirror_Source don't exit");
  790. return NULL;
  791. }
  792. sp<MediaSource> audioSource = mAudio_Mirror_Source(mSampleRate, mAudioChannels);
  793. int32_t maxInputSize;
  794. CHECK(audioSource->getFormat()->findInt32(
  795. kKeyMaxInputSize, &maxInputSize));
  796. sp<MetaData> encMeta = new MetaData;
  797. encMeta->setCString(kKeyMIMEType,MEDIA_MIMETYPE_AUDIO_AAC);
  798. encMeta->setInt32(kKeySampleRate, mSampleRate);
  799. encMeta->setInt32(kKeyChannelCount, mAudioChannels);
  800. encMeta->setInt32(kKeyMaxInputSize, maxInputSize);
  801. encMeta->setInt32(kKeyBitRate, mAudioBitRate);
  802. OMXClient client;
  803. CHECK_EQ(client.connect(), (status_t)OK);
  804. sp<MediaSource> audioEncoder =
  805. OMXCodec::Create(client.interface(), encMeta, true, audioSource,"AACEncoder");
  806. return audioEncoder;
  807. }
  808. }
  809. status_t StagefrightRecorder::startAACRecording() {
  810. // FIXME:
  811. // Add support for OUTPUT_FORMAT_AAC_ADIF
  812. CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_AAC_ADTS);
  813. CHECK(mAudioEncoder == AUDIO_ENCODER_AAC ||
  814. mAudioEncoder == AUDIO_ENCODER_HE_AAC ||
  815. mAudioEncoder == AUDIO_ENCODER_AAC_ELD);
  816. CHECK(mAudioSource != AUDIO_SOURCE_CNT);
  817. mWriter = new AACWriter(mOutputFd);
  818. status_t status = startRawAudioRecording();
  819. if (status != OK) {
  820. mWriter.clear();
  821. mWriter = NULL;
  822. }
  823. return status;
  824. }
  825. status_t StagefrightRecorder::startAMRRecording() {
  826. CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB ||
  827. mOutputFormat == OUTPUT_FORMAT_AMR_WB);
  828. if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) {
  829. if (mAudioEncoder != AUDIO_ENCODER_DEFAULT &&
  830. mAudioEncoder != AUDIO_ENCODER_AMR_NB) {
  831. ALOGE("Invalid encoder %d used for AMRNB recording",
  832. mAudioEncoder);
  833. return BAD_VALUE;
  834. }
  835. } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB
  836. if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) {
  837. ALOGE("Invlaid encoder %d used for AMRWB recording",
  838. mAudioEncoder);
  839. return BAD_VALUE;
  840. }
  841. }
  842. mWriter = new AMRWriter(mOutputFd);
  843. status_t status = startRawAudioRecording();
  844. if (status != OK) {
  845. mWriter.clear();
  846. mWriter = NULL;
  847. }
  848. return status;
  849. }
  850. status_t StagefrightRecorder::startRawAudioRecording() {
  851. if (mAudioSource >= AUDIO_SOURCE_CNT) {
  852. ALOGE("Invalid audio source: %d", mAudioSource);
  853. return BAD_VALUE;
  854. }
  855. status_t status = BAD_VALUE;
  856. if (OK != (status = checkAudioEncoderCapabilities())) {
  857. return status;
  858. }
  859. sp<MediaSource> audioEncoder = createAudioSource();
  860. if (audioEncoder == NULL) {
  861. return UNKNOWN_ERROR;
  862. }
  863. CHECK(mWriter != 0);
  864. mWriter->addSource(audioEncoder);
  865. if (mMaxFileDurationUs != 0) {
  866. mWriter->setMaxFileDuration(mMaxFileDurationUs);
  867. }
  868. if (mMaxFileSizeBytes != 0) {
  869. mWriter->setMaxFileSize(mMaxFileSizeBytes);
  870. }
  871. mWriter->setListener(mListener);
  872. mWriter->start();
  873. return OK;
  874. }
  875. status_t StagefrightRecorder::startRTPRecording() {
  876. CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP);
  877. if ((mAudioSource != AUDIO_SOURCE_CNT
  878. && mVideoSource != VIDEO_SOURCE_LIST_END)
  879. || (mAudioSource == AUDIO_SOURCE_CNT
  880. && mVideoSource == VIDEO_SOURCE_LIST_END)) {
  881. // Must have exactly one source.
  882. return BAD_VALUE;
  883. }
  884. if (mOutputFd < 0) {
  885. return BAD_VALUE;
  886. }
  887. sp<MediaSource> source;
  888. if (mAudioSource != AUDIO_SOURCE_CNT) {
  889. source = createAudioSource();
  890. } else {
  891. sp<MediaSource> mediaSource;
  892. status_t err = setupMediaSource(&mediaSource);
  893. if (err != OK) {
  894. return err;
  895. }
  896. err = setupVideoEncoder(mediaSource, mVideoBitRate, &source);
  897. if (err != OK) {
  898. return err;
  899. }
  900. }
  901. mWriter = new ARTPWriter(mOutputFd);
  902. mWriter->addSource(source);
  903. mWriter->setListener(mListener);
  904. return mWriter->start();
  905. }
  906. status_t StagefrightRecorder::startMPEG2TSRecording() {
  907. CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS);
  908. sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd);
  909. if (mAudioSource != AUDIO_SOURCE_CNT) {
  910. if (mAudioEncoder != AUDIO_ENCODER_AAC &&
  911. mAudioEncoder != AUDIO_ENCODER_HE_AAC &&
  912. mAudioEncoder != AUDIO_ENCODER_AAC_ELD) {
  913. return ERROR_UNSUPPORTED;
  914. }
  915. status_t err = setupAudioEncoder(writer);
  916. if (err != OK) {
  917. return err;
  918. }
  919. }
  920. if (mVideoSource < VIDEO_SOURCE_LIST_END) {
  921. if (mVideoEncoder != VIDEO_ENCODER_H264) {
  922. return ERROR_UNSUPPORTED;
  923. }
  924. sp<MediaSource> mediaSource;
  925. status_t err = setupMediaSource(&mediaSource);
  926. if (err != OK) {
  927. return err;
  928. }
  929. sp<MediaSource> encoder;
  930. err = setupVideoEncoder(mediaSource, mVideoBitRate, &encoder);
  931. if (err != OK) {
  932. return err;
  933. }
  934. writer->addSource(encoder);
  935. }
  936. if (mMaxFileDurationUs != 0) {
  937. writer->setMaxFileDuration(mMaxFileDurationUs);
  938. }
  939. if (mMaxFileSizeBytes != 0) {
  940. writer->setMaxFileSize(mMaxFileSizeBytes);
  941. }
  942. mWriter = writer;
  943. return mWriter->start();
  944. }
  945. status_t StagefrightRecorder::startMIRRORINGTSRecording() {
  946. CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MIRRORINGTS);
  947. MediaWriter* (*mMIRRORINGWriter)(unsigned long addr, unsigned short local_port,unsigned short remort_port);
  948. m_handle = dlopen("libstagefright.so", RTLD_LAZY | RTLD_LOCAL);
  949. if(m_handle == NULL)
  950. {
  951. ALOGE("lib sf can't be loaded");
  952. return UNKNOWN_ERROR;
  953. }
  954. mMIRRORINGWriter = (MediaWriter* (*)(unsigned long , unsigned short,unsigned short ))::dlsym(m_handle, "openMIRRORINGWriter");
  955. if(mMIRRORINGWriter==NULL)
  956. {
  957. ALOGE("MIRRORINGWriter don't exit");
  958. return UNKNOWN_ERROR;
  959. }
  960. sp<MediaWriter> writer = mMIRRORINGWriter(inet_addr("192.168.0.108"),65344,65344);//port&0xffff); //local_port, remort_port(addr,0xff40,0xff40);
  961. status_t err;
  962. if (mAudioSource != AUDIO_SOURCE_CNT) {
  963. if (mAudioEncoder != AUDIO_ENCODER_AAC) {
  964. return ERROR_UNSUPPORTED;
  965. }
  966. err = setupAudioEncoder(writer);
  967. if (err != OK) {
  968. return err;
  969. }
  970. }
  971. else{
  972. err = setupAudioEncoder(writer); //jmj
  973. if (err != OK) {
  974. return err;
  975. }
  976. }
  977. if(mMaxFileSizeBytes != 0x12345)
  978. {
  979. ALOGD("startMIRRORINGTSRecording mVideoSource %d mVideoEncoder %d", mVideoSource,mVideoEncoder);
  980. if (mVideoSource == VIDEO_SOURCE_LIST_END
  981. || mVideoSource == VIDEO_SOURCE_CAMERA|| mVideoSource == VIDEO_SOURCE_UI) {//jmj
  982. if (mVideoEncoder != VIDEO_ENCODER_H264) {
  983. return ERROR_UNSUPPORTED;
  984. }
  985. sp<MediaSource> mediaSource;
  986. err = setupMediaSource(&mediaSource);
  987. if (err != OK) {
  988. return err;
  989. }
  990. sp<MediaSource> encoder;
  991. err = setupVideoEncoder(mediaSource, mVideoBitRate, &encoder);
  992. if (err != OK) {
  993. return err;
  994. }
  995. writer->addSource(encoder);
  996. }
  997. }
  998. if (mMaxFileDurationUs != 0) {
  999. writer->setMaxFileDuration(mMaxFileDurationUs);
  1000. }
  1001. if (mMaxFileSizeBytes != 0) {
  1002. writer->setMaxFileSize(mMaxFileSizeBytes);
  1003. }
  1004. mWriter = writer;
  1005. if(m_handle!=NULL)
  1006. dlclose(m_handle);
  1007. else
  1008. return UNKNOWN_ERROR;
  1009. return mWriter->start();
  1010. }
  1011. void StagefrightRecorder::clipVideoFrameRate() {
  1012. ALOGV("clipVideoFrameRate: encoder %d", mVideoEncoder);
  1013. int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
  1014. "enc.vid.fps.min", mVideoEncoder);
  1015. int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
  1016. "enc.vid.fps.max", mVideoEncoder);
  1017. if (mFrameRate < minFrameRate && minFrameRate != -1) {
  1018. ALOGW("Intended video encoding frame rate (%d fps) is too small"
  1019. " and will be set to (%d fps)", mFrameRate, minFrameRate);
  1020. mFrameRate = minFrameRate;
  1021. } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) {
  1022. ALOGW("Intended video encoding frame rate (%d fps) is too large"
  1023. " and will be set to (%d fps)", mFrameRate, maxFrameRate);
  1024. mFrameRate = maxFrameRate;
  1025. }
  1026. }
  1027. void StagefrightRecorder::clipVideoBitRate() {
  1028. ALOGV("clipVideoBitRate: encoder %d", mVideoEncoder);
  1029. int minBitRate = mEncoderProfiles->getVideoEncoderParamByName(
  1030. "enc.vid.bps.min", mVideoEncoder);
  1031. int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName(
  1032. "enc.vid.bps.max", mVideoEncoder);
  1033. if (mVideoBitRate < minBitRate && minBitRate != -1) {
  1034. ALOGW("Intended video encoding bit rate (%d bps) is too small"
  1035. " and will be set to (%d bps)", mVideoBitRate, minBitRate);
  1036. mVideoBitRate = minBitRate;
  1037. } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) {
  1038. ALOGW("Intended video encoding bit rate (%d bps) is too large"
  1039. " and will be set to (%d bps)", mVideoBitRate, maxBitRate);
  1040. mVideoBitRate = maxBitRate;
  1041. }
  1042. }
  1043. void StagefrightRecorder::clipVideoFrameWidth() {
  1044. ALOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder);
  1045. int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
  1046. "enc.vid.width.min", mVideoEncoder);
  1047. int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
  1048. "enc.vid.width.max", mVideoEncoder);
  1049. if (mVideoWidth < minFrameWidth && minFrameWidth != -1) {
  1050. ALOGW("Intended video encoding frame width (%d) is too small"
  1051. " and will be set to (%d)", mVideoWidth, minFrameWidth);
  1052. mVideoWidth = minFrameWidth;
  1053. } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) {
  1054. ALOGW("Intended video encoding frame width (%d) is too large"
  1055. " and will be set to (%d)", mVideoWidth, maxFrameWidth);
  1056. mVideoWidth = maxFrameWidth;
  1057. }
  1058. }
  1059. status_t StagefrightRecorder::checkVideoEncoderCapabilities() {
  1060. if (!mCaptureTimeLapse) {
  1061. // Dont clip for time lapse capture as encoder will have enough
  1062. // time to encode because of slow capture rate of time lapse.
  1063. clipVideoBitRate();
  1064. clipVideoFrameRate();
  1065. clipVideoFrameWidth();
  1066. clipVideoFrameHeight();
  1067. setDefaultProfileIfNecessary();
  1068. }
  1069. return OK;
  1070. }
  1071. // Set to use AVC baseline profile if the encoding parameters matches
  1072. // CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service.
  1073. void StagefrightRecorder::setDefaultProfileIfNecessary() {
  1074. ALOGV("setDefaultProfileIfNecessary");
  1075. camcorder_quality quality = CAMCORDER_QUALITY_LOW;
  1076. int64_t durationUs = mEncoderProfiles->getCamcorderProfileParamByName(
  1077. "duration", mCameraId, quality) * 1000000LL;
  1078. int fileFormat = mEncoderProfiles->getCamcorderProfileParamByName(
  1079. "file.format", mCameraId, quality);
  1080. int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName(
  1081. "vid.codec", mCameraId, quality);
  1082. int videoBitRate = mEncoderProfiles->getCamcorderProfileParamByName(
  1083. "vid.bps", mCameraId, quality);
  1084. int videoFrameRate = mEncoderProfiles->getCamcorderProfileParamByName(
  1085. "vid.fps", mCameraId, quality);
  1086. int videoFrameWidth = mEncoderProfiles->getCamcorderProfileParamByName(
  1087. "vid.width", mCameraId, quality);
  1088. int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName(
  1089. "vid.height", mCameraId, quality);
  1090. int audioCodec = mEncoderProfiles->getCamcorderProfileParamByName(
  1091. "aud.codec", mCameraId, quality);
  1092. int audioBitRate = mEncoderProfiles->getCamcorderProfileParamByName(
  1093. "aud.bps", mCameraId, quality);
  1094. int audioSampleRate = mEncoderProfiles->getCamcorderProfileParamByName(
  1095. "aud.hz", mCameraId, quality);
  1096. int audioChannels = mEncoderProfiles->getCamcorderProfileParamByName(
  1097. "aud.ch", mCameraId, quality);
  1098. if (durationUs == mMaxFileDurationUs &&
  1099. fileFormat == mOutputFormat &&
  1100. videoCodec == mVideoEncoder &&
  1101. videoBitRate == mVideoBitRate &&
  1102. videoFrameRate == mFrameRate &&
  1103. videoFrameWidth == mVideoWidth &&
  1104. videoFrameHeight == mVideoHeight &&
  1105. audioCodec == mAudioEncoder &&
  1106. audioBitRate == mAudioBitRate &&
  1107. audioSampleRate == mSampleRate &&
  1108. audioChannels == mAudioChannels) {
  1109. if (videoCodec == VIDEO_ENCODER_H264) {
  1110. ALOGI("Force to use AVC baseline profile");
  1111. setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline);
  1112. }
  1113. }
  1114. }
  1115. status_t StagefrightRecorder::checkAudioEncoderCapabilities() {
  1116. clipAudioBitRate();
  1117. clipAudioSampleRate();
  1118. clipNumberOfAudioChannels();
  1119. return OK;
  1120. }
  1121. void StagefrightRecorder::clipAudioBitRate() {
  1122. ALOGV("clipAudioBitRate: encoder %d", mAudioEncoder);
  1123. int minAudioBitRate =
  1124. mEncoderProfiles->getAudioEncoderParamByName(
  1125. "enc.aud.bps.min", mAudioEncoder);
  1126. if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) {
  1127. ALOGW("Intended audio encoding bit rate (%d) is too small"
  1128. " and will be set to (%d)", mAudioBitRate, minAudioBitRate);
  1129. mAudioBitRate = minAudioBitRate;
  1130. }
  1131. int maxAudioBitRate =
  1132. mEncoderProfiles->getAudioEncoderParamByName(
  1133. "enc.aud.bps.max", mAudioEncoder);
  1134. if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) {
  1135. ALOGW("Intended audio encoding bit rate (%d) is too large"
  1136. " and will be set to (%d)", mAudioBitRate, maxAudioBitRate);
  1137. mAudioBitRate = maxAudioBitRate;
  1138. }
  1139. }
  1140. void StagefrightRecorder::clipAudioSampleRate() {
  1141. ALOGV("clipAudioSampleRate: encoder %d", mAudioEncoder);
  1142. int minSampleRate =
  1143. mEncoderProfiles->getAudioEncoderParamByName(
  1144. "enc.aud.hz.min", mAudioEncoder);
  1145. if (minSampleRate != -1 && mSampleRate < minSampleRate) {
  1146. ALOGW("Intended audio sample rate (%d) is too small"
  1147. " and will be set to (%d)", mSampleRate, minSampleRate);
  1148. mSampleRate = minSampleRate;
  1149. }
  1150. int maxSampleRate =
  1151. mEncoderProfiles->getAudioEncoderParamByName(
  1152. "enc.aud.hz.max", mAudioEncoder);
  1153. if (maxSampleRate != -1 && mSampleRate > maxSampleRate) {
  1154. ALOGW("Intended audio sample rate (%d) is too large"
  1155. " and will be set to (%d)", mSampleRate, maxSampleRate);
  1156. mSampleRate = maxSampleRate;
  1157. }
  1158. }
  1159. void StagefrightRecorder::clipNumberOfAudioChannels() {
  1160. ALOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder);
  1161. int minChannels =
  1162. mEncoderProfiles->getAudioEncoderParamByName(
  1163. "enc.aud.ch.min", mAudioEncoder);
  1164. if (minChannels != -1 && mAudioChannels < minChannels) {
  1165. ALOGW("Intended number of audio channels (%d) is too small"
  1166. " and will be set to (%d)", mAudioChannels, minChannels);
  1167. mAudioChannels = minChannels;
  1168. }
  1169. int maxChannels =
  1170. mEncoderProfiles->getAudioEncoderParamByName(
  1171. "enc.aud.ch.max", mAudioEncoder);
  1172. if (maxChannels != -1 && mAudioChannels > maxChannels) {
  1173. ALOGW("Intended number of audio channels (%d) is too large"
  1174. " and will be set to (%d)", mAudioChannels, maxChannels);
  1175. mAudioChannels = maxChannels;
  1176. }
  1177. }
  1178. void StagefrightRecorder::clipVideoFrameHeight() {
  1179. ALOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder);
  1180. int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
  1181. "enc.vid.height.min", mVideoEncoder);
  1182. int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
  1183. "enc.vid.height.max", mVideoEncoder);
  1184. if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) {
  1185. ALOGW("Intended video encoding frame height (%d) is too small"
  1186. " and will be set to (%d)", mVideoHeight, minFrameHeight);
  1187. mVideoHeight = minFrameHeight;
  1188. } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) {
  1189. ALOGW("Intended video encoding frame height (%d) is too large"
  1190. " and will be set to (%d)", mVideoHeight, maxFrameHeight);
  1191. mVideoHeight = maxFrameHeight;
  1192. }
  1193. }
  1194. // Set up the appropriate MediaSource depending on the chosen option
  1195. status_t StagefrightRecorder::setupMediaSource(
  1196. sp<MediaSource> *mediaSource) {
  1197. if (mVideoSource == VIDEO_SOURCE_DEFAULT
  1198. || mVideoSource == VIDEO_SOURCE_CAMERA ||mVideoSource == VIDEO_SOURCE_UI ) {
  1199. if(mVideoSource != VIDEO_SOURCE_UI)
  1200. {
  1201. sp<CameraSource> cameraSource;
  1202. status_t err = setupCameraSource(&cameraSource);
  1203. if (err != OK) {
  1204. return err;
  1205. }
  1206. *mediaSource = cameraSource;
  1207. }
  1208. else
  1209. {
  1210. sp<MediaSource> uiSource;
  1211. ALOGE("setupMediaSource setupUISource");
  1212. status_t err = setupUISource(&uiSource);
  1213. if (err != OK) {
  1214. return err;
  1215. }
  1216. *mediaSource = uiSource;
  1217. }
  1218. } else if (mVideoSource == VIDEO_SOURCE_GRALLOC_BUFFER) {
  1219. // If using GRAlloc buffers, setup surfacemediasource.
  1220. // Later a handle to that will be passed
  1221. // to the client side when queried
  1222. status_t err = setupSurfaceMediaSource();
  1223. if (err != OK) {
  1224. return err;
  1225. }
  1226. *mediaSource = mSurfaceMediaSource;
  1227. } else {
  1228. return INVALID_OPERATION;
  1229. }
  1230. return OK;
  1231. }
  1232. // setupSurfaceMediaSource creates a source with the given
  1233. // width and height and framerate.
  1234. // TODO: This could go in a static function inside SurfaceMediaSource
  1235. // similar to that in CameraSource
  1236. status_t StagefrightRecorder::setupSurfaceMediaSource() {
  1237. status_t err = OK;
  1238. mSurfaceMediaSource = new SurfaceMediaSource(mVideoWidth, mVideoHeight);
  1239. if (mSurfaceMediaSource == NULL) {
  1240. return NO_INIT;
  1241. }
  1242. if (mFrameRate == -1) {
  1243. int32_t frameRate = 0;
  1244. CHECK (mSurfaceMediaSource->getFormat()->findInt32(
  1245. kKeyFrameRate, &frameRate));
  1246. ALOGI("Frame rate is not explicitly set. Use the current frame "
  1247. "rate (%d fps)", frameRate);
  1248. mFrameRate = frameRate;
  1249. } else {
  1250. err = mSurfaceMediaSource->setFrameRate(mFrameRate);
  1251. }
  1252. CHECK(mFrameRate != -1);
  1253. mIsMetaDataStoredInVideoBuffers =
  1254. mSurfaceMediaSource->isMetaDataStoredInVideoBuffers();
  1255. return err;
  1256. }
  1257. status_t StagefrightRecorder::setupCameraSource(
  1258. sp<CameraSource> *cameraSource) {
  1259. status_t err = OK;
  1260. if ((err = checkVideoEncoderCapabilities()) != OK) {
  1261. return err;
  1262. }
  1263. Size videoSize;
  1264. videoSize.width = mVideoWidth;
  1265. videoSize.height = mVideoHeight;
  1266. if (mCaptureTimeLapse) {
  1267. if (mTimeBetweenTimeLapseFrameCaptureUs < 0) {
  1268. ALOGE("Invalid mTimeBetweenTimeLapseFrameCaptureUs value: %lld",
  1269. mTimeBetweenTimeLapseFrameCaptureUs);
  1270. return BAD_VALUE;
  1271. }
  1272. mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera(
  1273. mCamera, mCameraProxy, mCameraId,
  1274. videoSize, mFrameRate, mPreviewSurface,
  1275. mTimeBetweenTimeLapseFrameCaptureUs);
  1276. *cameraSource = mCameraSourceTimeLap