2017-03-06 42 views
2

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

new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "audio/mpeg", newStream);

因此,简而言之,如何转换https加密音频流动解密音频流?NanoHTTPD - 将https流转换为http

回答

0

好吧,设法让所有这些工作。使用https的Subsonic服务器,可通过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); 

我发现流FLAC文件与MP3转码给我的后手文件大小,但当然MP3流。这被证明很难处理html5音频元素,所以我恢复为将subsonic api调用格式=原始格式添加到流中。因此,无论用户通过https配置流式原始格式,它在测试中似乎都运行良好。

+0

正如Ben Baron在这里指出的... https://groups.google.com/forum/#!topic/subsonic-app-developers/xpB14ZgZ75I 我可以在流调用中使用'estimateContentLength = true'内容标题具有转码大小以避免使用原始格式 – milleph