服务器端代码设计
(1) 主启动类:SocketServer.java
/**RTSP方式(同TCP)*/
static ChannelFactory RTSPCHANNEL_FACTORY = new NioServerSocketChannelFactory(Executors.newFixedThreadPool(10), Executors.newFixedThreadPool(500));
/**RTSP方式(同TCP)*/
Static ServerBootstrap RTSPSERVER_BOOTSTRAP = new ServerBootstrap(RTSPCHANNEL_FACTORY);
/**
* RTSP服务器启动
*
* @author linfenliang
* @date 2012-11-20
* @version V1.0.0
* void
*/
public static void rtspServerStartUp() {
RTSPSERVER_BOOTSTRAP.setPipelineFactory(new RTSPServerPipelineFactory());
RTSPSERVER_BOOTSTRAP.setOption("child.tcpNoDelay", true);
RTSPSERVER_BOOTSTRAP.setOption("child.keepAlive", true);
RTSPSERVER_BOOTSTRAP.setOption("reuseAddress", true);
RTSPSERVER_BOOTSTRAP.setOption(
"receiveBufferSizePredictorFactory",
new FixedReceiveBufferSizePredictorFactory(65535));
RTSPSERVER_BOOTSTRAP.setOption("receiveBufferSize", 900000000);
// 绑定这个服务使用的端口
RTSPSERVER_BOOTSTRAP.bind(new InetSocketAddress(Constants.SERVER_NAME,Constants.RTSPSERVER_PORT));
LOGGER.info("FILETRANSFER工程RTSP服务绑定服务器地址:端口==>"+Constants.SERVER_NAME+":"+Constants.RTSPSERVER_PORT);
LOGGER.info("RTSP服务已启动....");
}
/**
* RTSP服务器关闭
*
* @author linfenliang
* @date 2012-11-20
* @version V1.0.0
* void
*/
public static void rtspServerShutDown(){
RTSPSERVER_BOOTSTRAP.releaseExternalResources();
LOGGER.info("RTSP服务已关闭");
}
public static void main(String[] args) {
rtspServerStartUp();
}
(2) Rtsp服务配置管道工厂类:RTSPServerPipelineFactory.java
public class RTSPServerPipelineFactory implements ChannelPipelineFactory {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
// pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
// pipeline.addLast("decoder", new RtspResponseDecoder());
pipeline.addLast("decoder", new RtspRequestDecoder());
// pipeline.addLast("encoder", new RtspRequestEncoder());
pipeline.addLast("encoder", new RtspResponseEncoder());
// pipeline.addLast("encoder", new RtspMessageEncoder() {
//
// @Override
// protected void encodeInitialLine(ChannelBuffer buf, HttpMessage message)
// throws Exception {
// HttpResponse response = (HttpResponse) message;
// try {
// buf.writeBytes(response.getProtocolVersion().toString().getBytes(
// "ASCII"));
// buf.writeByte((byte)' ');
// buf.writeBytes(String.valueOf(response.getStatus().getCode())
// .getBytes("ASCII"));
// buf.writeByte((byte)' ');
// buf.writeBytes(String.valueOf(
// response.getStatus().getReasonPhrase()).getBytes("ASCII"));
// buf.writeByte((byte) '\r');
// buf.writeByte((byte) '\n');
// } catch (UnsupportedEncodingException e) {
// throw (Error) new Error().initCause(e);
// }
//
// }
// });
// pipeline.addLast("decoder", new RtspMessageDecoder() {
//
// @Override
// protected boolean isDecodingRequest() {
// // TODO Auto-generated method stub
// return false;
// }
//
// @Override
// protected HttpMessage createMessage(String[] initialLine) throws Exception {
// // TODO Auto-generated method stub
// return null;
// }
// });
// pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", new RtspServerHandler());
return pipeline;
}
}