Java 如何编写服务器/客户端视频和音频流应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2308181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I code a server/client video and audio streaming application?
提问by soneangel
I have to create a client/server system to stream video and audio. It would be very simple. Like youtube style. The server should attend clients providing a list of medias first and waiting the choice of each client to start streaming the media. Until create a socket and showing a simple list I'm on it ;) But I don't know which class could I use to stream. The example is basically youtube style. How can I start streaming, How can client pause reproduction, how can? I know how to stream text but what about video? Do you know any tutorial page? It's very different from this simple server client example?
我必须创建一个客户端/服务器系统来流式传输视频和音频。这将非常简单。喜欢youtube的风格。服务器应该首先服务于提供媒体列表的客户端,并等待每个客户端的选择以开始流式传输媒体。在创建一个套接字并显示一个简单的列表之前,我正在使用它;) 但我不知道我可以使用哪个类来进行流式传输。该示例基本上是 youtube 风格。我如何开始流式传输,客户端如何暂停再现,如何?我知道如何流式传输文本,但视频呢?你知道任何教程页面吗?它与这个简单的服务器客户端示例有很大不同吗?
import java.io.*;
import java.io.*;
import java.net.*;
public class ThreadedEchoServer {
public static void main(String[] args) {
try {
int i = 1;
ServerSocket s = new ServerSocket(8189);
while(true) {
Runnable r = new ThreadedEchoHandler(incoming, i);
Thread t = new Thread(r);
t.start();
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ThreadedEchoHandler implements Runnable {
private Socket incoming;
private int counter;
public ThreadedEchoHandler(Socket i, int c) {
incoming = i;
counter = c;
}
public void run() {
try {
try {
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream);
out.println("BYE to exit");
boolean done = false;
while (!done && in.hasNextLine()) {
String line = in.nextLine()) {
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
out.println("BYE to exit");
}
} finally {
incoming.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Hope you could clarify my ideas. Kind regards.
希望你能澄清我的想法。亲切的问候。
采纳答案by Rafael Sierra
For streaming and talking to your clients, you need to define a protocol: Search the web for RTP and RTSP. It should give you a pretty good idea of what you need to implement these protocols or even create your own one.
为了流式传输和与客户交谈,您需要定义一个协议:在网络上搜索 RTP 和 RTSP。它应该让您很好地了解实现这些协议甚至创建自己的协议所需的条件。
As for implementing, take a look at the red5 project: http://red5.org/
至于实现,看一下red5项目:http: //red5.org/
Take a look at Xuggler as well: http://www.xuggle.com/xuggler/This project will help you saving lots of lines of code. Note that its development has gone stale.
也看看 Xuggler:http://www.xuggle.com/xuggler/ 这个项目将帮助您节省大量代码行。请注意,它的发展已经过时。
Cheers.
干杯。
回答by Kiril
Check out the Java Media Framework (it has tutorials): http://java.sun.com/javase/technologies/desktop/media/jmf/
查看 Java 媒体框架(它有教程):http: //java.sun.com/javase/technologies/desktop/media/jmf/
Does this even work?
这甚至有效吗?
while(true) {
Runnable r = new ThreadedEchoHandler(incoming, i);
Thread t = new Thread(r);
t.start();
i++;
}
I think your code would produce a bunch of threads with incoming socket connections... what you probably want to do is this:
我认为你的代码会产生一堆带有传入套接字连接的线程......你可能想要做的是:
while(true) {
Runnable r = new ThreadedEchoHandler(incoming.accept(), i);
Thread t = new Thread(r);
t.start();
i++;
}
The ThreadedEchoHandler
should take a Socket instead of a ServerSocket. Accept blocks until a client connects, otherwise you'll be spawning an infinite number of threads without a connection... I don't think you have anything that will stop you from doing that at the moment.
本ThreadedEchoHandler
应采取的Socket,而不是一个ServerSocket。在客户端连接之前接受块,否则您将在没有连接的情况下产生无限数量的线程......我认为您目前没有任何东西可以阻止您这样做。
回答by soneangel
Guys thank you very much for your answers and for editing title. I'm new here, new on java, new on networking. Why I'm making my skill on streaming? It's a study case. I'm looking at many tutorial about networking and I saw RTP but I didn't read about 'cause I thought (for reading on forums) it was just for real time streming meant as webcam streaming...but it's that I'm just so confused LOL
伙计们非常感谢您的回答和编辑标题。我是新来的,Java 新手,网络新手。为什么我要在流媒体上发挥我的技能?这是一个研究案例。我在看很多关于网络的教程,我看到了 RTP,但我没有读过,因为我认为(在论坛上阅读)它只是为了实时流媒体,意味着网络摄像头流......但就是这样只是很困惑 LOL
Lirik of course what you said, I forgot some lines of coding
Lirik当然你说的,我忘记了一些代码行
while(true) {
Socket incoming = s.accept();
Runnable r = new ThreadedEchoHandler(incoming, i);
...
or as you said
或者如你所说
while(true) {
Runnable r = new ThreadedEchoHandler(s.accept(), i);
...
Taking a look at what you said guys. Kind regards!
看看你说的伙计们。亲切的问候!