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