NanoHTTPD - 将https流转换为http

时间:2017-03-06 12:12:38

标签: java android https stream nanohttpd

要克服Chromecast对自我认证的https服务器(在我的情况下是Subsonic音乐服务器)的流媒体限制,我正在使用已经作为我的Android应用程序的一部分运行的NanoHTTPD服务器的实例。我们的想法是从Subsonic服务器(SSL)流式传输并将该流连接到NanoHTTP.Response的新流(非SSL)回到Chromecast。我让InputStream从Subsonic服务器(通过MediaPlayer播放)工作,但不知道如何为以下呼叫重新加密未加密:

new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "audio/mpeg", newStream);
<简而言之,如何将https加密音频流动态转换为解密音频流?

1 个答案:

答案 0 :(得分:0)

好的,设法让所有这些工作。使用https的亚音速服务器,可通过Android和Chromecast使用服务器的自签名证书访问。如果启用https,则Android和Chromecast都会使用在Android客户端上运行的nanohttpd代理服务器分别流式传输到Android MediaPlayer和html5音频元素。在Android上运行的nanohttpd服务器的服务函数覆盖包含以下代码: -

int filesize = .... obtained from Subsonic server for the track to be played

// establish the https connection using the self-signed certificate
// placed in the Android assets folder (code not shown here)
HttpURLConnection con = _getConnection(subsonic_url,"subsonic.cer")

// Establish the InputStream from the Subsonic server and
// the Piped Streams for re-serving the unencrypted data 
// back to the requestor
final InputStream is = con.getInputStream();
PipedInputStream sink = new PipedInputStream();
final PipedOutputStream source = new PipedOutputStream(sink);

// On a separate thread, read from Subsonic and write to the pipe 
Thread t = new Thread(new Runnable() {
                public void run () {
                    try {
                        byte[] b = new byte[1024];
                        int len;
                        while ((len = is.read(b,0,1024)) != -1)
                            source.write(b, 0, len);
                        source.flush();
                    }
                    catch (IOException e) {
                    }
                }});

t.start();
sleep(200); // just to let the PipedOutputStream start up

// Return the PipedInputStream to the requestor.
// Important to have the filesize argument
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, 
                              "audio/mpeg", sink, filesize);

我发现带有mp3转码的流式flac文件给了我flac文件大小但当然是mp3流。事实证明这对于html5音频元素来说很难处理,所以我又回到了为该流的Subsonic api调用添加&amp; format = raw。因此,无论用户在https上的配置如何,我都会对原始格式进行流式处理,到目前为止测试中它们似乎都运行良好。