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