Commit 0d02bc22ba33afd8aeb5c3029e070af3f3315046
1 parent
11a60adb
1
Showing
4 changed files
with
26 additions
and
58 deletions
src/main/java/cn/org/hentai/jtt1078/publisher/Channel.java
| ... | ... | @@ -92,10 +92,6 @@ public class Channel |
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - public void writeVideoRtp(byte[] h264) { | |
| 96 | - | |
| 97 | - } | |
| 98 | - | |
| 99 | 95 | public void broadcastVideo(long timeoffset, byte[] flvTag) |
| 100 | 96 | { |
| 101 | 97 | for (Subscriber subscriber : subscribers) |
| ... | ... | @@ -150,20 +146,9 @@ public class Channel |
| 150 | 146 | if (i == 0) continue; |
| 151 | 147 | byte[] nalu = new byte[i]; |
| 152 | 148 | buffer.sliceInto(nalu, i); |
| 153 | - //System.out.println(nalu.length); | |
| 154 | - //System.out.println(toHex(nalu)); | |
| 155 | 149 | return nalu; |
| 156 | 150 | } |
| 157 | 151 | } |
| 158 | 152 | return null; |
| 159 | 153 | } |
| 160 | - | |
| 161 | - public String toHex(byte[] bytes) { | |
| 162 | - StringBuilder sb = new StringBuilder(); | |
| 163 | - for (byte b : bytes) { | |
| 164 | - sb.append(String.format("%02X ", b)); | |
| 165 | - } | |
| 166 | - | |
| 167 | - return sb.toString(); | |
| 168 | - } | |
| 169 | 154 | } | ... | ... |
src/main/java/cn/org/hentai/jtt1078/server/Jtt1078Handler.java
| 1 | 1 | package cn.org.hentai.jtt1078.server; |
| 2 | 2 | |
| 3 | +import cn.org.hentai.jtt1078.entity.Media; | |
| 4 | +import cn.org.hentai.jtt1078.entity.MediaEncoding; | |
| 3 | 5 | import cn.org.hentai.jtt1078.publisher.Channel; |
| 4 | 6 | import cn.org.hentai.jtt1078.publisher.PublishManager; |
| 7 | +import cn.org.hentai.jtt1078.entity.Audio; | |
| 5 | 8 | import cn.org.hentai.jtt1078.rtp.H264Packeter; |
| 6 | 9 | import cn.org.hentai.jtt1078.rtsp.RtspSessionManager; |
| 7 | 10 | import cn.org.hentai.jtt1078.util.Packet; |
| ... | ... | @@ -13,13 +16,15 @@ import io.netty.channel.socket.SocketChannel; |
| 13 | 16 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 14 | 17 | import io.netty.handler.codec.rtsp.RtspDecoder; |
| 15 | 18 | import io.netty.handler.codec.rtsp.RtspEncoder; |
| 19 | +import io.netty.util.Attribute; | |
| 16 | 20 | import io.netty.util.AttributeKey; |
| 17 | 21 | import org.slf4j.Logger; |
| 18 | 22 | import org.slf4j.LoggerFactory; |
| 19 | -import io.netty.handler.timeout.IdleState; | |
| 20 | -import io.netty.handler.timeout.IdleStateEvent; | |
| 21 | 23 | |
| 22 | 24 | import java.net.InetSocketAddress; |
| 25 | +import java.util.Iterator; | |
| 26 | +import io.netty.handler.timeout.IdleState; | |
| 27 | +import io.netty.handler.timeout.IdleStateEvent; | |
| 23 | 28 | |
| 24 | 29 | /** |
| 25 | 30 | * Created by matrixy on 2019/4/9. |
| ... | ... | @@ -39,7 +44,8 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> |
| 39 | 44 | int channel = packet.nextByte() & 0xff; |
| 40 | 45 | String tag = sim + "-" + channel; |
| 41 | 46 | |
| 42 | - if (SessionManager.contains(nettyChannel, "tag") == false) { | |
| 47 | + if (SessionManager.contains(nettyChannel, "tag") == false) | |
| 48 | + { | |
| 43 | 49 | Channel chl = PublishManager.getInstance().open(tag); |
| 44 | 50 | SessionManager.set(nettyChannel, "tag", tag); |
| 45 | 51 | new Thread(new PushTask(tag)).start(); |
| ... | ... | @@ -68,7 +74,7 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> |
| 68 | 74 | SessionManager.set(nettyChannel, "video-sequence", sequence); |
| 69 | 75 | } |
| 70 | 76 | long timestamp = packet.seek(16).nextLong(); |
| 71 | - //PublishManager.getInstance().publishVideo(tag, sequence, timestamp, pt, packet.seek(lengthOffset + 2).nextBytes()); | |
| 77 | + PublishManager.getInstance().publishVideo(tag, sequence, timestamp, pt, packet.seek(lengthOffset + 2).nextBytes()); | |
| 72 | 78 | if (RtspSessionManager.isRegistered(tag)) { |
| 73 | 79 | io.netty.channel.Channel push = RtspSessionManager.getPush(tag); |
| 74 | 80 | H264Packeter packeter = (H264Packeter) push.attr(AttributeKey.valueOf(tag)).get(); | ... | ... |
src/main/java/cn/org/hentai/jtt1078/server/Jtt1078MessageDecoder.java
| ... | ... | @@ -23,45 +23,20 @@ public class Jtt1078MessageDecoder extends ByteToMessageDecoder |
| 23 | 23 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception |
| 24 | 24 | { |
| 25 | 25 | int length = in.readableBytes(); |
| 26 | - while (length > 30) { | |
| 27 | - in.markReaderIndex(); | |
| 28 | - int frameHeader = in.readInt(); | |
| 29 | - if (frameHeader == 0x30316364) { | |
| 30 | - in.skipBytes(11); | |
| 31 | - int dataType = (in.readByte() >> 4) & 0xFF, lengthOffset = 28; | |
| 32 | - // 透传数据类型:0100,没有后面的时间以及Last I Frame Interval和Last Frame Interval字段 | |
| 33 | - if (dataType == 0x04) lengthOffset = 28 - 8 - 2 - 2; | |
| 34 | - else if (dataType == 0x03) lengthOffset = 28 - 4; | |
| 35 | - in.skipBytes(lengthOffset - 16); | |
| 36 | - int bodyLength = in.readShort() & 0xFFFFFFFF; | |
| 37 | - int packetLength = lengthOffset + bodyLength + 2; | |
| 38 | - //System.out.println(bodyLength); | |
| 39 | - if (length < packetLength) { | |
| 40 | - in.resetReaderIndex(); | |
| 41 | - return; | |
| 42 | - } | |
| 43 | - byte[] data = new byte[packetLength]; | |
| 44 | - in.resetReaderIndex(); | |
| 45 | - in.readBytes(data); | |
| 46 | - Packet packet = Packet.create(data); | |
| 47 | - out.add(packet); | |
| 48 | -// System.out.println("start:" + (lengthOffset + 2)); | |
| 49 | -// System.out.println(toHex(data)); | |
| 50 | - } else { | |
| 51 | - in.resetReaderIndex(); | |
| 52 | - in.readByte(); | |
| 53 | - } | |
| 26 | + for (int i = 0, k = (int)Math.ceil(length / 512f); i < k; i++) | |
| 27 | + { | |
| 28 | + int l = i < k - 1 ? 512 : length - (i * 512); | |
| 29 | + in.readBytes(block, 0, l); | |
| 54 | 30 | |
| 55 | - length = in.readableBytes(); | |
| 56 | - } | |
| 57 | - } | |
| 31 | + decoder.write(block, 0, l); | |
| 58 | 32 | |
| 59 | - public String toHex(byte[] bytes) { | |
| 60 | - StringBuilder sb = new StringBuilder(); | |
| 61 | - for (byte b : bytes) { | |
| 62 | - sb.append(String.format("%02X ", b)); | |
| 63 | - } | |
| 33 | + while (true) | |
| 34 | + { | |
| 35 | + Packet p = decoder.decode(); | |
| 36 | + if (p == null) break; | |
| 64 | 37 | |
| 65 | - return sb.toString(); | |
| 38 | + out.add(p); | |
| 39 | + } | |
| 40 | + } | |
| 66 | 41 | } |
| 67 | 42 | } | ... | ... |
src/main/java/cn/org/hentai/jtt1078/server/RTSPHandler.java
| ... | ... | @@ -8,7 +8,9 @@ import io.netty.channel.*; |
| 8 | 8 | import io.netty.channel.nio.NioEventLoopGroup; |
| 9 | 9 | import io.netty.channel.socket.SocketChannel; |
| 10 | 10 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 11 | -import io.netty.handler.codec.http.*; | |
| 11 | +import io.netty.handler.codec.http.DefaultHttpContent; | |
| 12 | +import io.netty.handler.codec.http.DefaultHttpResponse; | |
| 13 | +import io.netty.handler.codec.http.HttpMethod; | |
| 12 | 14 | import io.netty.handler.codec.rtsp.*; |
| 13 | 15 | import io.netty.util.internal.StringUtil; |
| 14 | 16 | import org.slf4j.Logger; |
| ... | ... | @@ -21,7 +23,7 @@ public class RTSPHandler extends ChannelInboundHandlerAdapter { |
| 21 | 23 | |
| 22 | 24 | private final static Logger log = LoggerFactory.getLogger(RTSPHandler.class); |
| 23 | 25 | |
| 24 | - private String channelId = "138000099998-1"; | |
| 26 | + private String channelId = "122223333444-1"; | |
| 25 | 27 | |
| 26 | 28 | private HttpMethod currentMethod = RtspMethods.OPTIONS; |
| 27 | 29 | ... | ... |