?
Solved

Streaming video with JSP

Posted on 2004-04-20
15
Medium Priority
?
8,578 Views
Last Modified: 2013-11-24
I have written a JSP to do video streaming via http. However, I found that it's performance is not good as it cannot reach 13Mbps. If I use Tomcat 4.1.30 directly to serve the file, I can get 13Mbps.

My platform is WinXP, P41.8, 256M RAM, J2SDK1.4.2_02, Tomcat 4.1.30

Therefore, I need opinions from experts to furthur improve the code: (it's a JSP)

** Sometime, "getOutputStream() has already been called for this response" exception will occur, any way to get rid of this?

<%@ page import="java.io.*,java.nio.*, java.nio.channels.*"%>

<%
  FileChannel rbc = null;
  WritableByteChannel wbc = null;
 
  try {
      String filename = (String) request.getAttribute("path");
      File file = new File(filename);

      rbc = new FileInputStream(file).getChannel();
      rbc.position(0);
      wbc = Channels.newChannel(response.getOutputStream());
      response.setContentType(getServletContext().getMimeType(filename));
      response.setHeader("Content-Length", String.valueOf(file.length()));
 
      ByteBuffer bb = ByteBuffer.allocateDirect(11680);
 
        while (rbc.read(bb) != -1) {
            bb.flip();
            wbc.write(bb);
            bb.clear();
        }
 
      wbc.close();
      rbc.close();
  }
  catch(Exception exp) {
      exp.printStackTrace();

      if (wbc != null)
        wbc.close();

      if (rbc != null)
        rbc.close();
  }
%>
0
Comment
Question by:peikfeng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10866510
why jsp?
0
 

Author Comment

by:peikfeng
ID: 10866567
I wrote it as an extension to a web application. (Am I answering your question?)
0
 

Author Comment

by:peikfeng
ID: 10866951
In addition, the video file is NOT located in Tomcat context path.
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 92

Expert Comment

by:objects
ID: 10867189
it would probably be more suitable to code it as a servlet instead of a jsp.
0
 

Author Comment

by:peikfeng
ID: 10867341
I precompile my JSP.

Btw if I do it in servlet, what will the difference be when streaming the file? I mean the code.
0
 
LVL 7

Expert Comment

by:searlas
ID: 10867715
To get rid of the occasional exception, remove the whitespace here:
<%@ page import="java.io.*,java.nio.*, java.nio.channels.*"%>

<%
  FileChan....

becomes:
<%@ page import="java.io.*,java.nio.*, java.nio.channels.*"%><%
  FileChan....

The reason being, the whitespace goes into a buffer which appears to be being flushed AFTER you've streamed the video.  The exception should be harmless at this point, but if you want to get rid of it, that's how you do it.  You may also need to end with this (after your catch block):
if (true) { return; }

That's because you can never be sure your editor hasn't bunged in a newline or something after your final closing %>.
0
 

Expert Comment

by:sanjayjay
ID: 10867844
Why don't you try with a servlet rather than using jsp.
0
 

Author Comment

by:peikfeng
ID: 10867955
Will servlet improve the streaming?

I use JSP coz it is easy to deploy and make modification.  (just personal preference) :D
0
 
LVL 1

Expert Comment

by:manifoldronin
ID: 10870423
IMHO using a servlet wouldn't make much different as far as performance is concerned, because JSP _is_ servlet.

The buffer size you are using now is about 11k.  Any particular reason to pick this number?  I would play with it to find an optimal setting - too small you incur too much overhead in reading and writing while a too large value causes a long read/write latency.

Another idea would be using two threads - one reads from the file while the other writes to the output.  The rationale is that both disk and network I/O are blocking but don't require a lot of CPU, so a concurrent producer/consumer model may increase the performance.  Admittedly, though, the code will get a bit more complicated.

Just my 2 cents.
0
 

Author Comment

by:peikfeng
ID: 10875093
For the buffer size, 11680 was just one of the number I played around while trying to get best performance but I found that it didn't give much changes.
0
 
LVL 1

Accepted Solution

by:
manifoldronin earned 1000 total points
ID: 10878476
Looked at your code again - since you are using FileChannel, did you try to map it into the memory directly?  That's suppose to boost the reading performance.  See the javadoc for FileChannel.map().
0
 

Author Comment

by:peikfeng
ID: 10896172
I was trying to use map(), I haven't check it's transfer rate yet but the method fails to play a 2G file with the below exception:

java.io.IOException: Not enough storage is available to process this command
at sun.nio.ch.FileChannelImpl.map0(Native Method)
0
 
LVL 1

Expert Comment

by:manifoldronin
ID: 10909786
Did you call map with the length of the file?  Try a smaller value, e.g., half of your physical memory.
0
 

Author Comment

by:peikfeng
ID: 10924549
Still fail
0
 

Author Comment

by:peikfeng
ID: 10945102
Is there any way else that I could do to improve the streamming performance? such as control the packet size/windows size...
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
A solution for Fortify Path Manipulation.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question