如何编写服务器/客户端视频和音频流应用程序?

时间:2021-11-14 19:00:19

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.

希望你能澄清我的想法。亲切的问候。

3 个解决方案

#1


8  

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.

对于流式传输和与客户交谈,您需要定义协议:在Web上搜索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.

干杯。

#2


1  

Check out the Java Media Framework (it has tutorials): http://java.sun.com/javase/technologies/desktop/media/jmf/

查看Java Media Framework(它有教程):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。接受块直到客户端连接,否则你将在没有连接的情况下产生无限数量的线程...我认为你没有任何东西可以阻止你这样做。

#3


0  

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,但我没有看到因为我认为(因为在论坛上阅读)它只是用于实时流动意味着网络摄像头流......但它就是我的只是这么困惑大声笑

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!

看看你说的家伙们。亲切的问候!

#1


8  

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.

对于流式传输和与客户交谈,您需要定义协议:在Web上搜索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.

干杯。

#2


1  

Check out the Java Media Framework (it has tutorials): http://java.sun.com/javase/technologies/desktop/media/jmf/

查看Java Media Framework(它有教程):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。接受块直到客户端连接,否则你将在没有连接的情况下产生无限数量的线程...我认为你没有任何东西可以阻止你这样做。

#3


0  

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,但我没有看到因为我认为(因为在论坛上阅读)它只是用于实时流动意味着网络摄像头流......但它就是我的只是这么困惑大声笑

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!

看看你说的家伙们。亲切的问候!