简体   繁体   中英

Netty framework - multiple ports listening

I have the following problem. I have modified this example for myself : Webcam Capture Live Streaming Example

At the moment, the communication looks like client1 sends the image to the server, and the image from the server is sent to client2. There is no problem if I work with one camera. Problems start if I stream from two different cameras. I would like client1 to send the image to the server on a specific port and only on that port the server sends the image to the client.2 At the moment it is (I don't know why) the case that what the server gets, eg on port 2000 it sends to all ports, not only port 2000. Can you help me?

Some code from server:

@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);
}

Code from 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));
}

. Code from 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));
}

Can you help me? If you need more code, tell me.

PS When I do something like this in start server:

init("localhost",2000)
init("localhost",2001)

And I connect my client1 to server with port 2000 and I connect client2 to port 2001. I still see image from port 2000.

My guess is that your channelGroup is shared between all threads for all clients? In that SET you add all channels - no matters which port they are listening to:

 Channel channel = serverBootstrap.bind(streamAddress);
 channelGroup.add(channel); //I guess all channels are added here

Ans from netty documentation here write method does:

Writes the specified message to all Channels in this group. If the specified message is an instance of ByteBuf, it is automatically duplicated to avoid a race condition. The same is true for ByteBufHolder. Please note that this operation is asynchronous as Channel.write(Object) is.

So you basically here channelGroup.write(msg); you send the same message(image) to all channels. You would need to separate the channel for port 2000 from the one on port 2001. I don't think you even need a channelGroup. Just send the image for port 2000 to the channel created for port 2000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM