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