mediarecorder.cpp revision ad04d9201452001dbaac4349f084cc9316190b89
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 <ui/Surface.h>
22#include <media/mediarecorder.h>
23#include <utils/IServiceManager.h>
24#include <media/IMediaPlayerService.h>
25#include <media/IMediaRecorder.h>
26
27namespace android {
28
29status_t MediaRecorder::setCamera(const sp<ICamera>& camera)
30{
31    LOGV("setCamera(%p)", camera.get());
32    if(mMediaRecorder == NULL) {
33        LOGE("media recorder is not initialized yet");
34        return INVALID_OPERATION;
35    }
36    if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
37        LOGE("setCamera called in an invalid state(%d)", mCurrentState);
38        return INVALID_OPERATION;
39    }
40
41    status_t ret = mMediaRecorder->setCamera(camera);
42    if (OK != ret) {
43        LOGV("setCamera failed: %d", ret);
44        mCurrentState = MEDIA_RECORDER_ERROR;
45        return UNKNOWN_ERROR;
46    }
47    return ret;
48}
49
50status_t MediaRecorder::setPreviewSurface(const sp<Surface>& surface)
51{
52    LOGV("setPreviewSurface(%p)", surface.get());
53    if(mMediaRecorder == NULL) {
54        LOGE("media recorder is not initialized yet");
55        return INVALID_OPERATION;
56    }
57    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
58        LOGE("setPreviewSurface called in an invalid state(%d)", mCurrentState);
59        return INVALID_OPERATION;
60    }
61
62    status_t ret = mMediaRecorder->setPreviewSurface(surface->getISurface());
63    if (OK != ret) {
64        LOGV("setPreviewSurface failed: %d", ret);
65        mCurrentState = MEDIA_RECORDER_ERROR;
66        return UNKNOWN_ERROR;
67    }
68    return ret;
69}
70
71status_t MediaRecorder::init()
72{
73    LOGV("init");
74    if(mMediaRecorder == NULL) {
75        LOGE("media recorder is not initialized yet");
76        return INVALID_OPERATION;
77    }
78    if (!(mCurrentState & MEDIA_RECORDER_IDLE)) {
79        LOGE("init called in an invalid state(%d)", mCurrentState);
80        return INVALID_OPERATION;
81    }
82
83    status_t ret = mMediaRecorder->init();
84    if (OK != ret) {
85        LOGV("init failed: %d", ret);
86        mCurrentState = MEDIA_RECORDER_ERROR;
87        return UNKNOWN_ERROR;
88    }
89    mCurrentState = MEDIA_RECORDER_INITIALIZED;
90    return ret;
91}
92
93status_t MediaRecorder::setVideoSource(int vs)
94{
95    LOGV("setVideoSource(%d)", vs);
96    if(mMediaRecorder == NULL) {
97        LOGE("media recorder is not initialized yet");
98        return INVALID_OPERATION;
99    }
100    if (mIsVideoSourceSet) {
101        LOGE("video source has already been set");
102        return INVALID_OPERATION;
103    }
104    if (mCurrentState & MEDIA_RECORDER_IDLE) {
105        LOGV("Call init() since the media recorder is not initialized yet");
106        status_t ret = init();
107        if (OK != ret) {
108            return ret;
109        }
110    }
111    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
112        LOGE("setVideoSource called in an invalid state(%d)", mCurrentState);
113        return INVALID_OPERATION;
114    }
115
116    status_t ret = mMediaRecorder->setVideoSource(vs);
117    if (OK != ret) {
118        LOGV("setVideoSource failed: %d", ret);
119        mCurrentState = MEDIA_RECORDER_ERROR;
120        return UNKNOWN_ERROR;
121    }
122    mIsVideoSourceSet = true;
123    return ret;
124}
125
126status_t MediaRecorder::setAudioSource(int as)
127{
128    LOGV("setAudioSource(%d)", as);
129    if(mMediaRecorder == NULL) {
130        LOGE("media recorder is not initialized yet");
131        return INVALID_OPERATION;
132    }
133    if (mCurrentState & MEDIA_RECORDER_IDLE) {
134        LOGV("Call init() since the media recorder is not initialized yet");
135        status_t ret = init();
136        if (OK != ret) {
137            return ret;
138        }
139    }
140    if (mIsAudioSourceSet) {
141        LOGE("audio source has already been set");
142        return INVALID_OPERATION;
143    }
144    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
145        LOGE("setAudioSource called in an invalid state(%d)", mCurrentState);
146        return INVALID_OPERATION;
147    }
148
149    status_t ret = mMediaRecorder->setAudioSource(as);
150    if (OK != ret) {
151        LOGV("setAudioSource failed: %d", ret);
152        mCurrentState = MEDIA_RECORDER_ERROR;
153        return UNKNOWN_ERROR;
154    }
155    mIsAudioSourceSet = true;
156    return ret;
157}
158
159status_t MediaRecorder::setOutputFormat(int of)
160{
161    LOGV("setOutputFormat(%d)", of);
162    if(mMediaRecorder == NULL) {
163        LOGE("media recorder is not initialized yet");
164        return INVALID_OPERATION;
165    }
166    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
167        LOGE("setOutputFormat called in an invalid state: %d", mCurrentState);
168        return INVALID_OPERATION;
169    }
170
171    status_t ret = mMediaRecorder->setOutputFormat(of);
172    if (OK != ret) {
173        LOGE("setOutputFormat failed: %d", ret);
174        mCurrentState = MEDIA_RECORDER_ERROR;
175        return UNKNOWN_ERROR;
176    }
177    mCurrentState = MEDIA_RECORDER_DATASOURCE_CONFIGURED;
178    return ret;
179}
180
181status_t MediaRecorder::setVideoEncoder(int ve)
182{
183    LOGV("setVideoEncoder(%d)", ve);
184    if(mMediaRecorder == NULL) {
185        LOGE("media recorder is not initialized yet");
186        return INVALID_OPERATION;
187    }
188    if (mIsVideoEncoderSet) {
189        LOGE("video encoder has already been set");
190        return INVALID_OPERATION;
191    }
192    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
193        LOGE("setVideoEncoder called in an invalid state(%d)", mCurrentState);
194        return INVALID_OPERATION;
195    }
196
197    status_t ret = mMediaRecorder->setVideoEncoder(ve);
198    if (OK != ret) {
199        LOGV("setVideoEncoder failed: %d", ret);
200        mCurrentState = MEDIA_RECORDER_ERROR;
201        return UNKNOWN_ERROR;
202    }
203    mIsVideoEncoderSet = true;
204    return ret;
205}
206
207status_t MediaRecorder::setAudioEncoder(int ae)
208{
209    LOGV("setAudioEncoder(%d)", ae);
210    if(mMediaRecorder == NULL) {
211        LOGE("media recorder is not initialized yet");
212        return INVALID_OPERATION;
213    }
214    if (mIsAudioEncoderSet) {
215        LOGE("audio encoder has already been set");
216        return INVALID_OPERATION;
217    }
218    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
219        LOGE("setAudioEncoder called in an invalid state(%d)", mCurrentState);
220        return INVALID_OPERATION;
221    }
222
223    status_t ret = mMediaRecorder->setAudioEncoder(ae);
224    if (OK != ret) {
225        LOGV("setAudioEncoder failed: %d", ret);
226        mCurrentState = MEDIA_RECORDER_ERROR;
227        return UNKNOWN_ERROR;
228    }
229    mIsAudioEncoderSet = true;
230    return ret;
231}
232
233status_t MediaRecorder::setOutputFile(const char* path)
234{
235    LOGV("setOutputFile(%s)", path);
236    if(mMediaRecorder == NULL) {
237        LOGE("media recorder is not initialized yet");
238        return INVALID_OPERATION;
239    }
240    if (mIsOutputFileSet) {
241        LOGE("output file has already been set");
242        return INVALID_OPERATION;
243    }
244    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
245        LOGE("setOutputFile called in an invalid state(%d)", mCurrentState);
246        return INVALID_OPERATION;
247    }
248
249    status_t ret = mMediaRecorder->setOutputFile(path);
250    if (OK != ret) {
251        LOGV("setOutputFile failed: %d", ret);
252        mCurrentState = MEDIA_RECORDER_ERROR;
253        return UNKNOWN_ERROR;
254    }
255    mIsOutputFileSet = true;
256    return ret;
257}
258
259status_t MediaRecorder::setOutputFile(int fd, int64_t offset, int64_t length)
260{
261    LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
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(fd, offset, length);
276    if (OK != ret) {
277        LOGV("setOutputFile failed: %d", ret);
278        mCurrentState = MEDIA_RECORDER_ERROR;
279        return UNKNOWN_ERROR;
280    }
281    mIsOutputFileSet = true;
282    return ret;
283}
284
285status_t MediaRecorder::setVideoSize(int width, int height)
286{
287    LOGV("setVideoSize(%d, %d)", width, height);
288    if(mMediaRecorder == NULL) {
289        LOGE("media recorder is not initialized yet");
290        return INVALID_OPERATION;
291    }
292    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
293        LOGE("setVideoSize called in an invalid state: %d", mCurrentState);
294        return INVALID_OPERATION;
295    }
296
297    status_t ret = mMediaRecorder->setVideoSize(width, height);
298    if (OK != ret) {
299        LOGE("setVideoSize failed: %d", ret);
300        mCurrentState = MEDIA_RECORDER_ERROR;
301        return UNKNOWN_ERROR;
302    }
303    return ret;
304}
305
306status_t MediaRecorder::setVideoFrameRate(int frames_per_second)
307{
308    LOGV("setVideoFrameRate(%d)", frames_per_second);
309    if(mMediaRecorder == NULL) {
310        LOGE("media recorder is not initialized yet");
311        return INVALID_OPERATION;
312    }
313    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
314        LOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
315        return INVALID_OPERATION;
316    }
317
318    status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second);
319    if (OK != ret) {
320        LOGE("setVideoFrameRate failed: %d", ret);
321        mCurrentState = MEDIA_RECORDER_ERROR;
322        return UNKNOWN_ERROR;
323    }
324    return ret;
325}
326
327status_t MediaRecorder::prepare()
328{
329    LOGV("prepare");
330    if(mMediaRecorder == NULL) {
331        LOGE("media recorder is not initialized yet");
332        return INVALID_OPERATION;
333    }
334    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
335        LOGE("prepare called in an invalid state: %d", mCurrentState);
336        return INVALID_OPERATION;
337    }
338
339    status_t ret = mMediaRecorder->prepare();
340    if (OK != ret) {
341        LOGE("prepare failed: %d", ret);
342        mCurrentState = MEDIA_RECORDER_ERROR;
343        return UNKNOWN_ERROR;
344    }
345    mCurrentState = MEDIA_RECORDER_PREPARED;
346    return ret;
347}
348
349status_t MediaRecorder::getMaxAmplitude(int* max)
350{
351    LOGV("getMaxAmplitude");
352    if(mMediaRecorder == NULL) {
353        LOGE("media recorder is not initialized yet");
354        return INVALID_OPERATION;
355    }
356    if (mCurrentState & MEDIA_RECORDER_ERROR) {
357        LOGE("getMaxAmplitude called in an invalid state: %d", mCurrentState);
358        return INVALID_OPERATION;
359    }
360
361    status_t ret = mMediaRecorder->getMaxAmplitude(max);
362    if (OK != ret) {
363        LOGE("getMaxAmplitude failed: %d", ret);
364        mCurrentState = MEDIA_RECORDER_ERROR;
365        return UNKNOWN_ERROR;
366    }
367    return ret;
368}
369
370status_t MediaRecorder::start()
371{
372    LOGV("start");
373    if (mMediaRecorder == NULL) {
374        LOGE("media recorder is not initialized yet");
375        return INVALID_OPERATION;
376    }
377    if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) {
378        LOGE("start called in an invalid state: %d", mCurrentState);
379        return INVALID_OPERATION;
380    }
381
382    status_t ret = mMediaRecorder->start();
383    if (OK != ret) {
384        LOGE("start failed: %d", ret);
385        mCurrentState = MEDIA_RECORDER_ERROR;
386        return UNKNOWN_ERROR;
387    }
388    mCurrentState = MEDIA_RECORDER_RECORDING;
389    return ret;
390}
391
392status_t MediaRecorder::stop()
393{
394    LOGV("stop");
395    if (mMediaRecorder == NULL) {
396        LOGE("media recorder is not initialized yet");
397        return INVALID_OPERATION;
398    }
399    if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
400        LOGE("stop called in an invalid state: %d", mCurrentState);
401        return INVALID_OPERATION;
402    }
403
404    status_t ret = mMediaRecorder->stop();
405    if (OK != ret) {
406        LOGE("stop failed: %d", ret);
407        mCurrentState = MEDIA_RECORDER_ERROR;
408        return UNKNOWN_ERROR;
409    }
410    mCurrentState = MEDIA_RECORDER_IDLE;
411    return ret;
412}
413
414// Reset should be OK in any state
415status_t MediaRecorder::reset()
416{
417    LOGV("reset");
418    if (mMediaRecorder == NULL) {
419        LOGE("media recorder is not initialized yet");
420        return INVALID_OPERATION;
421    }
422
423    doCleanUp();
424    status_t ret = UNKNOWN_ERROR;
425    switch(mCurrentState) {
426        case MEDIA_RECORDER_IDLE:
427            ret = OK;
428            break;
429
430        case MEDIA_RECORDER_RECORDING:
431        case MEDIA_RECORDER_DATASOURCE_CONFIGURED:
432        case MEDIA_RECORDER_PREPARED:
433        case MEDIA_RECORDER_ERROR: {
434            ret = doReset();
435            if (OK != ret) {
436               return ret;  // No need to continue
437            }
438        }  // Intentional fall through
439        case MEDIA_RECORDER_INITIALIZED:
440            ret = close();
441            break;
442
443        default: {
444            LOGE("Unexpected non-existing state: %d", mCurrentState);
445            break;
446        }
447    }
448    return ret;
449}
450
451status_t MediaRecorder::close()
452{
453    LOGV("close");
454    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
455        LOGE("close called in an invalid state: %d", mCurrentState);
456        return INVALID_OPERATION;
457    }
458    status_t ret = mMediaRecorder->close();
459    if (OK != ret) {
460        LOGE("close failed: %d", ret);
461        mCurrentState = MEDIA_RECORDER_ERROR;
462        return UNKNOWN_ERROR;
463    } else {
464        mCurrentState = MEDIA_RECORDER_IDLE;
465    }
466    return ret;
467}
468
469status_t MediaRecorder::doReset()
470{
471    LOGV("doReset");
472    status_t ret = mMediaRecorder->reset();
473    if (OK != ret) {
474        LOGE("doReset failed: %d", ret);
475        mCurrentState = MEDIA_RECORDER_ERROR;
476        return UNKNOWN_ERROR;
477    } else {
478        mCurrentState = MEDIA_RECORDER_INITIALIZED;
479    }
480    return ret;
481}
482
483void MediaRecorder::doCleanUp()
484{
485    LOGV("doCleanUp");
486    mIsAudioSourceSet  = false;
487    mIsVideoSourceSet  = false;
488    mIsAudioEncoderSet = false;
489    mIsVideoEncoderSet = false;
490    mIsOutputFileSet   = false;
491}
492
493// Release should be OK in any state
494status_t MediaRecorder::release()
495{
496    LOGV("release");
497    if (mMediaRecorder != NULL) {
498        return mMediaRecorder->release();
499    }
500    return INVALID_OPERATION;
501}
502
503MediaRecorder::MediaRecorder()
504{
505    LOGV("constructor");
506    sp<IServiceManager> sm = defaultServiceManager();
507    sp<IBinder> binder;
508
509    do {
510        binder = sm->getService(String16("media.player"));
511        if (binder != NULL) {
512            break;
513        }
514        LOGW("MediaPlayerService not published, waiting...");
515        usleep(500000); // 0.5 s
516    } while(true);
517
518    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
519    if (service != NULL) {
520        mMediaRecorder = service->createMediaRecorder(getpid());
521    }
522    if (mMediaRecorder != NULL) {
523        mCurrentState = MEDIA_RECORDER_IDLE;
524    }
525    doCleanUp();
526}
527
528status_t MediaRecorder::initCheck()
529{
530    return mMediaRecorder != 0 ? NO_ERROR : NO_INIT;
531}
532
533MediaRecorder::~MediaRecorder()
534{
535    LOGV("destructor");
536    if (mMediaRecorder != NULL) {
537        mMediaRecorder.clear();
538    }
539}
540
541}; // namespace android
542
543