Solved

Streaming video with JSP

Posted on 2004-04-20
15
8,195 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
  • 8
  • 3
  • 2
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
why jsp?
0
 

Author Comment

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

Author Comment

by:peikfeng
Comment Utility
In addition, the video file is NOT located in Tomcat context path.
0
 
LVL 92

Expert Comment

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

Author Comment

by:peikfeng
Comment Utility
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
Comment Utility
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
Comment Utility
Why don't you try with a servlet rather than using jsp.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:peikfeng
Comment Utility
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
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Still fail
0
 

Author Comment

by:peikfeng
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now