Android视频直播 ------ 3.1 传输 -- netty的集成。

1、网络通讯协议netty,官网:https://netty.io/

1.1  AndroidStudio 集成netty ,在build.gradle 中
dependencies{
        api  'io.netty:netty-all:4.1.9.Final'

}

1.2  开启netty服务端,port是端口号,例如 2233

此处需要跨平台传输视频流,文件流, 文本消息,使用的是protobuf传输序列化对象,接收的也是序列化对象。后续3.2讲protobuf集成,怎么集成怎么使用。

 socketChannel.pipeline().addLast(), 可以添加各种数据编码解码器,例如String,Object。视频流还是建议用protobuf,或者使用byte,使用byte编码解码器,就需要自己处理视频信息,分包,拆包,组装,这里就不介绍。

public static void start(int port) {

        if (channelFuture ==null){

                channelFuture =new ServerBootstrap()

                .group(new NioEventLoopGroup(),new NioEventLoopGroup())//创建工作线程

                .channel(NioServerSocketChannel.class)

                .option(ChannelOption.SO_BACKLOG,128)

                .option(ChannelOption.SO_BROADCAST,true)// 不延迟,直接发送

                .childOption(ChannelOption.SO_KEEPALIVE,true)// 保持长连接状态

                .childHandler(new ChannelInitializer() {

                    @Override

                            protected void initChannel(SocketChannel socketChannel) {

                                    ChannelPipeline pipeline = socketChannel.pipeline();

                                    pipeline.addLast(new IdleStateHandler(0,5,0,TimeUnit.SECONDS));//心跳检测

                                    //protobuf编码解码

                                    pipeline.addLast(new ProtobufVarint32FrameDecoder());

                                    pipeline.addLast(new ProtobufDecoder(NettyMessage.RegisterProto.getDefaultInstance()));

                                    pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());

                                    pipeline.addLast(new ProtobufEncoder());

                                    //服务端消息数据处理器

                                    pipeline.addLast(new NettyServerMultiHandler());

            }})

        .bind(port);

        channelFuture.addListener((ChannelFutureListener) future -> {

        if (future.isSuccess()) {

                NettyLibUtils.i("netty server start");

        }else {

                NettyLibUtils.i("netty server start failed");

        }

    });

}

1.3 客户端使用,使用的是局域网连接
host:即手机wifi ip地址,例如 192.168.0.1

port:即端口号,和服务端填写需要一致,例如 2233

new Bootstrap()

        .channel(NioSocketChannel.class)

        .group(group)

        .option(ChannelOption.SO_KEEPALIVE,true)

        .option(ChannelOption.SO_BROADCAST,true)// 不延迟,直接发送

        .handler(new ChannelInitializer() {

                @Override

                protected void initChannel(SocketChannel socketChannel)throws Exception {

                    ChannelPipeline pipeline = socketChannel.pipeline();

                    //心跳检测

                    pipeline.addLast(new IdleStateHandler(0,5,0,TimeUnit.SECONDS));

                    //编码解码

                    pipeline.addLast(new ProtobufVarint32FrameDecoder());

                    pipeline.addLast(new ProtobufDecoder(NettyMessage.RegisterProto.getDefaultInstance()));

                    pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());

                    pipeline.addLast(new ProtobufEncoder());

                    //客户端消息处理器

                    pipeline.addLast(new ChannelHandle());

        }

        })

        .connect(new InetSocketAddress(host, NettyLibUtils.MULTI_PORT))

        .addListener((ChannelFutureListener) future -> {

            if (future.isSuccess()) {

                    socketChannel = (SocketChannel) future.channel();

                    callback.onEvent(MsgCode.LOGIN_IN,"连接成功",null);

            }else {

                    NettyLibUtils.i("connect 连接失败1 ");

                    future.channel().close();

                    group.shutdownGracefully();

            }

});

1.4 连接成功就可以发送消息,用 socketChannel.writeAndFlush(msg)

具体发送消息,消息处理,见3.2 protobuf使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容