NanoHTTPD - 将https流转换为http [英] NanoHTTPD - convert https stream to http

查看:760
本文介绍了NanoHTTPD - 将https流转换为http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要克服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加密音频流转换为解密后的音频流飞?

To overcome Chromecast's restriction on streaming from self-certificated https servers (in my case the Subsonic music server) I'm utilizing an instance of the NanoHTTPD server already running as part of my Android app. The idea is to stream from the Subsonic server (SSL) and connect that stream to a new stream (non SSL) for the NanoHTTP.Response back to Chromecast. I have the InputStream working from the Subsonic server (which plays through the MediaPlayer) but don't know how to re-stream unencrypted for the following call:

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

So in a nutshell, how do I convert an https encrypted audio stream to a decrypted audio stream on the fly?

推荐答案

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

Ok, managed to get all of this working. Subsonic server using https, accessible from Android and Chromecast using the server's self-signed certificate. If https is on then both Android and Chromecast use a nanohttpd proxy server running on the Android client to stream to the Android MediaPlayer and the html5 audio element respectively. The serve function override of the nanohttpd server running on Android contains the following code:-

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上的配置如何,我都会对原始格式进行流式处理,到目前为止测试中它们似乎都运行良好。

I found that streaming flac files with mp3 transcoding on gave me the flac filesize but of course the mp3 stream. This proved difficult to handle for the html5 audio element so I reverted to adding &format=raw to the Subsonic api call for the stream. So regardless of user's configuration over https I stream raw format and it all seems to be working well so far in testing.

这篇关于NanoHTTPD - 将https流转换为http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