diff options
-rw-r--r-- | src/com/android/camera/Camera.java | 66 | ||||
-rw-r--r-- | src/com/android/camera/VideoCamera.java | 63 |
2 files changed, 74 insertions, 55 deletions
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java index a647b49..fac239c 100644 --- a/src/com/android/camera/Camera.java +++ b/src/com/android/camera/Camera.java @@ -423,6 +423,8 @@ public class Camera extends Activity implements View.OnClickListener, Log.v(TAG, "mJpegPictureCallbackTimeLag = " + mJpegPictureCallbackTimeLag + "ms"); mJpegPictureCallbackTime = 0; + } else { + Log.v(TAG, "Got first frame"); } } } @@ -726,24 +728,24 @@ public class Camera extends Activity implements View.OnClickListener, public void onCreate(Bundle icicle) { super.onCreate(icicle); + Window win = getWindow(); + win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + setContentView(R.layout.camera); + mSurfaceView = (VideoPreview) findViewById(R.id.camera_preview); + mViewFinderWidth = mSurfaceView.getLayoutParams().width; + mViewFinderHeight = mSurfaceView.getLayoutParams().height; + mPreferences = PreferenceManager.getDefaultSharedPreferences(this); + /* - * To reduce startup time, we open camera device in another thread. - * We make sure the camera is opened at the end of onCreate. + * To reduce startup time, we start the preview in another thread. + * We make sure the preview is started at the end of onCreate. */ - Thread openCameraThread = new Thread(new Runnable() { + Thread startPreviewThread = new Thread(new Runnable() { public void run() { - mCameraDevice = CameraHolder.instance().open(); + startPreview(); } }); - openCameraThread.start(); - - mPreferences = PreferenceManager.getDefaultSharedPreferences(this); - - Window win = getWindow(); - win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - setContentView(R.layout.camera); - - mSurfaceView = (VideoPreview) findViewById(R.id.camera_preview); + startPreviewThread.start(); // don't set mSurfaceHolder here. We have it set ONLY within // surfaceChanged / surfaceDestroyed, other parts of the code @@ -768,9 +770,9 @@ public class Camera extends Activity implements View.OnClickListener, mSwitcher.setOnSwitchListener(this); } - // Make sure the services are loaded. + // Make sure preview is started. try { - openCameraThread.join(); + startPreviewThread.join(); } catch (InterruptedException ex) { // ignore } @@ -1020,12 +1022,12 @@ public class Camera extends Activity implements View.OnClickListener, mJpegPictureCallbackTime = 0; mImageCapture = new ImageCapture(); - if (mSurfaceHolder != null) { - // If surface holder is created, start the preview now. Otherwise, - // wait until surfaceChanged. - mSurfaceView.setAspectRatio(VideoPreview.DONT_CARE); + // Start the preview if it is not started. + if (!mPreviewing) { startPreview(); + } + if (mSurfaceHolder != null) { // If first time initialization is not finished, put it in the // message queue. if (!mFirstTimeInitialized) { @@ -1269,8 +1271,11 @@ public class Camera extends Activity implements View.OnClickListener, // Sometimes surfaceChanged is called after onPause. Ignore it. if (mPausing) return; - // Start the preview. - startPreview(); + // Set preview display if the surface is being created. Preview was + // already started. + if (holder.isCreating()) { + setPreviewDisplay(holder); + } // If first time initialization is not finished, send a message to do // it later. We want to finish surfaceChanged as soon as possible to let @@ -1335,26 +1340,27 @@ public class Camera extends Activity implements View.OnClickListener, calculatePicturesRemaining(); } + private void setPreviewDisplay(SurfaceHolder holder) { + try { + mCameraDevice.setPreviewDisplay(holder); + } catch (Throwable ex) { + closeCamera(); + throw new RuntimeException("setPreviewDisplay failed", ex); + } + } + private void startPreview() { if (mPausing) return; if (!ensureCameraDevice()) return; - if (mSurfaceHolder == null) return; - if (isFinishing()) return; // If we're previewing already, stop the preview first (this will blank // the screen). if (mPreviewing) stopPreview(); - // this blanks the screen if the surface changed, no-op otherwise - try { - mCameraDevice.setPreviewDisplay(mSurfaceHolder); - } catch (Throwable ex) { - closeCamera(); - throw new RuntimeException("setPreviewDisplay failed", ex); - } + setPreviewDisplay(mSurfaceHolder); setCameraParameter(); diff --git a/src/com/android/camera/VideoCamera.java b/src/com/android/camera/VideoCamera.java index bbfaa1f..f404145 100644 --- a/src/com/android/camera/VideoCamera.java +++ b/src/com/android/camera/VideoCamera.java @@ -212,20 +212,20 @@ public class VideoCamera extends Activity implements View.OnClickListener, public void onCreate(Bundle icicle) { super.onCreate(icicle); + mPreferences = PreferenceManager.getDefaultSharedPreferences(this); + readVideoSizePreference(); + /* - * To reduce startup time, we open camera device in another thread. - * Camera is opened in onCreate instead of onResume because there are - * lots of things to do here and camera open can be done in parallel. We - * will make sure the camera is opened at the end of onCreate. + * To reduce startup time, we start the preview in another thread. + * We make sure the preview is started at the end of onCreate. */ - Thread openCameraThread = new Thread(new Runnable() { + Thread startPreviewThread = new Thread(new Runnable() { public void run() { - mCameraDevice = CameraHolder.instance().open(); + startPreview(); } }); - openCameraThread.start(); + startPreviewThread.start(); - mPreferences = PreferenceManager.getDefaultSharedPreferences(this); mContentResolver = getContentResolver(); requestWindowFeature(Window.FEATURE_PROGRESS); @@ -275,9 +275,9 @@ public class VideoCamera extends Activity implements View.OnClickListener, mShutterButton.setOnShutterButtonListener(this); mShutterButton.requestFocus(); - // Make sure the camera is opened. + // Make sure preview is started. try { - openCameraThread.join(); + startPreviewThread.join(); } catch (InterruptedException ex) { // ignore } @@ -474,8 +474,12 @@ public class VideoCamera extends Activity implements View.OnClickListener, super.onResume(); mPausing = false; - setScreenTimeoutLong(); readVideoSizePreference(); + if (!mPreviewing) { + startPreview(); + } + + setScreenTimeoutLong(); // install an intent filter to receive SD card related events. IntentFilter intentFilter = @@ -495,7 +499,6 @@ public class VideoCamera extends Activity implements View.OnClickListener, }, 200); if (mSurfaceHolder != null) { - startPreview(); mRecorderInitialized = false; mHandler.sendEmptyMessage(INIT_RECORDER); } @@ -508,7 +511,15 @@ public class VideoCamera extends Activity implements View.OnClickListener, mCameraDevice.setParameters(param); } - // Precondition: mSurfaceHolder != null + private void setPreviewDisplay(SurfaceHolder holder) { + try { + mCameraDevice.setPreviewDisplay(holder); + } catch (Throwable ex) { + closeCamera(); + throw new RuntimeException("setPreviewDisplay failed", ex); + } + } + private void startPreview() { Log.v(TAG, "startPreview"); if (mPreviewing) { @@ -523,12 +534,8 @@ public class VideoCamera extends Activity implements View.OnClickListener, } setCameraParameters(); - try { - mCameraDevice.setPreviewDisplay(mSurfaceHolder); - } catch (Throwable ex) { - closeCamera(); - throw new RuntimeException("setPreviewDisplay failed", ex); - } + + setPreviewDisplay(mSurfaceHolder); try { mCameraDevice.startPreview(); @@ -537,7 +544,13 @@ public class VideoCamera extends Activity implements View.OnClickListener, closeCamera(); throw new RuntimeException("startPreview failed", ex); } - mCameraDevice.unlock(); + + // If setPreviewDisplay has been set with a valid surface, unlock now. + // If surface is null, unlock later. Otherwise, setPreviewDisplay in + // surfaceChanged will fail. + if (mSurfaceHolder != null) { + mCameraDevice.unlock(); + } } private void closeCamera() { @@ -664,11 +677,11 @@ public class VideoCamera extends Activity implements View.OnClickListener, stopVideoRecording(); } - // Start the preview if it is not started yet. Preview may be already - // started in onResume and then surfaceChanged is called due to - // orientation change. - if (!mPreviewing) { - startPreview(); + // Set preview display if the surface is being created. Preview was + // already started. + if (holder.isCreating()) { + setPreviewDisplay(holder); + mCameraDevice.unlock(); mRecorderInitialized = false; mHandler.sendEmptyMessage(INIT_RECORDER); } |