This repository has been archived by the owner on Mar 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CustomWebcamCapturer.java
148 lines (108 loc) · 3.5 KB
/
CustomWebcamCapturer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.example.learn_opentok_android;
import android.content.Context;
import android.graphics.ImageFormat;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.opentok.android.BaseVideoCapturer;
import com.serenegiant.usb.UVCCamera;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by quanhua on 14/10/2015.
*/
public class CustomWebcamCapturer extends BaseVideoCapturer {
private final static String LOGTAG = "customer-video-capturer";
private Context context;
private ReentrantLock mPreviewBufferLock = new ReentrantLock(); // sync
// start/stop
// capture
// and
// surface
// changes
private final static int PIXEL_FORMAT = ImageFormat.NV21;
private final static int PREFERRED_CAPTURE_WIDTH = UVCCamera.DEFAULT_PREVIEW_WIDTH;
private final static int PREFERRED_CAPTURE_HEIGHT = UVCCamera.DEFAULT_PREVIEW_HEIGHT;
private Long lastCaptureFrame = 0L;
private Long currentCaptureFrame = 0L;
private boolean isCaptureStarted = false;
private boolean isCaptureRunning = false;
private int mCaptureWidth = -1;
private int mCaptureHeight = -1;
private int mCaptureFPS = -1;
private Display mCurrentDisplay;
public CustomWebcamCapturer(Context context) {
// Initialize front camera by default
Log.d(LOGTAG, "CustomWebcamCapturer constructor");
this.context = context;
// Get current display to query UI orientation
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
mCurrentDisplay = windowManager.getDefaultDisplay();
}
public void addFrame(byte[] data){
if(data == null){
// Log.d(LOGTAG, "hasFrame \ NULL");
return;
}
mPreviewBufferLock.lock();
if(data != null){
if (isCaptureRunning) {
provideByteArrayFrame(data, NV21, mCaptureWidth,
mCaptureHeight, 0, false);
}
currentCaptureFrame = System.currentTimeMillis();
}
mPreviewBufferLock.unlock();
}
@Override
public int startCapture() {
if (isCaptureStarted) {
return -1;
}
Log.d(LOGTAG, "startCapture");
mPreviewBufferLock.lock();
isCaptureRunning = true;
mPreviewBufferLock.unlock();
isCaptureStarted = true;
return 0;
}
@Override
public int stopCapture() {
isCaptureStarted = false;
return 0;
}
@Override
public void destroy() {
stopCapture();
}
@Override
public boolean isCaptureStarted() {
return isCaptureStarted;
}
@Override
public CaptureSettings getCaptureSettings() {
// Set the preferred capturing size
configureCaptureSize(PREFERRED_CAPTURE_WIDTH, PREFERRED_CAPTURE_HEIGHT);
CaptureSettings settings = new CaptureSettings();
settings.fps = mCaptureFPS;
settings.width = mCaptureWidth;
settings.height = mCaptureHeight;
settings.format = NV21;
settings.expectedDelay = 0;
return settings;
}
@Override
public void onPause() {
}
@Override
public void onResume() {
}
private void configureCaptureSize(int preferredWidth, int preferredHeight) {
mCaptureFPS = 12;
mCaptureWidth = preferredWidth;
mCaptureHeight = preferredHeight;
}
@Override
public void init() {
}
}