3

我正在尝试将我的手机用作实时 MJPEG 视频源。到目前为止,捕获帧并将其转换为 JPEG 并不是什么大问题。我真正的问题是正确发送多部分响应。有大量关于发送多部分响应的文档,但它们的问题是他们都希望所有图像在 HTTP 请求到来时都可用(例如用于多图像上传)。当然,为了实时流式传输,我需要能够开始发送多部分响应,同时不断在正文中添加 jpeg。我绝不是 HTTP 爱好者,所以不希望我需要滚动自己的 HTTP 响应并直接写入套接字。是否有支持这种行为的库?我已经在互联网上搜索了解决方案,

有任何想法吗?在最坏的情况下,我愿意查看有关如何手动编写多部分响应的人类可读文档,但如果可能的话,我真的宁愿使用库。

提前致谢。

编辑:根据 sigmavirus 的建议,使用 orielly servlet 库让它工作。请注意,MJPEG 流或多或少是从我发送的 multipart/x-mixed-replace 中仅包含图像/jpeg 的事实隐含地推断出来的。查看我的代码中的注释以获取教程,该教程显示了运行此程序所需的码头库。当然,您还需要 cos.jar,即 Orielly servlet 库。代码如下:

package edu.stevens.arpac.webclient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.conn.util.InetAddressUtils;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.Request;

import com.oreilly.servlet.MultipartResponse;
import com.oreilly.servlet.ServletUtils;


import android.os.Environment;
import android.util.Log;
// holla at http://puregeekjoy.blogspot.com/2011/06/running-embedded-jetty-in-android-app.html
public class JettyServer extends Thread 
{
private static final String TAG = "JettyServer";
private Server webServer;
private Boolean isStarted = false;

public JettyServer()
{
    super();
    Log.i(TAG, "Initializing server to port 8080");
    webServer = new Server(8080);

    Handler handler = new AbstractHandler() {
        public void handle(String target, Request request, HttpServletRequest servletRequest,
                HttpServletResponse servletResponse) throws IOException, ServletException {

            ServletOutputStream out = servletResponse.getOutputStream();

             MultipartResponse multi = new MultipartResponse(servletResponse);


             Boolean go = true;
             while( go )
             {

                 try
                 {
                     multi.startResponse("image/jpeg");
                     ServletUtils.returnFile(Environment.getExternalStorageDirectory().getPath() + "/ARPac/twi.jpg", out);
                     multi.endResponse();
                 }
                 catch(IOException ex)
                 {
                    go = false;
                    Log.i(TAG, "IO Failed with exception " + ex.getMessage());
                 }
             }
             request.setHandled(true);
        }
    };
    webServer.setHandler(handler);

    try {
        webServer.start();
        Log.d(TAG, "started Web server @ " + getIPAddress());
        isStarted = true;

    }
    catch (Exception e) {
        Log.d(TAG, "unexpected exception starting Web server: " + e);
    }
}

/**
 * Get IP address from first non-localhost interface
 * @return  address or empty string
 */
private String getIPAddress() 
{
    try 
    {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) 
        {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) 
            {
                if (!addr.isLoopbackAddress())
                {
                    String sAddr = addr.getHostAddress().toUpperCase(); 
                    if (InetAddressUtils.isIPv4Address(sAddr))
                    {
                        //Log.d(TAG, "IP address is: " + sAddr);
                        return sAddr;
                    } 
                }
            }
        }
    } 
    catch (Exception ex) 
    { 
        Log.e(TAG, "could not get IP address: " + ex.getMessage());
    } // for now eat exceptions
    Log.e(TAG, "Could not find a non-loopback IPv4 address!");
    return "";
}

public void teardown()
{
    if( isStarted )
    {
        try {
            webServer.stop();
            isStarted = false;
        } catch (Exception e) {
            Log.e(TAG, "Couldn't stop server. Probably was called when server already stopped.");
        }
    }
}

public void run() 
 {

 }

}

4

1 回答 1

1

你见过这个吗?http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartResponse.html看起来该示例单独发送每个部分并在发送下一个或接收中断之前等待指定的时间限制。

于 2013-01-09T17:47:36.817 回答