-
Notifications
You must be signed in to change notification settings - Fork 5
/
DrawTask.java
240 lines (214 loc) · 7.51 KB
/
DrawTask.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* ScreenRecordingSample
* Sample project to cature and save audio from internal and video from screen as MPEG4 file.
*
* Copyright (c) 2015-2016 saki t_saki@serenegiant.com
*
* File name: MediaScreenEncoder.java
*
* 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.
*
* All files in the folder are under this Apache License, Version 2.0.
*/
/*
* =======
* Origin:
* =======
* https://github.com/saki4510t/ScreenRecordingSample
* https://github.com/saki4510t/ScreenRecordingSample/blob/9419559a193f2b90c8f86de82c975494a7b2f7d0/app/src/main/java/com/serenegiant/media/MediaScreenEncoder.java#L112
*/
package net.majorkernelpanic.screening.video;
import com.serenegiant.glutils.EGLBase;
import com.serenegiant.glutils.EglTask;
import com.serenegiant.glutils.GLDrawer2D;
import android.graphics.SurfaceTexture;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.opengl.GLES20;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.Surface;
public final class DrawTask extends EglTask {
private static final boolean DEBUG = true;
private static final String TAG = "VideoStream:DrawTask";
private final Object mSync = new Object();
private final float[] mTexMatrix = new float[16];
private MediaProjection mMediaProjection;
private Surface mSurface;
private VideoQuality mQuality;
private Handler mHandler;
private GLDrawer2D mDrawer;
private int mTexId;
private SurfaceTexture mSourceTexture;
private Surface mSourceSurface;
private EGLBase.IEglSurface mEncoderSurface;
private long intervals;
private VirtualDisplay display;
private boolean mIsRecording;
private boolean requestDraw;
// Callback listener when receiving the video at TextureSurface
private final SurfaceTexture.OnFrameAvailableListener mOnFrameAvailableListener = new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
//if (DEBUG) Log.v(TAG, "SurfaceTexture.Callback#onFrameAvailable: mIsRecording=" + mIsRecording);
if (mIsRecording) {
synchronized (mSync) {
requestDraw = true;
mSync.notifyAll();
}
}
}
};
private final VirtualDisplay.Callback mCallback = (!DEBUG) ? null : new VirtualDisplay.Callback() {
/**
* Called when the virtual display video projection has been
* paused by the system or when the surface has been detached
* by the application by calling setSurface(null).
* The surface will not receive any more buffers while paused.
*/
@Override
public void onPaused() {
if (DEBUG) Log.v(TAG, "VirtualDisplay.Callback#onPaused");
}
/**
* Called when the virtual display video projection has been
* resumed after having been paused.
*/
@Override
public void onResumed() {
if (DEBUG) Log.v(TAG, "VirtualDisplay.Callback#onResumed");
}
/**
* Called when the virtual display video projection has been
* stopped by the system. It will no longer receive frames
* and it will never be resumed. It is still the responsibility
* of the application to release() the virtual display.
*/
@Override
public void onStopped() {
if (DEBUG) Log.v(TAG, "VirtualDisplay.Callback#onStopped");
}
};
private final Runnable mDrawTask = new Runnable() {
@Override
public void run() {
if (DEBUG) Log.v(TAG, "draw");
boolean local_request_draw;
synchronized (mSync) {
local_request_draw = requestDraw;
if (!requestDraw) {
try {
mSync.wait(intervals);
local_request_draw = requestDraw;
requestDraw = false;
} catch (final InterruptedException e) {
return;
}
}
}
if (mIsRecording) {
if (local_request_draw) {
mSourceTexture.updateTexImage();
mSourceTexture.getTransformMatrix(mTexMatrix);
}
// Draw the image received by SurfaceTexture on the input Surface of MediaCodec
mEncoderSurface.makeCurrent();
mDrawer.draw(mTexId, mTexMatrix, 0);
mEncoderSurface.swap();
// Workaround for models that hang if not drawn off-screen for EGL retention
makeCurrent();
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glFlush();
queueEvent(this);
} else {
releaseSelf();
}
if (DEBUG) Log.v(TAG, "draw:finished");
}
};
public DrawTask(
final EGLBase.IContext sharedContext,
final int flags,
final MediaProjection mediaProjection,
final Surface surface,
final VideoQuality quality
) {
super(sharedContext, flags);
this.mMediaProjection = mediaProjection;
this.mSurface = surface;
this.mQuality = quality;
final HandlerThread thread = new HandlerThread(TAG);
thread.start();
mHandler = new Handler(thread.getLooper());
}
@Override
protected void onStart() {
if (DEBUG) Log.d(TAG, "onStart");
mDrawer = new GLDrawer2D(true);
mTexId = mDrawer.initTex();
mSourceTexture = new SurfaceTexture(mTexId);
mSourceTexture.setDefaultBufferSize(mQuality.screenWidth, mQuality.screenHeight); // If you don't put this in, you can't get the image
mSourceSurface = new Surface(mSourceTexture);
mSourceTexture.setOnFrameAvailableListener(mOnFrameAvailableListener, mHandler);
mEncoderSurface = getEgl().createFromSurface(mSurface);
if (DEBUG) Log.d(TAG, "setup VirtualDisplay");
intervals = (long)(1000f / mQuality.framerate);
display = mMediaProjection.createVirtualDisplay(
"RTSP ScreenCaster",
mQuality.screenWidth, mQuality.screenHeight, mQuality.screenDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSourceSurface, mCallback, mHandler
);
if (DEBUG) Log.v(TAG, "screen capture loop: display=" + display);
mIsRecording = true;
requestDraw = false;
queueEvent(mDrawTask);
}
@Override
protected void onStop() {
mIsRecording = false;
if (display != null) {
if (DEBUG) Log.v(TAG, "release VirtualDisplay");
display.release();
}
if (mEncoderSurface != null) {
mEncoderSurface.release();
mEncoderSurface = null;
}
if (mSourceSurface != null) {
mSourceSurface.release();
mSourceSurface = null;
}
if (mSourceTexture != null) {
mSourceTexture.release();
mSourceTexture = null;
}
if (mDrawer != null) {
mDrawer.release();
mDrawer = null;
}
makeCurrent();
if (DEBUG) Log.v(TAG, "onStop");
}
@Override
protected boolean onError(final Exception e) {
if (DEBUG) Log.w(TAG, e);
return false;
}
@Override
protected Object processRequest(final int request, final int arg1, final int arg2, final Object obj) {
return null;
}
}