流媒体传输知识整理(三)

服务器端代码设计

(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;

    }

 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值