• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     pid_t pid = IPCThreadState::self()->getCallingPid();
128     uid_t uid = IPCThreadState::self()->getCallingUid();
129 
130     if ((as == AUDIO_SOURCE_FM_TUNER && !captureAudioOutputAllowed(pid, uid))
131             || !recordingAllowed(String16(""), pid, uid)) {
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,pid_t pid,const String16 & opPackageName)380 MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid,
381         const String16& opPackageName)
382 {
383     ALOGV("Client constructor");
384     mPid = pid;
385     mRecorder = new StagefrightRecorder(opPackageName);
386     mMediaPlayerService = service;
387 }
388 
~MediaRecorderClient()389 MediaRecorderClient::~MediaRecorderClient()
390 {
391     ALOGV("Client destructor");
392     release();
393 }
394 
AudioDeviceUpdatedNotifier(const sp<IMediaRecorderClient> & listener)395 MediaRecorderClient::AudioDeviceUpdatedNotifier::AudioDeviceUpdatedNotifier(
396         const sp<IMediaRecorderClient>& listener) {
397     mListener = listener;
398 }
399 
~AudioDeviceUpdatedNotifier()400 MediaRecorderClient::AudioDeviceUpdatedNotifier::~AudioDeviceUpdatedNotifier() {
401 }
402 
onAudioDeviceUpdate(audio_io_handle_t audioIo,audio_port_handle_t deviceId)403 void MediaRecorderClient::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
404         audio_io_handle_t audioIo,
405         audio_port_handle_t deviceId) {
406     sp<IMediaRecorderClient> listener = mListener.promote();
407     if (listener != NULL) {
408         listener->notify(MEDIA_RECORDER_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
409     } else {
410         ALOGW("listener for process %d death is gone", MEDIA_RECORDER_AUDIO_ROUTING_CHANGED);
411     }
412 }
413 
setListener(const sp<IMediaRecorderClient> & listener)414 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
415 {
416     ALOGV("setListener");
417     Mutex::Autolock lock(mLock);
418     mDeathNotifiers.clear();
419     if (mRecorder == NULL) {
420         ALOGE("recorder is not initialized");
421         return NO_INIT;
422     }
423     mRecorder->setListener(listener);
424 
425     sp<IServiceManager> sm = defaultServiceManager();
426 
427     static const bool sCameraDisabled = CameraUtils::isCameraServiceDisabled();
428 
429     if (!sCameraDisabled) {
430         // WORKAROUND: We don't know if camera exists here and getService might block for 5 seconds.
431         // Use checkService for camera if we don't know it exists.
432         static std::atomic<bool> sCameraChecked(false);  // once true never becomes false.
433         static std::atomic<bool> sCameraVerified(false); // once true never becomes false.
434 
435         sp<IBinder> binder = (sCameraVerified || !sCameraChecked)
436             ? sm->getService(String16("media.camera")) : sm->checkService(String16("media.camera"));
437         // If the device does not have a camera, do not create a death listener for it.
438         if (binder != NULL) {
439             sCameraVerified = true;
440             mDeathNotifiers.emplace_back(
441                     binder, [l = wp<IMediaRecorderClient>(listener)](){
442                 sp<IMediaRecorderClient> listener = l.promote();
443                 if (listener) {
444                     ALOGV("media.camera service died. "
445                           "Sending death notification.");
446                     listener->notify(
447                             MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
448                             MediaPlayerService::CAMERA_PROCESS_DEATH);
449                 } else {
450                     ALOGW("media.camera service died without a death handler.");
451                 }
452             });
453         }
454         sCameraChecked = true;
455     }
456 
457     {
458         using ::android::hidl::base::V1_0::IBase;
459 
460         // Listen to OMX's IOmxStore/default
461         {
462             sp<IBase> base = ::android::hardware::media::omx::V1_0::
463                     IOmx::getService();
464             if (base == nullptr) {
465                 ALOGD("OMX service is not available");
466             } else {
467                 mDeathNotifiers.emplace_back(
468                         base, [l = wp<IMediaRecorderClient>(listener)](){
469                     sp<IMediaRecorderClient> listener = l.promote();
470                     if (listener) {
471                         ALOGV("OMX service died. "
472                               "Sending death notification.");
473                         listener->notify(
474                                 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
475                                 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
476                     } else {
477                         ALOGW("OMX service died without a death handler.");
478                     }
479                 });
480             }
481         }
482 
483         // Listen to Codec2's IComponentStore instances
484         {
485             for (std::shared_ptr<Codec2Client> const& client :
486                     Codec2Client::CreateFromAllServices()) {
487                 sp<IBase> base = client->getBase();
488                 mDeathNotifiers.emplace_back(
489                         base, [l = wp<IMediaRecorderClient>(listener),
490                                name = std::string(client->getServiceName())]() {
491                     sp<IMediaRecorderClient> listener = l.promote();
492                     if (listener) {
493                         ALOGV("Codec2 service \"%s\" died. "
494                               "Sending death notification",
495                               name.c_str());
496                         listener->notify(
497                                 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
498                                 MediaPlayerService::MEDIACODEC_PROCESS_DEATH);
499                     } else {
500                         ALOGW("Codec2 service \"%s\" died "
501                               "without a death handler",
502                               name.c_str());
503                     }
504                 });
505             }
506         }
507     }
508 
509     mAudioDeviceUpdatedNotifier = new AudioDeviceUpdatedNotifier(listener);
510     mRecorder->setAudioDeviceCallback(mAudioDeviceUpdatedNotifier);
511 
512     return OK;
513 }
514 
setClientName(const String16 & clientName)515 status_t MediaRecorderClient::setClientName(const String16& clientName) {
516     ALOGV("setClientName(%s)", String8(clientName).string());
517     Mutex::Autolock lock(mLock);
518     if (mRecorder == NULL) {
519         ALOGE("recorder is not initialized");
520         return NO_INIT;
521     }
522     return mRecorder->setClientName(clientName);
523 }
524 
dump(int fd,const Vector<String16> & args)525 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) {
526     if (mRecorder != NULL) {
527         return mRecorder->dump(fd, args);
528     }
529     return OK;
530 }
531 
setInputDevice(audio_port_handle_t deviceId)532 status_t MediaRecorderClient::setInputDevice(audio_port_handle_t deviceId) {
533     ALOGV("setInputDevice(%d)", deviceId);
534     Mutex::Autolock lock(mLock);
535     if (mRecorder != NULL) {
536         return mRecorder->setInputDevice(deviceId);
537     }
538     return NO_INIT;
539 }
540 
getRoutedDeviceId(audio_port_handle_t * deviceId)541 status_t MediaRecorderClient::getRoutedDeviceId(audio_port_handle_t* deviceId) {
542     ALOGV("getRoutedDeviceId");
543     Mutex::Autolock lock(mLock);
544     if (mRecorder != NULL) {
545         return mRecorder->getRoutedDeviceId(deviceId);
546     }
547     return NO_INIT;
548 }
549 
enableAudioDeviceCallback(bool enabled)550 status_t MediaRecorderClient::enableAudioDeviceCallback(bool enabled) {
551     ALOGV("enableDeviceCallback: %d", enabled);
552     Mutex::Autolock lock(mLock);
553     if (mRecorder != NULL) {
554         return mRecorder->enableAudioDeviceCallback(enabled);
555     }
556     return NO_INIT;
557 }
558 
getActiveMicrophones(std::vector<media::MicrophoneInfo> * activeMicrophones)559 status_t MediaRecorderClient::getActiveMicrophones(
560         std::vector<media::MicrophoneInfo>* activeMicrophones) {
561     ALOGV("getActiveMicrophones");
562     Mutex::Autolock lock(mLock);
563     if (mRecorder != NULL) {
564         return mRecorder->getActiveMicrophones(activeMicrophones);
565     }
566     return NO_INIT;
567 }
568 
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)569 status_t MediaRecorderClient::setPreferredMicrophoneDirection(
570             audio_microphone_direction_t direction) {
571     ALOGV("setPreferredMicrophoneDirection(%d)", direction);
572     if (mRecorder != NULL) {
573         return mRecorder->setPreferredMicrophoneDirection(direction);
574     }
575     return NO_INIT;
576 }
577 
setPreferredMicrophoneFieldDimension(float zoom)578 status_t MediaRecorderClient::setPreferredMicrophoneFieldDimension(float zoom) {
579     ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
580     if (mRecorder != NULL) {
581         return mRecorder->setPreferredMicrophoneFieldDimension(zoom);
582     }
583     return NO_INIT;
584 }
585 
getPortId(audio_port_handle_t * portId)586 status_t MediaRecorderClient::getPortId(audio_port_handle_t *portId) {
587     ALOGV("getPortId");
588     Mutex::Autolock lock(mLock);
589     if (mRecorder != NULL) {
590         return mRecorder->getPortId(portId);
591     }
592     return NO_INIT;
593 }
594 }; // namespace android
595