为了克服 Chromecast 对来自自认证 https 服务器(在我的例子中是 Subsonic 音乐服务器)进行流传输的限制,我正在利用已经作为我的 Android 应用程序的一部分运行的 NanoHTTPD 服务器实例。这个想法是从 Subsonic 服务器 (SSL) 进行流传输,并将该流连接到新流(非 SSL),以便 NanoHTTP.Response 返回 Chromecast。我有从 Subsonic 服务器(通过 MediaPlayer 播放)工作的 InputStream,但不知道如何为以下调用重新传输未加密的流:
new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "audio/mpeg", newStream);
简而言之,如何将 https 加密音频流即时转换为解密音频流?
好的,设法让所有这些工作正常。使用 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);
我发现打开 mp3 转码功能的流式 flac 文件给了我 flac 文件大小,当然还有 mp3 流。事实证明,这对于 html5 音频元素来说很难处理,因此我转而将 &format=raw 添加到流的 Subsonic api 调用中。因此,无论用户通过 https 进行的配置如何,我都会流式传输原始格式,并且到目前为止在测试中一切似乎都运行良好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)