Skip to main content

file streaming help

2 replies [Last post]
tera_byte
Offline
Joined: 2010-02-13

hello java.net community, although i'm not new to java i'm a bit new to servlets and server side coding
and i've been having a problem streaming an avi file to multiple clients.
here's my code:
upstream.java (the code that is suppose to read the file and stream it)

<br />
package com.finalboot;<br />
import java.io.*;<br />
import java.util.*;<br />
import java.util.concurrent.*;<br />
import java.util.logging.Level;<br />
import java.util.logging.Logger;<br />
public class upStream implements Runnable{</p>
<p>public static PipedOutputStream pipeout=new PipedOutputStream();</p>
<p>public static byte [] header=new byte[56];<br />
public static boolean started=false;</p>
<p>static LinkedList pipelist=new LinkedList();</p>
<p>boolean done=false;<br />
public PipedOutputStream getPipeOut(){</p>
<p> return pipeout;<br />
}<br />
public void run(){<br />
stream();</p>
<p>}</p>
<p>public PipedInputStream addPipeOut(){<br />
    PipedOutputStream p=new PipedOutputStream();<br />
    PipedInputStream pin=null;<br />
    synchronized(pipelist){<br />
    pipelist.add(p);<br />
            try {<br />
                pin = new PipedInputStream(p);<br />
            } catch (IOException ex) {<br />
              ex.printStackTrace();<br />
            }<br />
    }</p>
<p>    return  pin;<br />
}<br />
public void stream(){<br />
    try{<br />
      File f=new File("/srv/tomcat/webapps/ROOT/WEB-INF/videos/vid.avi");</p>
<p>DataInputStream dis=new DataInputStream(new FileInputStream(f));<br />
while(dis.available()>0){<br />
//System.out.print("_");<br />
    if(pipelist!=null){<br />
    synchronized(pipelist){<br />
 if(pipelist.size()>0){<br />
  int data=dis.read();<br />
//System.out.print("$$");<br />
  for(PipedOutputStream pipeout: pipelist){</p>
<p>        pipeout.write(data);<br />
        pipeout.flush();<br />
    //   System.out.print((char)data+"|");<br />
      //  dos.flush();<br />
    }<br />
//  System.out.println("~~~~!");<br />
 }<br />
    }<br />
}<br />
}<br />
    }catch(Exception e){<br />
        e.printStackTrace();<br />
    }<br />
    done=true;<br />
}<br />
public boolean isDone(){<br />
    return done;<br />
}<br />
public  upStream(){</p>
<p>try{<br />
File f=new File("/srv/tomcat/webapps/ROOT/WEB-INF/videos/vid.avi");</p>
<p>DataInputStream dis=new DataInputStream(new FileInputStream(f));<br />
for(int i=0;i<56;i++){<br />
    header[i]=dis.readByte();<br />
}<br />
dis.close();<br />
////////////////////////////////////////////////////<br />
Thread t=new Thread(this);<br />
//t.setPriority(6);<br />
t.start();<br />
}catch(Exception e){<br />
    e.printStackTrace();<br />
}</p>
<p>}<br />
 public static int swap (int value)<br />
  {<br />
    int b1 = (value >>  0) & 0xff;<br />
    int b2 = (value >>  8) & 0xff;<br />
    int b3 = (value >> 16) & 0xff;<br />
    int b4 = (value >> 24) & 0xff;</p>
<p>    return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;<br />
  }</p>
<p>}</p>
<p>

and streaming .java (the servlet)

<br />
package com.finalboot;<br />
import java.io.*;<br />
import javax.servlet.*;<br />
import javax.servlet.http.*;<br />
import com.finalboot.*;<br />
public class streaming extends HttpServlet implements Runnable{</p>
<p>    public void doGet(HttpServletRequest request, HttpServletResponse response)<br />
		throws IOException, ServletException {<br />
        System.out.println("Serving streaming servlet");<br />
        upStream u=new upStream();<br />
response.setContentType("video/avi");<br />
DataOutputStream dos=new DataOutputStream(response.getOutputStream());<br />
PipedInputStream pipein=u.addPipeOut();<br />
    if(u.pipelist.size()>1){<br />
        for(int i=0;i<56;i++){<br />
            dos.writeByte(u.header[i]);<br />
        }<br />
    }<br />
while(!u.isDone()){<br />
    // System.out.print("#");<br />
     if(pipein.available()>0){<br />
    dos.write(pipein.read());<br />
     }else{<br />
      //   System.out.print("pipin not available");<br />
         Thread.currentThread().yield();<br />
     }</p>
<p>}<br />
        }<br />
    public void run(){</p>
<p>    }<br />
public void init()throws ServletException{</p>
<p>}<br />
}<br />

this code streams fine with one client but fails when another client connects.

any help is appreciated. thank you in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

Your fields are [i]static[/i] but they shouldn't be. Read up on static in the [url=http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html]Java tutorial[/url] if you're confused about its uses. As a rule, don't add the static keyword unless you have very specific reasons for doing so.

tera_byte
Offline
Joined: 2010-02-13

i know what static does.i wanted there to be only 1 copy of the linkedlist. if it isn't static the data would be written into the list but only the object that is used to to upload will have that specific list. i wanted every object instance to share the the same list.

afaik static is used when you don't want every instance of your class to have it's own copy of a field
but correct me if i'm wrong.

