views:

455

answers:

5

Hi guys, 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?

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.

A: 

Check out the Java Media Framework (it has tutorials): 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.

Lirik
What I finally wrote is
soneangel
A: 

Hi.

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.

As for implementing, take a look at the red5 project: http://osflash.org/red5

Take a look at Xuggler as well: http://www.xuggle.com/xuggler/ This project will help you saving lots of lines of code.

Cheers.

Rafael
A: 

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

Lirik of course what you said, I forgot some lines of coding

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!

soneangel
A: 

Hi guys, I'm almost on it! About networking I understood how it works much better than when I wrote this post. I've managed to create a chat between clients using a server to hand communication. I've used DataInputStream and DataOutputStream to send messages over client/server system. I've red about Real Time Protocol over UDP that's what I really need for my project. So I need to use RTP over a unicast network service. But even if I undestan exactly protocols and so on I don't know how to use yet. I mean for simple data I used DataInputStream and DataOutputStream but How should I define packets for streaming video?

Thanx

soneangel
A: 

Hey soneangel

could you share the code you were working on. I am working on a similar kind of project and your code can be of great help.

monk
Code I wrote would be too long to post here. By the way you can see on my last question what I'm working on.Of course you could be a great help for me too!
soneangel