用libvlc進行網路串流streaming

Just4life發表於2014-02-26

vlc具有豐富、強大的命令列引數,使用者可以方便地進行轉碼、IO重定向(檔案、網路。。。)等等,網路上相關的資料也很多,在此就不囉嗦了。這裡貼一點關於使用libvlc進行串流的經驗,和大家分享。

1. 首先,從http://download.videolan.org/pub/videolan/vlc/下載libvlc所需要的.lib和.h檔案,在各版本的win32目錄下有一個.zip的檔案,這個zip裡的sdk有include目錄和lib目錄,將lib目錄下的兩個.dll.a檔案重新命名為.lib檔案,在VC工程裡配置好目錄。使用libvlc需要包含"vlc/vlc.h"這個標頭檔案和連線libvlc.lib和libvlccore.lib兩個庫檔案。

2. libvlc需要stdint.h,如果沒有這個檔案的話,下載到這個檔案後放到sdk\include\vlc目錄下,並將libvlc_structures.h中的#include <stdint.h>替換為#include  "stdint.h",編譯就沒有問題了。

3. 連線libvlc後程式執行需要libvlc.dll和libvlccore.dll以及一系列外掛功能dll,這些在1中下載的zip檔案裡都有,複製到程式所在目錄就可以了。注意保持目錄結構不變,例如:

E:\Project\VLC\bin>tree /F
│ libvlc.dll
│ libvlccore.dll
│ libvlcTutorial.exe

└─plugins
liba52tofloat32_plugin.dll
liba52tospdif_plugin.dll
liba52_plugin.dll
libaccess_attachment_plugin.dll
// other dll

4. vlc的wiki提供了libvlc的簡單例子,下面給出的程式碼主要增加了引數配置進行網路串流的部分:(libvlcTutorial.cpp)

// libvlcTutorial.cpp : 定義控制檯應用程式的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 #include "vlc/vlc.h"
 7 
 8 // vlc command line
 9 /*
10 vlc -vvv --extraintf logger --logfile vlc_server.log 
11 dshow:// :dshow-vdev= :dshow-adev= 
12 :sout="#duplicate{dst=display,
13 dst='transcode{vcodec=h264,vb=8,fps=30,width=320,height=240,scale=1,acodec=none}
14 :rtp{dst=127.0.0.1,mux=ts,port=1234,caching=100}'}"
15  */
16 
17 
18 int _tmain(int argc, _TCHAR* argv[])
19 {
20     libvlc_instance_t * inst;
21     libvlc_media_player_t *mp;
22     libvlc_media_t *m;
23     libvlc_log_t *log;
24 
25     /* Load the VLC engine */
26     inst = libvlc_new (0, NULL);
27 
28     // logging
29     log = libvlc_log_open (inst);    
30     libvlc_set_log_verbosity (inst, 2);
31     unsigned int level = libvlc_get_log_verbosity (inst);
32     printf ("vlc log verbosity level = %d\n", level);
33 
34 
35     /* Create a new item */
36 //    m = libvlc_media_new_path (inst, "f:\\downloads\\sample.avi");
37     m = libvlc_media_new_path (inst, "dshow://// :dshow-vdev= :dshow-adev=");
38 
39     // media option
40     const char *options[] = {
41         ":no-audio",
42         ":video-title-position=4",
43         ":sout=#duplicate{dst=display,dst=\'transcode{venc=x264{profile=baseline},vcodec=h264,vb=10,width=320,height=240,fps=10,scale=1}:rtp{dst=127.0.0.1,port=1234,mux=ts}\'}",
44         ":sout-udp-caching=1",
45         ":sout-rtp-caching=1",
46         ":sout-mux-caching=1",
47         ":sout-ts-dts-delay=60"
48         
49     };
50     for (int i = 0; i < sizeof(options) / sizeof(options[0]); i++)
51         libvlc_media_add_option (m, options[i]);
52 
53     /* Create a media player playing environement */
54     mp = libvlc_media_player_new_from_media (m);
55 
56     /* No need to keep the media now */
57     libvlc_media_release (m);
58 
59 #if 0
60     /* This is a non working code that show how to hooks into a window,
61     * if we have a window around */
62     libvlc_media_player_set_xdrawable (mp, xdrawable);
63     /* or on windows */
64     libvlc_media_player_set_hwnd (mp, hwnd);
65     /* or on mac os */
66     libvlc_media_player_set_nsobject (mp, view);
67 #endif
68 
69     /* play the media_player */
70     libvlc_media_player_play (mp);
71 
72     while (!_kbhit())
73         Sleep (100); /* Let it play a bit */
74 
75     /* Stop playing */
76     libvlc_media_player_stop (mp);
77 
78     /* Free the media_player */
79     libvlc_media_player_release (mp);     
80 
81     libvlc_release (inst);
82 
83 
84     printf ("message in log = %d\n", libvlc_log_count (log));
85     system("pause");
86     return 0;
87 
88 }


需要說明的是,上面的程式碼只是給出了實現串流的一種實現,我也不知道是否為標準做法。43行中sout配置為一路在本地顯示原始碼流,另一路經x264編碼後進行網路串流,venc=x264後面的{}內可以設定的引數還相當多,這裡只舉了一個例子;另外一個經驗是,第二個dst後面的一對\'也是不可少的,也可以替換為一對{},否則執行時會出現引數解析錯誤。


不清楚為了實現低位元速率的編碼和串流,除了vb、fps還有哪些影響較大的引數,繼續研究中。

相關文章