1 /*
2  **
3  ** Copyright (c) 2008 The Android Open Source Project
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17 
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "MediaRecorder"
20 
21 #include <inttypes.h>
22 
23 #include <utils/Log.h>
24 #include <media/mediarecorder.h>
25 #include <binder/IServiceManager.h>
26 #include <utils/String8.h>
27 #include <media/IMediaPlayerService.h>
28 #include <media/IMediaRecorder.h>
29 #include <media/mediaplayer.h>  // for MEDIA_ERROR_SERVER_DIED
30 #include <media/stagefright/PersistentSurface.h>
31 #include <gui/IGraphicBufferProducer.h>
32 
33 namespace android {
34 
setCamera(const sp<hardware::ICamera> & camera,const sp<ICameraRecordingProxy> & proxy)35 status_t MediaRecorder::setCamera(const sp<hardware::ICamera>& camera,
36         const sp<ICameraRecordingProxy>& proxy)
37 {
38     ALOGV("setCamera(%p,%p)", camera.get(), proxy.get());
39     if (mMediaRecorder == NULL) {
40         ALOGE("media recorder is not initialized yet");
41         return INVALID_OPERATION;
42     }
43     if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
44         ALOGE("setCamera called in an invalid state(%d)", mCurrentState);
45         return INVALID_OPERATION;
46     }
47 
48     status_t ret = mMediaRecorder->setCamera(camera, proxy);
49     if (OK != ret) {
50         ALOGV("setCamera failed: %d", ret);
51         mCurrentState = MEDIA_RECORDER_ERROR;
52         return ret;
53     }
54     return ret;
55 }
56 
setPreviewSurface(const sp<IGraphicBufferProducer> & surface)57 status_t MediaRecorder::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
58 {
59     ALOGV("setPreviewSurface(%p)", surface.get());
60     if (mMediaRecorder == NULL) {
61         ALOGE("media recorder is not initialized yet");
62         return INVALID_OPERATION;
63     }
64     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
65         ALOGE("setPreviewSurface called in an invalid state(%d)", mCurrentState);
66         return INVALID_OPERATION;
67     }
68     if (!mIsVideoSourceSet) {
69         ALOGE("try to set preview surface without setting the video source first");
70         return INVALID_OPERATION;
71     }
72 
73     status_t ret = mMediaRecorder->setPreviewSurface(surface);
74     if (OK != ret) {
75         ALOGV("setPreviewSurface failed: %d", ret);
76         mCurrentState = MEDIA_RECORDER_ERROR;
77         return ret;
78     }
79     return ret;
80 }
81 
init()82 status_t MediaRecorder::init()
83 {
84     ALOGV("init");
85     if (mMediaRecorder == NULL) {
86         ALOGE("media recorder is not initialized yet");
87         return INVALID_OPERATION;
88     }
89     if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
90         ALOGE("init called in an invalid state(%d)", mCurrentState);
91         return INVALID_OPERATION;
92     }
93 
94     status_t ret = mMediaRecorder->init();
95     if (OK != ret) {
96         ALOGV("init failed: %d", ret);
97         mCurrentState = MEDIA_RECORDER_ERROR;
98         return ret;
99     }
100 
101     ret = mMediaRecorder->setListener(this);
102     if (OK != ret) {
103         ALOGV("setListener failed: %d", ret);
104         mCurrentState = MEDIA_RECORDER_ERROR;
105         return ret;
106     }
107 
108     mCurrentState = MEDIA_RECORDER_INITIALIZED;
109     return ret;
110 }
111 
setVideoSource(int vs)112 status_t MediaRecorder::setVideoSource(int vs)
113 {
114     ALOGV("setVideoSource(%d)", vs);
115     if (mMediaRecorder == NULL) {
116         ALOGE("media recorder is not initialized yet");
117         return INVALID_OPERATION;
118     }
119     if (mIsVideoSourceSet) {
120         ALOGE("video source has already been set");
121         return INVALID_OPERATION;
122     }
123     if (mCurrentState & MEDIA_RECORDER_IDLE) {
124         ALOGV("Call init() since the media recorder is not initialized yet");
125         status_t ret = init();
126         if (OK != ret) {
127             return ret;
128         }
129     }
130     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
131         ALOGE("setVideoSource called in an invalid state(%d)", mCurrentState);
132         return INVALID_OPERATION;
133     }
134 
135     // following call is made over the Binder Interface
136     status_t ret = mMediaRecorder->setVideoSource(vs);
137 
138     if (OK != ret) {
139         ALOGV("setVideoSource failed: %d", ret);
140         mCurrentState = MEDIA_RECORDER_ERROR;
141         return ret;
142     }
143     mIsVideoSourceSet = true;
144     return ret;
145 }
146 
setAudioSource(int as)147 status_t MediaRecorder::setAudioSource(int as)
148 {
149     ALOGV("setAudioSource(%d)", as);
150     if (mMediaRecorder == NULL) {
151         ALOGE("media recorder is not initialized yet");
152         return INVALID_OPERATION;
153     }
154     if (mCurrentState & MEDIA_RECORDER_IDLE) {
155         ALOGV("Call init() since the media recorder is not initialized yet");
156         status_t ret = init();
157         if (OK != ret) {
158             return ret;
159         }
160     }
161     if (mIsAudioSourceSet) {
162         ALOGE("audio source has already been set");
163         return INVALID_OPERATION;
164     }
165     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
166         ALOGE("setAudioSource called in an invalid state(%d)", mCurrentState);
167         return INVALID_OPERATION;
168     }
169 
170     status_t ret = mMediaRecorder->setAudioSource(as);
171     if (OK != ret) {
172         ALOGV("setAudioSource failed: %d", ret);
173         mCurrentState = MEDIA_RECORDER_ERROR;
174         return ret;
175     }
176     mIsAudioSourceSet = true;
177     return ret;
178 }
179 
setOutputFormat(int of)180 status_t MediaRecorder::setOutputFormat(int of)
181 {
182     ALOGV("setOutputFormat(%d)", of);
183     if (mMediaRecorder == NULL) {
184         ALOGE("media recorder is not initialized yet");
185         return INVALID_OPERATION;
186     }
187     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
188         ALOGE("setOutputFormat called in an invalid state: %d", mCurrentState);
189         return INVALID_OPERATION;
190     }
191     if (mIsVideoSourceSet
192             && of >= OUTPUT_FORMAT_AUDIO_ONLY_START //first non-video output format
193             && of < OUTPUT_FORMAT_AUDIO_ONLY_END) {
194         ALOGE("output format (%d) is meant for audio recording only"
195               " and incompatible with video recording", of);
196         return INVALID_OPERATION;
197     }
198 
199     status_t ret = mMediaRecorder->setOutputFormat(of);
200     if (OK != ret) {
201         ALOGE("setOutputFormat failed: %d", ret);
202         mCurrentState = MEDIA_RECORDER_ERROR;
203         return ret;
204     }
205     mCurrentState = MEDIA_RECORDER_DATASOURCE_CONFIGURED;
206     return ret;
207 }
208 
setVideoEncoder(int ve)209 status_t MediaRecorder::setVideoEncoder(int ve)
210 {
211     ALOGV("setVideoEncoder(%d)", ve);
212     if (mMediaRecorder == NULL) {
213         ALOGE("media recorder is not initialized yet");
214         return INVALID_OPERATION;
215     }
216     if (!mIsVideoSourceSet) {
217         ALOGE("try to set the video encoder without setting the video source first");
218         return INVALID_OPERATION;
219     }
220     if (mIsVideoEncoderSet) {
221         ALOGE("video encoder has already been set");
222         return INVALID_OPERATION;
223     }
224     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
225         ALOGE("setVideoEncoder called in an invalid state(%d)", mCurrentState);
226         return INVALID_OPERATION;
227     }
228 
229     status_t ret = mMediaRecorder->setVideoEncoder(ve);
230     if (OK != ret) {
231         ALOGV("setVideoEncoder failed: %d", ret);
232         mCurrentState = MEDIA_RECORDER_ERROR;
233         return ret;
234     }
235     mIsVideoEncoderSet = true;
236     return ret;
237 }
238 
setAudioEncoder(int ae)239 status_t MediaRecorder::setAudioEncoder(int ae)
240 {
241     ALOGV("setAudioEncoder(%d)", ae);
242     if (mMediaRecorder == NULL) {
243         ALOGE("media recorder is not initialized yet");
244         return INVALID_OPERATION;
245     }
246     if (!mIsAudioSourceSet) {
247         ALOGE("try to set the audio encoder without setting the audio source first");
248         return INVALID_OPERATION;
249     }
250     if (mIsAudioEncoderSet) {
251         ALOGE("audio encoder has already been set");
252         return INVALID_OPERATION;
253     }
254     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
255         ALOGE("setAudioEncoder called in an invalid state(%d)", mCurrentState);
256         return INVALID_OPERATION;
257     }
258 
259     status_t ret = mMediaRecorder->setAudioEncoder(ae);
260     if (OK != ret) {
261         ALOGV("setAudioEncoder failed: %d", ret);
262         mCurrentState = MEDIA_RECORDER_ERROR;
263         return ret;
264     }
265     mIsAudioEncoderSet = true;
266     return ret;
267 }
268 
setOutputFile(int fd,int64_t offset,int64_t length)269 status_t MediaRecorder::setOutputFile(int fd, int64_t offset, int64_t length)
270 {
271     ALOGV("setOutputFile(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
272     if (mMediaRecorder == NULL) {
273         ALOGE("media recorder is not initialized yet");
274         return INVALID_OPERATION;
275     }
276     if (mIsOutputFileSet) {
277         ALOGE("output file has already been set");
278         return INVALID_OPERATION;
279     }
280     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
281         ALOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
282         return INVALID_OPERATION;
283     }
284 
285     // It appears that if an invalid file descriptor is passed through
286     // binder calls, the server-side of the inter-process function call
287     // is skipped. As a result, the check at the server-side to catch
288     // the invalid file descritpor never gets invoked. This is to workaround
289     // this issue by checking the file descriptor first before passing
290     // it through binder call.
291     if (fd < 0) {
292         ALOGE("Invalid file descriptor: %d", fd);
293         return BAD_VALUE;
294     }
295 
296     status_t ret = mMediaRecorder->setOutputFile(fd, offset, length);
297     if (OK != ret) {
298         ALOGV("setOutputFile failed: %d", ret);
299         mCurrentState = MEDIA_RECORDER_ERROR;
300         return ret;
301     }
302     mIsOutputFileSet = true;
303     return ret;
304 }
305 
setVideoSize(int width,int height)306 status_t MediaRecorder::setVideoSize(int width, int height)
307 {
308     ALOGV("setVideoSize(%d, %d)", width, height);
309     if (mMediaRecorder == NULL) {
310         ALOGE("media recorder is not initialized yet");
311         return INVALID_OPERATION;
312     }
313     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
314         ALOGE("setVideoSize called in an invalid state: %d", mCurrentState);
315         return INVALID_OPERATION;
316     }
317     if (!mIsVideoSourceSet) {
318         ALOGE("Cannot set video size without setting video source first");
319         return INVALID_OPERATION;
320     }
321 
322     status_t ret = mMediaRecorder->setVideoSize(width, height);
323     if (OK != ret) {
324         ALOGE("setVideoSize failed: %d", ret);
325         mCurrentState = MEDIA_RECORDER_ERROR;
326         return ret;
327     }
328 
329     return ret;
330 }
331 
332 // Query a SurfaceMediaSurface through the Mediaserver, over the
333 // binder interface. This is used by the Filter Framework (MediaEncoder)
334 // to get an <IGraphicBufferProducer> object to hook up to ANativeWindow.
335 sp<IGraphicBufferProducer> MediaRecorder::
querySurfaceMediaSourceFromMediaServer()336         querySurfaceMediaSourceFromMediaServer()
337 {
338     Mutex::Autolock _l(mLock);
339     mSurfaceMediaSource =
340             mMediaRecorder->querySurfaceMediaSource();
341     if (mSurfaceMediaSource == NULL) {
342         ALOGE("SurfaceMediaSource could not be initialized!");
343     }
344     return mSurfaceMediaSource;
345 }
346 
347 
348 
setInputSurface(const sp<PersistentSurface> & surface)349 status_t MediaRecorder::setInputSurface(const sp<PersistentSurface>& surface)
350 {
351     ALOGV("setInputSurface");
352     if (mMediaRecorder == NULL) {
353         ALOGE("media recorder is not initialized yet");
354         return INVALID_OPERATION;
355     }
356     bool isInvalidState = (mCurrentState &
357                            (MEDIA_RECORDER_PREPARED |
358                             MEDIA_RECORDER_RECORDING));
359     if (isInvalidState) {
360         ALOGE("setInputSurface is called in an invalid state: %d", mCurrentState);
361         return INVALID_OPERATION;
362     }
363 
364     return mMediaRecorder->setInputSurface(surface->getBufferConsumer());
365 }
366 
setVideoFrameRate(int frames_per_second)367 status_t MediaRecorder::setVideoFrameRate(int frames_per_second)
368 {
369     ALOGV("setVideoFrameRate(%d)", frames_per_second);
370     if (mMediaRecorder == NULL) {
371         ALOGE("media recorder is not initialized yet");
372         return INVALID_OPERATION;
373     }
374     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
375         ALOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
376         return INVALID_OPERATION;
377     }
378     if (!mIsVideoSourceSet) {
379         ALOGE("Cannot set video frame rate without setting video source first");
380         return INVALID_OPERATION;
381     }
382 
383     status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second);
384     if (OK != ret) {
385         ALOGE("setVideoFrameRate failed: %d", ret);
386         mCurrentState = MEDIA_RECORDER_ERROR;
387         return ret;
388     }
389     return ret;
390 }
391 
setParameters(const String8 & params)392 status_t MediaRecorder::setParameters(const String8& params) {
393     ALOGV("setParameters(%s)", params.string());
394     if (mMediaRecorder == NULL) {
395         ALOGE("media recorder is not initialized yet");
396         return INVALID_OPERATION;
397     }
398 
399     bool isInvalidState = (mCurrentState &
400                            (MEDIA_RECORDER_PREPARED |
401                             MEDIA_RECORDER_RECORDING |
402                             MEDIA_RECORDER_ERROR));
403     if (isInvalidState) {
404         ALOGE("setParameters is called in an invalid state: %d", mCurrentState);
405         return INVALID_OPERATION;
406     }
407 
408     status_t ret = mMediaRecorder->setParameters(params);
409     if (OK != ret) {
410         ALOGE("setParameters(%s) failed: %d", params.string(), ret);
411         // Do not change our current state to MEDIA_RECORDER_ERROR, failures
412         // of the only currently supported parameters, "max-duration" and
413         // "max-filesize" are _not_ fatal.
414     }
415 
416     return ret;
417 }
418 
prepare()419 status_t MediaRecorder::prepare()
420 {
421     ALOGV("prepare");
422     if (mMediaRecorder == NULL) {
423         ALOGE("media recorder is not initialized yet");
424         return INVALID_OPERATION;
425     }
426     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
427         ALOGE("prepare called in an invalid state: %d", mCurrentState);
428         return INVALID_OPERATION;
429     }
430     if (mIsAudioSourceSet != mIsAudioEncoderSet) {
431         if (mIsAudioSourceSet) {
432             ALOGE("audio source is set, but audio encoder is not set");
433         } else {  // must not happen, since setAudioEncoder checks this already
434             ALOGE("audio encoder is set, but audio source is not set");
435         }
436         return INVALID_OPERATION;
437     }
438 
439     if (mIsVideoSourceSet != mIsVideoEncoderSet) {
440         if (mIsVideoSourceSet) {
441             ALOGE("video source is set, but video encoder is not set");
442         } else {  // must not happen, since setVideoEncoder checks this already
443             ALOGE("video encoder is set, but video source is not set");
444         }
445         return INVALID_OPERATION;
446     }
447 
448     status_t ret = mMediaRecorder->prepare();
449     if (OK != ret) {
450         ALOGE("prepare failed: %d", ret);
451         mCurrentState = MEDIA_RECORDER_ERROR;
452         return ret;
453     }
454     mCurrentState = MEDIA_RECORDER_PREPARED;
455     return ret;
456 }
457 
getMaxAmplitude(int * max)458 status_t MediaRecorder::getMaxAmplitude(int* max)
459 {
460     ALOGV("getMaxAmplitude");
461     if (mMediaRecorder == NULL) {
462         ALOGE("media recorder is not initialized yet");
463         return INVALID_OPERATION;
464     }
465     if (mCurrentState & MEDIA_RECORDER_ERROR) {
466         ALOGE("getMaxAmplitude called in an invalid state: %d", mCurrentState);
467         return INVALID_OPERATION;
468     }
469 
470     status_t ret = mMediaRecorder->getMaxAmplitude(max);
471     if (OK != ret) {
472         ALOGE("getMaxAmplitude failed: %d", ret);
473         mCurrentState = MEDIA_RECORDER_ERROR;
474         return ret;
475     }
476     return ret;
477 }
478 
start()479 status_t MediaRecorder::start()
480 {
481     ALOGV("start");
482     if (mMediaRecorder == NULL) {
483         ALOGE("media recorder is not initialized yet");
484         return INVALID_OPERATION;
485     }
486     if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) {
487         ALOGE("start called in an invalid state: %d", mCurrentState);
488         return INVALID_OPERATION;
489     }
490 
491     status_t ret = mMediaRecorder->start();
492     if (OK != ret) {
493         ALOGE("start failed: %d", ret);
494         mCurrentState = MEDIA_RECORDER_ERROR;
495         return ret;
496     }
497     mCurrentState = MEDIA_RECORDER_RECORDING;
498     return ret;
499 }
500 
stop()501 status_t MediaRecorder::stop()
502 {
503     ALOGV("stop");
504     if (mMediaRecorder == NULL) {
505         ALOGE("media recorder is not initialized yet");
506         return INVALID_OPERATION;
507     }
508     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
509         ALOGE("stop called in an invalid state: %d", mCurrentState);
510         return INVALID_OPERATION;
511     }
512 
513     status_t ret = mMediaRecorder->stop();
514     if (OK != ret) {
515         ALOGE("stop failed: %d", ret);
516         mCurrentState = MEDIA_RECORDER_ERROR;
517         return ret;
518     }
519 
520     // FIXME:
521     // stop and reset are semantically different.
522     // We treat them the same for now, and will change this in the future.
523     doCleanUp();
524     mCurrentState = MEDIA_RECORDER_IDLE;
525     return ret;
526 }
527 
528 // Reset should be OK in any state
reset()529 status_t MediaRecorder::reset()
530 {
531     ALOGV("reset");
532     if (mMediaRecorder == NULL) {
533         ALOGE("media recorder is not initialized yet");
534         return INVALID_OPERATION;
535     }
536 
537     doCleanUp();
538     status_t ret = UNKNOWN_ERROR;
539     switch (mCurrentState) {
540         case MEDIA_RECORDER_IDLE:
541             ret = OK;
542             break;
543 
544         case MEDIA_RECORDER_RECORDING:
545         case MEDIA_RECORDER_DATASOURCE_CONFIGURED:
546         case MEDIA_RECORDER_PREPARED:
547         case MEDIA_RECORDER_ERROR: {
548             ret = doReset();
549             if (OK != ret) {
550                 return ret;  // No need to continue
551             }
552         }  // Intentional fall through
553         case MEDIA_RECORDER_INITIALIZED:
554             ret = close();
555             break;
556 
557         default: {
558             ALOGE("Unexpected non-existing state: %d", mCurrentState);
559             break;
560         }
561     }
562     return ret;
563 }
564 
pause()565 status_t MediaRecorder::pause()
566 {
567     ALOGV("pause");
568     if (mMediaRecorder == NULL) {
569         ALOGE("media recorder is not initialized yet");
570         return INVALID_OPERATION;
571     }
572     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
573         ALOGE("stop called in an invalid state: %d", mCurrentState);
574         return INVALID_OPERATION;
575     }
576 
577     status_t ret = mMediaRecorder->pause();
578     if (OK != ret) {
579         ALOGE("pause failed: %d", ret);
580         mCurrentState = MEDIA_RECORDER_ERROR;
581         return ret;
582     }
583 
584     return ret;
585 }
586 
resume()587 status_t MediaRecorder::resume()
588 {
589     ALOGV("resume");
590     if (mMediaRecorder == NULL) {
591         ALOGE("media recorder is not initialized yet");
592         return INVALID_OPERATION;
593     }
594     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
595         ALOGE("resume called in an invalid state: %d", mCurrentState);
596         return INVALID_OPERATION;
597     }
598 
599     status_t ret = mMediaRecorder->resume();
600     if (OK != ret) {
601         ALOGE("resume failed: %d", ret);
602         mCurrentState = MEDIA_RECORDER_ERROR;
603         return ret;
604     }
605 
606     return ret;
607 }
608 
close()609 status_t MediaRecorder::close()
610 {
611     ALOGV("close");
612     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
613         ALOGE("close called in an invalid state: %d", mCurrentState);
614         return INVALID_OPERATION;
615     }
616     status_t ret = mMediaRecorder->close();
617     if (OK != ret) {
618         ALOGE("close failed: %d", ret);
619         mCurrentState = MEDIA_RECORDER_ERROR;
620         return UNKNOWN_ERROR;
621     } else {
622         mCurrentState = MEDIA_RECORDER_IDLE;
623     }
624     return ret;
625 }
626 
doReset()627 status_t MediaRecorder::doReset()
628 {
629     ALOGV("doReset");
630     status_t ret = mMediaRecorder->reset();
631     if (OK != ret) {
632         ALOGE("doReset failed: %d", ret);
633         mCurrentState = MEDIA_RECORDER_ERROR;
634         return ret;
635     } else {
636         mCurrentState = MEDIA_RECORDER_INITIALIZED;
637     }
638     return ret;
639 }
640 
doCleanUp()641 void MediaRecorder::doCleanUp()
642 {
643     ALOGV("doCleanUp");
644     mIsAudioSourceSet  = false;
645     mIsVideoSourceSet  = false;
646     mIsAudioEncoderSet = false;
647     mIsVideoEncoderSet = false;
648     mIsOutputFileSet   = false;
649 }
650 
651 // Release should be OK in any state
release()652 status_t MediaRecorder::release()
653 {
654     ALOGV("release");
655     if (mMediaRecorder != NULL) {
656         return mMediaRecorder->release();
657     }
658     return INVALID_OPERATION;
659 }
660 
MediaRecorder(const String16 & opPackageName)661 MediaRecorder::MediaRecorder(const String16& opPackageName) : mSurfaceMediaSource(NULL)
662 {
663     ALOGV("constructor");
664 
665     const sp<IMediaPlayerService> service(getMediaPlayerService());
666     if (service != NULL) {
667         mMediaRecorder = service->createMediaRecorder(opPackageName);
668     }
669     if (mMediaRecorder != NULL) {
670         mCurrentState = MEDIA_RECORDER_IDLE;
671     }
672 
673 
674     doCleanUp();
675 }
676 
initCheck()677 status_t MediaRecorder::initCheck()
678 {
679     return mMediaRecorder != 0 ? NO_ERROR : NO_INIT;
680 }
681 
~MediaRecorder()682 MediaRecorder::~MediaRecorder()
683 {
684     ALOGV("destructor");
685     if (mMediaRecorder != NULL) {
686         mMediaRecorder.clear();
687     }
688 
689     if (mSurfaceMediaSource != NULL) {
690         mSurfaceMediaSource.clear();
691     }
692 }
693 
setListener(const sp<MediaRecorderListener> & listener)694 status_t MediaRecorder::setListener(const sp<MediaRecorderListener>& listener)
695 {
696     ALOGV("setListener");
697     Mutex::Autolock _l(mLock);
698     mListener = listener;
699 
700     return NO_ERROR;
701 }
702 
setClientName(const String16 & clientName)703 status_t MediaRecorder::setClientName(const String16& clientName)
704 {
705     ALOGV("setClientName");
706     if (mMediaRecorder == NULL) {
707         ALOGE("media recorder is not initialized yet");
708         return INVALID_OPERATION;
709     }
710     bool isInvalidState = (mCurrentState &
711                            (MEDIA_RECORDER_PREPARED |
712                             MEDIA_RECORDER_RECORDING |
713                             MEDIA_RECORDER_ERROR));
714     if (isInvalidState) {
715         ALOGE("setClientName is called in an invalid state: %d", mCurrentState);
716         return INVALID_OPERATION;
717     }
718 
719     mMediaRecorder->setClientName(clientName);
720 
721     return NO_ERROR;
722 }
723 
notify(int msg,int ext1,int ext2)724 void MediaRecorder::notify(int msg, int ext1, int ext2)
725 {
726     ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
727 
728     sp<MediaRecorderListener> listener;
729     mLock.lock();
730     listener = mListener;
731     mLock.unlock();
732 
733     if (listener != NULL) {
734         Mutex::Autolock _l(mNotifyLock);
735         ALOGV("callback application");
736         listener->notify(msg, ext1, ext2);
737         ALOGV("back from callback");
738     }
739 }
740 
died()741 void MediaRecorder::died()
742 {
743     ALOGV("died");
744     notify(MEDIA_RECORDER_EVENT_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
745 }
746 
747 } // namespace android
748