用libvlc進行網絡串流streaming

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)

 1 // 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 }
89 
90 


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


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章