简体   繁体   English

带有netty的音频流服务器

[英]audio stream server with netty

I'm trying create a simple audio stream server like a concept proof, but I'm having some dificulties. 我正在尝试创建一个简单的音频流服务器(如概念证明),但是有一些麻烦。 I'm streaming a single file to start, I searched but didn't found enought information to create a audio stream server, so I just created a simple server based on my little knowledge about servers. 我正在流式传输单个文件以进行启动,我进行了搜索,但没有找到足够的信息来创建音频流服务器,因此我只是基于对服务器的一点了解创建了一个简单的服务器。 I've created it with netty passing the stream to ChunkedStream object and wrote it on channel: 我已经用netty创建了它,并将流传递给ChunkedStream对象,并将其写在通道上:

public class CastServerHandler extends SimpleChannelHandler {

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
        throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();
    if (request.getMethod() != GET) {
        sendError(ctx, METHOD_NOT_ALLOWED);
        return;
    }

    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    System.out.println(response.toString());
    Channel channel = e.getChannel();
    channel.write(response);
    ChannelFuture writeFuture;
    StreamSource source = StreamSource.getInstance();
    ChunkedStream stream = new ChunkedStream(source.getLiveStream());
    writeFuture = channel.write(stream);
    writeFuture.addListener(new ChannelFutureProgressListener() {
        public void operationComplete(ChannelFuture future) {
            System.out.println("terminou");
            future.getChannel().close();
        }

        public void operationProgressed(ChannelFuture future, long amount,
                long current, long total) {
            System.out.println("Transferido: " + current + " de " + total);
        }
    });
    if (!isKeepAlive(request)) {            
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.setContent(ChannelBuffers.copiedBuffer(
            "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));

    // Close the connection as soon as the error message is sent.
    ctx.getChannel().write(response)
            .addListener(ChannelFutureListener.CLOSE);
}

private void writeLiveStream(Channel channel) {
    StreamSource source = StreamSource.getInstance();
    ChunkedStream stream = new ChunkedStream(source.getLiveStream());
    channel.write(stream);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
        throws Exception {
    e.getCause().printStackTrace();
    e.getChannel().close();
}
}

Ufortunately, I didn't successfully streamed the audio directly to web browser, so I tryied to figure out what icecast returns as response to web browser, and it return these properties in header: 不幸的是,我没有成功将音频直接流式传输到Web浏览器,因此我尝试找出Icecast作为对Web浏览器的响应返回的内容,并且它在标头中返回以下属性:

Cache-Control:no-cache 缓存控制:无缓存
Content-Type:application/ogg 内容类型:application / ogg
Server:Icecast 2.3.2 伺服器:Icecast 2.3.2
ice-audio-info:samplerate=44100;channels=2;quality=3%2e00 ice-audio-info:采样率= 44100;通道= 2;质量= 3%2e00
icy-description:Stream de teste icy-description:流的teste
icy-genre:Rock 冰冷的类型:摇滚
icy-name:Radio teste Brevleq 冰冷名称:无线电睾丸Brevleq
icy-pub:0 icy-pub:0

Is there a simple way netty use to put these content in HttpResponse header (specially Content-type:applicatio/ogg)?? Netty是否有一种简单的方法将这些内容放入HttpResponse标头(特别是Content-type:applicatio / ogg)? I hope this is the problem... 我希望这是问题所在...

See the API of HttpResponse . 请参阅HttpResponseAPI It has setHeader method. 它具有setHeader方法。

I'd consider going with a straight binary protocol, and creating an HTTP interface only for a proxy. 我会考虑使用直接的二进制协议,并仅为代理创建HTTP接口。 There's no reason to deal with a text based protocol for something like this. 这样的事情没有理由处理基于文本的协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM