Solved

Streaming video with JSP

Posted on 2004-04-20
15
8,523 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

724 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