mediarecorder.cpp revision 7b5eb023f8d87cca6d830ae6c11c6aadbe02aca8
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("setAudioEncoder failed: %d", ret);
252        mCurrentState = MEDIA_RECORDER_ERROR;
253        return UNKNOWN_ERROR;
254    }
255    mIsOutputFileSet = true;
256    return ret;
257}
258
259status_t MediaRecorder::setVideoSize(int width, int height)
260{
261    LOGV("setVideoSize(%d, %d)", width, height);
262    if(mMediaRecorder == NULL) {
263        LOGE("media recorder is not initialized yet");
264        return INVALID_OPERATION;
265    }
266    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
267        LOGE("setVideoSize called in an invalid state: %d", mCurrentState);
268        return INVALID_OPERATION;
269    }
270
271    status_t ret = mMediaRecorder->setVideoSize(width, height);
272    if (OK != ret) {
273        LOGE("setVideoSize failed: %d", ret);
274        mCurrentState = MEDIA_RECORDER_ERROR;
275        return UNKNOWN_ERROR;
276    }
277    return ret;
278}
279
280status_t MediaRecorder::setVideoFrameRate(int frames_per_second)
281{
282    LOGV("setVideoFrameRate(%d)", frames_per_second);
283    if(mMediaRecorder == NULL) {
284        LOGE("media recorder is not initialized yet");
285        return INVALID_OPERATION;
286    }
287    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
288        LOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
289        return INVALID_OPERATION;
290    }
291
292    status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second);
293    if (OK != ret) {
294        LOGE("setVideoFrameRate failed: %d", ret);
295        mCurrentState = MEDIA_RECORDER_ERROR;
296        return UNKNOWN_ERROR;
297    }
298    return ret;
299}
300
301status_t MediaRecorder::prepare()
302{
303    LOGV("prepare");
304    if(mMediaRecorder == NULL) {
305        LOGE("media recorder is not initialized yet");
306        return INVALID_OPERATION;
307    }
308    if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
309        LOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
310        return INVALID_OPERATION;
311    }
312
313    status_t ret = mMediaRecorder->prepare();
314    if (OK != ret) {
315        LOGE("prepare failed: %d", ret);
316        mCurrentState = MEDIA_RECORDER_ERROR;
317        return UNKNOWN_ERROR;
318    }
319    mCurrentState = MEDIA_RECORDER_PREPARED;
320    return ret;
321}
322
323status_t MediaRecorder::getMaxAmplitude(int* max)
324{
325    LOGV("getMaxAmplitude");
326    if(mMediaRecorder == NULL) {
327        LOGE("media recorder is not initialized yet");
328        return INVALID_OPERATION;
329    }
330    if (mCurrentState & MEDIA_RECORDER_ERROR) {
331        LOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState);
332        return INVALID_OPERATION;
333    }
334
335    status_t ret = mMediaRecorder->getMaxAmplitude(max);
336    if (OK != ret) {
337        LOGE("getMaxAmplitude failed: %d", ret);
338        mCurrentState = MEDIA_RECORDER_ERROR;
339        return UNKNOWN_ERROR;
340    }
341    return ret;
342}
343
344status_t MediaRecorder::start()
345{
346    LOGV("start");
347    if (mMediaRecorder == NULL) {
348        LOGE("media recorder is not initialized yet");
349        return INVALID_OPERATION;
350    }
351    if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) {
352        LOGE("start called in an invalid state: %d", mCurrentState);
353        return INVALID_OPERATION;
354    }
355
356    status_t ret = mMediaRecorder->start();
357    if (OK != ret) {
358        LOGE("start failed: %d", ret);
359        mCurrentState = MEDIA_RECORDER_ERROR;
360        return UNKNOWN_ERROR;
361    }
362    mCurrentState = MEDIA_RECORDER_RECORDING;
363    return ret;
364}
365
366status_t MediaRecorder::stop()
367{
368    LOGV("stop");
369    if (mMediaRecorder == NULL) {
370        LOGE("media recorder is not initialized yet");
371        return INVALID_OPERATION;
372    }
373    if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) {
374        LOGE("stop called in an invalid state: %d", mCurrentState);
375        return INVALID_OPERATION;
376    }
377
378    status_t ret = mMediaRecorder->stop();
379    if (OK != ret) {
380        LOGE("stop failed: %d", ret);
381        mCurrentState = MEDIA_RECORDER_ERROR;
382        return UNKNOWN_ERROR;
383    }
384    mCurrentState = MEDIA_RECORDER_IDLE;
385    return ret;
386}
387
388// Reset should be OK in any state
389status_t MediaRecorder::reset()
390{
391    LOGV("reset");
392    if (mMediaRecorder == NULL) {
393        LOGE("media recorder is not initialized yet");
394        return INVALID_OPERATION;
395    }
396
397    doCleanUp();
398    status_t ret = UNKNOWN_ERROR;
399    switch(mCurrentState) {
400        case MEDIA_RECORDER_IDLE:
401            ret = OK;
402            break;
403
404        case MEDIA_RECORDER_RECORDING:
405        case MEDIA_RECORDER_DATASOURCE_CONFIGURED:
406        case MEDIA_RECORDER_PREPARED:
407        case MEDIA_RECORDER_ERROR: {
408            ret = doReset();
409            if (OK != ret) {
410               return ret;  // No need to continue
411            }
412        }  // Intentional fall through
413        case MEDIA_RECORDER_INITIALIZED:
414            ret = close();
415            break;
416
417        default: {
418            LOGE("Unexpected non-existing state: %d", mCurrentState);
419            break;
420        }
421    }
422    return ret;
423}
424
425status_t MediaRecorder::close()
426{
427    LOGV("close");
428    if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) {
429        LOGE("close called in an invalid state: %d", mCurrentState);
430        return INVALID_OPERATION;
431    }
432    status_t ret = mMediaRecorder->close();
433    if (OK != ret) {
434        LOGE("close failed: %d", ret);
435        mCurrentState = MEDIA_RECORDER_ERROR;
436        return UNKNOWN_ERROR;
437    } else {
438        mCurrentState = MEDIA_RECORDER_IDLE;
439    }
440    return ret;
441}
442
443status_t MediaRecorder::doReset()
444{
445    LOGV("doReset");
446    status_t ret = mMediaRecorder->reset();
447    if (OK != ret) {
448        LOGE("doReset failed: %d", ret);
449        mCurrentState = MEDIA_RECORDER_ERROR;
450        return UNKNOWN_ERROR;
451    } else {
452        mCurrentState = MEDIA_RECORDER_INITIALIZED;
453    }
454    return ret;
455}
456
457void MediaRecorder::doCleanUp()
458{
459    LOGV("doCleanUp");
460    mIsAudioSourceSet  = false;
461    mIsVideoSourceSet  = false;
462    mIsAudioEncoderSet = false;
463    mIsVideoEncoderSet = false;
464    mIsOutputFileSet   = false;
465}
466
467// Release should be OK in any state
468status_t MediaRecorder::release()
469{
470    LOGV("release");
471    if (mMediaRecorder != NULL) {
472        return mMediaRecorder->release();
473    }
474    return INVALID_OPERATION;
475}
476
477MediaRecorder::MediaRecorder()
478{
479    LOGV("constructor");
480    sp<IServiceManager> sm = defaultServiceManager();
481    sp<IBinder> binder;
482
483    do {
484        binder = sm->getService(String16("media.player"));
485        if (binder != NULL) {
486            break;
487        }
488        LOGW("MediaPlayerService not published, waiting...");
489        usleep(500000); // 0.5 s
490    } while(true);
491
492    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
493    if (service != NULL) {
494        mMediaRecorder = service->createMediaRecorder(getpid());
495    }
496
497    mMediaRecorder = service->createMediaRecorder(getpid());
498    if (mMediaRecorder != NULL) {
499        mCurrentState = MEDIA_RECORDER_IDLE;
500    }
501    doCleanUp();
502}
503
504status_t MediaRecorder::initCheck()
505{
506    return mMediaRecorder != 0 ? NO_ERROR : NO_INIT;
507}
508
509MediaRecorder::~MediaRecorder()
510{
511    LOGV("destructor");
512    if (mMediaRecorder != NULL) {
513        mMediaRecorder.clear();
514    }
515}
516
517}; // namespace android
518
519