MediaRecorderClient.cpp revision dd0f7a7127fdb04863b4ad66d2b846ee94358314
1/*
2 ** Copyright 2008, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaRecorderService"
19#include <utils/Log.h>
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <dirent.h>
24#include <unistd.h>
25#include <string.h>
26#include <cutils/atomic.h>
27#include <cutils/properties.h> // for property_get
28#include <android_runtime/ActivityManager.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31#include <binder/MemoryHeapBase.h>
32#include <binder/MemoryBase.h>
33
34#ifndef NO_OPENCORE
35#include <media/PVMediaRecorder.h>
36#endif
37
38#include <utils/String16.h>
39
40#include <media/AudioTrack.h>
41
42#include "MediaRecorderClient.h"
43#include "MediaPlayerService.h"
44
45#include "StagefrightRecorder.h"
46
47namespace android {
48
49const char* cameraPermission = "android.permission.CAMERA";
50const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
51
52static bool checkPermission(const char* permissionString) {
53#ifndef HAVE_ANDROID_OS
54    return true;
55#endif
56    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
57    bool ok = checkCallingPermission(String16(permissionString));
58    if (!ok) LOGE("Request requires %s", permissionString);
59    return ok;
60}
61
62status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
63{
64    LOGV("setCamera");
65    Mutex::Autolock lock(mLock);
66    if (mRecorder == NULL) {
67        LOGE("recorder is not initialized");
68        return NO_INIT;
69    }
70    return mRecorder->setCamera(camera);
71}
72
73status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
74{
75    LOGV("setPreviewSurface");
76    Mutex::Autolock lock(mLock);
77    if (mRecorder == NULL) {
78        LOGE("recorder is not initialized");
79        return NO_INIT;
80    }
81    return mRecorder->setPreviewSurface(surface);
82}
83
84status_t MediaRecorderClient::setVideoSource(int vs)
85{
86    LOGV("setVideoSource(%d)", vs);
87    if (!checkPermission(cameraPermission)) {
88        return PERMISSION_DENIED;
89    }
90    Mutex::Autolock lock(mLock);
91    if (mRecorder == NULL)	{
92        LOGE("recorder is not initialized");
93        return NO_INIT;
94    }
95    return mRecorder->setVideoSource((video_source)vs);
96}
97
98status_t MediaRecorderClient::setAudioSource(int as)
99{
100    LOGV("setAudioSource(%d)", as);
101    if (!checkPermission(recordAudioPermission)) {
102        return PERMISSION_DENIED;
103    }
104    Mutex::Autolock lock(mLock);
105    if (mRecorder == NULL)  {
106        LOGE("recorder is not initialized");
107        return NO_INIT;
108    }
109    return mRecorder->setAudioSource((audio_source)as);
110}
111
112status_t MediaRecorderClient::setOutputFormat(int of)
113{
114    LOGV("setOutputFormat(%d)", of);
115    Mutex::Autolock lock(mLock);
116    if (mRecorder == NULL) {
117        LOGE("recorder is not initialized");
118        return NO_INIT;
119    }
120    return mRecorder->setOutputFormat((output_format)of);
121}
122
123status_t MediaRecorderClient::setVideoEncoder(int ve)
124{
125    LOGV("setVideoEncoder(%d)", ve);
126    Mutex::Autolock lock(mLock);
127    if (mRecorder == NULL) {
128        LOGE("recorder is not initialized");
129        return NO_INIT;
130    }
131    return mRecorder->setVideoEncoder((video_encoder)ve);
132}
133
134status_t MediaRecorderClient::setAudioEncoder(int ae)
135{
136    LOGV("setAudioEncoder(%d)", ae);
137    Mutex::Autolock lock(mLock);
138    if (mRecorder == NULL) {
139        LOGE("recorder is not initialized");
140        return NO_INIT;
141    }
142    return mRecorder->setAudioEncoder((audio_encoder)ae);
143}
144
145status_t MediaRecorderClient::setOutputFile(const char* path)
146{
147    LOGV("setOutputFile(%s)", path);
148    Mutex::Autolock lock(mLock);
149    if (mRecorder == NULL) {
150        LOGE("recorder is not initialized");
151        return NO_INIT;
152    }
153    return mRecorder->setOutputFile(path);
154}
155
156status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
157{
158    LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
159    Mutex::Autolock lock(mLock);
160    if (mRecorder == NULL) {
161        LOGE("recorder is not initialized");
162        return NO_INIT;
163    }
164    return mRecorder->setOutputFile(fd, offset, length);
165}
166
167status_t MediaRecorderClient::setVideoSize(int width, int height)
168{
169    LOGV("setVideoSize(%dx%d)", width, height);
170    Mutex::Autolock lock(mLock);
171    if (mRecorder == NULL) {
172        LOGE("recorder is not initialized");
173        return NO_INIT;
174    }
175    return mRecorder->setVideoSize(width, height);
176}
177
178status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
179{
180    LOGV("setVideoFrameRate(%d)", frames_per_second);
181    Mutex::Autolock lock(mLock);
182    if (mRecorder == NULL) {
183        LOGE("recorder is not initialized");
184        return NO_INIT;
185    }
186    return mRecorder->setVideoFrameRate(frames_per_second);
187}
188
189status_t MediaRecorderClient::setParameters(const String8& params) {
190    LOGV("setParameters(%s)", params.string());
191    Mutex::Autolock lock(mLock);
192    if (mRecorder == NULL) {
193        LOGE("recorder is not initialized");
194        return NO_INIT;
195    }
196    return mRecorder->setParameters(params);
197}
198
199status_t MediaRecorderClient::prepare()
200{
201    LOGV("prepare");
202    Mutex::Autolock lock(mLock);
203    if (mRecorder == NULL) {
204        LOGE("recorder is not initialized");
205        return NO_INIT;
206    }
207    return mRecorder->prepare();
208}
209
210
211status_t MediaRecorderClient::getMaxAmplitude(int* max)
212{
213    LOGV("getMaxAmplitude");
214    Mutex::Autolock lock(mLock);
215    if (mRecorder == NULL) {
216        LOGE("recorder is not initialized");
217        return NO_INIT;
218    }
219    return mRecorder->getMaxAmplitude(max);
220}
221
222status_t MediaRecorderClient::start()
223{
224    LOGV("start");
225    Mutex::Autolock lock(mLock);
226    if (mRecorder == NULL) {
227        LOGE("recorder is not initialized");
228        return NO_INIT;
229    }
230    return mRecorder->start();
231
232}
233
234status_t MediaRecorderClient::stop()
235{
236    LOGV("stop");
237    Mutex::Autolock lock(mLock);
238    if (mRecorder == NULL) {
239        LOGE("recorder is not initialized");
240        return NO_INIT;
241    }
242    return mRecorder->stop();
243}
244
245status_t MediaRecorderClient::init()
246{
247    LOGV("init");
248    Mutex::Autolock lock(mLock);
249    if (mRecorder == NULL) {
250        LOGE("recorder is not initialized");
251        return NO_INIT;
252    }
253    return mRecorder->init();
254}
255
256status_t MediaRecorderClient::close()
257{
258    LOGV("close");
259    Mutex::Autolock lock(mLock);
260    if (mRecorder == NULL) {
261        LOGE("recorder is not initialized");
262        return NO_INIT;
263    }
264    return mRecorder->close();
265}
266
267
268status_t MediaRecorderClient::reset()
269{
270    LOGV("reset");
271    Mutex::Autolock lock(mLock);
272    if (mRecorder == NULL) {
273        LOGE("recorder is not initialized");
274        return NO_INIT;
275    }
276    return mRecorder->reset();
277}
278
279status_t MediaRecorderClient::release()
280{
281    LOGV("release");
282    Mutex::Autolock lock(mLock);
283    if (mRecorder != NULL) {
284        delete mRecorder;
285        mRecorder = NULL;
286        wp<MediaRecorderClient> client(this);
287        mMediaPlayerService->removeMediaRecorderClient(client);
288    }
289    return NO_ERROR;
290}
291
292MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
293{
294    LOGV("Client constructor");
295    mPid = pid;
296
297    char value[PROPERTY_VALUE_MAX];
298    if (!property_get("media.stagefright.enable-record", value, NULL)
299        || !strcmp(value, "1") || !strcasecmp(value, "true")) {
300        mRecorder = new StagefrightRecorder;
301    } else
302#ifndef NO_OPENCORE
303    {
304        mRecorder = new PVMediaRecorder();
305    }
306#else
307    {
308        mRecorder = NULL;
309    }
310#endif
311
312    mMediaPlayerService = service;
313}
314
315MediaRecorderClient::~MediaRecorderClient()
316{
317    LOGV("Client destructor");
318    release();
319}
320
321status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
322{
323    LOGV("setListener");
324    Mutex::Autolock lock(mLock);
325    if (mRecorder == NULL) {
326        LOGE("recorder is not initialized");
327        return NO_INIT;
328    }
329    return mRecorder->setListener(listener);
330}
331
332status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
333    if (mRecorder != NULL) {
334        return mRecorder->dump(fd, args);
335    }
336    return OK;
337}
338
339}; // namespace android
340