2017-03-06 167 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