libstreaming本身并不支持多端拉流
需求场景:手机作为Server端推流,电脑端或其他设备作为拉流端
网上看到有人提出用组播来实现多端拉流
方案一:采用组播,让服务端推流到组播地址,然后组播内成员均可以拉流
Thanks Zhehua Chang, your solution works!
Bit slow but works. :) The only issue is the writing to the broadcast ip.
As another approach, I have tried to write the buffer into two sockets.
Managed to get the frames in two clients. But has some issues.
_Here I did the modifications in the AbstractPacketizer class
and H264Packetizer class. Created two *_RtpSocket and buffer instances. *
*Anyone tried this before? *
2016-01-28 15:41 GMT+05:30 Zhehua Chang [notifications@github.com](mailto:notifications@github.com):
> Here's my old code [https://github.com/Oo-Dev/OoDroid2](https://github.com/Oo-Dev/OoDroid2) for it, but
> honestly, the code is really ugly and I guess you may get confused...
> You must be aware that broadcast packets challenges the router and it
> could cause a crash in the network.
>
> Anyway, if my solution really helps, it is as following :
>
> 1. Firstly, set your IP address to "239.1.1.1" or some other broadcast IPs.
> 2. Then build up a session as usual.After that, you can get SDP as a
> string by calling session.getSessionDescription().
> The above two steps correspond to the normal way to build up a sessoin
> [https://github.com/Oo-Dev/OoDroid2/blob/master/app/src/main/java/org/oo/oodroid2/OoDroidActivity.java#L100](https://github.com/Oo-Dev/OoDroid2/blob/master/app/src/main/java/org/oo/oodroid2/OoDroidActivity.java#L100)
> .
> 3. As far as I know, this SDP string can identify a session. You just
> save it into a .sdp file and then open it with VLC.This player is
> amazing because it parses the file and connect the RTSP server
> (libstreaming) automatically. So, the problem is to send the .sdp file
> to clients, and I build up another socket server for it.
>
> In conclusion, the server builds up a session and start to send broadcast
> packets.Then you build up another server to dispatch SDP strings obtained
> from session object.
> The clients connect to the latter server firstly to fetch the SDP string
> and save it as a .sdp file.Then, open the .sdp file with VLC. All done~
>
> Forgive me but I'm not a native English speaker.And anywhere unclear, feel
> free to reply.
>
> —
> Reply to this email directly or view it on GitHub
提供的服务端OoDroid2项目地址:https://github.com/Oo-Dev/OoDroid2
客户端OoDroid-client
OoDroid2是基于libstreaming项目写的,就是新增了一个sdp分发类,将生成的session.sdp发送给客户端,然后客户端用vlc播放该sdp,从而实现拉流
准备工作:
1.OoDroid2项目中的SDPDistributor.java改动一下,去掉else
public void startServer() throws IOException {
if(mRequestListener == null)
mRequestListener = new RequestListener();
if(!alive)//else去掉
mRequestListener.start();
//mDistributor.setSoTimeout(100);
}
2.OoDroid-client项目中MainActivity中的DEFAULT_HOST改成服务端的ip地址
3.通过测试发现组播地址224.0.0.1可用,则
OoDroid2项目的OoDroidActivity类作如下修改:
destinationIP = sp.getString(getString(R.string.key_destination_IP), "224.0.0.1");
videoEncoding = sp.getString(getString(R.string.key_video_encoding), "H.264");
接下来,将两个项目分别运行到两个设备上,服务端点击play按钮开启推流,然后客户端点击连接按钮和服务端建立socket连接,接着通过socket来接收session.sdp,下一步用vlc打开该文件,从而实现拉流。
sdp文件也可以通过直接手动拷贝复制到客户端上,没必要通过socket传输过去
上面的demo调试通过,接下来开整我们的面板机项目
本来的项目是通过vlc输入url向服务端发起请求,服务端解析url然后启动相机直播,接着推流给vlc,服务端和vlc之间建立了会话连接。
但现在要推流到组播地址,如果还通过上述方式,vlc总是自动断开连接,因为服务端并没有推流到vlc,vlc需要通过打开sdp来拉流
所以我把vlc这块操作砍掉,直接把url放到代码内,解析后生成Session,然后延迟个三五秒在执行推流,不然总是预览黑屏。然后通过日志打印拿到sdp的内容,复制出来创建sdp文件,然后电脑端或其他设备就可以打开sdp来拉流了,基本就这样,但是拉流着实卡的很
MainActivity.java
@Override
protected void onResume() {
super.onResume();
//延时开启
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
try {
checkPermissionAndStartServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 5000);
}
//尺寸写320-240才能正常预览
String url = "rtsp://10.4.161.10:1234?x264=1024-20-320-240&multicast=224.0.0.1";
session = UriParser.parse(url);
// session.setOrigin(client.getLocalAddress().getHostAddress());
// session.setClientSocket(client);
if (!session.isStreaming()) {
session.configure();
session.start();
Toast.makeText(MainActivity.this, "Streaming has started", Toast.LENGTH_SHORT).show();
}
方案二:通过单播方式,然后服务端推流到同一个目的地的两个端口方案解决
实现:通过建立两个H264Packetizer实例,必须两个ByteBufferInputStream来实现,因为第一个ByteNufferInputStream使用后就变更了,当第二次在使用时已经变化了,所以需要两个
//H264Stream_x264.java
public H264Stream_x264(int cameraId) {
super(cameraId);
mMimeType = "video/avc";
mCameraImageFormat = ImageFormat.NV21;
mVideoEncoder = MediaRecorder.VideoEncoder.H264;
mPacketizer =new H264Packetizer();
mPacketizer2=new H264Packetizer();
}
public synchronized void start() throws IllegalStateException, IOException {
if (!mStreaming) {
configure();
byte[] pps = Base64.decode(mConfig.getB64PPS(), Base64.NO_WRAP);
byte[] sps = Base64.decode(mConfig.getB64SPS(), Base64.NO_WRAP);
((H264Packetizer) mPacketizer).setStreamParameters(pps, sps);
((H264Packetizer) mPacketizer2).setStreamParameters(pps, sps);
super.start();
}
}
protected void encodeWithMediaCodec() throws RuntimeException, IOException {
...
final ByteBufferInputStream bufferInputStream = new ByteBufferInputStream(this);
final ByteBufferInputStream bufferInputStream2 = new ByteBufferInputStream(this);
...
bufferInputStream.updateData(outputBuffer, output_pts[0], size);
bufferInputStream2.updateData(outputBuffer, output_pts[0], size);
...
mPacketizer.setInputStream(bufferInputStream);
mPacketizer2.setInputStream(bufferInputStream2);
mPacketizer.start();
mPacketizer2.start();
}
MediaStream.java
public synchronized void configure() throws IllegalStateException, IOException {
if (mStreaming) throw new IllegalStateException("Can't be called while streaming.");
if (mPacketizer != null) {
mPacketizer.setDestination(mDestination, mRtpPort, mRtcpPort);
mPacketizer.getRtpSocket().setOutputStream(mOutputStream, mChannelIdentifier);
}
if (mPacketizer2 != null) {
mPacketizer2.setDestination(mDestination, 5008, 5009);
mPacketizer2.getRtpSocket().setOutputStream(mOutputStream, mChannelIdentifier);
}
mMode = mRequestedMode;
}
//MainActivity.java
String url = "rtsp://10.4.161.10:1234?x264=1024-20-320-240&unicast=10.4.165.254";
最后创建两个sdp文件,分别拉取两个端口的流信息,
//test.sdp
v=0
o=- 0 0 IN IP4 10.4.161.10
s=Unnamed
i=N/A
c=IN IP4 10.4.165.254
t=0 0
a=recvonly
m=video 5006 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42c014;sprop-parameter-sets=Z0LAFNoFB+hAAAADAEAAAAojxQqo,aM48gA==;
a=control:trackID=1
//test2.sdp
v=0
o=- 0 0 IN IP4 10.4.161.10
s=Unnamed
i=N/A
c=IN IP4 10.4.165.254
t=0 0
a=recvonly
m=video 5008 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42c014;sprop-parameter-sets=Z0LAFNoFB+hAAAADAEAAAAojxQqo,aM48gA==;
a=control:trackID=1
用两个vlc分别打开sdp文件