• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/macros.h>
24 #include <utils/Log.h>
25 #include <media/mediarecorder.h>
26 #include <binder/IServiceManager.h>
27 #include <utils/String8.h>
28 #include <media/IMediaPlayerService.h>
29 #include <media/IMediaRecorder.h>
30 #include <media/mediaplayer.h>  // for MEDIA_ERROR_SERVER_DIED
31 #include <media/stagefright/PersistentSurface.h>
32 #include <gui/IGraphicBufferProducer.h>
33 
34 namespace android {
35 
36 using content::AttributionSourceState;
37 
setCamera(const sp<hardware::ICamera> & camera,const sp<ICameraRecordingProxy> & proxy)38 status_t MediaRecorder::setCamera(const sp<hardware::ICamera>& camera,
39         const sp<ICameraRecordingProxy>& proxy)
40 {
41     ALOGV("setCamera(%p,%p)", camera.get(), proxy.get());
42     if (mMediaRecorder == NULL) {
43         ALOGE("media recorder is not initialized yet");
44         return INVALID_OPERATION;
45     }
46     if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
47         ALOGE("setCamera called in an invalid state(%d)", mCurrentState);
48         return INVALID_OPERATION;
49     }
50 
51     status_t ret = mMediaRecorder->setCamera(camera, proxy);
52     if (OK != ret) {
53         ALOGV("setCamera failed: %d", ret);
54         mCurrentState = MEDIA_RECORDER_ERROR;
55         return ret;
56     }
57     return ret;
58 }
59 
setPreviewSurface(const sp<IGraphicBufferProducer> & surface)60 status_t MediaRecorder::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
61 {
62     ALOGV("setPreviewSurface(%p)", surface.get());
63     if (mMediaRecorder == NULL) {
64         ALOGE("media recorder is not initialized yet");
65         return INVALID_OPERATION;
66     }
67     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
68         ALOGE("setPreviewSurface called in an invalid state(%d)", mCurrentState);
69         return INVALID_OPERATION;
70     }
71     if (!mIsVideoSourceSet) {
72         ALOGE("try to set preview surface without setting the video source first");
73         return INVALID_OPERATION;
74     }
75 
76     status_t ret = mMediaRecorder->setPreviewSurface(surface);
77     if (OK != ret) {
78         ALOGV("setPreviewSurface failed: %d", ret);
79         mCurrentState = MEDIA_RECORDER_ERROR;
80         return ret;
81     }
82     return ret;
83 }
84 
init()85 status_t MediaRecorder::init()
86 {
87     ALOGV("init");
88     if (mMediaRecorder == NULL) {
89         ALOGE("media recorder is not initialized yet");
90         return INVALID_OPERATION;
91     }
92     if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
93         ALOGE("init called in an invalid state(%d)", mCurrentState);
94         return INVALID_OPERATION;
95     }
96 
97     status_t ret = mMediaRecorder->init();
98     if (OK != ret) {
99         ALOGV("init failed: %d", ret);
100         mCurrentState = MEDIA_RECORDER_ERROR;
101         return ret;
102     }
103 
104     ret = mMediaRecorder->setListener(this);
105     if (OK != ret) {
106         ALOGV("setListener failed: %d", ret);
107         mCurrentState = MEDIA_RECORDER_ERROR;
108         return ret;
109     }
110 
111     mCurrentState = MEDIA_RECORDER_INITIALIZED;
112     return ret;
113 }
114 
setVideoSource(int vs)115 status_t MediaRecorder::setVideoSource(int vs)
116 {
117     ALOGV("setVideoSource(%d)", vs);
118     if (mMediaRecorder == NULL) {
119         ALOGE("media recorder is not initialized yet");
120         return INVALID_OPERATION;
121     }
122     if (mIsVideoSourceSet) {
123         ALOGE("video source has already been set");
124         return INVALID_OPERATION;
125     }
126     if (mCurrentState & MEDIA_RECORDER_IDLE) {
127         ALOGV("Call init() since the media recorder is not initialized yet");
128         status_t ret = init();
129         if (OK != ret) {
130             return ret;
131         }
132     }
133     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
134         ALOGE("setVideoSource called in an invalid state(%d)", mCurrentState);
135         return INVALID_OPERATION;
136     }
137 
138     // following call is made over the Binder Interface
139     status_t ret = mMediaRecorder->setVideoSource(vs);
140 
141     if (OK != ret) {
142         ALOGV("setVideoSource failed: %d", ret);
143         mCurrentState = MEDIA_RECORDER_ERROR;
144         return ret;
145     }
146     mIsVideoSourceSet = true;
147     return ret;
148 }
149 
setAudioSource(int as)150 status_t MediaRecorder::setAudioSource(int as)
151 {
152     ALOGV("setAudioSource(%d)", as);
153     if (mMediaRecorder == NULL) {
154         ALOGE("media recorder is not initialized yet");
155         return INVALID_OPERATION;
156     }
157     if (mCurrentState & MEDIA_RECORDER_IDLE) {
158         ALOGV("Call init() since the media recorder is not initialized yet");
159         status_t ret = init();
160         if (OK != ret) {
161             return ret;
162         }
163     }
164     if (mIsAudioSourceSet) {
165         ALOGE("audio source has already been set");
166         return INVALID_OPERATION;
167     }
168     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
169         ALOGE("setAudioSource called in an invalid state(%d)", mCurrentState);
170         return INVALID_OPERATION;
171     }
172 
173     status_t ret = mMediaRecorder->setAudioSource(as);
174     if (OK != ret) {
175         ALOGV("setAudioSource failed: %d", ret);
176         mCurrentState = MEDIA_RECORDER_ERROR;
177         return ret;
178     }
179     mIsAudioSourceSet = true;
180     return ret;
181 }
182 
setPrivacySensitive(bool privacySensitive)183 status_t MediaRecorder::setPrivacySensitive(bool privacySensitive)
184 {
185     ALOGV("%s(%s)", __func__, privacySensitive ? "true" : "false");
186     if (mMediaRecorder == NULL) {
187         ALOGE("%s: media recorder is not initialized yet", __func__);
188         return INVALID_OPERATION;
189     }
190 
191     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED) || !mIsAudioSourceSet) {
192         ALOGE("%s called in an invalid state(%d) or audio source not (%d)",
193             __func__, mCurrentState, mIsAudioSourceSet);
194         return INVALID_OPERATION;
195     }
196 
197     status_t ret = mMediaRecorder->setPrivacySensitive(privacySensitive);
198     if (OK != ret) {
199         ALOGV("%s failed: %d", __func__, ret);
200         mCurrentState = MEDIA_RECORDER_ERROR;
201         return ret;
202     }
203     return ret;
204 }
205 
isPrivacySensitive(bool * privacySensitive) const206 status_t MediaRecorder::isPrivacySensitive(bool *privacySensitive) const
207 {
208     if (mMediaRecorder == NULL) {
209         ALOGE("%s: media recorder is not initialized yet", __func__);
210         return INVALID_OPERATION;
211     }
212 
213     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED) || !mIsAudioSourceSet) {
214         ALOGE("%s called in an invalid state(%d) or audio source not (%d)",
215             __func__, mCurrentState, mIsAudioSourceSet);
216         return INVALID_OPERATION;
217     }
218 
219     status_t ret = mMediaRecorder->isPrivacySensitive(privacySensitive);
220     ALOGV("%s status: %d eanbled %s", __func__, ret, *privacySensitive ? "enabled" : "disabled");
221     return ret;
222 }
223 
setOutputFormat(int of)224 status_t MediaRecorder::setOutputFormat(int of)
225 {
226     ALOGV("setOutputFormat(%d)", of);
227     if (mMediaRecorder == NULL) {
228         ALOGE("media recorder is not initialized yet");
229         return INVALID_OPERATION;
230     }
231     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
232         ALOGE("setOutputFormat called in an invalid state: %d", mCurrentState);
233         return INVALID_OPERATION;
234     }
235     if (mIsVideoSourceSet
236             && of >= OUTPUT_FORMAT_AUDIO_ONLY_START //first non-video output format
237             && of < OUTPUT_FORMAT_AUDIO_ONLY_END) {
238         ALOGE("output format (%d) is meant for audio recording only"
239               " and incompatible with video recording", of);
240         return INVALID_OPERATION;
241     }
242 
243     status_t ret = mMediaRecorder->setOutputFormat(of);
244     if (OK != ret) {
245         ALOGE("setOutputFormat failed: %d", ret);
246         mCurrentState = MEDIA_RECORDER_ERROR;
247         return ret;
248     }
249     mOutputFormat = (output_format)of;
250     mCurrentState = MEDIA_RECORDER_DATASOURCE_CONFIGURED;
251     return ret;
252 }
253 
setVideoEncoder(int ve)254 status_t MediaRecorder::setVideoEncoder(int ve)
255 {
256     ALOGV("setVideoEncoder(%d)", ve);
257     if (mMediaRecorder == NULL) {
258         ALOGE("media recorder is not initialized yet");
259         return INVALID_OPERATION;
260     }
261     if (!mIsVideoSourceSet) {
262         ALOGE("try to set the video encoder without setting the video source first");
263         return INVALID_OPERATION;
264     }
265     if (mIsVideoEncoderSet) {
266         ALOGE("video encoder has already been set");
267         return INVALID_OPERATION;
268     }
269     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
270         ALOGE("setVideoEncoder called in an invalid state(%d)", mCurrentState);
271         return INVALID_OPERATION;
272     }
273 
274     status_t ret = mMediaRecorder->setVideoEncoder(ve);
275     if (OK != ret) {
276         ALOGV("setVideoEncoder failed: %d", ret);
277         mCurrentState = MEDIA_RECORDER_ERROR;
278         return ret;
279     }
280     mIsVideoEncoderSet = true;
281     return ret;
282 }
283 
setAudioEncoder(int ae)284 status_t MediaRecorder::setAudioEncoder(int ae)
285 {
286     ALOGV("setAudioEncoder(%d)", ae);
287     if (mMediaRecorder == NULL) {
288         ALOGE("media recorder is not initialized yet");
289         return INVALID_OPERATION;
290     }
291     if (!mIsAudioSourceSet) {
292         ALOGE("try to set the audio encoder without setting the audio source first");
293         return INVALID_OPERATION;
294     }
295     if (mIsAudioEncoderSet) {
296         ALOGE("audio encoder has already been set");
297         return INVALID_OPERATION;
298     }
299     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
300         ALOGE("setAudioEncoder called in an invalid state(%d)", mCurrentState);
301         return INVALID_OPERATION;
302     }
303 
304 
305     status_t ret = mMediaRecorder->setAudioEncoder(ae);
306     if (OK != ret) {
307         ALOGV("setAudioEncoder failed: %d", ret);
308         mCurrentState = MEDIA_RECORDER_ERROR;
309         return ret;
310     }
311     mIsAudioEncoderSet = true;
312     return ret;
313 }
314 
setOutputFile(int fd)315 status_t MediaRecorder::setOutputFile(int fd)
316 {
317     ALOGV("setOutputFile(%d)", fd);
318     if (mMediaRecorder == NULL) {
319         ALOGE("media recorder is not initialized yet");
320         return INVALID_OPERATION;
321     }
322     if (mIsOutputFileSet) {
323         ALOGE("output file has already been set");
324         return INVALID_OPERATION;
325     }
326     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
327         ALOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
328         return INVALID_OPERATION;
329     }
330 
331     // It appears that if an invalid file descriptor is passed through
332     // binder calls, the server-side of the inter-process function call
333     // is skipped. As a result, the check at the server-side to catch
334     // the invalid file descritpor never gets invoked. This is to workaround
335     // this issue by checking the file descriptor first before passing
336     // it through binder call.
337     int flags = fcntl(fd, F_GETFL);
338     if (flags == -1) {
339         ALOGE("Fail to get File Status Flags err: %s", strerror(errno));
340     }
341     // fd must be in read-write mode or write-only mode.
342     if ((flags & (O_RDWR | O_WRONLY)) == 0) {
343         ALOGE("File descriptor is not in read-write mode or write-only mode");
344         return BAD_VALUE;
345     }
346 
347     status_t ret = mMediaRecorder->setOutputFile(fd);
348     if (OK != ret) {
349         ALOGE("setOutputFile failed: %d", ret);
350         mCurrentState = MEDIA_RECORDER_ERROR;
351         return ret;
352     }
353     mIsOutputFileSet = true;
354     return ret;
355 }
356 
setNextOutputFile(int fd)357 status_t MediaRecorder::setNextOutputFile(int fd)
358 {
359     ALOGV("setNextOutputFile(%d)", fd);
360     if (mMediaRecorder == NULL) {
361         ALOGE("media recorder is not initialized yet");
362         return INVALID_OPERATION;
363     }
364 
365     // It appears that if an invalid file descriptor is passed through
366     // binder calls, the server-side of the inter-process function call
367     // is skipped. As a result, the check at the server-side to catch
368     // the invalid file descritpor never gets invoked. This is to workaround
369     // this issue by checking the file descriptor first before passing
370     // it through binder call.
371     int flags = fcntl(fd, F_GETFL);
372     if (flags == -1) {
373         ALOGE("Fail to get File Status Flags err: %s", strerror(errno));
374     }
375     // fd must be in read-write mode or write-only mode.
376     if ((flags & (O_RDWR | O_WRONLY)) == 0) {
377         ALOGE("File descriptor is not in read-write mode or write-only mode");
378         return BAD_VALUE;
379     }
380 
381     status_t ret = mMediaRecorder->setNextOutputFile(fd);
382     if (OK != ret) {
383         ALOGE("setNextOutputFile failed: %d", ret);
384     }
385     return ret;
386 }
387 
setVideoSize(int width,int height)388 status_t MediaRecorder::setVideoSize(int width, int height)
389 {
390     ALOGV("setVideoSize(%d, %d)", width, height);
391     if (mMediaRecorder == NULL) {
392         ALOGE("media recorder is not initialized yet");
393         return INVALID_OPERATION;
394     }
395     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
396         ALOGE("setVideoSize called in an invalid state: %d", mCurrentState);
397         return INVALID_OPERATION;
398     }
399     if (!mIsVideoSourceSet) {
400         ALOGE("Cannot set video size without setting video source first");
401         return INVALID_OPERATION;
402     }
403 
404     status_t ret = mMediaRecorder->setVideoSize(width, height);
405     if (OK != ret) {
406         ALOGE("setVideoSize failed: %d", ret);
407         mCurrentState = MEDIA_RECORDER_ERROR;
408         return ret;
409     }
410 
411     return ret;
412 }
413 
414 // Query a SurfaceMediaSurface through the Mediaserver, over the
415 // binder interface. This is used by the Filter Framework (MediaEncoder)
416 // to get an <IGraphicBufferProducer> object to hook up to ANativeWindow.
417 sp<IGraphicBufferProducer> MediaRecorder::
querySurfaceMediaSourceFromMediaServer()418         querySurfaceMediaSourceFromMediaServer()
419 {
420     Mutex::Autolock _l(mLock);
421     mSurfaceMediaSource =
422             mMediaRecorder->querySurfaceMediaSource();
423     if (mSurfaceMediaSource == NULL) {
424         ALOGE("SurfaceMediaSource could not be initialized!");
425     }
426     return mSurfaceMediaSource;
427 }
428 
429 
430 
setInputSurface(const sp<PersistentSurface> & surface)431 status_t MediaRecorder::setInputSurface(const sp<PersistentSurface>& surface)
432 {
433     ALOGV("setInputSurface");
434     if (mMediaRecorder == NULL) {
435         ALOGE("media recorder is not initialized yet");
436         return INVALID_OPERATION;
437     }
438     bool isInvalidState = (mCurrentState &
439                            (MEDIA_RECORDER_PREPARED |
440                             MEDIA_RECORDER_RECORDING));
441     if (isInvalidState) {
442         ALOGE("setInputSurface is called in an invalid state: %d", mCurrentState);
443         return INVALID_OPERATION;
444     }
445 
446     return mMediaRecorder->setInputSurface(surface);
447 }
448 
setVideoFrameRate(int frames_per_second)449 status_t MediaRecorder::setVideoFrameRate(int frames_per_second)
450 {
451     ALOGV("setVideoFrameRate(%d)", frames_per_second);
452     if (mMediaRecorder == NULL) {
453         ALOGE("media recorder is not initialized yet");
454         return INVALID_OPERATION;
455     }
456     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
457         ALOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
458         return INVALID_OPERATION;
459     }
460     if (!mIsVideoSourceSet) {
461         ALOGE("Cannot set video frame rate without setting video source first");
462         return INVALID_OPERATION;
463     }
464 
465     status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second);
466     if (OK != ret) {
467         ALOGE("setVideoFrameRate failed: %d", ret);
468         mCurrentState = MEDIA_RECORDER_ERROR;
469         return ret;
470     }
471     return ret;
472 }
473 
setParameters(const String8 & params)474 status_t MediaRecorder::setParameters(const String8& params) {
475     ALOGV("setParameters(%s)", params.string());
476     if (mMediaRecorder == NULL) {
477         ALOGE("media recorder is not initialized yet");
478         return INVALID_OPERATION;
479     }
480 
481     bool isInvalidState = (mCurrentState &
482                            (MEDIA_RECORDER_PREPARED |
483                             MEDIA_RECORDER_RECORDING |
484                             MEDIA_RECORDER_ERROR));
485 
486     // For RTP video, parameter should be set dynamically.
487     if (isInvalidState) {
488         if (mCurrentState == MEDIA_RECORDER_RECORDING &&
489             mOutputFormat == OUTPUT_FORMAT_RTP_AVP)
490             isInvalidState = false;
491     }
492     if (isInvalidState) {
493         ALOGE("setParameters is called in an invalid state: %d", mCurrentState);
494         return INVALID_OPERATION;
495     }
496 
497     status_t ret = mMediaRecorder->setParameters(params);
498     if (OK != ret) {
499         ALOGE("setParameters(%s) failed: %d", params.string(), ret);
500         // Do not change our current state to MEDIA_RECORDER_ERROR, failures
501         // of the only currently supported parameters, "max-duration" and
502         // "max-filesize" are _not_ fatal.
503     }
504 
505     return ret;
506 }
507 
prepare()508 status_t MediaRecorder::prepare()
509 {
510     ALOGV("prepare");
511     if (mMediaRecorder == NULL) {
512         ALOGE("media recorder is not initialized yet");
513         return INVALID_OPERATION;
514     }
515     if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
516         ALOGE("prepare called in an invalid state: %d", mCurrentState);
517         return INVALID_OPERATION;
518     }
519     if (mIsAudioSourceSet != mIsAudioEncoderSet) {
520         if (mIsAudioSourceSet) {
521             ALOGE("audio source is set, but audio encoder is not set");
522         } else {  // must not happen, since setAudioEncoder checks this already
523             ALOGE("audio encoder is set, but audio source is not set");
524         }
525         return INVALID_OPERATION;
526     }
527 
528     if (mIsVideoSourceSet != mIsVideoEncoderSet) {
529         if (mIsVideoSourceSet) {
530             ALOGE("video source is set, but video encoder is not set");
531         } else {  // must not happen, since setVideoEncoder checks this already
532             ALOGE("video encoder is set, but video source is not set");
533         }
534         return INVALID_OPERATION;
535     }
536 
537     status_t ret = mMediaRecorder->prepare();
538     if (OK != ret) {
539         ALOGE("prepare failed: %d", ret);
540         mCurrentState = MEDIA_RECORDER_ERROR;
541         return ret;
542     }
543     mCurrentState = MEDIA_RECORDER_PREPARED;
544     return ret;
545 }
546 
getMaxAmplitude(int * max)547 status_t MediaRecorder::getMaxAmplitude(int* max)
548 {
549     ALOGV("getMaxAmplitude");
550     if (mMediaRecorder == NULL) {
551         ALOGE("media recorder is not initialized yet");
552         return INVALID_OPERATION;
553     }
554     if (mCurrentState & MEDIA_RECORDER_ERROR) {
555         ALOGE("getMaxAmplitude called in an invalid state: %d", mCurrentState);
556         return INVALID_OPERATION;
557     }
558 
559     status_t ret = mMediaRecorder->getMaxAmplitude(max);
560     if (OK != ret) {
561         ALOGE("getMaxAmplitude failed: %d", ret);
562         mCurrentState = MEDIA_RECORDER_ERROR;
563         return ret;
564     }
565     return ret;
566 }
567 
getMetrics(Parcel * reply)568 status_t MediaRecorder::getMetrics(Parcel *reply) {
569 
570     ALOGV("getMetrics");
571 
572     status_t ret = mMediaRecorder->getMetrics(reply);
573     if (OK != ret) {
574         ALOGE("getMetrics failed: %d", ret);
575     }
576     return ret;
577 }
578 
start()579 status_t MediaRecorder::start()
580 {
581     ALOGV("start");
582     if (mMediaRecorder == NULL) {
583         ALOGE("media recorder is not initialized yet");
584         return INVALID_OPERATION;
585     }
586     if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) {
587         ALOGE("start called in an invalid state: %d", mCurrentState);
588         return INVALID_OPERATION;
589     }
590 
591     status_t ret = mMediaRecorder->start();
592     if (OK != ret) {
593         ALOGE("start failed: %d", ret);
594         mCurrentState = MEDIA_RECORDER_ERROR;
595         return ret;
596     }
597     mCurrentState = MEDIA_RECORDER_RECORDING;
598     return ret;
599 }
600 
stop()601 status_t MediaRecorder::stop()
602 {
603     ALOGV("stop");
604     if (mMediaRecorder == NULL) {
605         ALOGE("media recorder is not initialized yet");
606         return INVALID_OPERATION;
607     }
608     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
609         ALOGE("stop called in an invalid state: %d", mCurrentState);
610         return INVALID_OPERATION;
611     }
612 
613     status_t ret = mMediaRecorder->stop();
614     if (OK != ret) {
615         ALOGE("stop failed: %d", ret);
616         mCurrentState = MEDIA_RECORDER_ERROR;
617         return ret;
618     }
619 
620     // FIXME:
621     // stop and reset are semantically different.
622     // We treat them the same for now, and will change this in the future.
623     doCleanUp();
624     mCurrentState = MEDIA_RECORDER_IDLE;
625     return ret;
626 }
627 
628 // Reset should be OK in any state
reset()629 status_t MediaRecorder::reset()
630 {
631     ALOGV("reset");
632     if (mMediaRecorder == NULL) {
633         ALOGE("media recorder is not initialized yet");
634         return INVALID_OPERATION;
635     }
636 
637     doCleanUp();
638     status_t ret = UNKNOWN_ERROR;
639     switch (mCurrentState) {
640         case MEDIA_RECORDER_IDLE:
641             ret = OK;
642             break;
643 
644         case MEDIA_RECORDER_RECORDING:
645         case MEDIA_RECORDER_DATASOURCE_CONFIGURED:
646         case MEDIA_RECORDER_PREPARED:
647         case MEDIA_RECORDER_ERROR: {
648             ret = doReset();
649             if (OK != ret) {
650                 return ret;  // No need to continue
651             }
652             FALLTHROUGH_INTENDED;
653         }
654         case MEDIA_RECORDER_INITIALIZED:
655             ret = close();
656             break;
657 
658         default: {
659             ALOGE("Unexpected non-existing state: %d", mCurrentState);
660             break;
661         }
662     }
663     return ret;
664 }
665 
pause()666 status_t MediaRecorder::pause()
667 {
668     ALOGV("pause");
669     if (mMediaRecorder == NULL) {
670         ALOGE("media recorder is not initialized yet");
671         return INVALID_OPERATION;
672     }
673     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
674         ALOGE("stop called in an invalid state: %d", mCurrentState);
675         return INVALID_OPERATION;
676     }
677 
678     status_t ret = mMediaRecorder->pause();
679     if (OK != ret) {
680         ALOGE("pause failed: %d", ret);
681         mCurrentState = MEDIA_RECORDER_ERROR;
682         return ret;
683     }
684 
685     return ret;
686 }
687 
resume()688 status_t MediaRecorder::resume()
689 {
690     ALOGV("resume");
691     if (mMediaRecorder == NULL) {
692         ALOGE("media recorder is not initialized yet");
693         return INVALID_OPERATION;
694     }
695     if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
696         ALOGE("resume called in an invalid state: %d", mCurrentState);
697         return INVALID_OPERATION;
698     }
699 
700     status_t ret = mMediaRecorder->resume();
701     if (OK != ret) {
702         ALOGE("resume failed: %d", ret);
703         mCurrentState = MEDIA_RECORDER_ERROR;
704         return ret;
705     }
706 
707     return ret;
708 }
709 
close()710 status_t MediaRecorder::close()
711 {
712     ALOGV("close");
713     if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
714         ALOGE("close called in an invalid state: %d", mCurrentState);
715         return INVALID_OPERATION;
716     }
717     status_t ret = mMediaRecorder->close();
718     if (OK != ret) {
719         ALOGE("close failed: %d", ret);
720         mCurrentState = MEDIA_RECORDER_ERROR;
721         return UNKNOWN_ERROR;
722     } else {
723         mCurrentState = MEDIA_RECORDER_IDLE;
724     }
725     return ret;
726 }
727 
doReset()728 status_t MediaRecorder::doReset()
729 {
730     ALOGV("doReset");
731     status_t ret = mMediaRecorder->reset();
732     if (OK != ret) {
733         ALOGE("doReset failed: %d", ret);
734         mCurrentState = MEDIA_RECORDER_ERROR;
735         return ret;
736     } else {
737         mCurrentState = MEDIA_RECORDER_INITIALIZED;
738     }
739     return ret;
740 }
741 
doCleanUp()742 void MediaRecorder::doCleanUp()
743 {
744     ALOGV("doCleanUp");
745     mIsAudioSourceSet  = false;
746     mIsVideoSourceSet  = false;
747     mIsAudioEncoderSet = false;
748     mIsVideoEncoderSet = false;
749     mIsOutputFileSet   = false;
750     mOutputFormat      = OUTPUT_FORMAT_DEFAULT;
751 }
752 
753 // Release should be OK in any state
release()754 status_t MediaRecorder::release()
755 {
756     ALOGV("release");
757     if (mMediaRecorder != NULL) {
758         return mMediaRecorder->release();
759     }
760     return INVALID_OPERATION;
761 }
762 
MediaRecorder(const AttributionSourceState & attributionSource)763 MediaRecorder::MediaRecorder(const AttributionSourceState &attributionSource)
764         : mSurfaceMediaSource(NULL)
765 {
766     ALOGV("constructor");
767 
768     const sp<IMediaPlayerService> service(getMediaPlayerService());
769     if (service != NULL) {
770         mMediaRecorder = service->createMediaRecorder(attributionSource);
771     }
772     if (mMediaRecorder != NULL) {
773         mCurrentState = MEDIA_RECORDER_IDLE;
774     }
775 
776 
777     doCleanUp();
778 }
779 
initCheck()780 status_t MediaRecorder::initCheck()
781 {
782     return mMediaRecorder != 0 ? NO_ERROR : NO_INIT;
783 }
784 
~MediaRecorder()785 MediaRecorder::~MediaRecorder()
786 {
787     ALOGV("destructor");
788     if (mMediaRecorder != NULL) {
789         mMediaRecorder.clear();
790     }
791 
792     if (mSurfaceMediaSource != NULL) {
793         mSurfaceMediaSource.clear();
794     }
795 }
796 
setListener(const sp<MediaRecorderListener> & listener)797 status_t MediaRecorder::setListener(const sp<MediaRecorderListener>& listener)
798 {
799     ALOGV("setListener");
800     Mutex::Autolock _l(mLock);
801     mListener = listener;
802 
803     return NO_ERROR;
804 }
805 
setClientName(const String16 & clientName)806 status_t MediaRecorder::setClientName(const String16& clientName)
807 {
808     ALOGV("setClientName");
809     if (mMediaRecorder == NULL) {
810         ALOGE("media recorder is not initialized yet");
811         return INVALID_OPERATION;
812     }
813     bool isInvalidState = (mCurrentState &
814                            (MEDIA_RECORDER_PREPARED |
815                             MEDIA_RECORDER_RECORDING |
816                             MEDIA_RECORDER_ERROR));
817     if (isInvalidState) {
818         ALOGE("setClientName is called in an invalid state: %d", mCurrentState);
819         return INVALID_OPERATION;
820     }
821 
822     mMediaRecorder->setClientName(clientName);
823 
824     return NO_ERROR;
825 }
826 
notify(int msg,int ext1,int ext2)827 void MediaRecorder::notify(int msg, int ext1, int ext2)
828 {
829     ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
830 
831     sp<MediaRecorderListener> listener;
832     mLock.lock();
833     listener = mListener;
834     mLock.unlock();
835 
836     if (listener != NULL) {
837         Mutex::Autolock _l(mNotifyLock);
838         ALOGV("callback application");
839         listener->notify(msg, ext1, ext2);
840         ALOGV("back from callback");
841     }
842 }
843 
died()844 void MediaRecorder::died()
845 {
846     ALOGV("died");
847     notify(MEDIA_RECORDER_EVENT_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
848 }
849 
setInputDevice(audio_port_handle_t deviceId)850 status_t MediaRecorder::setInputDevice(audio_port_handle_t deviceId)
851 {
852     ALOGV("setInputDevice");
853 
854     if (mMediaRecorder == NULL) {
855         ALOGE("media recorder is not initialized yet");
856         return INVALID_OPERATION;
857     }
858     return mMediaRecorder->setInputDevice(deviceId);
859 }
860 
getRoutedDeviceId(audio_port_handle_t * deviceId)861 status_t MediaRecorder::getRoutedDeviceId(audio_port_handle_t* deviceId)
862 {
863     ALOGV("getRoutedDeviceId");
864 
865     if (mMediaRecorder == NULL) {
866         ALOGE("media recorder is not initialized yet");
867         return INVALID_OPERATION;
868     }
869     status_t status = mMediaRecorder->getRoutedDeviceId(deviceId);
870     if (status != NO_ERROR) {
871         *deviceId = AUDIO_PORT_HANDLE_NONE;
872     }
873     return status;
874 }
875 
enableAudioDeviceCallback(bool enabled)876 status_t MediaRecorder::enableAudioDeviceCallback(bool enabled)
877 {
878     ALOGV("enableAudioDeviceCallback");
879 
880     if (mMediaRecorder == NULL) {
881         ALOGE("media recorder is not initialized yet");
882         return INVALID_OPERATION;
883     }
884     return mMediaRecorder->enableAudioDeviceCallback(enabled);
885 }
886 
getActiveMicrophones(std::vector<media::MicrophoneInfo> * activeMicrophones)887 status_t MediaRecorder::getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones)
888 {
889     ALOGV("getActiveMicrophones");
890 
891     if (mMediaRecorder == NULL) {
892         ALOGE("media recorder is not initialized yet");
893         return INVALID_OPERATION;
894     }
895     return mMediaRecorder->getActiveMicrophones(activeMicrophones);
896 }
897 
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)898 status_t MediaRecorder::setPreferredMicrophoneDirection(audio_microphone_direction_t direction) {
899     ALOGV("setPreferredMicrophoneDirection(%d)", direction);
900     return mMediaRecorder->setPreferredMicrophoneDirection(direction);
901 }
902 
setPreferredMicrophoneFieldDimension(float zoom)903 status_t MediaRecorder::setPreferredMicrophoneFieldDimension(float zoom) {
904     ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
905     return mMediaRecorder->setPreferredMicrophoneFieldDimension(zoom);
906 }
907 
getPortId(audio_port_handle_t * portId) const908 status_t MediaRecorder::getPortId(audio_port_handle_t *portId) const
909 {
910     ALOGV("getPortId");
911 
912     if (mMediaRecorder == NULL) {
913         ALOGE("media recorder is not initialized yet");
914         return INVALID_OPERATION;
915     }
916     return mMediaRecorder->getPortId(portId);
917 }
918 
getRtpDataUsage(uint64_t * bytes)919 status_t MediaRecorder::getRtpDataUsage(uint64_t *bytes)
920 {
921     ALOGV("getRtpDataUsage");
922 
923     if (mMediaRecorder == NULL) {
924         ALOGE("media recorder is not initialized yet");
925         return INVALID_OPERATION;
926     }
927     return mMediaRecorder->getRtpDataUsage(bytes);
928 }
929 } // namespace android
930