maybe you didn't understand my code. anyways here's how it is now after doing some changes(if you think i'm still using it in a wrong way please tell me):
here's upstream.java:
package com.finalboot;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class upStream implements Runnable{

public static PipedOutputStream pipeout=new PipedOutputStream();
public static LinkedList list=new LinkedList();
public static Integer [] header=new Integer[32000];
public static boolean started=false,reset=false;
int index=0;
int count=0;
int lasthash=-1;
static LinkedList pipelist=new LinkedList();

static boolean done=false;
public PipedOutputStream getPipeOut(){

return pipeout;
}
public void run(){
Thread thread=new Thread(container);
//thread.setPriority(1);
thread.start();
stream();

}
public int available(){
return list.size();
}
public PipedInputStream addPipeOut(){
PipedOutputStream p=new PipedOutputStream();
PipedInputStream pin=null;
synchronized(pipelist){
pipelist.add(p);
try {
pin = new PipedInputStream(p);
System.out.println(pipelist.size());
} catch (IOException ex) {
ex.printStackTrace();
}
}

return pin;
}
Runnable container=new Runnable(){
public void run(){
try {
while(!isDone()){
if(list!=null){
// System.out.println(list.size());

if(list.size()>9999){
synchronized(list){
for(int i=0;list.size()>9999;i++){
list.remove();
}
}
reset=true;
}else{reset=false;}
Thread.currentThread().sleep(200);
}}} catch (Exception ex) {
ex.printStackTrace();
}
}
};
public void stream(){
try{
File f=new File("/srv/tomcat/webapps/ROOT/WEB-INF/videos/output.flv");
long length=f.length();
final DataInputStream dis=new DataInputStream(new FileInputStream(f));

while(dis.available()>0){
//System.out.print("_");

synchronized(list){

int data=dis.read();
if(data==-1){
System.out.println("negatives!!");
// break;

}

list.add(new Data(data));
++count;
//System.out.println(count+"|"+dis.available());
// for(PipedOutputStream pipeout: pipelist){
//
// pipeout.write(data);
// pipeout.flush();
// // System.out.print((char)data+"|");
// // dos.flush();
// }
//System.out.println("~~~~!");

}
Thread.currentThread().yield();
}done=true;
}catch(Exception e){
e.printStackTrace();
}
done=true;
list.clear();
}
public Integer read(){

int i=-1;
synchronized(list){
if(list!=null){
if(index
//System.out.println(index+"-"+list.size());
if(lasthash!=list.get(index).hashCode()){
i=list.get(index).getValue();
System.out.println(i+"---"+list.get(index).hashCode());
lasthash=list.get(index).hashCode();
}else{i=-1;
//System.out.print(list.get(index).getValue());
}
}else{

if(list.size()>0){
if(lasthash!=list.getLast().hashCode()){
i=list.getLast().getValue();
lasthash=list.getLast().hashCode();
System.out.println(i+"---"+list.getLast().hashCode());
}else{
i=-1;
// System.out.print(list.getLast().getValue());
}
}
}
if(index
++index;

}}
return i;
}
public boolean isDone(){
return done;
}
public upStream(){

}
public void goLive(){
try{

Thread t=new Thread(this);
//t.setPriority(6);
t.start();
System.out.println("went live");
}catch(Exception e){
e.printStackTrace();
}
}
public static int swap (int value)
{
int b1 = (value >> 0) & 0xff;
int b2 = (value >> 8) & 0xff;
int b3 = (value >> 16) & 0xff;
int b4 = (value >> 24) & 0xff;

return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
}

}

________________________________________________________________

Data.java:

package com.finalboot;
import java.math.*;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Data {
int hash;
int data;

Data(){
Random r=new Random(System.currentTimeMillis());
hash=this.hashCode()+r.nextInt()+(int)System.currentTimeMillis();
data=-1;
}
Data(int d){
Random r=new Random(System.currentTimeMillis());
hash=this.hashCode()+r.nextInt()+(int)System.currentTimeMillis();
data=d;

}
public int getValue(){
return data;
}
public int hashCode(){

return hash;
}

}

_______________________________________________
streaming.java:

package com.finalboot;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import com.finalboot.*;
public class streaming extends HttpServlet implements Runnable{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("Serving streaming servlet");
upStream u=new upStream();
response.setContentType("video/x-flv");
DataOutputStream dos=new DataOutputStream(response.getOutputStream());
final DataOutputStream dbg=new DataOutputStream(new FileOutputStream(new File("dbg2.flv")));
//broadcast b=new broadcast(response);
//b.start();
try{
while(u.isDone()==false){
if(u.available()>0){
Integer c=u.read();
if(c>=0){
dos.write(c);

//System.out.print((char)c);
dbg.write(c);
}else{
// System.out.print(c+"|");
}
}else{
System.out.print("no more");
}
}
System.out.println("done");
}catch(Exception e){
e.printStackTrace();
}

}
public void run(){

}
public void init()throws ServletException{
new upStream().goLive();

}
}

__________________________________________________________________________
the data get's read in right and get's stored in the list right it just doesn't get read from the list and to the servlet right and i'm trying to fix that.

i know everyone here is busy and value your time, but i really would appreciate any input like the one above. or even a better solution than the one i'm trying to code.