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,audio_port_handle_t deviceId)410 void MediaRecorderClient::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
411 audio_io_handle_t audioIo,
412 audio_port_handle_t deviceId) {
413 sp<IMediaRecorderClient> listener = mListener.promote();
414 if (listener != NULL) {
415 listener->notify(MEDIA_RECORDER_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
416 } else {
417 ALOGW("listener for process %d death is gone", MEDIA_RECORDER_AUDIO_ROUTING_CHANGED);
418 }
419 }
420
setListener(const sp<IMediaRecorderClient> & listener)421 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
422 {
423 ALOGV("setListener");
424 Mutex::Autolock lock(mLock);
425 mDeathNotifiers.clear();
426 if (mRecorder == NULL) {
427 ALOGE("recorder is not initialized");
428 return NO_INIT;
429 }
430 mRecorder->setListener(listener);
431
432 sp<IServiceManager> sm = defaultServiceManager();
433
434 static const bool sCameraDisabled = CameraUtils::isCameraServiceDisabled();
435
436 if (!sCameraDisabled) {
437 // WORKAROUND: We don't know if camera exists here and getService might block for 5 seconds.
438 // Use checkService for camera if we don't know it exists.
439 static std::atomic<bool> sCameraChecked(false); // once true never becomes false.
440 static std::atomic<bool> sCameraVerified(false); // once true never becomes false.
441
442 sp<IBinder> binder = (sCameraVerified || !sCameraChecked)
443 ? sm->getService(String16("media.camera")) : sm->checkService(String16("media.camera"));
444 // If the device does not have a camera, do not create a death listener for it.
445 if (binder != NULL) {
446 sCameraVerified = true;
447 mDeathNotifiers.emplace_back(
448 binder, [l = wp<IMediaRecorderClient>(listener)](){
449 sp<IMediaRecorderClient> listener = l.promote();
450 if (listener) {
451 ALOGV("media.camera service died. "
452 "Sending death notification.");
453 listener->notify(
454 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
455 MediaPlayerService::CAMERA_PROCESS_DEATH);
456 } else {
457 ALOGW("media.camera service died without a death handler.");
458 }
459 });
460 }
461 sCameraChecked = true;
462 }
463
464 {
465 using ::android::hidl::base::V1_0::IBase;
466
467 // Listen to OMX's IOmxStore/default
468 {
469 sp<IBase> base = ::android::hardware::media::omx::V1_0::
470 IOmx::getService();
471 if (base == nullptr) {
472 ALOGD("OMX service is not available");
473 } else {
474 mDeathNotifiers.emplace_back(
475 base, [l = wp<IMediaRecorderClient>(listener)](){
476 sp<IMediaRecorderClient> listener = l.promote();
477 if (listener) {
478 ALOGV("OMX service died. "
479 "Sending death notification.");
480 listener->notify(
481 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
482 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
483 } else {
484 ALOGW("OMX service died without a death handler.");
485 }
486 });
487 }
488 }
489
490 // Listen to Codec2's IComponentStore instances
491 {
492 for (std::shared_ptr<Codec2Client> const& client :
493 Codec2Client::CreateFromAllServices()) {
494 sp<IBase> hidlBase = client->getHidlBase();
495 ::ndk::SpAIBinder aidlBase = client->getAidlBase();
496 auto onBinderDied = [l = wp<IMediaRecorderClient>(listener),
497 name = std::string(client->getServiceName())]() {
498 sp<IMediaRecorderClient> listener = l.promote();
499 if (listener) {
500 ALOGV("Codec2 service \"%s\" died. "
501 "Sending death notification",
502 name.c_str());
503 listener->notify(
504 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
505 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
506 } else {
507 ALOGW("Codec2 service \"%s\" died "
508 "without a death handler",
509 name.c_str());
510 }
511 };
512 if (hidlBase) {
513 mDeathNotifiers.emplace_back(hidlBase, onBinderDied);
514 } else if (aidlBase.get() != nullptr) {
515 mDeathNotifiers.emplace_back(aidlBase, onBinderDied);
516 }
517 }
518 }
519 }
520
521 mAudioDeviceUpdatedNotifier = new AudioDeviceUpdatedNotifier(listener);
522 mRecorder->setAudioDeviceCallback(mAudioDeviceUpdatedNotifier);
523
524 return OK;
525 }
526
setClientName(const String16 & clientName)527 status_t MediaRecorderClient::setClientName(const String16& clientName) {
528 ALOGV("setClientName(%s)", String8(clientName).c_str());
529 Mutex::Autolock lock(mLock);
530 if (mRecorder == NULL) {
531 ALOGE("recorder is not initialized");
532 return NO_INIT;
533 }
534 return mRecorder->setClientName(clientName);
535 }
536
dump(int fd,const Vector<String16> & args)537 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) {
538 if (mRecorder != NULL) {
539 return mRecorder->dump(fd, args);
540 }
541 return OK;
542 }
543
setInputDevice(audio_port_handle_t deviceId)544 status_t MediaRecorderClient::setInputDevice(audio_port_handle_t deviceId) {
545 ALOGV("setInputDevice(%d)", deviceId);
546 Mutex::Autolock lock(mLock);
547 if (mRecorder != NULL) {
548 return mRecorder->setInputDevice(deviceId);
549 }
550 return NO_INIT;
551 }
552
getRoutedDeviceId(audio_port_handle_t * deviceId)553 status_t MediaRecorderClient::getRoutedDeviceId(audio_port_handle_t* deviceId) {
554 ALOGV("getRoutedDeviceId");
555 Mutex::Autolock lock(mLock);
556 if (mRecorder != NULL) {
557 return mRecorder->getRoutedDeviceId(deviceId);
558 }
559 return NO_INIT;
560 }
561
enableAudioDeviceCallback(bool enabled)562 status_t MediaRecorderClient::enableAudioDeviceCallback(bool enabled) {
563 ALOGV("enableDeviceCallback: %d", enabled);
564 Mutex::Autolock lock(mLock);
565 if (mRecorder != NULL) {
566 return mRecorder->enableAudioDeviceCallback(enabled);
567 }
568 return NO_INIT;
569 }
570
getActiveMicrophones(std::vector<media::MicrophoneInfoFw> * activeMicrophones)571 status_t MediaRecorderClient::getActiveMicrophones(
572 std::vector<media::MicrophoneInfoFw>* activeMicrophones) {
573 ALOGV("getActiveMicrophones");
574 Mutex::Autolock lock(mLock);
575 if (mRecorder != NULL) {
576 return mRecorder->getActiveMicrophones(activeMicrophones);
577 }
578 return NO_INIT;
579 }
580
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)581 status_t MediaRecorderClient::setPreferredMicrophoneDirection(
582 audio_microphone_direction_t direction) {
583 ALOGV("setPreferredMicrophoneDirection(%d)", direction);
584 if (mRecorder != NULL) {
585 return mRecorder->setPreferredMicrophoneDirection(direction);
586 }
587 return NO_INIT;
588 }
589
setPreferredMicrophoneFieldDimension(float zoom)590 status_t MediaRecorderClient::setPreferredMicrophoneFieldDimension(float zoom) {
591 ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
592 if (mRecorder != NULL) {
593 return mRecorder->setPreferredMicrophoneFieldDimension(zoom);
594 }
595 return NO_INIT;
596 }
597
getPortId(audio_port_handle_t * portId)598 status_t MediaRecorderClient::getPortId(audio_port_handle_t *portId) {
599 ALOGV("getPortId");
600 Mutex::Autolock lock(mLock);
601 if (mRecorder != NULL) {
602 return mRecorder->getPortId(portId);
603 }
604 return NO_INIT;
605 }
606
getRtpDataUsage(uint64_t * bytes)607 status_t MediaRecorderClient::getRtpDataUsage(uint64_t *bytes) {
608 ALOGV("getRtpDataUsage");
609 Mutex::Autolock lock(mLock);
610 if (mRecorder != NULL) {
611 return mRecorder->getRtpDataUsage(bytes);
612 }
613 return NO_INIT;
614 }
615 }; // namespace android
616