2011-12-13 347 views
1

我想創建一個簡單的音頻流服務器像一個概念證明,但我有一些貧困。 我正在流式傳輸一個文件來啓動,我搜索了但沒有找到足夠的信息來創建一個音頻流服務器,所以我只是基於我對服務器的一些瞭解創建了一個簡單的服務器。我和網狀創建它傳遞流ChunkedStream對象,並寫在了信道:netty音頻流服務器

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,我沒有成功,直接流音頻到Web瀏覽器,所以我tryied弄清楚什麼的Icecast回報

緩存控制::作爲響應於網絡瀏覽器,並將其在報頭返回這些屬性無緩存
內容類型:應用/ OGG
服務器:2.3.2的Icecast
冰 - 音頻信息:採樣率= 44100;信道= 2;質量= 3%2e00
冰冷-描述:流去睾丸中
冰冷的流派:搖滾
冰冷的名稱:無線泰斯特Brevleq
冰冷-PUB:0

有一個簡單的辦法網狀使用,以把這些內容HttpResponse頭(特別是Content-type:applicatio/ogg)??我希望這是問題...

+0

唯一需要的頭是`Content-Type`。其他一切只是額外的信息。 – Brad 2011-12-14 14:26:02

回答

0

我會考慮去一個直接的二進制協議,並創建一個HTTP接口只爲代理。沒有理由處理這樣的基於文本的協議。