339 lines
8.5 KiB
C++
Raw Normal View History

/*
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "MediaRecorderService"
#include <utils/Log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <cutils/atomic.h>
#include <cutils/properties.h> // for property_get
#include <android_runtime/ActivityManager.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/MemoryHeapBase.h>
#include <binder/MemoryBase.h>
#include <utils/String16.h>
#include <media/AudioTrack.h>
#include <system/audio.h>
#include "MediaRecorderClient.h"
#include "MediaPlayerService.h"
#include "StagefrightRecorder.h"
#include <gui/ISurfaceTexture.h>
namespace android {
const char* cameraPermission = "android.permission.CAMERA";
const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
static bool checkPermission(const char* permissionString) {
#ifndef HAVE_ANDROID_OS
return true;
#endif
if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
bool ok = checkCallingPermission(String16(permissionString));
if (!ok) LOGE("Request requires %s", permissionString);
return ok;
}
sp<ISurfaceTexture> MediaRecorderClient::querySurfaceMediaSource()
{
LOGV("Query SurfaceMediaSource");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NULL;
}
return mRecorder->querySurfaceMediaSource();
}
Add framework support for camcorder zoom. The purpose of ICameraRecordingProxy and ICameraRecordingProxyListener is to allow applications using the camera during recording. Camera service allows only one client at a time. Since camcorder application needs to own the camera to do things like zoom, the media recorder cannot access the camera directly during recording. So ICameraRecordingProxy is a proxy of ICamera, which allows the media recorder to start/stop the recording and release recording frames. ICameraRecordingProxyListener is an interface that allows the recorder to receive video frames during recording. ICameraRecordingProxy startRecording() stopRecording() releaseRecordingFrame() ICameraRecordingProxyListener dataCallbackTimestamp() The camcorder app opens the camera and starts the preview. The app passes ICamera and ICameraRecordingProxy to the media recorder by MediaRecorder::setCamera(). The recorder uses ICamera to setup the camera in MediaRecorder::start(). After setup, the recorder disconnects from camera service. The recorder calls ICameraRecordingProxy::startRecording() and passes a ICameraRecordingProxyListener to the app. The app connects back to camera service and starts the recording. The app owns the camera and can do things like zoom. The media recorder receives the video frames from the listener and releases them by ICameraRecordingProxy::releaseRecordingFrame. The recorder calls ICameraRecordingProxy::stopRecording() to stop the recording. The call sequences are as follows: 1. The app: Camera.unlock(). 2. The app: MediaRecorder.setCamera(). 3. Start recording (1) The app: MediaRecorder.start(). (2) The recorder: ICamera.unlock() and ICamera.disconnect(). (3) The recorder: ICameraRecordingProxy.startRecording(). (4) The app: ICamera.reconnect(). (5) The app: ICamera.startRecording(). 4. During recording (1) The recorder: receive frames from ICameraRecordingProxyListener.dataCallbackTimestamp() (2) The recorder: release frames by ICameraRecordingProxy.releaseRecordingFrame(). 5. Stop recording (1) The app: MediaRecorder.stop() (2) The recorder: ICameraRecordingProxy.stopRecording(). (3) The app: ICamera.stopRecording(). bug:2644213 Change-Id: I15269397defc25cbbcae16abc071c8349c123122
2011-06-01 17:22:24 +08:00
status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
const sp<ICameraRecordingProxy>& proxy)
{
LOGV("setCamera");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
Add framework support for camcorder zoom. The purpose of ICameraRecordingProxy and ICameraRecordingProxyListener is to allow applications using the camera during recording. Camera service allows only one client at a time. Since camcorder application needs to own the camera to do things like zoom, the media recorder cannot access the camera directly during recording. So ICameraRecordingProxy is a proxy of ICamera, which allows the media recorder to start/stop the recording and release recording frames. ICameraRecordingProxyListener is an interface that allows the recorder to receive video frames during recording. ICameraRecordingProxy startRecording() stopRecording() releaseRecordingFrame() ICameraRecordingProxyListener dataCallbackTimestamp() The camcorder app opens the camera and starts the preview. The app passes ICamera and ICameraRecordingProxy to the media recorder by MediaRecorder::setCamera(). The recorder uses ICamera to setup the camera in MediaRecorder::start(). After setup, the recorder disconnects from camera service. The recorder calls ICameraRecordingProxy::startRecording() and passes a ICameraRecordingProxyListener to the app. The app connects back to camera service and starts the recording. The app owns the camera and can do things like zoom. The media recorder receives the video frames from the listener and releases them by ICameraRecordingProxy::releaseRecordingFrame. The recorder calls ICameraRecordingProxy::stopRecording() to stop the recording. The call sequences are as follows: 1. The app: Camera.unlock(). 2. The app: MediaRecorder.setCamera(). 3. Start recording (1) The app: MediaRecorder.start(). (2) The recorder: ICamera.unlock() and ICamera.disconnect(). (3) The recorder: ICameraRecordingProxy.startRecording(). (4) The app: ICamera.reconnect(). (5) The app: ICamera.startRecording(). 4. During recording (1) The recorder: receive frames from ICameraRecordingProxyListener.dataCallbackTimestamp() (2) The recorder: release frames by ICameraRecordingProxy.releaseRecordingFrame(). 5. Stop recording (1) The app: MediaRecorder.stop() (2) The recorder: ICameraRecordingProxy.stopRecording(). (3) The app: ICamera.stopRecording(). bug:2644213 Change-Id: I15269397defc25cbbcae16abc071c8349c123122
2011-06-01 17:22:24 +08:00
return mRecorder->setCamera(camera, proxy);
}
status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
{
LOGV("setPreviewSurface");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setPreviewSurface(surface);
}
status_t MediaRecorderClient::setVideoSource(int vs)
{
LOGV("setVideoSource(%d)", vs);
if (!checkPermission(cameraPermission)) {
return PERMISSION_DENIED;
}
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setVideoSource((video_source)vs);
}
status_t MediaRecorderClient::setAudioSource(int as)
{
LOGV("setAudioSource(%d)", as);
if (!checkPermission(recordAudioPermission)) {
return PERMISSION_DENIED;
}
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setAudioSource((audio_source_t)as);
}
status_t MediaRecorderClient::setOutputFormat(int of)
{
LOGV("setOutputFormat(%d)", of);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setOutputFormat((output_format)of);
}
status_t MediaRecorderClient::setVideoEncoder(int ve)
{
LOGV("setVideoEncoder(%d)", ve);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setVideoEncoder((video_encoder)ve);
}
status_t MediaRecorderClient::setAudioEncoder(int ae)
{
LOGV("setAudioEncoder(%d)", ae);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setAudioEncoder((audio_encoder)ae);
}
status_t MediaRecorderClient::setOutputFile(const char* path)
{
LOGV("setOutputFile(%s)", path);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setOutputFile(path);
}
status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
{
LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setOutputFile(fd, offset, length);
}
status_t MediaRecorderClient::setVideoSize(int width, int height)
{
LOGV("setVideoSize(%dx%d)", width, height);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setVideoSize(width, height);
}
status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
{
LOGV("setVideoFrameRate(%d)", frames_per_second);
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setVideoFrameRate(frames_per_second);
}
status_t MediaRecorderClient::setParameters(const String8& params) {
LOGV("setParameters(%s)", params.string());
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setParameters(params);
}
status_t MediaRecorderClient::prepare()
{
LOGV("prepare");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->prepare();
}
status_t MediaRecorderClient::getMaxAmplitude(int* max)
{
LOGV("getMaxAmplitude");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->getMaxAmplitude(max);
}
status_t MediaRecorderClient::start()
{
LOGV("start");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->start();
}
status_t MediaRecorderClient::stop()
{
LOGV("stop");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->stop();
}
status_t MediaRecorderClient::init()
{
LOGV("init");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->init();
}
status_t MediaRecorderClient::close()
{
LOGV("close");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->close();
}
status_t MediaRecorderClient::reset()
{
LOGV("reset");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->reset();
}
status_t MediaRecorderClient::release()
{
LOGV("release");
Mutex::Autolock lock(mLock);
if (mRecorder != NULL) {
delete mRecorder;
mRecorder = NULL;
wp<MediaRecorderClient> client(this);
mMediaPlayerService->removeMediaRecorderClient(client);
}
return NO_ERROR;
}
MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
{
LOGV("Client constructor");
mPid = pid;
mRecorder = new StagefrightRecorder;
mMediaPlayerService = service;
}
MediaRecorderClient::~MediaRecorderClient()
{
LOGV("Client destructor");
release();
}
status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
{
LOGV("setListener");
Mutex::Autolock lock(mLock);
if (mRecorder == NULL) {
LOGE("recorder is not initialized");
return NO_INIT;
}
return mRecorder->setListener(listener);
}
status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
if (mRecorder != NULL) {
return mRecorder->dump(fd, args);
}
return OK;
}
}; // namespace android