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