Solved

Streaming video with JSP

Posted on 2004-04-20
15
8,416 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 250 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 52
Tomcat: Unable to run tomcat service. 2 30
program arguments vs VM arguments 4 29
Github api to fetch the number of collaborators in a git repo 1 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

733 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