MediaRecorderClient.cpp revision 510d6566bd2494991fac07aee46fcb2ddac99c13
1/*
2 ** Copyright 2008, HTC Inc.
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 <android_runtime/ActivityManager.h>
28#include <utils/IPCThreadState.h>
29#include <utils/IServiceManager.h>
30#include <utils/MemoryHeapBase.h>
31#include <utils/MemoryBase.h>
32#include <media/PVMediaRecorder.h>
33#include <utils/String16.h>
34
35#include "MediaRecorderClient.h"
36
37namespace android {
38
39const char* cameraPermission = "android.permission.CAMERA";
40const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
41
42static bool checkPermission(const char* permissionString) {
43#ifndef HAVE_ANDROID_OS
44    return true;
45#endif
46    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
47    bool ok = checkCallingPermission(String16(permissionString));
48    if (!ok) LOGE("Request requires %s", permissionString);
49    return ok;
50}
51
52status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
53{
54    LOGV("setCamera");
55    Mutex::Autolock lock(mLock);
56    if (mRecorder == NULL) {
57        LOGE("recorder is not initialized");
58        return NO_INIT;
59    }
60    return mRecorder->setCamera(camera);
61}
62
63status_t MediaRecorderClient::setPreviewSurface(const sp<ISurface>& surface)
64{
65    LOGV("setPreviewSurface");
66    Mutex::Autolock lock(mLock);
67    if (mRecorder == NULL) {
68        LOGE("recorder is not initialized");
69        return NO_INIT;
70    }
71    return mRecorder->setPreviewSurface(surface);
72}
73
74status_t MediaRecorderClient::setVideoSource(int vs)
75{
76    LOGV("setVideoSource(%d)", vs);
77    if (!checkPermission(cameraPermission)) {
78        return PERMISSION_DENIED;
79    }
80    Mutex::Autolock lock(mLock);
81    if (mRecorder == NULL)	{
82        LOGE("recorder is not initialized");
83    }
84    return mRecorder->setVideoSource((video_source)vs);
85}
86
87status_t MediaRecorderClient::setAudioSource(int as)
88{
89    LOGV("setAudioSource(%d)", as);
90    if (!checkPermission(recordAudioPermission)) {
91        return PERMISSION_DENIED;
92    }
93    Mutex::Autolock lock(mLock);
94    if (mRecorder == NULL)  {
95        LOGE("recorder is not initialized");
96    }
97    return mRecorder->setAudioSource((audio_source)as);
98}
99
100status_t MediaRecorderClient::setOutputFormat(int of)
101{
102    LOGV("setOutputFormat(%d)", of);
103    Mutex::Autolock lock(mLock);
104    if (mRecorder == NULL) {
105        LOGE("recorder is not initialized");
106        return NO_INIT;
107    }
108    return mRecorder->setOutputFormat((output_format)of);
109}
110
111status_t MediaRecorderClient::setVideoEncoder(int ve)
112{
113    LOGV("setVideoEncoder(%d)", ve);
114    Mutex::Autolock lock(mLock);
115    if (mRecorder == NULL) {
116        LOGE("recorder is not initialized");
117        return NO_INIT;
118    }
119    return mRecorder->setVideoEncoder((video_encoder)ve);
120}
121
122status_t MediaRecorderClient::setAudioEncoder(int ae)
123{
124    LOGV("setAudioEncoder(%d)", ae);
125    Mutex::Autolock lock(mLock);
126    if (mRecorder == NULL) {
127        LOGE("recorder is not initialized");
128        return NO_INIT;
129    }
130    return mRecorder->setAudioEncoder((audio_encoder)ae);
131}
132
133status_t MediaRecorderClient::setOutputFile(const char* path)
134{
135    LOGV("setOutputFile(%s)", path);
136    Mutex::Autolock lock(mLock);
137    if (mRecorder == NULL) {
138        LOGE("recorder is not initialized");
139        return NO_INIT;
140    }
141    return mRecorder->setOutputFile(path);
142}
143
144status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
145{
146    LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
147    Mutex::Autolock lock(mLock);
148    if (mRecorder == NULL) {
149        LOGE("recorder is not initialized");
150        return NO_INIT;
151    }
152    return mRecorder->setOutputFile(fd, offset, length);
153}
154
155status_t MediaRecorderClient::setVideoSize(int width, int height)
156{
157    LOGV("setVideoSize(%dx%d)", width, height);
158    Mutex::Autolock lock(mLock);
159    if (mRecorder == NULL) {
160        LOGE("recorder is not initialized");
161        return NO_INIT;
162    }
163    return mRecorder->setVideoSize(width, height);
164}
165
166status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
167{
168    LOGV("setVideoFrameRate(%d)", frames_per_second);
169    Mutex::Autolock lock(mLock);
170    if (mRecorder == NULL) {
171        LOGE("recorder is not initialized");
172        return NO_INIT;
173    }
174    return mRecorder->setVideoFrameRate(frames_per_second);
175}
176
177status_t MediaRecorderClient::setParameters(const String8& params) {
178    LOGV("setParameters(%s)", params.string());
179    Mutex::Autolock lock(mLock);
180    if (mRecorder == NULL) {
181        LOGE("recorder is not initialized");
182        return NO_INIT;
183    }
184    return mRecorder->setParameters(params);
185}
186
187status_t MediaRecorderClient::prepare()
188{
189    LOGV("prepare");
190    Mutex::Autolock lock(mLock);
191    if (mRecorder == NULL) {
192        LOGE("recorder is not initialized");
193        return NO_INIT;
194    }
195    return mRecorder->prepare();
196}
197
198
199status_t MediaRecorderClient::getMaxAmplitude(int* max)
200{
201    LOGV("getMaxAmplitude");
202    Mutex::Autolock lock(mLock);
203    if (mRecorder == NULL) {
204        LOGE("recorder is not initialized");
205        return NO_INIT;
206    }
207    return mRecorder->getMaxAmplitude(max);
208}
209
210status_t MediaRecorderClient::start()
211{
212    LOGV("start");
213    Mutex::Autolock lock(mLock);
214    if (mRecorder == NULL) {
215        LOGE("recorder is not initialized");
216        return NO_INIT;
217    }
218    return mRecorder->start();
219
220}
221
222status_t MediaRecorderClient::stop()
223{
224    LOGV("stop");
225    Mutex::Autolock lock(mLock);
226    if (mRecorder == NULL) {
227        LOGE("recorder is not initialized");
228        return NO_INIT;
229    }
230    return mRecorder->stop();
231}
232
233status_t MediaRecorderClient::init()
234{
235    LOGV("init");
236    Mutex::Autolock lock(mLock);
237    if (mRecorder == NULL) {
238        LOGE("recorder is not initialized");
239        return NO_INIT;
240    }
241    return mRecorder->init();
242}
243
244status_t MediaRecorderClient::close()
245{
246    LOGV("close");
247    Mutex::Autolock lock(mLock);
248    if (mRecorder == NULL) {
249        LOGE("recorder is not initialized");
250        return NO_INIT;
251    }
252    return mRecorder->close();
253}
254
255
256status_t MediaRecorderClient::reset()
257{
258    LOGV("reset");
259    Mutex::Autolock lock(mLock);
260    if (mRecorder == NULL) {
261        LOGE("recorder is not initialized");
262        return NO_INIT;
263    }
264    return mRecorder->reset();
265}
266
267status_t MediaRecorderClient::release()
268{
269    LOGV("release");
270    Mutex::Autolock lock(mLock);
271    if (mRecorder != NULL) {
272        delete mRecorder;
273        mRecorder = NULL;
274    }
275    return NO_ERROR;
276}
277
278MediaRecorderClient::MediaRecorderClient(pid_t pid)
279{
280    LOGV("Client constructor");
281    mPid = pid;
282    mRecorder = new PVMediaRecorder();
283}
284
285MediaRecorderClient::~MediaRecorderClient()
286{
287    LOGV("Client destructor");
288    release();
289}
290
291status_t MediaRecorderClient::setListener(const sp<IMediaPlayerClient>& listener)
292{
293    LOGV("setListener");
294    Mutex::Autolock lock(mLock);
295    if (mRecorder == NULL) {
296        LOGE("recorder is not initialized");
297        return NO_INIT;
298    }
299    return mRecorder->setListener(listener);
300}
301
302}; // namespace android
303
304