forked from ob-f/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRtspServer.java
275 lines (230 loc) · 7.87 KB
/
RtspServer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package org.openbot.env;
import static org.openbot.utils.ConnectionUtils.getIPAddress;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.media.ToneGenerator;
import android.util.Log;
import android.util.Size;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.pedro.rtplibrary.view.OpenGlView;
import com.pedro.rtsp.utils.ConnectCheckerRtsp;
import com.pedro.rtspserver.RtspServerCamera1;
import java.util.concurrent.TimeUnit;
import org.openbot.customview.AutoFitSurfaceView;
import org.openbot.customview.AutoFitTextureView;
import org.openbot.utils.AndGate;
import org.openbot.utils.ConnectionUtils;
import org.openbot.utils.DelayedRunner;
import org.webrtc.SurfaceViewRenderer;
import timber.log.Timber;
public class RtspServer
implements ConnectCheckerRtsp,
SurfaceHolder.Callback,
TextureView.SurfaceTextureListener,
IVideoServer {
private final String TAG = "RtspServerPedroOpenGL";
private RtspServerCamera1 rtspServerCamera1;
private View view;
private AndGate andGate;
private Context context;
private Size resolution = new Size(640, 360);
private final int PORT = 1935;
public RtspServer() {}
// IVideoServer Interface
@Override
public void init(Context context) {
this.context = context;
/*
AndGate will run 'startServer()' if all its input conditions are met.
This is useful if we do not know the order of the updates to the conditions.
*/
AndGate.Action startAction = () -> startServer();
AndGate.Action stopAction = () -> stopServer();
andGate = new AndGate(startAction, stopAction);
andGate.addCondition("connected");
andGate.addCondition("surfaceCreated");
andGate.addCondition("view set");
andGate.addCondition("camera permission");
andGate.addCondition("resolution set");
andGate.addCondition("can start");
int camera = ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA);
andGate.set("camera permission", camera == PackageManager.PERMISSION_GRANTED);
}
@Override
public boolean isRunning() {
return rtspServerCamera1 != null && rtspServerCamera1.isStreaming();
}
@Override
public void startClient() {
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("VIDEO_PROTOCOL", "RTSP"));
sendServerUrl();
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("VIDEO_COMMAND", "START"));
rtspServerCamera1.enableAudio();
}
@Override
public void sendServerUrl() {
BotToControllerEventBus.emitEvent(
ConnectionUtils.createStatus(
"VIDEO_SERVER_URL", "rtsp://" + getIPAddress(true) + ":" + PORT
// "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"
));
}
@Override
public void sendVideoStoppedStatus() {
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("VIDEO_COMMAND", "STOP"));
}
@Override
public void setView(SurfaceView view) {
this.view = view;
((AutoFitSurfaceView) this.view).getHolder().addCallback(this);
andGate.set("view set", true);
}
@Override
public void setView(TextureView view) {
this.view = view;
((AutoFitTextureView) this.view).setSurfaceTextureListener(this);
andGate.set("view set", true);
}
@Override
public void setView(SurfaceViewRenderer view) {
this.view = view;
andGate.set("view set", true);
}
@Override
public void setView(OpenGlView view) {
this.view = view;
((OpenGlView) this.view).getHolder().addCallback(this);
andGate.set("view set", true);
}
@Override
public void setCanStart(boolean canStart) {
andGate.set("can start", canStart);
}
@Override
public void setConnected(boolean connected) {
int camera = ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA);
andGate.set("camera permission", camera == PackageManager.PERMISSION_GRANTED);
andGate.set("connected", connected);
}
// end Interface
// Local methods
private void stopServer() {
try {
if (rtspServerCamera1 != null) {
if (rtspServerCamera1.isRecording()) {
rtspServerCamera1.stopRecord();
}
if (rtspServerCamera1.isStreaming()) {
rtspServerCamera1.stopStream();
}
rtspServerCamera1.stopPreview();
rtspServerCamera1 = null;
}
} catch (Exception e) {
Log.d(TAG, "Got error stopping server: " + e);
}
}
private void startServer() {
startServer(resolution, PORT);
}
private void startServer(Size resolution, int port) {
if (rtspServerCamera1 == null) {
Timber.d("Resolution %dx%d", resolution.getWidth(), resolution.getHeight());
String viewType = this.view.getClass().getName();
if (viewType.contains("AutoFitTextureView")) {
rtspServerCamera1 = new RtspServerCamera1((AutoFitTextureView) view, this, port);
}
if (viewType.contains("AutoFitSurfaceView")) {
rtspServerCamera1 = new RtspServerCamera1((SurfaceView) view, this, port);
}
if (viewType.contains("AutoFitSurfaceGlView")) {
rtspServerCamera1 = new RtspServerCamera1((OpenGlView) view, this, port);
}
}
if (!rtspServerCamera1.isStreaming()) {
if (rtspServerCamera1.prepareAudio(64 * 1024, 32000, false, true, true)
&& rtspServerCamera1.prepareVideo(
resolution.getWidth(), resolution.getHeight(), 20, 1200 * 1024, 2, 0)) {
rtspServerCamera1.startStream("");
// Delay starting the client for a second to make sure the server is started.
Runnable action = () -> startClient();
new DelayedRunner().runAfter(action, 1000L, TimeUnit.MILLISECONDS);
}
}
}
@Override
public void setResolution(int w, int h) {
resolution = new Size(w, h);
andGate.set("resolution set", true);
}
// ConnectCheckerRtsp callbacks
@Override
public void onConnectionSuccessRtsp() {
Log.i(TAG, "onConnectionSuccessRtsp");
}
@Override
public void onConnectionFailedRtsp(final String reason) {
rtspServerCamera1.stopStream();
beep();
}
@Override
public void onNewBitrateRtsp(long bitrate) {
Log.i(TAG, "Bitrate set to " + bitrate);
}
@Override
public void onDisconnectRtsp() {
Log.i(TAG, "onDisconnectRtsp");
}
@Override
public void onAuthErrorRtsp() {
beep();
}
@Override
public void onAuthSuccessRtsp() {}
// SurfaceHolder.Callback callbacks
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
Log.d(TAG, "Surface created...");
andGate.set("surfaceCreated", true);
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
if (rtspServerCamera1 != null) {
rtspServerCamera1.startPreview();
}
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
andGate.set("surfaceCreated", false);
sendVideoStoppedStatus();
andGate.set("surfaceCreated", false);
}
// SurfaceTextureListener callbacks
@Override
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
andGate.set("surfaceCreated", true);
}
@Override
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {}
@Override
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
sendVideoStoppedStatus();
andGate.set("surfaceCreated", false);
return false;
}
@Override
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
Log.i(TAG, "onSurfaceTextureUpdated called");
}
// Utils
private void beep() {
final ToneGenerator tg = new ToneGenerator(6, 100);
tg.startTone(ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE);
}
}