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