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