Camera: Lazy load sound files to speed up camera startup

Lazy loading of sound files can speed up camera startup more than 60ms,
it gives users a great experience. Many apps do not playSound when camera
open or they may use their own audio files. so we load audio files as needed.

Bug: 128432959
Test: install wechat app,open camera,use systrace to see the uiThread wait time.
Change-Id: I3b3697cf9d0d919b88276f6d8e7fdd84578f4fcd
gugelfrei
username 6 years ago committed by Shuzhen Wang
parent fe610f0fa3
commit 5755fea3fb

@ -2027,31 +2027,37 @@ sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) {
return mp; return mp;
} }
void CameraService::loadSound() { void CameraService::increaseSoundRef() {
ATRACE_CALL();
Mutex::Autolock lock(mSoundLock); Mutex::Autolock lock(mSoundLock);
LOG1("CameraService::loadSound ref=%d", mSoundRef); mSoundRef++;
if (mSoundRef++) return; }
mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); void CameraService::loadSoundLocked(sound_kind kind) {
if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { ATRACE_CALL();
mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
} LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef);
mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) {
if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg");
mSoundPlayer[SOUND_RECORDING_START] = if (mSoundPlayer[SOUND_SHUTTER] == nullptr) {
mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
}
} else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) {
mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg");
if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) {
mSoundPlayer[SOUND_RECORDING_START] =
newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
} }
mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) {
if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg");
mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) {
mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
}
} }
} }
void CameraService::releaseSound() { void CameraService::decreaseSoundRef() {
Mutex::Autolock lock(mSoundLock); Mutex::Autolock lock(mSoundLock);
LOG1("CameraService::releaseSound ref=%d", mSoundRef); LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef);
if (--mSoundRef) return; if (--mSoundRef) return;
for (int i = 0; i < NUM_SOUNDS; i++) { for (int i = 0; i < NUM_SOUNDS; i++) {
@ -2067,6 +2073,7 @@ void CameraService::playSound(sound_kind kind) {
LOG1("playSound(%d)", kind); LOG1("playSound(%d)", kind);
Mutex::Autolock lock(mSoundLock); Mutex::Autolock lock(mSoundLock);
loadSoundLocked(kind);
sp<MediaPlayer> player = mSoundPlayer[kind]; sp<MediaPlayer> player = mSoundPlayer[kind];
if (player != 0) { if (player != 0) {
player->seekTo(0); player->seekTo(0);
@ -2096,7 +2103,7 @@ CameraService::Client::Client(const sp<CameraService>& cameraService,
mRemoteCallback = cameraClient; mRemoteCallback = cameraClient;
cameraService->loadSound(); cameraService->increaseSoundRef();
LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
} }
@ -2106,7 +2113,7 @@ CameraService::Client::~Client() {
ALOGV("~Client"); ALOGV("~Client");
mDestructionStarted = true; mDestructionStarted = true;
sCameraService->releaseSound(); sCameraService->decreaseSoundRef();
// unconditionally disconnect. function is idempotent // unconditionally disconnect. function is idempotent
Client::disconnect(); Client::disconnect();
} }

@ -177,10 +177,10 @@ public:
NUM_SOUNDS NUM_SOUNDS
}; };
void loadSound();
void playSound(sound_kind kind); void playSound(sound_kind kind);
void releaseSound(); void loadSoundLocked(sound_kind kind);
void decreaseSoundRef();
void increaseSoundRef();
/** /**
* Update the state of a given camera device (open/close/active/idle) with * Update the state of a given camera device (open/close/active/idle) with
* the camera proxy service in the system service * the camera proxy service in the system service

Loading…
Cancel
Save