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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaRecorderService"
19 #include <utils/Log.h>
20
21 #include "MediaRecorderClient.h"
22 #include "MediaPlayerService.h"
23 #include "StagefrightRecorder.h"
24
25 #include <android/binder_auto_utils.h>
26 #include <android/hardware/media/omx/1.0/IOmx.h>
27 #include <android/hardware/media/c2/1.0/IComponentStore.h>
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/MemoryHeapBase.h>
31 #include <binder/MemoryBase.h>
32 #include <camera/CameraUtils.h>
33 #include <codec2/hidl/client.h>
34 #include <cutils/atomic.h>
35 #include <cutils/properties.h> // for property_get
36 #include <gui/IGraphicBufferProducer.h>
37 #include <mediautils/ServiceUtilities.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <system/audio.h>
41 #include <utils/String16.h>
42
43 #include <dirent.h>
44 #include <unistd.h>
45 #include <string.h>
46
47 namespace android {
48
49 const char* cameraPermission = "android.permission.CAMERA";
50
checkPermission(const char * permissionString)51 static bool checkPermission(const char* permissionString) {
52 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
53 bool ok = checkCallingPermission(String16(permissionString));
54 if (!ok) ALOGE("Request requires %s", permissionString);
55 return ok;
56 }
57
setInputSurface(const sp<PersistentSurface> & surface)58 status_t MediaRecorderClient::setInputSurface(const sp<PersistentSurface>& surface)
59 {
60 ALOGV("setInputSurface");
61 Mutex::Autolock lock(mLock);
62 if (mRecorder == NULL) {
63 ALOGE("recorder is not initialized");
64 return NO_INIT;
65 }
66 return mRecorder->setInputSurface(surface);
67 }
68
querySurfaceMediaSource()69 sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource()
70 {
71 ALOGV("Query SurfaceMediaSource");
72 Mutex::Autolock lock(mLock);
73 if (mRecorder == NULL) {
74 ALOGE("recorder is not initialized");
75 return NULL;
76 }
77 return mRecorder->querySurfaceMediaSource();
78 }
79
80
81
setCamera(const sp<hardware::ICamera> & camera,const sp<ICameraRecordingProxy> & proxy)82 status_t MediaRecorderClient::setCamera(const sp<hardware::ICamera>& camera,
83 const sp<ICameraRecordingProxy>& proxy)
84 {
85 ALOGV("setCamera");
86 Mutex::Autolock lock(mLock);
87 if (mRecorder == NULL) {
88 ALOGE("recorder is not initialized");
89 return NO_INIT;
90 }
91 return mRecorder->setCamera(camera, proxy);
92 }
93
setPreviewSurface(const sp<IGraphicBufferProducer> & surface)94 status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
95 {
96 ALOGV("setPreviewSurface");
97 Mutex::Autolock lock(mLock);
98 if (mRecorder == NULL) {
99 ALOGE("recorder is not initialized");
100 return NO_INIT;
101 }
102 return mRecorder->setPreviewSurface(surface);
103 }
104
setVideoSource(int vs)105 status_t MediaRecorderClient::setVideoSource(int vs)
106 {
107 ALOGV("setVideoSource(%d)", vs);
108 // Check camera permission for sources other than SURFACE
109 if (vs != VIDEO_SOURCE_SURFACE && !checkPermission(cameraPermission)) {
110 return PERMISSION_DENIED;
111 }
112 Mutex::Autolock lock(mLock);
113 if (mRecorder == NULL) {
114 ALOGE("recorder is not initialized");
115 return NO_INIT;
116 }
117 return mRecorder->setVideoSource((video_source)vs);
118 }
119
setAudioSource(int as)120 status_t MediaRecorderClient::setAudioSource(int as)
121 {
122 ALOGV("setAudioSource(%d)", as);
123 if (as < AUDIO_SOURCE_DEFAULT
124 || (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) {
125 ALOGE("Invalid audio source: %d", as);
126 return BAD_VALUE;
127 }
128
129 if ((as == AUDIO_SOURCE_FM_TUNER
130 && !(captureAudioOutputAllowed(mAttributionSource)
131 || captureTunerAudioInputAllowed(mAttributionSource)))
132 || (as == AUDIO_SOURCE_REMOTE_SUBMIX
133 && !(captureAudioOutputAllowed(mAttributionSource)
134 || modifyAudioRoutingAllowed(mAttributionSource)))
135 || (as == AUDIO_SOURCE_ECHO_REFERENCE
136 && !captureAudioOutputAllowed(mAttributionSource))
137 || !recordingAllowed(mAttributionSource, (audio_source_t)as)) {
138 return PERMISSION_DENIED;
139 }
140 Mutex::Autolock lock(mLock);
141 if (mRecorder == NULL) {
142 ALOGE("recorder is not initialized");
143 return NO_INIT;
144 }
145 return mRecorder->setAudioSource((audio_source_t)as);
146 }
147
setPrivacySensitive(bool privacySensitive)148 status_t MediaRecorderClient::setPrivacySensitive(bool privacySensitive)
149 {
150 ALOGV("%s(%s)", __func__, privacySensitive ? "true" : "false");
151 Mutex::Autolock lock(mLock);
152 if (mRecorder == NULL) {
153 ALOGE("%s: recorder is not initialized", __func__);
154 return NO_INIT;
155 }
156 return mRecorder->setPrivacySensitive(privacySensitive);
157 }
158
isPrivacySensitive(bool * privacySensitive) const159 status_t MediaRecorderClient::isPrivacySensitive(bool *privacySensitive) const
160 {
161 Mutex::Autolock lock(mLock);
162 if (mRecorder == NULL) {
163 ALOGE("%s: recorder is not initialized", __func__);
164 return NO_INIT;
165 }
166 status_t status = mRecorder->isPrivacySensitive(privacySensitive);
167 ALOGV("%s: status: %d enabled: %s", __func__, status, *privacySensitive ? "true" : "false");
168 return status;
169 }
170
setOutputFormat(int of)171 status_t MediaRecorderClient::setOutputFormat(int of)
172 {
173 ALOGV("setOutputFormat(%d)", of);
174 Mutex::Autolock lock(mLock);
175 if (mRecorder == NULL) {
176 ALOGE("recorder is not initialized");
177 return NO_INIT;
178 }
179 return mRecorder->setOutputFormat((output_format)of);
180 }
181
setVideoEncoder(int ve)182 status_t MediaRecorderClient::setVideoEncoder(int ve)
183 {
184 ALOGV("setVideoEncoder(%d)", ve);
185 Mutex::Autolock lock(mLock);
186 if (mRecorder == NULL) {
187 ALOGE("recorder is not initialized");
188 return NO_INIT;
189 }
190 return mRecorder->setVideoEncoder((video_encoder)ve);
191 }
192
setAudioEncoder(int ae)193 status_t MediaRecorderClient::setAudioEncoder(int ae)
194 {
195 ALOGV("setAudioEncoder(%d)", ae);
196 Mutex::Autolock lock(mLock);
197 if (mRecorder == NULL) {
198 ALOGE("recorder is not initialized");
199 return NO_INIT;
200 }
201 return mRecorder->setAudioEncoder((audio_encoder)ae);
202 }
203
setOutputFile(int fd)204 status_t MediaRecorderClient::setOutputFile(int fd)
205 {
206 ALOGV("setOutputFile(%d)", fd);
207 Mutex::Autolock lock(mLock);
208 if (mRecorder == NULL) {
209 ALOGE("recorder is not initialized");
210 return NO_INIT;
211 }
212 return mRecorder->setOutputFile(fd);
213 }
214
setNextOutputFile(int fd)215 status_t MediaRecorderClient::setNextOutputFile(int fd)
216 {
217 ALOGV("setNextOutputFile(%d)", fd);
218 Mutex::Autolock lock(mLock);
219 if (mRecorder == NULL) {
220 ALOGE("recorder is not initialized");
221 return NO_INIT;
222 }
223 return mRecorder->setNextOutputFile(fd);
224 }
225
setVideoSize(int width,int height)226 status_t MediaRecorderClient::setVideoSize(int width, int height)
227 {
228 ALOGV("setVideoSize(%dx%d)", width, height);
229 Mutex::Autolock lock(mLock);
230 if (mRecorder == NULL) {
231 ALOGE("recorder is not initialized");
232 return NO_INIT;
233 }
234 return mRecorder->setVideoSize(width, height);
235 }
236
setVideoFrameRate(int frames_per_second)237 status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
238 {
239 ALOGV("setVideoFrameRate(%d)", frames_per_second);
240 Mutex::Autolock lock(mLock);
241 if (mRecorder == NULL) {
242 ALOGE("recorder is not initialized");
243 return NO_INIT;
244 }
245 return mRecorder->setVideoFrameRate(frames_per_second);
246 }
247
setParameters(const String8 & params)248 status_t MediaRecorderClient::setParameters(const String8& params) {
249 ALOGV("setParameters(%s)", params.c_str());
250 Mutex::Autolock lock(mLock);
251 if (mRecorder == NULL) {
252 ALOGE("recorder is not initialized");
253 return NO_INIT;
254 }
255 return mRecorder->setParameters(params);
256 }
257
prepare()258 status_t MediaRecorderClient::prepare()
259 {
260 ALOGV("prepare");
261 Mutex::Autolock lock(mLock);
262 if (mRecorder == NULL) {
263 ALOGE("recorder is not initialized");
264 return NO_INIT;
265 }
266 return mRecorder->prepare();
267 }
268
269
getMaxAmplitude(int * max)270 status_t MediaRecorderClient::getMaxAmplitude(int* max)
271 {
272 ALOGV("getMaxAmplitude");
273 Mutex::Autolock lock(mLock);
274 if (mRecorder == NULL) {
275 ALOGE("recorder is not initialized");
276 return NO_INIT;
277 }
278 return mRecorder->getMaxAmplitude(max);
279 }
280
getMetrics(Parcel * reply)281 status_t MediaRecorderClient::getMetrics(Parcel* reply)
282 {
283 ALOGV("MediaRecorderClient::getMetrics");
284 Mutex::Autolock lock(mLock);
285 if (mRecorder == NULL) {
286 ALOGE("recorder is not initialized");
287 return NO_INIT;
288 }
289 return mRecorder->getMetrics(reply);
290 }
291
start()292 status_t MediaRecorderClient::start()
293 {
294 ALOGV("start");
295 Mutex::Autolock lock(mLock);
296 if (mRecorder == NULL) {
297 ALOGE("recorder is not initialized");
298 return NO_INIT;
299 }
300 return mRecorder->start();
301
302 }
303
stop()304 status_t MediaRecorderClient::stop()
305 {
306 ALOGV("stop");
307 Mutex::Autolock lock(mLock);
308 if (mRecorder == NULL) {
309 ALOGE("recorder is not initialized");
310 return NO_INIT;
311 }
312 return mRecorder->stop();
313 }
314
pause()315 status_t MediaRecorderClient::pause()
316 {
317 ALOGV("pause");
318 Mutex::Autolock lock(mLock);
319 if (mRecorder == NULL) {
320 ALOGE("recorder is not initialized");
321 return NO_INIT;
322 }
323 return mRecorder->pause();
324
325 }
326
resume()327 status_t MediaRecorderClient::resume()
328 {
329 ALOGV("resume");
330 Mutex::Autolock lock(mLock);
331 if (mRecorder == NULL) {
332 ALOGE("recorder is not initialized");
333 return NO_INIT;
334 }
335 return mRecorder->resume();
336 }
337
init()338 status_t MediaRecorderClient::init()
339 {
340 ALOGV("init");
341 Mutex::Autolock lock(mLock);
342 if (mRecorder == NULL) {
343 ALOGE("recorder is not initialized");
344 return NO_INIT;
345 }
346 return mRecorder->init();
347 }
348
close()349 status_t MediaRecorderClient::close()
350 {
351 ALOGV("close");
352 Mutex::Autolock lock(mLock);
353 if (mRecorder == NULL) {
354 ALOGE("recorder is not initialized");
355 return NO_INIT;
356 }
357 return mRecorder->close();
358 }
359
360
reset()361 status_t MediaRecorderClient::reset()
362 {
363 ALOGV("reset");
364 Mutex::Autolock lock(mLock);
365 if (mRecorder == NULL) {
366 ALOGE("recorder is not initialized");
367 return NO_INIT;
368 }
369 return mRecorder->reset();
370 }
371
release()372 status_t MediaRecorderClient::release()
373 {
374 ALOGV("release");
375 Mutex::Autolock lock(mLock);
376 if (mRecorder != NULL) {
377 delete mRecorder;
378 mRecorder = NULL;
379 wp<MediaRecorderClient> client(this);
380 mMediaPlayerService->removeMediaRecorderClient(client);
381 }
382 mDeathNotifiers.clear();
383 return NO_ERROR;
384 }
385
MediaRecorderClient(const sp<MediaPlayerService> & service,const AttributionSourceState & attributionSource)386 MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service,
387 const AttributionSourceState& attributionSource)
388 {
389 ALOGV("Client constructor");
390 // attribution source already validated in createMediaRecorder
391 mAttributionSource = attributionSource;
392 mRecorder = new StagefrightRecorder(attributionSource);
393 mMediaPlayerService = service;
394 }
395
~MediaRecorderClient()396 MediaRecorderClient::~MediaRecorderClient()
397 {
398 ALOGV("Client destructor");
399 release();
400 }
401
AudioDeviceUpdatedNotifier(const sp<IMediaRecorderClient> & listener)402 MediaRecorderClient::AudioDeviceUpdatedNotifier::AudioDeviceUpdatedNotifier(
403 const sp<IMediaRecorderClient>& listener) {
404 mListener = listener;
405 }
406
~AudioDeviceUpdatedNotifier()407 MediaRecorderClient::AudioDeviceUpdatedNotifier::~AudioDeviceUpdatedNotifier() {
408 }
409
onAudioDeviceUpdate(audio_io_handle_t audioIo,const DeviceIdVector & deviceIds)410 void MediaRecorderClient::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
411 audio_io_handle_t audioIo,
412 const DeviceIdVector& deviceIds) {
413 ALOGD("onAudioDeviceUpdate deviceIds: %s", toString(deviceIds).c_str());
414 sp<IMediaRecorderClient> listener = mListener.promote();
415 if (listener != NULL) {
416 // Java should query the new device ids once it gets the event.
417 // TODO(b/378505346): Pass the deviceIds to Java to avoid race conditions.
418 listener->notify(MEDIA_RECORDER_AUDIO_ROUTING_CHANGED, audioIo, 0 /*ext2*/);
419 } else {
420 ALOGW("listener for process %d death is gone", MEDIA_RECORDER_AUDIO_ROUTING_CHANGED);
421 }
422 }
423
setListener(const sp<IMediaRecorderClient> & listener)424 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
425 {
426 ALOGV("setListener");
427 Mutex::Autolock lock(mLock);
428 mDeathNotifiers.clear();
429 if (mRecorder == NULL) {
430 ALOGE("recorder is not initialized");
431 return NO_INIT;
432 }
433 mRecorder->setListener(listener);
434
435 sp<IServiceManager> sm = defaultServiceManager();
436
437 static const bool sCameraDisabled = CameraUtils::isCameraServiceDisabled();
438
439 if (!sCameraDisabled) {
440 // WORKAROUND: We don't know if camera exists here and getService might block for 5 seconds.
441 // Use checkService for camera if we don't know it exists.
442 static std::atomic<bool> sCameraChecked(false); // once true never becomes false.
443 static std::atomic<bool> sCameraVerified(false); // once true never becomes false.
444
445 sp<IBinder> binder = (sCameraVerified || !sCameraChecked)
446 ? sm->getService(String16("media.camera")) : sm->checkService(String16("media.camera"));
447 // If the device does not have a camera, do not create a death listener for it.
448 if (binder != NULL) {
449 sCameraVerified = true;
450 mDeathNotifiers.emplace_back(
451 binder, [l = wp<IMediaRecorderClient>(listener)](){
452 sp<IMediaRecorderClient> listener = l.promote();
453 if (listener) {
454 ALOGV("media.camera service died. "
455 "Sending death notification.");
456 listener->notify(
457 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
458 MediaPlayerService::CAMERA_PROCESS_DEATH);
459 } else {
460 ALOGW("media.camera service died without a death handler.");
461 }
462 });
463 }
464 sCameraChecked = true;
465 }
466
467 {
468 using ::android::hidl::base::V1_0::IBase;
469
470 // Listen to OMX's IOmxStore/default
471 {
472 sp<IBase> base = ::android::hardware::media::omx::V1_0::
473 IOmx::getService();
474 if (base == nullptr) {
475 ALOGD("OMX service is not available");
476 } else {
477 mDeathNotifiers.emplace_back(
478 base, [l = wp<IMediaRecorderClient>(listener)](){
479 sp<IMediaRecorderClient> listener = l.promote();
480 if (listener) {
481 ALOGV("OMX service died. "
482 "Sending death notification.");
483 listener->notify(
484 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
485 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
486 } else {
487 ALOGW("OMX service died without a death handler.");
488 }
489 });
490 }
491 }
492
493 // Listen to Codec2's IComponentStore instances
494 {
495 for (std::shared_ptr<Codec2Client> const& client :
496 Codec2Client::CreateFromAllServices()) {
497 sp<IBase> hidlBase = client->getHidlBase();
498 ::ndk::SpAIBinder aidlBase = client->getAidlBase();
499 auto onBinderDied = [l = wp<IMediaRecorderClient>(listener),
500 name = std::string(client->getServiceName())]() {
501 sp<IMediaRecorderClient> listener = l.promote();
502 if (listener) {
503 ALOGV("Codec2 service \"%s\" died. "
504 "Sending death notification",
505 name.c_str());
506 listener->notify(
507 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
508 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
509 } else {
510 ALOGW("Codec2 service \"%s\" died "
511 "without a death handler",
512 name.c_str());
513 }
514 };
515 if (hidlBase) {
516 mDeathNotifiers.emplace_back(hidlBase, onBinderDied);
517 } else if (aidlBase.get() != nullptr) {
518 mDeathNotifiers.emplace_back(aidlBase, onBinderDied);
519 }
520 }
521 }
522 }
523
524 mAudioDeviceUpdatedNotifier = new AudioDeviceUpdatedNotifier(listener);
525 mRecorder->setAudioDeviceCallback(mAudioDeviceUpdatedNotifier);
526
527 return OK;
528 }
529
setClientName(const String16 & clientName)530 status_t MediaRecorderClient::setClientName(const String16& clientName) {
531 ALOGV("setClientName(%s)", String8(clientName).c_str());
532 Mutex::Autolock lock(mLock);
533 if (mRecorder == NULL) {
534 ALOGE("recorder is not initialized");
535 return NO_INIT;
536 }
537 return mRecorder->setClientName(clientName);
538 }
539
dump(int fd,const Vector<String16> & args)540 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) {
541 if (mRecorder != NULL) {
542 return mRecorder->dump(fd, args);
543 }
544 return OK;
545 }
546
setInputDevice(audio_port_handle_t deviceId)547 status_t MediaRecorderClient::setInputDevice(audio_port_handle_t deviceId) {
548 ALOGV("setInputDevice(%d)", deviceId);
549 Mutex::Autolock lock(mLock);
550 if (mRecorder != NULL) {
551 return mRecorder->setInputDevice(deviceId);
552 }
553 return NO_INIT;
554 }
555
getRoutedDeviceIds(DeviceIdVector & deviceIds)556 status_t MediaRecorderClient::getRoutedDeviceIds(DeviceIdVector& deviceIds) {
557 ALOGV("getRoutedDeviceIds");
558 Mutex::Autolock lock(mLock);
559 if (mRecorder != NULL) {
560 return mRecorder->getRoutedDeviceIds(deviceIds);
561 }
562 return NO_INIT;
563 }
564
enableAudioDeviceCallback(bool enabled)565 status_t MediaRecorderClient::enableAudioDeviceCallback(bool enabled) {
566 ALOGV("enableDeviceCallback: %d", enabled);
567 Mutex::Autolock lock(mLock);
568 if (mRecorder != NULL) {
569 return mRecorder->enableAudioDeviceCallback(enabled);
570 }
571 return NO_INIT;
572 }
573
getActiveMicrophones(std::vector<media::MicrophoneInfoFw> * activeMicrophones)574 status_t MediaRecorderClient::getActiveMicrophones(
575 std::vector<media::MicrophoneInfoFw>* activeMicrophones) {
576 ALOGV("getActiveMicrophones");
577 Mutex::Autolock lock(mLock);
578 if (mRecorder != NULL) {
579 return mRecorder->getActiveMicrophones(activeMicrophones);
580 }
581 return NO_INIT;
582 }
583
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)584 status_t MediaRecorderClient::setPreferredMicrophoneDirection(
585 audio_microphone_direction_t direction) {
586 ALOGV("setPreferredMicrophoneDirection(%d)", direction);
587 if (mRecorder != NULL) {
588 return mRecorder->setPreferredMicrophoneDirection(direction);
589 }
590 return NO_INIT;
591 }
592
setPreferredMicrophoneFieldDimension(float zoom)593 status_t MediaRecorderClient::setPreferredMicrophoneFieldDimension(float zoom) {
594 ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
595 if (mRecorder != NULL) {
596 return mRecorder->setPreferredMicrophoneFieldDimension(zoom);
597 }
598 return NO_INIT;
599 }
600
getPortId(audio_port_handle_t * portId)601 status_t MediaRecorderClient::getPortId(audio_port_handle_t *portId) {
602 ALOGV("getPortId");
603 Mutex::Autolock lock(mLock);
604 if (mRecorder != NULL) {
605 return mRecorder->getPortId(portId);
606 }
607 return NO_INIT;
608 }
609
getRtpDataUsage(uint64_t * bytes)610 status_t MediaRecorderClient::getRtpDataUsage(uint64_t *bytes) {
611 ALOGV("getRtpDataUsage");
612 Mutex::Autolock lock(mLock);
613 if (mRecorder != NULL) {
614 return mRecorder->getRtpDataUsage(bytes);
615 }
616 return NO_INIT;
617 }
618 }; // namespace android
619