我有以下问题。我自己修改了这个示例:摄像头捕获实时流示例
目前,通信看起来像是client1将图像发送到服务器,来自服务器的图像被发送到client2。如果我用一台相机工作就没问题了。如果我从两个不同的摄像机里流出来,问题就开始了。我希望client1将映像发送到特定端口上的服务器,而只有在该端口上,服务器才将映像发送给客户。2目前(我不知道为什么)是服务器得到什么的情况,例如在端口2000上,它发送到所有端口,而不仅仅是端口2000。你能帮帮我吗?
来自服务器的一些代码:
@Override
public void start(SocketAddress streamAddress) {
logger.info("server started:{}", streamAddress);
Channel channel = serverBootstrap.bind(streamAddress);
channelGroup.add(channel);
}。
this.serverBootstrap = new ServerBootstrap();
this.serverBootstrap.setFactory(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
this.serverBootstrap.setPipelineFactory(new StreamServerChannelPipelineFactory(new StreamServerListenerIMPL(), streamFrameListener));。
public static void send(BufferedImage image) throws Exception {
Object msg = h264StreamEncoder.encode(image);
channelGroup.write(msg);
}来自client1的代码:
public static void init(String host, int port) {
Webcam webcam = Webcam.getWebcams().get(0);
Dimension sizeVideo = WebcamResolution.QVGA.getSize();
webcam.setViewSize(sizeVideo);
StreamAtmAgent atmAgent = new StreamAtmAgent(webcam, sizeVideo);
atmAgent.connect(new InetSocketAddress(host, port));
}。来自client2的代码:
public static void init(String host, int port, ImageListener il) {
displayWindow.setVisible(true);
logger.info("Ustawione wymiary :{}", dimension);
StreamClientAgent clientAgent = new StreamClientAgent(il, dimension);
clientAgent.connect(new InetSocketAddress(host, port));
}你能帮帮我吗?如果你需要更多的代码,告诉我。
当我在启动服务器上做这样的事情时:
init("localhost",2000)
init("localhost",2001)我将我的client1与服务器连接到端口2000,并将client2连接到端口2001。我仍然看到来自端口2000的图像。
发布于 2018-04-24 12:38:10
我猜您的channelGroup是在所有客户端的所有线程之间共享的吗?在该集合中,您将添加所有通道--不管它们侦听的是哪个端口:
Channel channel = serverBootstrap.bind(streamAddress);
channelGroup.add(channel); //I guess all channels are added here来自netty文档的Ans 这里 write方法可以:
将指定的消息写入该组中的所有通道。如果指定的消息是ByteBuf的实例,则会自动重复该消息以避免竞争条件。ByteBufHolder也是如此。请注意,与Channel.write(Object)一样,此操作是异步的。
所以,你基本上在这里,channelGroup.write(msg);,你发送相同的信息(图像)到所有渠道。您需要将端口2000的通道与端口2001上的通道分开。我认为你甚至不需要一个channelGroup。只需将端口2000的图像发送到为端口2000创建的通道。
https://stackoverflow.com/questions/50001626
复制相似问题