提交 0139e4a4 编写于 作者: 云逸之's avatar 云逸之 💬

Merge branch 'udp-channel-simple' into 'master'

将udp2作为正式版

See merge request qq_26700087/simpleVideoServer!1
package org.btik.server.util; package org.btik.server.util;
import java.io.IOException;
/** /**
* @author lustre * @author lustre
* @version 1.0 * @version 1.0
......
...@@ -5,7 +5,6 @@ import org.btik.server.util.ByteUtil; ...@@ -5,7 +5,6 @@ import org.btik.server.util.ByteUtil;
import org.btik.server.util.NamePrefixThreadFactory; import org.btik.server.util.NamePrefixThreadFactory;
import org.btik.server.video.device.iface.DevChannel; import org.btik.server.video.device.iface.DevChannel;
import org.btik.server.video.device.iface.VideoChannel; import org.btik.server.video.device.iface.VideoChannel;
import org.btik.server.video.device.task.AsyncTaskExecutor;
import java.io.IOException; import java.io.IOException;
......
...@@ -28,8 +28,6 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant { ...@@ -28,8 +28,6 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant {
private final Set<Socket> clients = Collections.newSetFromMap(new ConcurrentHashMap<>()); private final Set<Socket> clients = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final byte[] clientLock = new byte[0];
public MJPEGVideoChannel(String channelId, AsyncTaskExecutor asyncTaskExecutor) { public MJPEGVideoChannel(String channelId, AsyncTaskExecutor asyncTaskExecutor) {
this.asyncTaskExecutor = asyncTaskExecutor; this.asyncTaskExecutor = asyncTaskExecutor;
this.channelId = channelId; this.channelId = channelId;
...@@ -40,23 +38,21 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant { ...@@ -40,23 +38,21 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant {
public void sendFrame(byte[] frame, int len) { public void sendFrame(byte[] frame, int len) {
byte[] lenStrBytes = ByteUtil.toString(len); byte[] lenStrBytes = ByteUtil.toString(len);
byte[] lenHexStrBytes = ByteUtil.toHexString(len); byte[] lenHexStrBytes = ByteUtil.toHexString(len);
synchronized (clientLock) {
for (Socket client : clients) {
try {
OutputStream outputStream = client.getOutputStream();
sendChunk(_STREAM_BOUNDARY, outputStream);
sendChunk(outputStream, _STREAM_PART, lenStrBytes, DOUBLE_LINE);
sendChunk(frame, len, lenHexStrBytes, outputStream);
outputStream.flush();
} catch (IOException e) {
checkState(client, e);
}
for (Socket client : clients) {
try {
OutputStream outputStream = client.getOutputStream();
sendChunk(_STREAM_BOUNDARY, outputStream);
sendChunk(outputStream, _STREAM_PART, lenStrBytes, DOUBLE_LINE);
sendChunk(frame, len, lenHexStrBytes, outputStream);
outputStream.flush();
} catch (IOException e) {
checkState(client, e);
} }
} }
}
static long l = System.currentTimeMillis(); }
@Override @Override
public void sendFrame(byte[][] frame, int[] len, int segmentCount) { public void sendFrame(byte[][] frame, int[] len, int segmentCount) {
...@@ -65,20 +61,20 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant { ...@@ -65,20 +61,20 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant {
allLen += len[i]; allLen += len[i];
} }
byte[] lenStrBytes = ByteUtil.toString(allLen); byte[] lenStrBytes = ByteUtil.toString(allLen);
synchronized (clientLock) {
for (Socket client : clients) {
try {
OutputStream outputStream = client.getOutputStream();
sendChunk(_STREAM_BOUNDARY, outputStream);
sendChunk(outputStream, _STREAM_PART, lenStrBytes, DOUBLE_LINE);
sendChunk(outputStream, allLen, len, frame);
outputStream.flush();
} catch (IOException e) {
checkState(client, e);
}
for (Socket client : clients) {
try {
OutputStream outputStream = client.getOutputStream();
sendChunk(_STREAM_BOUNDARY, outputStream);
sendChunk(outputStream, _STREAM_PART, lenStrBytes, DOUBLE_LINE);
sendChunk(outputStream, allLen, len, frame);
outputStream.flush();
} catch (IOException e) {
checkState(client, e);
} }
} }
} }
/** /**
...@@ -151,6 +147,7 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant { ...@@ -151,6 +147,7 @@ public class MJPEGVideoChannel implements VideoChannel, HttpConstant {
try { try {
System.err.println("close:" + socket.getRemoteSocketAddress()); System.err.println("close:" + socket.getRemoteSocketAddress());
socket.close(); socket.close();
clients.remove(socket);
} catch (IOException e0) { } catch (IOException e0) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
......
.\jre\bin\java -classpath SimpleVideoServer org.btik.server.video.UDPMain .\jre\bin\java -classpath SimpleVideoServer org.btik.server.video.UDP2Main
\ No newline at end of file \ No newline at end of file
./jre/bin/java -classpath SimpleVideoServer org.btik.server.video.UDPMain ./jre/bin/java -classpath SimpleVideoServer org.btik.server.video.UDP2Main
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册