Home | History | Annotate | Download | only in libmediaplayerservice
      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 
     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 
     58 
     59 sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource()
     60 {
     61     ALOGV("Query SurfaceMediaSource");
     62     Mutex::Autolock lock(mLock);
     63     if (mRecorder == NULL) {
     64         ALOGE("recorder is not initialized");
     65         return NULL;
     66     }
     67     return mRecorder->querySurfaceMediaSource();
     68 }
     69 
     70 
     71 
     72 status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
     73                                         const sp<ICameraRecordingProxy>& proxy)
     74 {
     75     ALOGV("setCamera");
     76     Mutex::Autolock lock(mLock);
     77     if (mRecorder == NULL) {
     78         ALOGE("recorder is not initialized");
     79         return NO_INIT;
     80     }
     81     return mRecorder->setCamera(camera, proxy);
     82 }
     83 
     84 status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
     85 {
     86     ALOGV("setPreviewSurface");
     87     Mutex::Autolock lock(mLock);
     88     if (mRecorder == NULL) {
     89         ALOGE("recorder is not initialized");
     90         return NO_INIT;
     91     }
     92     return mRecorder->setPreviewSurface(surface);
     93 }
     94 
     95 status_t MediaRecorderClient::setVideoSource(int vs)
     96 {
     97     ALOGV("setVideoSource(%d)", vs);
     98     if (!checkPermission(cameraPermission)) {
     99         return PERMISSION_DENIED;
    100     }
    101     Mutex::Autolock lock(mLock);
    102     if (mRecorder == NULL)     {
    103         ALOGE("recorder is not initialized");
    104         return NO_INIT;
    105     }
    106     return mRecorder->setVideoSource((video_source)vs);
    107 }
    108 
    109 status_t MediaRecorderClient::setAudioSource(int as)
    110 {
    111     ALOGV("setAudioSource(%d)", as);
    112     if (!checkPermission(recordAudioPermission)) {
    113         return PERMISSION_DENIED;
    114     }
    115     Mutex::Autolock lock(mLock);
    116     if (mRecorder == NULL)  {
    117         ALOGE("recorder is not initialized");
    118         return NO_INIT;
    119     }
    120     return mRecorder->setAudioSource((audio_source_t)as);
    121 }
    122 
    123 status_t MediaRecorderClient::setOutputFormat(int of)
    124 {
    125     ALOGV("setOutputFormat(%d)", of);
    126     Mutex::Autolock lock(mLock);
    127     if (mRecorder == NULL) {
    128         ALOGE("recorder is not initialized");
    129         return NO_INIT;
    130     }
    131     return mRecorder->setOutputFormat((output_format)of);
    132 }
    133 
    134 status_t MediaRecorderClient::setVideoEncoder(int ve)
    135 {
    136     ALOGV("setVideoEncoder(%d)", ve);
    137     Mutex::Autolock lock(mLock);
    138     if (mRecorder == NULL) {
    139         ALOGE("recorder is not initialized");
    140         return NO_INIT;
    141     }
    142     return mRecorder->setVideoEncoder((video_encoder)ve);
    143 }
    144 
    145 status_t MediaRecorderClient::setAudioEncoder(int ae)
    146 {
    147     ALOGV("setAudioEncoder(%d)", ae);
    148     Mutex::Autolock lock(mLock);
    149     if (mRecorder == NULL) {
    150         ALOGE("recorder is not initialized");
    151         return NO_INIT;
    152     }
    153     return mRecorder->setAudioEncoder((audio_encoder)ae);
    154 }
    155 
    156 status_t MediaRecorderClient::setOutputFile(const char* path)
    157 {
    158     ALOGV("setOutputFile(%s)", path);
    159     Mutex::Autolock lock(mLock);
    160     if (mRecorder == NULL) {
    161         ALOGE("recorder is not initialized");
    162         return NO_INIT;
    163     }
    164     return mRecorder->setOutputFile(path);
    165 }
    166 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    303 MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
    304 {
    305     ALOGV("Client constructor");
    306     mPid = pid;
    307     mRecorder = new StagefrightRecorder;
    308     mMediaPlayerService = service;
    309 }
    310 
    311 MediaRecorderClient::~MediaRecorderClient()
    312 {
    313     ALOGV("Client destructor");
    314     release();
    315 }
    316 
    317 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
    318 {
    319     ALOGV("setListener");
    320     Mutex::Autolock lock(mLock);
    321     if (mRecorder == NULL) {
    322         ALOGE("recorder is not initialized");
    323         return NO_INIT;
    324     }
    325     return mRecorder->setListener(listener);
    326 }
    327 
    328 status_t MediaRecorderClient::setClientName(const String16& clientName) {
    329     ALOGV("setClientName(%s)", String8(clientName).string());
    330     Mutex::Autolock lock(mLock);
    331     if (mRecorder == NULL) {
    332         ALOGE("recorder is not initialized");
    333         return NO_INIT;
    334     }
    335     return mRecorder->setClientName(clientName);
    336 }
    337 
    338 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
    339     if (mRecorder != NULL) {
    340         return mRecorder->dump(fd, args);
    341     }
    342     return OK;
    343 }
    344 
    345 }; // namespace android
    346