Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterwyld committed Dec 24, 2020
1 parent 612af06 commit 22bfb41
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ VSRS(Very Simple Rtmp Server) is a rtmp server written in java.
### Start server
```
mvn clean package
java -jar target/vsrs.jar
java -jar rtmpserver.jar
```

### Publish/Play stream
```
ffmpeg -re -i video.flv -f flv -c copy "rtmp://127.0.0.1/live/stream"
ffplay "rtmp://127.0.0.1/live/stream"
```
or use publisher like OBS, player like VLC

### Publish/Play text messages
```
java -jar rtmpclient.jar -publish text.json "rtmp://127.0.0.1/live/text"
java -jar rtmpclient.jar -play "rtmp://127.0.0.1/live/test"
```
44 changes: 32 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,44 @@
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>rtmpserver</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.wanghao.vsrs.server.RtmpServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>rtmpserver</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>rtmpclient</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.wanghao.vsrs.client.RtmpClient</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>rtmpclient</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.wanghao.vsrs.server.RtmpServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>vsrs</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/wanghao/vsrs/client/RtmpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import com.wanghao.vsrs.client.handler.netty.HandshakeHandler;
import com.wanghao.vsrs.common.handler.netty.ChunkDecoder;
import com.wanghao.vsrs.common.handler.netty.ChunkEncoder;
import com.wanghao.vsrs.common.util.Utils;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.log4j.Logger;

import java.net.InetSocketAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -25,8 +23,6 @@
public class RtmpClient {
private static final Logger logger = Logger.getLogger(RtmpClient.class);

private static final int port = Utils.getRandomPortWithin(50000, 50100);

// 客户端最大重连次数
private static final int MAX_RECONNECT = 3;
// 客户端已尝试重连次数
Expand All @@ -37,9 +33,9 @@ public class RtmpClient {

public static void main(String[] args) throws Exception {
if (args.length != 2 && args.length != 3) {
throw new Exception("invalid number of args");
printUsage(new Exception("invalid number of args"));
}
String url;
String url = null;
if ("-play".equals(args[0])) {
isPlayer = true;
publishFilePath = null;
Expand All @@ -49,7 +45,7 @@ public static void main(String[] args) throws Exception {
publishFilePath = args[1];
url = args[2];
} else {
throw new Exception("invalid args[0]");
printUsage(new Exception("invalid args[0]"));
}

boolean success = parseUrl(url);
Expand All @@ -63,7 +59,6 @@ public static void main(String[] args) throws Exception {
bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.remoteAddress(serverHost, serverPort)
// 开启TCP no delay,允许较小数据包的发送
.option(ChannelOption.TCP_NODELAY, true)
Expand All @@ -78,7 +73,6 @@ protected void initChannel(SocketChannel ch) throws Exception {
}
});

logger.info("Rtmp client started on port " + port);
connect();
}

Expand Down Expand Up @@ -138,4 +132,12 @@ private static boolean parseUrl(String url) {
}
return false;
}

private static void printUsage(Exception e) throws Exception {
System.out.println("--------------- USAGE ---------------");
System.out.println("Publish: java -jar rtmpclient.jar -publish text.json rtmp://127.0.0.1/live/test");
System.out.println("Play: java -jar rtmpclient.jar -play rtmp://127.0.0.1/live/test");
System.out.println("-------------------------------------");
throw e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ private void startPublishText(ChannelHandlerContext ctx) {
logger.error("read file error: " + publishFilePath);
return;
}
//String text = "[{\"time\":1608711651000,\"data\":{\"user\":\"我是丁真\",\"text\":\"我要去喂我的小马了\"}},{\"time\":1608711652000,\"data\":{\"user\":\"马保国\",\"text\":\"年轻人不讲武德\"}},{\"time\":1608711654000,\"data\":{\"user\":\"cxk\",\"text\":\"吃我一记连五鞭\"}},{\"time\":1608711654500,\"data\":{\"user\":\"我是丁真\",\"text\":\"cxk是我大哥\"}},{\"time\":1608711654800,\"data\":{\"user\":\"马保国\",\"text\":\"就骗!就偷袭!\"}},{\"time\":1608711655500,\"data\":{\"user\":\"cxk\",\"text\":\"心疼保国老师...\"}},{\"time\":1608711655600,\"data\":{\"user\":\"我是丁真\",\"text\":\"传统功夫讲究点到为止\"}},{\"time\":1608711655900,\"data\":{\"user\":\"马保国\",\"text\":\"噫!大意了,没有闪\"}},{\"time\":1608711656000,\"data\":{\"user\":\"cxk\",\"text\":\"我劝你们年轻人耗子尾汁\"}},{\"time\":1608711656050,\"data\":{\"user\":\"马保国\",\"text\":\"浑元形意太极门掌门人\"}}]";
//String textToPublish = "[{\"time\":1608711651000,\"data\":{\"user\":\"我是丁真\",\"text\":\"我要去喂我的小马了\"}},{\"time\":1608711652000,\"data\":{\"user\":\"马保国\",\"text\":\"年轻人不讲武德\"}},{\"time\":1608711654000,\"data\":{\"user\":\"cxk\",\"text\":\"吃我一记连五鞭\"}},{\"time\":1608711654500,\"data\":{\"user\":\"我是丁真\",\"text\":\"cxk是我大哥\"}},{\"time\":1608711654800,\"data\":{\"user\":\"马保国\",\"text\":\"就骗!就偷袭!\"}},{\"time\":1608711655500,\"data\":{\"user\":\"cxk\",\"text\":\"心疼保国老师...\"}},{\"time\":1608711655600,\"data\":{\"user\":\"我是丁真\",\"text\":\"传统功夫讲究点到为止\"}},{\"time\":1608711655900,\"data\":{\"user\":\"马保国\",\"text\":\"噫!大意了,没有闪\"}},{\"time\":1608711656000,\"data\":{\"user\":\"cxk\",\"text\":\"我劝你们年轻人耗子尾汁\"}},{\"time\":1608711656050,\"data\":{\"user\":\"马保国\",\"text\":\"浑元形意太极门掌门人\"}}]";
final Object[] objects = JSON.parseArray(textToPublish).toArray();
logger.info("Ready to send " + objects.length + " messages");
logger.info("Ready to publish " + objects.length + " messages");
int curIdx = 0;
long beginTime = System.currentTimeMillis();
long gapTime = Long.MIN_VALUE;
Expand Down
File renamed without changes.

0 comments on commit 22bfb41

Please sign in to comment.