| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package org.pjsip; |
| 19 |
|
| 20 |
import android.graphics.SurfaceTexture; |
| 21 |
import android.hardware.Camera; |
| 22 |
import android.util.Log; |
| 23 |
import android.view.SurfaceView; |
| 24 |
import android.view.SurfaceHolder; |
| 25 |
|
| 26 |
import java.io.IOException; |
| 27 |
|
| 28 |
public class PjCamera implements Camera.PreviewCallback, SurfaceHolder.Callback |
| 29 |
{ |
| 30 |
private final String TAG = "PjCamera"; |
| 31 |
|
| 32 |
public class Param { |
| 33 |
public int width; |
| 34 |
public int height; |
| 35 |
public int format; |
| 36 |
public int fps1000; |
| 37 |
} |
| 38 |
|
| 39 |
private Camera camera = null; |
| 40 |
private boolean isRunning = false; |
| 41 |
private int camIdx; |
| 42 |
private long userData; |
| 43 |
|
| 44 |
private Param param = null; |
| 45 |
|
| 46 |
private SurfaceView surfaceView = null; |
| 47 |
private SurfaceHolder surfaceHolder = null; |
| 48 |
private SurfaceTexture surfaceTexture = null; |
| 49 |
|
| 50 |
public PjCamera(int idx, int w, int h, int fmt, int fps, |
| 51 |
long userData_, SurfaceView surface) |
| 52 |
{ |
| 53 |
camIdx = idx; |
| 54 |
userData = userData_; |
| 55 |
|
| 56 |
param = new Param(); |
| 57 |
param.width = w; |
| 58 |
param.height = h; |
| 59 |
param.format = fmt; |
| 60 |
param.fps1000 = fps; |
| 61 |
|
| 62 |
SetSurfaceView(surface); |
| 63 |
} |
| 64 |
|
| 65 |
public void SetSurfaceView(SurfaceView surface) |
| 66 |
{ |
| 67 |
boolean isCaptureRunning = isRunning; |
| 68 |
|
| 69 |
if (isCaptureRunning) |
| 70 |
Stop(); |
| 71 |
|
| 72 |
if (surface != null) { |
| 73 |
surfaceView = surface; |
| 74 |
surfaceHolder = surfaceView.getHolder(); |
| 75 |
} else { |
| 76 |
|
| 77 |
surfaceHolder = null; |
| 78 |
surfaceView = null; |
| 79 |
if (surfaceTexture == null) { |
| 80 |
surfaceTexture = new SurfaceTexture(10); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (isCaptureRunning) |
| 85 |
Start(); |
| 86 |
} |
| 87 |
|
| 88 |
public int SwitchDevice(int idx) |
| 89 |
{ |
| 90 |
boolean isCaptureRunning = isRunning; |
| 91 |
int oldIdx = camIdx; |
| 92 |
|
| 93 |
if (isCaptureRunning) |
| 94 |
Stop(); |
| 95 |
|
| 96 |
camIdx = idx; |
| 97 |
|
| 98 |
if (isCaptureRunning) { |
| 99 |
int ret = Start(); |
| 100 |
if (ret != 0) { |
| 101 |
|
| 102 |
camIdx = oldIdx; |
| 103 |
Start(); |
| 104 |
return ret; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
public int Start() |
| 112 |
{ |
| 113 |
try { |
| 114 |
camera = Camera.open(camIdx); |
| 115 |
} catch (Exception e) { |
| 116 |
Log.d("IOException", e.getMessage()); |
| 117 |
return -10; |
| 118 |
} |
| 119 |
|
| 120 |
try { |
| 121 |
if (surfaceHolder != null) { |
| 122 |
camera.setPreviewDisplay(surfaceHolder); |
| 123 |
surfaceHolder.addCallback(this); |
| 124 |
} else { |
| 125 |
camera.setPreviewTexture(surfaceTexture); |
| 126 |
} |
| 127 |
} catch (IOException e) { |
| 128 |
Log.d("IOException", e.getMessage()); |
| 129 |
return -20; |
| 130 |
} |
| 131 |
|
| 132 |
Camera.Parameters cp = camera.getParameters(); |
| 133 |
cp.setPreviewSize(param.width, param.height); |
| 134 |
cp.setPreviewFormat(param.format); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
try { |
| 140 |
camera.setParameters(cp); |
| 141 |
} catch (RuntimeException e) { |
| 142 |
Log.d("RuntimeException", e.getMessage()); |
| 143 |
return -30; |
| 144 |
} |
| 145 |
|
| 146 |
camera.setPreviewCallback(this); |
| 147 |
camera.startPreview(); |
| 148 |
isRunning = true; |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
public void Stop() |
| 154 |
{ |
| 155 |
isRunning = false; |
| 156 |
if (camera == null) |
| 157 |
return; |
| 158 |
|
| 159 |
if (surfaceHolder != null) |
| 160 |
surfaceHolder.removeCallback(this); |
| 161 |
|
| 162 |
camera.setPreviewCallback(null); |
| 163 |
camera.stopPreview(); |
| 164 |
camera.release(); |
| 165 |
camera = null; |
| 166 |
} |
| 167 |
|
| 168 |
native void PushFrame(byte[] data, int length, long userData_); |
| 169 |
|
| 170 |
public void onPreviewFrame(byte[] data, Camera camera) |
| 171 |
{ |
| 172 |
if (isRunning) { |
| 173 |
PushFrame(data, data.length, userData); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public void surfaceChanged(SurfaceHolder holder, |
| 178 |
int format, int width, int height) |
| 179 |
{ |
| 180 |
Log.d(TAG, "VideoCaptureAndroid::surfaceChanged"); |
| 181 |
} |
| 182 |
|
| 183 |
public void surfaceCreated(SurfaceHolder holder) |
| 184 |
{ |
| 185 |
Log.d(TAG, "VideoCaptureAndroid::surfaceCreated"); |
| 186 |
try { |
| 187 |
if(camera != null) { |
| 188 |
camera.setPreviewDisplay(holder); |
| 189 |
} |
| 190 |
} catch (IOException e) { |
| 191 |
Log.e(TAG, "Failed to set preview surface!", e); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public void surfaceDestroyed(SurfaceHolder holder) |
| 196 |
{ |
| 197 |
Log.d(TAG, "VideoCaptureAndroid::surfaceDestroyed"); |
| 198 |
try { |
| 199 |
if(camera != null) { |
| 200 |
camera.setPreviewDisplay(null); |
| 201 |
} |
| 202 |
} catch (IOException e) { |
| 203 |
Log.e(TAG, "Failed to clear preview surface!", e); |
| 204 |
} catch (RuntimeException e) { |
| 205 |
Log.w(TAG, "Clear preview surface useless", e); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
} |