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