Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Streaming video with JSP

Posted on 2004-04-20
15
Medium Priority
?
8,659 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 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