mediarecorder.cpp revision f9f083e2853740c97588f4db82c24645ae5880e4
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#include <utils/Log.h>
21#include <surfaceflinger/Surface.h>
22#include <media/mediarecorder.h>
23#include <binder/IServiceManager.h>
24#include <utils/String8.h>
25#include <media/IMediaPlayerService.h>
26#include <media/IMediaRecorder.h>
27#include <media/mediaplayer.h>  // for MEDIA_ERROR_SERVER_DIED
28
29namespace android {
30
31status_t MediaRecorder::setCamera(const sp<ICamera>& camera)
32{
33    LOGV("setCamera(%p)", camera.get());
34    if(mMediaRecorder == NULL) {
35        LOGE("media recorder is not initialized yet");
36        return INVALID_OPERATION;
37    }
38    if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
39        LOGE("setCamera called in an invalid state(%d)", mCurrentState);
40        return INVALID_OPERATION;
41    }
42
43    status_t ret = mMediaRecorder->setCamera(camera);
44    if (OK != ret) {
45        LOGV("setCamera failed: %d", ret);
46        mCurrentState = MEDIA_RECORDER_ERROR;
47        return ret;
48    }
49    return ret;
50}
51
52status_t MediaRecorder::setPreviewSurface(const sp<Surface>& surface)
53{
54    LOGV("setPreviewSurface(%p)", surface.get());
55    if(mMediaRecorder == NULL) {
56        LOGE("media recorder is not initialized yet");
57        return INVALID_OPERATION;
58    }
59    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
60        LOGE("setPreviewSurface called in an invalid state(%d)", mCurrentState);
61        return INVALID_OPERATION;
62    }
63    if (!mIsVideoSourceSet) {
64        LOGE("try to set preview surface without setting the video source first");
65        return INVALID_OPERATION;
66    }
67
68    status_t ret = mMediaRecorder->setPreviewSurface(surface);
69    if (OK != ret) {
70        LOGV("setPreviewSurface failed: %d", ret);
71        mCurrentState = MEDIA_RECORDER_ERROR;
72        return ret;
73    }
74    return ret;
75}
76
77status_t MediaRecorder::init()
78{
79    LOGV("init");
80    if(mMediaRecorder == NULL) {
81        LOGE("media recorder is not initialized yet");
82        return INVALID_OPERATION;
83    }
84    if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
85        LOGE("init called in an invalid state(%d)", mCurrentState);
86        return INVALID_OPERATION;
87    }
88
89    status_t ret = mMediaRecorder->init();
90    if (OK != ret) {
91        LOGV("init failed: %d", ret);
92        mCurrentState = MEDIA_RECORDER_ERROR;
93        return ret;
94    }
95
96    ret = mMediaRecorder->setListener(this);
97    if (OK != ret) {
98        LOGV("setListener failed: %d", ret);
99        mCurrentState = MEDIA_RECORDER_ERROR;
100        return ret;
101    }
102
103    mCurrentState = MEDIA_RECORDER_INITIALIZED;
104    return ret;
105}
106
107status_t MediaRecorder::setVideoSource(int vs)
108{
109    LOGV("setVideoSource(%d)", vs);
110    if(mMediaRecorder == NULL) {
111        LOGE("media recorder is not initialized yet");
112        return INVALID_OPERATION;
113    }
114    if (mIsVideoSourceSet) {
115        LOGE("video source has already been set");
116        return INVALID_OPERATION;
117    }
118    if (mCurrentState & MEDIA_RECORDER_IDLE) {
119        LOGV("Call init() since the media recorder is not initialized yet");
120        status_t ret = init();
121        if (OK != ret) {
122            return ret;
123        }
124    }
125    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
126        LOGE("setVideoSource called in an invalid state(%d)", mCurrentState);
127        return INVALID_OPERATION;
128    }
129
130    status_t ret = mMediaRecorder->setVideoSource(vs);
131    if (OK != ret) {
132        LOGV("setVideoSource failed: %d", ret);
133        mCurrentState = MEDIA_RECORDER_ERROR;
134        return ret;
135    }
136    mIsVideoSourceSet = true;
137    return ret;
138}
139
140status_t MediaRecorder::setAudioSource(int as)
141{
142    LOGV("setAudioSource(%d)", as);
143    if(mMediaRecorder == NULL) {
144        LOGE("media recorder is not initialized yet");
145        return INVALID_OPERATION;
146    }
147    if (mCurrentState & MEDIA_RECORDER_IDLE) {
148        LOGV("Call init() since the media recorder is not initialized yet");
149        status_t ret = init();
150        if (OK != ret) {
151            return ret;
152        }
153    }
154    if (mIsAudioSourceSet) {
155        LOGE("audio source has already been set");
156        return INVALID_OPERATION;
157    }
158    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
159        LOGE("setAudioSource called in an invalid state(%d)", mCurrentState);
160        return INVALID_OPERATION;
161    }
162
163    status_t ret = mMediaRecorder->setAudioSource(as);
164    if (OK != ret) {
165        LOGV("setAudioSource failed: %d", ret);
166        mCurrentState = MEDIA_RECORDER_ERROR;
167        return ret;
168    }
169    mIsAudioSourceSet = true;
170    return ret;
171}
172
173status_t MediaRecorder::setOutputFormat(int of)
174{
175    LOGV("setOutputFormat(%d)", of);
176    if(mMediaRecorder == NULL) {
177        LOGE("media recorder is not initialized yet");
178        return INVALID_OPERATION;
179    }
180    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
181        LOGE("setOutputFormat called in an invalid state: %d", mCurrentState);
182        return INVALID_OPERATION;
183    }
184    if (mIsVideoSourceSet && of >= OUTPUT_FORMAT_AUDIO_ONLY_START && of != OUTPUT_FORMAT_RTP_AVP && of != OUTPUT_FORMAT_MPEG2TS) { //first non-video output format
185        LOGE("output format (%d) is meant for audio recording only and incompatible with video recording", of);
186        return INVALID_OPERATION;
187    }
188
189    status_t ret = mMediaRecorder->setOutputFormat(of);
190    if (OK != ret) {
191        LOGE("setOutputFormat failed: %d", ret);
192        mCurrentState = MEDIA_RECORDER_ERROR;
193        return ret;
194    }
195    mCurrentState = MEDIA_RECORDER_DATASOURCE_CONFIGURED;
196    return ret;
197}
198
199status_t MediaRecorder::setVideoEncoder(int ve)
200{
201    LOGV("setVideoEncoder(%d)", ve);
202    if(mMediaRecorder == NULL) {
203        LOGE("media recorder is not initialized yet");
204        return INVALID_OPERATION;
205    }
206    if (!mIsVideoSourceSet) {
207        LOGE("try to set the video encoder without setting the video source first");
208        return INVALID_OPERATION;
209    }
210    if (mIsVideoEncoderSet) {
211        LOGE("video encoder has already been set");
212        return INVALID_OPERATION;
213    }
214    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
215        LOGE("setVideoEncoder called in an invalid state(%d)", mCurrentState);
216        return INVALID_OPERATION;
217    }
218
219    status_t ret = mMediaRecorder->setVideoEncoder(ve);
220    if (OK != ret) {
221        LOGV("setVideoEncoder failed: %d", ret);
222        mCurrentState = MEDIA_RECORDER_ERROR;
223        return ret;
224    }
225    mIsVideoEncoderSet = true;
226    return ret;
227}
228
229status_t MediaRecorder::setAudioEncoder(int ae)
230{
231    LOGV("setAudioEncoder(%d)", ae);
232    if(mMediaRecorder == NULL) {
233        LOGE("media recorder is not initialized yet");
234        return INVALID_OPERATION;
235    }
236    if (!mIsAudioSourceSet) {
237        LOGE("try to set the audio encoder without setting the audio source first");
238        return INVALID_OPERATION;
239    }
240    if (mIsAudioEncoderSet) {
241        LOGE("audio encoder has already been set");
242        return INVALID_OPERATION;
243    }
244    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
245        LOGE("setAudioEncoder called in an invalid state(%d)", mCurrentState);
246        return INVALID_OPERATION;
247    }
248
249    status_t ret = mMediaRecorder->setAudioEncoder(ae);
250    if (OK != ret) {
251        LOGV("setAudioEncoder failed: %d", ret);
252        mCurrentState = MEDIA_RECORDER_ERROR;
253        return ret;
254    }
255    mIsAudioEncoderSet = true;
256    return ret;
257}
258
259status_t MediaRecorder::setOutputFile(const char* path)
260{
261    LOGV("setOutputFile(%s)", path);
262    if(mMediaRecorder == NULL) {
263        LOGE("media recorder is not initialized yet");
264        return INVALID_OPERATION;
265    }
266    if (mIsOutputFileSet) {
267        LOGE("output file has already been set");
268        return INVALID_OPERATION;
269    }
270    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
271        LOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
272        return INVALID_OPERATION;
273    }
274
275    status_t ret = mMediaRecorder->setOutputFile(path);
276    if (OK != ret) {
277        LOGV("setOutputFile failed: %d", ret);
278        mCurrentState = MEDIA_RECORDER_ERROR;
279        return ret;
280    }
281    mIsOutputFileSet = true;
282    return ret;
283}
284
285status_t MediaRecorder::setOutputFile(int fd, int64_t offset, int64_t length)
286{
287    LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
288    if(mMediaRecorder == NULL) {
289        LOGE("media recorder is not initialized yet");
290        return INVALID_OPERATION;
291    }
292    if (mIsOutputFileSet) {
293        LOGE("output file has already been set");
294        return INVALID_OPERATION;
295    }
296    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
297        LOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
298        return INVALID_OPERATION;
299    }
300
301    status_t ret = mMediaRecorder->setOutputFile(fd, offset, length);
302    if (OK != ret) {
303        LOGV("setOutputFile failed: %d", ret);
304        mCurrentState = MEDIA_RECORDER_ERROR;
305        return ret;
306    }
307    mIsOutputFileSet = true;
308    return ret;
309}
310
311status_t MediaRecorder::setOutputFileAuxiliary(int fd)
312{
313    LOGV("setOutputFileAuxiliary(%d)", fd);
314    if(mMediaRecorder == NULL) {
315        LOGE("media recorder is not initialized yet");
316        return INVALID_OPERATION;
317    }
318    if (mIsAuxiliaryOutputFileSet) {
319        LOGE("output file has already been set");
320        return INVALID_OPERATION;
321    }
322    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
323        LOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
324        return INVALID_OPERATION;
325    }
326
327    status_t ret = mMediaRecorder->setOutputFileAuxiliary(fd);
328    if (OK != ret) {
329        LOGV("setOutputFileAuxiliary failed: %d", ret);
330        mCurrentState = MEDIA_RECORDER_ERROR;
331        return ret;
332    }
333    mIsAuxiliaryOutputFileSet = true;
334    return ret;
335}
336
337status_t MediaRecorder::setVideoSize(int width, int height)
338{
339    LOGV("setVideoSize(%d, %d)", width, height);
340    if(mMediaRecorder == NULL) {
341        LOGE("media recorder is not initialized yet");
342        return INVALID_OPERATION;
343    }
344    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
345        LOGE("setVideoSize called in an invalid state: %d", mCurrentState);
346        return INVALID_OPERATION;
347    }
348    if (!mIsVideoSourceSet) {
349        LOGE("try to set video size without setting video source first");
350        return INVALID_OPERATION;
351    }
352
353    status_t ret = mMediaRecorder->setVideoSize(width, height);
354    if (OK != ret) {
355        LOGE("setVideoSize failed: %d", ret);
356        mCurrentState = MEDIA_RECORDER_ERROR;
357        return ret;
358    }
359    return ret;
360}
361
362status_t MediaRecorder::setVideoFrameRate(int frames_per_second)
363{
364    LOGV("setVideoFrameRate(%d)", frames_per_second);
365    if(mMediaRecorder == NULL) {
366        LOGE("media recorder is not initialized yet");
367        return INVALID_OPERATION;
368    }
369    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
370        LOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
371        return INVALID_OPERATION;
372    }
373    if (!mIsVideoSourceSet) {
374        LOGE("try to set video frame rate without setting video source first");
375        return INVALID_OPERATION;
376    }
377
378    status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second);
379    if (OK != ret) {
380        LOGE("setVideoFrameRate failed: %d", ret);
381        mCurrentState = MEDIA_RECORDER_ERROR;
382        return ret;
383    }
384    return ret;
385}
386
387status_t MediaRecorder::setParameters(const String8& params) {
388    LOGV("setParameters(%s)", params.string());
389    if(mMediaRecorder == NULL) {
390        LOGE("media recorder is not initialized yet");
391        return INVALID_OPERATION;
392    }
393
394    bool isInvalidState = (mCurrentState &
395                           (MEDIA_RECORDER_PREPARED |
396                            MEDIA_RECORDER_RECORDING |
397                            MEDIA_RECORDER_ERROR));
398    if (isInvalidState) {
399        LOGE("setParameters is called in an invalid state: %d", mCurrentState);
400        return INVALID_OPERATION;
401    }
402
403    status_t ret = mMediaRecorder->setParameters(params);
404    if (OK != ret) {
405        LOGE("setParameters(%s) failed: %d", params.string(), ret);
406        // Do not change our current state to MEDIA_RECORDER_ERROR, failures
407        // of the only currently supported parameters, "max-duration" and
408        // "max-filesize" are _not_ fatal.
409    }
410
411    return ret;
412}
413
414status_t MediaRecorder::prepare()
415{
416    LOGV("prepare");
417    if(mMediaRecorder == NULL) {
418        LOGE("media recorder is not initialized yet");
419        return INVALID_OPERATION;
420    }
421    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
422        LOGE("prepare called in an invalid state: %d", mCurrentState);
423        return INVALID_OPERATION;
424    }
425    if (mIsAudioSourceSet != mIsAudioEncoderSet) {
426        if (mIsAudioSourceSet) {
427            LOGE("audio source is set, but audio encoder is not set");
428        } else {  // must not happen, since setAudioEncoder checks this already
429            LOGE("audio encoder is set, but audio source is not set");
430        }
431        return INVALID_OPERATION;
432    }
433
434    if (mIsVideoSourceSet != mIsVideoEncoderSet) {
435        if (mIsVideoSourceSet) {
436            LOGE("video source is set, but video encoder is not set");
437        } else {  // must not happen, since setVideoEncoder checks this already
438            LOGE("video encoder is set, but video source is not set");
439        }
440        return INVALID_OPERATION;
441    }
442
443    status_t ret = mMediaRecorder->prepare();
444    if (OK != ret) {
445        LOGE("prepare failed: %d", ret);
446        mCurrentState = MEDIA_RECORDER_ERROR;
447        return ret;
448    }
449    mCurrentState = MEDIA_RECORDER_PREPARED;
450    return ret;
451}
452
453status_t MediaRecorder::getMaxAmplitude(int* max)
454{
455    LOGV("getMaxAmplitude");
456    if(mMediaRecorder == NULL) {
457        LOGE("media recorder is not initialized yet");
458        return INVALID_OPERATION;
459    }
460    if (mCurrentState & MEDIA_RECORDER_ERROR) {
461        LOGE("getMaxAmplitude called in an invalid state: %d", mCurrentState);
462        return INVALID_OPERATION;
463    }
464
465    status_t ret = mMediaRecorder->getMaxAmplitude(max);
466    if (OK != ret) {
467        LOGE("getMaxAmplitude failed: %d", ret);
468        mCurrentState = MEDIA_RECORDER_ERROR;
469        return ret;
470    }
471    return ret;
472}
473
474status_t MediaRecorder::start()
475{
476    LOGV("start");
477    if (mMediaRecorder == NULL) {
478        LOGE("media recorder is not initialized yet");
479        return INVALID_OPERATION;
480    }
481    if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) {
482        LOGE("start called in an invalid state: %d", mCurrentState);
483        return INVALID_OPERATION;
484    }
485
486    status_t ret = mMediaRecorder->start();
487    if (OK != ret) {
488        LOGE("start failed: %d", ret);
489        mCurrentState = MEDIA_RECORDER_ERROR;
490        return ret;
491    }
492    mCurrentState = MEDIA_RECORDER_RECORDING;
493    return ret;
494}
495
496status_t MediaRecorder::stop()
497{
498    LOGV("stop");
499    if (mMediaRecorder == NULL) {
500        LOGE("media recorder is not initialized yet");
501        return INVALID_OPERATION;
502    }
503    if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
504        LOGE("stop called in an invalid state: %d", mCurrentState);
505        return INVALID_OPERATION;
506    }
507
508    status_t ret = mMediaRecorder->stop();
509    if (OK != ret) {
510        LOGE("stop failed: %d", ret);
511        mCurrentState = MEDIA_RECORDER_ERROR;
512        return ret;
513    }
514
515    // FIXME:
516    // stop and reset are semantically different.
517    // We treat them the same for now, and will change this in the future.
518    doCleanUp();
519    mCurrentState = MEDIA_RECORDER_IDLE;
520    return ret;
521}
522
523// Reset should be OK in any state
524status_t MediaRecorder::reset()
525{
526    LOGV("reset");
527    if (mMediaRecorder == NULL) {
528        LOGE("media recorder is not initialized yet");
529        return INVALID_OPERATION;
530    }
531
532    doCleanUp();
533    status_t ret = UNKNOWN_ERROR;
534    switch(mCurrentState) {
535        case MEDIA_RECORDER_IDLE:
536            ret = OK;
537            break;
538
539        case MEDIA_RECORDER_RECORDING:
540        case MEDIA_RECORDER_DATASOURCE_CONFIGURED:
541        case MEDIA_RECORDER_PREPARED:
542        case MEDIA_RECORDER_ERROR: {
543            ret = doReset();
544            if (OK != ret) {
545               return ret;  // No need to continue
546            }
547        }  // Intentional fall through
548        case MEDIA_RECORDER_INITIALIZED:
549            ret = close();
550            break;
551
552        default: {
553            LOGE("Unexpected non-existing state: %d", mCurrentState);
554            break;
555        }
556    }
557    return ret;
558}
559
560status_t MediaRecorder::close()
561{
562    LOGV("close");
563    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
564        LOGE("close called in an invalid state: %d", mCurrentState);
565        return INVALID_OPERATION;
566    }
567    status_t ret = mMediaRecorder->close();
568    if (OK != ret) {
569        LOGE("close failed: %d", ret);
570        mCurrentState = MEDIA_RECORDER_ERROR;
571        return UNKNOWN_ERROR;
572    } else {
573        mCurrentState = MEDIA_RECORDER_IDLE;
574    }
575    return ret;
576}
577
578status_t MediaRecorder::doReset()
579{
580    LOGV("doReset");
581    status_t ret = mMediaRecorder->reset();
582    if (OK != ret) {
583        LOGE("doReset failed: %d", ret);
584        mCurrentState = MEDIA_RECORDER_ERROR;
585        return ret;
586    } else {
587        mCurrentState = MEDIA_RECORDER_INITIALIZED;
588    }
589    return ret;
590}
591
592void MediaRecorder::doCleanUp()
593{
594    LOGV("doCleanUp");
595    mIsAudioSourceSet  = false;
596    mIsVideoSourceSet  = false;
597    mIsAudioEncoderSet = false;
598    mIsVideoEncoderSet = false;
599    mIsOutputFileSet   = false;
600    mIsAuxiliaryOutputFileSet = false;
601}
602
603// Release should be OK in any state
604status_t MediaRecorder::release()
605{
606    LOGV("release");
607    if (mMediaRecorder != NULL) {
608        return mMediaRecorder->release();
609    }
610    return INVALID_OPERATION;
611}
612
613MediaRecorder::MediaRecorder()
614{
615    LOGV("constructor");
616
617    const sp<IMediaPlayerService>& service(getMediaPlayerService());
618    if (service != NULL) {
619        mMediaRecorder = service->createMediaRecorder(getpid());
620    }
621    if (mMediaRecorder != NULL) {
622        mCurrentState = MEDIA_RECORDER_IDLE;
623    }
624    doCleanUp();
625}
626
627status_t MediaRecorder::initCheck()
628{
629    return mMediaRecorder != 0 ? NO_ERROR : NO_INIT;
630}
631
632MediaRecorder::~MediaRecorder()
633{
634    LOGV("destructor");
635    if (mMediaRecorder != NULL) {
636        mMediaRecorder.clear();
637    }
638}
639
640status_t MediaRecorder::setListener(const sp<MediaRecorderListener>& listener)
641{
642    LOGV("setListener");
643    Mutex::Autolock _l(mLock);
644    mListener = listener;
645
646    return NO_ERROR;
647}
648
649void MediaRecorder::notify(int msg, int ext1, int ext2)
650{
651    LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
652
653    sp<MediaRecorderListener> listener;
654    mLock.lock();
655    listener = mListener;
656    mLock.unlock();
657
658    if (listener != NULL) {
659        Mutex::Autolock _l(mNotifyLock);
660        LOGV("callback application");
661        listener->notify(msg, ext1, ext2);
662        LOGV("back from callback");
663    }
664}
665
666void MediaRecorder::died()
667{
668    LOGV("died");
669    notify(MEDIA_RECORDER_EVENT_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
670}
671
672}; // namespace android
673