MediaRecorderClient.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. ** Copyright 2008, 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 "MediaRecorderService"
  18. #include <utils/Log.h>
  19. #include "MediaRecorderClient.h"
  20. #include "MediaPlayerService.h"
  21. #include "StagefrightRecorder.h"
  22. #include <android/hardware/media/omx/1.0/IOmx.h>
  23. #include <android/hardware/media/c2/1.0/IComponentStore.h>
  24. #include <binder/IPCThreadState.h>
  25. #include <binder/IServiceManager.h>
  26. #include <binder/MemoryHeapBase.h>
  27. #include <binder/MemoryBase.h>
  28. #include <codec2/hidl/client.h>
  29. #include <cutils/atomic.h>
  30. #include <cutils/properties.h> // for property_get
  31. #include <gui/IGraphicBufferProducer.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <system/audio.h>
  35. #include <utils/String16.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. namespace android {
  40. const char* cameraPermission = "android.permission.CAMERA";
  41. const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
  42. static bool checkPermission(const char* permissionString) {
  43. if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
  44. bool ok = checkCallingPermission(String16(permissionString));
  45. if (!ok) ALOGE("Request requires %s", permissionString);
  46. return ok;
  47. }
  48. status_t MediaRecorderClient::setInputSurface(const sp<PersistentSurface>& surface)
  49. {
  50. ALOGV("setInputSurface");
  51. Mutex::Autolock lock(mLock);
  52. if (mRecorder == NULL) {
  53. ALOGE("recorder is not initialized");
  54. return NO_INIT;
  55. }
  56. return mRecorder->setInputSurface(surface);
  57. }
  58. sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource()
  59. {
  60. ALOGV("Query SurfaceMediaSource");
  61. Mutex::Autolock lock(mLock);
  62. if (mRecorder == NULL) {
  63. ALOGE("recorder is not initialized");
  64. return NULL;
  65. }
  66. return mRecorder->querySurfaceMediaSource();
  67. }
  68. status_t MediaRecorderClient::setCamera(const sp<hardware::ICamera>& camera,
  69. const sp<ICameraRecordingProxy>& proxy)
  70. {
  71. ALOGV("setCamera");
  72. Mutex::Autolock lock(mLock);
  73. if (mRecorder == NULL) {
  74. ALOGE("recorder is not initialized");
  75. return NO_INIT;
  76. }
  77. return mRecorder->setCamera(camera, proxy);
  78. }
  79. status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
  80. {
  81. ALOGV("setPreviewSurface");
  82. Mutex::Autolock lock(mLock);
  83. if (mRecorder == NULL) {
  84. ALOGE("recorder is not initialized");
  85. return NO_INIT;
  86. }
  87. return mRecorder->setPreviewSurface(surface);
  88. }
  89. status_t MediaRecorderClient::setVideoSource(int vs)
  90. {
  91. ALOGV("setVideoSource(%d)", vs);
  92. // Check camera permission for sources other than SURFACE
  93. if (vs != VIDEO_SOURCE_SURFACE && !checkPermission(cameraPermission)) {
  94. return PERMISSION_DENIED;
  95. }
  96. Mutex::Autolock lock(mLock);
  97. if (mRecorder == NULL) {
  98. ALOGE("recorder is not initialized");
  99. return NO_INIT;
  100. }
  101. return mRecorder->setVideoSource((video_source)vs);
  102. }
  103. status_t MediaRecorderClient::setAudioSource(int as)
  104. {
  105. ALOGV("setAudioSource(%d)", as);
  106. if (!checkPermission(recordAudioPermission)) {
  107. return PERMISSION_DENIED;
  108. }
  109. Mutex::Autolock lock(mLock);
  110. if (mRecorder == NULL) {
  111. ALOGE("recorder is not initialized");
  112. return NO_INIT;
  113. }
  114. return mRecorder->setAudioSource((audio_source_t)as);
  115. }
  116. status_t MediaRecorderClient::setOutputFormat(int of)
  117. {
  118. ALOGV("setOutputFormat(%d)", of);
  119. Mutex::Autolock lock(mLock);
  120. if (mRecorder == NULL) {
  121. ALOGE("recorder is not initialized");
  122. return NO_INIT;
  123. }
  124. return mRecorder->setOutputFormat((output_format)of);
  125. }
  126. status_t MediaRecorderClient::setVideoEncoder(int ve)
  127. {
  128. ALOGV("setVideoEncoder(%d)", ve);
  129. Mutex::Autolock lock(mLock);
  130. if (mRecorder == NULL) {
  131. ALOGE("recorder is not initialized");
  132. return NO_INIT;
  133. }
  134. return mRecorder->setVideoEncoder((video_encoder)ve);
  135. }
  136. status_t MediaRecorderClient::setAudioEncoder(int ae)
  137. {
  138. ALOGV("setAudioEncoder(%d)", ae);
  139. Mutex::Autolock lock(mLock);
  140. if (mRecorder == NULL) {
  141. ALOGE("recorder is not initialized");
  142. return NO_INIT;
  143. }
  144. return mRecorder->setAudioEncoder((audio_encoder)ae);
  145. }
  146. status_t MediaRecorderClient::setOutputFile(int fd)
  147. {
  148. ALOGV("setOutputFile(%d)", fd);
  149. Mutex::Autolock lock(mLock);
  150. if (mRecorder == NULL) {
  151. ALOGE("recorder is not initialized");
  152. return NO_INIT;
  153. }
  154. return mRecorder->setOutputFile(fd);
  155. }
  156. status_t MediaRecorderClient::setNextOutputFile(int fd)
  157. {
  158. ALOGV("setNextOutputFile(%d)", fd);
  159. Mutex::Autolock lock(mLock);
  160. if (mRecorder == NULL) {
  161. ALOGE("recorder is not initialized");
  162. return NO_INIT;
  163. }
  164. return mRecorder->setNextOutputFile(fd);
  165. }
  166. status_t MediaRecorderClient::setVideoSize(int width, int height)
  167. {
  168. ALOGV("setVideoSize(%dx%d)", width, height);
  169. Mutex::Autolock lock(mLock);
  170. if (mRecorder == NULL) {
  171. ALOGE("recorder is not initialized");
  172. return NO_INIT;
  173. }
  174. return mRecorder->setVideoSize(width, height);
  175. }
  176. status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
  177. {
  178. ALOGV("setVideoFrameRate(%d)", frames_per_second);
  179. Mutex::Autolock lock(mLock);
  180. if (mRecorder == NULL) {
  181. ALOGE("recorder is not initialized");
  182. return NO_INIT;
  183. }
  184. return mRecorder->setVideoFrameRate(frames_per_second);
  185. }
  186. status_t MediaRecorderClient::setParameters(const String8& params) {
  187. ALOGV("setParameters(%s)", params.string());
  188. Mutex::Autolock lock(mLock);
  189. if (mRecorder == NULL) {
  190. ALOGE("recorder is not initialized");
  191. return NO_INIT;
  192. }
  193. return mRecorder->setParameters(params);
  194. }
  195. status_t MediaRecorderClient::prepare()
  196. {
  197. ALOGV("prepare");
  198. Mutex::Autolock lock(mLock);
  199. if (mRecorder == NULL) {
  200. ALOGE("recorder is not initialized");
  201. return NO_INIT;
  202. }
  203. return mRecorder->prepare();
  204. }
  205. status_t MediaRecorderClient::getMaxAmplitude(int* max)
  206. {
  207. ALOGV("getMaxAmplitude");
  208. Mutex::Autolock lock(mLock);
  209. if (mRecorder == NULL) {
  210. ALOGE("recorder is not initialized");
  211. return NO_INIT;
  212. }
  213. return mRecorder->getMaxAmplitude(max);
  214. }
  215. status_t MediaRecorderClient::getMetrics(Parcel* reply)
  216. {
  217. ALOGV("MediaRecorderClient::getMetrics");
  218. Mutex::Autolock lock(mLock);
  219. if (mRecorder == NULL) {
  220. ALOGE("recorder is not initialized");
  221. return NO_INIT;
  222. }
  223. return mRecorder->getMetrics(reply);
  224. }
  225. status_t MediaRecorderClient::start()
  226. {
  227. ALOGV("start");
  228. Mutex::Autolock lock(mLock);
  229. if (mRecorder == NULL) {
  230. ALOGE("recorder is not initialized");
  231. return NO_INIT;
  232. }
  233. return mRecorder->start();
  234. }
  235. status_t MediaRecorderClient::stop()
  236. {
  237. ALOGV("stop");
  238. Mutex::Autolock lock(mLock);
  239. if (mRecorder == NULL) {
  240. ALOGE("recorder is not initialized");
  241. return NO_INIT;
  242. }
  243. return mRecorder->stop();
  244. }
  245. status_t MediaRecorderClient::pause()
  246. {
  247. ALOGV("pause");
  248. Mutex::Autolock lock(mLock);
  249. if (mRecorder == NULL) {
  250. ALOGE("recorder is not initialized");
  251. return NO_INIT;
  252. }
  253. return mRecorder->pause();
  254. }
  255. status_t MediaRecorderClient::resume()
  256. {
  257. ALOGV("resume");
  258. Mutex::Autolock lock(mLock);
  259. if (mRecorder == NULL) {
  260. ALOGE("recorder is not initialized");
  261. return NO_INIT;
  262. }
  263. return mRecorder->resume();
  264. }
  265. status_t MediaRecorderClient::init()
  266. {
  267. ALOGV("init");
  268. Mutex::Autolock lock(mLock);
  269. if (mRecorder == NULL) {
  270. ALOGE("recorder is not initialized");
  271. return NO_INIT;
  272. }
  273. return mRecorder->init();
  274. }
  275. status_t MediaRecorderClient::close()
  276. {
  277. ALOGV("close");
  278. Mutex::Autolock lock(mLock);
  279. if (mRecorder == NULL) {
  280. ALOGE("recorder is not initialized");
  281. return NO_INIT;
  282. }
  283. return mRecorder->close();
  284. }
  285. status_t MediaRecorderClient::reset()
  286. {
  287. ALOGV("reset");
  288. Mutex::Autolock lock(mLock);
  289. if (mRecorder == NULL) {
  290. ALOGE("recorder is not initialized");
  291. return NO_INIT;
  292. }
  293. return mRecorder->reset();
  294. }
  295. status_t MediaRecorderClient::release()
  296. {
  297. ALOGV("release");
  298. Mutex::Autolock lock(mLock);
  299. if (mRecorder != NULL) {
  300. delete mRecorder;
  301. mRecorder = NULL;
  302. wp<MediaRecorderClient> client(this);
  303. mMediaPlayerService->removeMediaRecorderClient(client);
  304. }
  305. mDeathNotifiers.clear();
  306. return NO_ERROR;
  307. }
  308. MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid,
  309. const String16& opPackageName)
  310. {
  311. ALOGV("Client constructor");
  312. mPid = pid;
  313. mRecorder = new StagefrightRecorder(opPackageName);
  314. mMediaPlayerService = service;
  315. }
  316. MediaRecorderClient::~MediaRecorderClient()
  317. {
  318. ALOGV("Client destructor");
  319. release();
  320. }
  321. MediaRecorderClient::AudioDeviceUpdatedNotifier::AudioDeviceUpdatedNotifier(
  322. const sp<IMediaRecorderClient>& listener) {
  323. mListener = listener;
  324. }
  325. MediaRecorderClient::AudioDeviceUpdatedNotifier::~AudioDeviceUpdatedNotifier() {
  326. }
  327. void MediaRecorderClient::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
  328. audio_io_handle_t audioIo,
  329. audio_port_handle_t deviceId) {
  330. sp<IMediaRecorderClient> listener = mListener.promote();
  331. if (listener != NULL) {
  332. listener->notify(MEDIA_RECORDER_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
  333. } else {
  334. ALOGW("listener for process %d death is gone", MEDIA_RECORDER_AUDIO_ROUTING_CHANGED);
  335. }
  336. }
  337. status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
  338. {
  339. ALOGV("setListener");
  340. Mutex::Autolock lock(mLock);
  341. mDeathNotifiers.clear();
  342. if (mRecorder == NULL) {
  343. ALOGE("recorder is not initialized");
  344. return NO_INIT;
  345. }
  346. mRecorder->setListener(listener);
  347. sp<IServiceManager> sm = initdefaultServiceManager();
  348. // WORKAROUND: We don't know if camera exists here and getService might block for 5 seconds.
  349. // Use checkService for camera if we don't know it exists.
  350. static std::atomic<bool> sCameraChecked(false); // once true never becomes false.
  351. static std::atomic<bool> sCameraVerified(false); // once true never becomes false.
  352. sp<IBinder> binder = (sCameraVerified || !sCameraChecked)
  353. ? sm->getService(String16("media.camera")) : sm->checkService(String16("media.camera"));
  354. // If the device does not have a camera, do not create a death listener for it.
  355. if (binder != NULL) {
  356. sCameraVerified = true;
  357. mDeathNotifiers.emplace_back(
  358. binder, [l = wp<IMediaRecorderClient>(listener)](){
  359. sp<IMediaRecorderClient> listener = l.promote();
  360. if (listener) {
  361. ALOGV("media.camera service died. "
  362. "Sending death notification.");
  363. listener->notify(
  364. MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  365. MediaPlayerService::CAMERA_PROCESS_DEATH);
  366. } else {
  367. ALOGW("media.camera service died without a death handler.");
  368. }
  369. });
  370. }
  371. sCameraChecked = true;
  372. {
  373. using ::android::hidl::base::V1_0::IBase;
  374. // Listen to OMX's IOmxStore/default
  375. {
  376. sp<IBase> base = ::android::hardware::media::omx::V1_0::
  377. IOmx::getService();
  378. if (base == nullptr) {
  379. ALOGD("OMX service is not available");
  380. } else {
  381. mDeathNotifiers.emplace_back(
  382. base, [l = wp<IMediaRecorderClient>(listener)](){
  383. sp<IMediaRecorderClient> listener = l.promote();
  384. if (listener) {
  385. ALOGV("OMX service died. "
  386. "Sending death notification.");
  387. listener->notify(
  388. MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  389. MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
  390. } else {
  391. ALOGW("OMX service died without a death handler.");
  392. }
  393. });
  394. }
  395. }
  396. // Listen to Codec2's IComponentStore instances
  397. {
  398. for (std::shared_ptr<Codec2Client> const& client :
  399. Codec2Client::CreateFromAllServices()) {
  400. sp<IBase> base = client->getBase();
  401. mDeathNotifiers.emplace_back(
  402. base, [l = wp<IMediaRecorderClient>(listener),
  403. name = std::string(client->getServiceName())]() {
  404. sp<IMediaRecorderClient> listener = l.promote();
  405. if (listener) {
  406. ALOGV("Codec2 service \"%s\" died. "
  407. "Sending death notification",
  408. name.c_str());
  409. listener->notify(
  410. MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  411. MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
  412. } else {
  413. ALOGW("Codec2 service \"%s\" died "
  414. "without a death handler",
  415. name.c_str());
  416. }
  417. });
  418. }
  419. }
  420. }
  421. mAudioDeviceUpdatedNotifier = new AudioDeviceUpdatedNotifier(listener);
  422. mRecorder->setAudioDeviceCallback(mAudioDeviceUpdatedNotifier);
  423. return OK;
  424. }
  425. status_t MediaRecorderClient::setClientName(const String16& clientName) {
  426. ALOGV("setClientName(%s)", String8(clientName).string());
  427. Mutex::Autolock lock(mLock);
  428. if (mRecorder == NULL) {
  429. ALOGE("recorder is not initialized");
  430. return NO_INIT;
  431. }
  432. return mRecorder->setClientName(clientName);
  433. }
  434. status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) {
  435. if (mRecorder != NULL) {
  436. return mRecorder->dump(fd, args);
  437. }
  438. return OK;
  439. }
  440. status_t MediaRecorderClient::setInputDevice(audio_port_handle_t deviceId) {
  441. ALOGV("setInputDevice(%d)", deviceId);
  442. Mutex::Autolock lock(mLock);
  443. if (mRecorder != NULL) {
  444. return mRecorder->setInputDevice(deviceId);
  445. }
  446. return NO_INIT;
  447. }
  448. status_t MediaRecorderClient::getRoutedDeviceId(audio_port_handle_t* deviceId) {
  449. ALOGV("getRoutedDeviceId");
  450. Mutex::Autolock lock(mLock);
  451. if (mRecorder != NULL) {
  452. return mRecorder->getRoutedDeviceId(deviceId);
  453. }
  454. return NO_INIT;
  455. }
  456. status_t MediaRecorderClient::enableAudioDeviceCallback(bool enabled) {
  457. ALOGV("enableDeviceCallback: %d", enabled);
  458. Mutex::Autolock lock(mLock);
  459. if (mRecorder != NULL) {
  460. return mRecorder->enableAudioDeviceCallback(enabled);
  461. }
  462. return NO_INIT;
  463. }
  464. status_t MediaRecorderClient::getActiveMicrophones(
  465. std::vector<media::MicrophoneInfo>* activeMicrophones) {
  466. ALOGV("getActiveMicrophones");
  467. Mutex::Autolock lock(mLock);
  468. if (mRecorder != NULL) {
  469. return mRecorder->getActiveMicrophones(activeMicrophones);
  470. }
  471. return NO_INIT;
  472. }
  473. status_t MediaRecorderClient::setPreferredMicrophoneDirection(
  474. audio_microphone_direction_t direction) {
  475. ALOGV("setPreferredMicrophoneDirection(%d)", direction);
  476. if (mRecorder != NULL) {
  477. return mRecorder->setPreferredMicrophoneDirection(direction);
  478. }
  479. return NO_INIT;
  480. }
  481. status_t MediaRecorderClient::setPreferredMicrophoneFieldDimension(float zoom) {
  482. ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
  483. if (mRecorder != NULL) {
  484. return mRecorder->setPreferredMicrophoneFieldDimension(zoom);
  485. }
  486. return NO_INIT;
  487. }
  488. status_t MediaRecorderClient::getPortId(audio_port_handle_t *portId) {
  489. ALOGV("getPortId");
  490. Mutex::Autolock lock(mLock);
  491. if (mRecorder != NULL) {
  492. return mRecorder->getPortId(portId);
  493. }
  494. return NO_INIT;
  495. }
  496. }; // namespace android