RTMPPublisher.java
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cn.org.hentai.jtt1078.subscriber;
import cn.org.hentai.jtt1078.codec.MP3Encoder;
import cn.org.hentai.jtt1078.flv.AudioTag;
import cn.org.hentai.jtt1078.flv.FlvAudioTagEncoder;
import cn.org.hentai.jtt1078.flv.FlvEncoder;
import cn.org.hentai.jtt1078.util.*;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
public class RTMPPublisher extends Thread
{
static Logger logger = LoggerFactory.getLogger(RTMPPublisher.class);
String tag = null;
Process process = null;
public RTMPPublisher(String tag)
{
this.tag = tag;
}
@Override
public void run()
{
InputStream stderr = null;
int len = -1;
byte[] buff = new byte[512];
boolean debugMode = "on".equalsIgnoreCase(Configs.get("debug.mode"));
try
{
String rtmpUrl = Configs.get("rtmp.url").replaceAll("\\{TAG\\}", tag);
String cmd = String.format("%s -i http://localhost:%d/video/%s -vcodec copy -acodec aac -f flv %s",
Configs.get("ffmpeg.path"),
Configs.getInt("server.http.port", 3333),
tag,
rtmpUrl
);
logger.info("Execute: {}", cmd);
process = Runtime.getRuntime().exec(cmd);
stderr = process.getErrorStream();
while ((len = stderr.read(buff)) > -1)
{
if (debugMode) System.out.print(new String(buff, 0, len));
}
logger.info("Process FFMPEG exited...");
}
catch(Exception ex)
{
logger.error("publish failed", ex);
}
}
public void close()
{
try { if (process != null) process.destroyForcibly(); } catch(Exception e) { }
}
}