2017-03-05 64 views
0

我試圖寫一個Java程序,它允許一個用戶充當服務器和流自己的桌面(視頻&音頻),然後其他用戶作爲客戶和觀看他們的桌面的實時數據流(類似於Twitch,Webex,Skype屏幕共享等)。我正在使用VLCJ,儘管我沒有使用它的承諾,所以如果有更好的解決方案,我全是耳朵。這是從我提供的鏈接複製的代碼:流桌面的VLCJ

package test.java; 

import uk.co.caprica.vlcj.discovery.NativeDiscovery; 
import uk.co.caprica.vlcj.player.MediaPlayerFactory; 
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer; 
import test.java.VlcjTest; 

/** 
* An example of how to stream a media file over HTTP. 
* <p> 
* The client specifies an MRL of <code>http://127.0.0.1:5555</code> 
*/ 
public class StreamHttp extends VlcjTest { 

    //when running this it requires an MRL (Media Resource Locator) 
    //fancy term for saying the file you want to stream. This could be a url to another 
    //location that streams media or a filepath to a media file you want to stream 
    //on the system you are running this code on. 
    public static void main(String[] args) throws Exception { 
     new NativeDiscovery().discover(); 
     if(args.length != 1) { 
      System.out.println("Specify a single MRL to stream"); 
      System.exit(1); 
     } 

     //the media you are wanting to stream 
     String media = args[0]; 
     //this is the IP address and port you are wanting to stream at 
     //this means clients will connect to http://127.0.0.1:5555 
     //to watch the stream 
     String options = formatHttpStream("127.0.0.1", 5555); 

     System.out.println("Streaming '" + media + "' to '" + options + "'"); 

     //this creates a the actual media player that will make calls into the native 
     //vlc libraries to actually play the media you supplied. It does it in 
     //a headless fashion, as you are going to stream it over http to be watched 
     //instead of playing it locally to be watched.  
     MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args); 
     HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer(); 

     //this simply starts the player playing the media you gave it 
     mediaPlayer.playMedia(media, options); 

     // Don't exit 
     //basically you don't want the thread to end and kill the player, 
     //so it just hangs around and waits for it to end. 
     Thread.currentThread().join(); 
    } 

    private static String formatHttpStream(String serverAddress, int serverPort) { 
     StringBuilder sb = new StringBuilder(60); 
     sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,"); 
     sb.append("dst="); 
     sb.append(serverAddress); 
     sb.append(':'); 
     sb.append(serverPort); 
     sb.append("}}"); 
     return sb.toString(); 
    } 
} 

我將「screen://」作爲參數傳遞給此程序。當我運行的代碼,我得到這個錯誤信息:

[000000000038b250] access_output_http access out: Consider passing --http-host=IP on the command line instead. 
[000000001ccaa220] core mux error: cannot add this stream 
[000000001cc72100] core decoder error: cannot create packetizer output (RV32) 

我試圖尋找一個解決方案,但所有我能找到的是這樣的: Video Streaming in vlcj 雖然這個用戶有同樣的錯誤,我解決不了我的問題從這個鏈接,雖然我確實使用StreamHttp代碼示例。我是一個相對缺乏經驗的程序員,所以如果我錯過了一個明顯的解決方案,那麼我很抱歉。我正在使用Java 1.8,Windows 7 64位。

回答

1

你需要的東西是這樣的:

String media = "screen://"; 
String[] options = { 
    ":sout=#transcode{vcodec=FLV1,vb=4096,scale=0.500000}:http{mux=ffmpeg{mux=flv},dst=:5000/" 
}; 

這裏顯示的關鍵的東西是一個「SOUT」字符串(通過http在這種情況下),以視頻轉碼,然後又追加「SOUT」字符串流。

在這個例子中的字符串,HTTP流僅端口(5000,任意選擇的)被指定。沒有指定主機,所以它表示本地主機。你可以有像「dst = 127.0.0.1:8080 /」或任何你需要的東西。

你將不得不選擇/實驗所需的特定轉碼/流媒體選項。這些選項沒有一個適合所有人的尺寸。

足注:

您實際上可以使用VLC本身產生此字符串爲您服務。

開始VLC,然後選擇要播放的媒體。

,而不用按「播放」的,使用小窗口以選擇「流」來代替。這將打開Streaming嚮導,您可以在其中選擇所有選項。

在嚮導的最後,你開始玩之前,它會顯示你需要的字符串。

+0

OK,所以我嘗試了以下步驟: – anvijost

+0

打開VLC,Media> Stream> Capture Device,將Capture模式設置爲Desktop,添加目標:RTP/MPEG傳輸流,地址= 230.0.0.1,基本端口= 5555,轉碼配置文件:視頻 - H.264 + MP3(MP4),然後我看到你提到的字符串。但是,一旦我點擊「Stream」,VLC就會崩潰。我不確定是什麼導致了這個錯誤,它可能是我選擇的轉碼選項還是目的地?我會繼續嘗試不同的組合。感謝您一直以來的幫助! (對不起,以上評論過早發佈) – anvijost

+0

Followup to my previous comment:我測試了每個默認轉碼選項,默認設置爲HTTP(path = /,port = 8080),VLC隨每個選項都崩潰,除了最終的僅音頻選項[音頻 - Vorbis(OGG)]。我不確定這是什麼意思,但我會用其他傳輸協議(RTP等)進行相同的測試,看看我是否得到不同的結果。 – anvijost