您的位置:首页 > 理论基础 > 计算机网络

用libvlc进行网络串流streaming

2014-02-26 20:09 309 查看
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还有哪些影响较大的参数,继续研究中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: