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