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