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