首页 > 代码库 > VLC 使用
VLC 使用
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 }
========================================================================
#include "Stdafx.h"
#include "AVPlayer.h"
#include <cmath>
#include "include/vlc/vlc.h"
#pragma comment(lib, "lib/libvlc.lib")
#pragma comment(lib, "lib/libvlccore.lib")
// VLC的事件管理
void OnVLC_EndReached(const libvlc_event_t *event, void *data);
void OnVLC_PositionChanged(const libvlc_event_t *event, void *data);
CAVPlayer::CAVPlayer(void) :
m_pVLC_Inst(NULL),
m_pVLC_Player(NULL),
m_hWnd(NULL),
m_pfn(NULL)
{
m_strIp="";
m_strPort="";
isSendToNet = FALSE;
}
CAVPlayer::~CAVPlayer(void)
{
Release();
}
void CAVPlayer::Init()
{
if (! m_pVLC_Inst)
{
m_pVLC_Inst = libvlc_new(0, NULL);
}
}
void CAVPlayer::Release()
{
Stop();
if (m_pVLC_Inst)
{
libvlc_release (m_pVLC_Inst);
m_pVLC_Inst = NULL;
}
}
bool CAVPlayer::Play(const std::string &strPath)
{
if (! m_pVLC_Inst)
{
Init();
}
if(strPath.empty() || ! m_pVLC_Inst)
{
return false;
}
Stop();
bool bRet = false;
if (m = libvlc_media_new_path(m_pVLC_Inst, strPath.c_str()))
{
if (m_pVLC_Player = libvlc_media_player_new_from_media(m))
{
libvlc_media_player_set_hwnd(m_pVLC_Player, m_hWnd);
if(isSendToNet){
CString strSend,strServer;
strSend.Format(":sout=#rtp{dst=%s,sdp=rtsp://%s:%s/stream}",m_strIp,m_strIp,m_strPort);
//strSend.Format(":sout=#rtp{dst=%s,sdp=rtsp://%s:%s/stream}",m_strIp,m_strIp,m_strPort);
strServer.Format(":rtsp-host=rtsp://%s:%s/stream",m_strIp,m_strPort);
const char *options[] = {
//":no-audio",
":video-title-position=4",
strSend.GetBuffer(0),
//":sout=#duplicate{dst=display,#rtp{dst=192.168.0.36,sdp=rtsp://192.168.0.36:11230/stream}",
//":sout=#duplicate{dst=display,dst=rtp{dst=192.168.0.36,sdp=rtsp://192.168.0.36:10086/stream}",
//vlc -vvv rtsp://192.1.101.51 --sout #rtp{dst=192.1.101.77,sdp=rtsp://192.1.101.77/live_vlc.sdp}
//strSend.GetBuffer(0),
":sout-udp-caching=1",
":sout-rtp-caching=1",
":sout-mux-caching=1",
":sout-ts-dts-delay=1024",
//":rtsp-host=rtsp://192.168.0.36:11230/stream"
strServer.GetBuffer(0),
":rtsp-throttle-users:500",
":rtsp-session-timeout:10"
};
for (int i = 0; i < sizeof(options) / sizeof(options[0]); i++)
libvlc_media_add_option (m, options[i]);
}
libvlc_media_player_play(m_pVLC_Player);
// 事件管理
libvlc_event_manager_t *vlc_evt_man = libvlc_media_player_event_manager(m_pVLC_Player);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnVLC_EndReached, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnVLC_PositionChanged, this);
bRet = true;
}
libvlc_media_release(m);
}
return bRet;
}
void CAVPlayer::Stop()
{
if (m_pVLC_Player)
{
libvlc_media_player_stop (m_pVLC_Player); /* Stop playing */
libvlc_media_player_release (m_pVLC_Player); /* Free the media_player */
m_pVLC_Player = NULL;
}
/* Stop the media */
//if (libvlc_media_player_get_state(vlcPlayer) == libvlc_Ended)
}
void CAVPlayer::Play()
{
if (m_pVLC_Player)
{
libvlc_media_player_play(m_pVLC_Player);
}
}
void CAVPlayer::Pause()
{
if (m_pVLC_Player)
{
libvlc_media_player_pause(m_pVLC_Player);
}
}
void CAVPlayer::Volume(int nVol)
{
if (m_pVLC_Player)
{
libvlc_audio_set_volume(m_pVLC_Player,nVol);
if(nVol == 0){
Pause();
}else if(nVol == 100){
Play();
}
}
}
void CAVPlayer::VolumeIncrease()
{
if (m_pVLC_Player)
{
int nVol = libvlc_audio_get_volume(m_pVLC_Player);
Volume((int)ceil(nVol * 1.1));
}
}
void CAVPlayer::VolumeReduce()
{
if (m_pVLC_Player)
{
int nVol = libvlc_audio_get_volume(m_pVLC_Player);
Volume((int)floor(nVol * 0.9));
}
}
int CAVPlayer::GetPos()
{
if (m_pVLC_Player)
{
return (int)(100 * libvlc_media_player_get_position(m_pVLC_Player));
}
return 0;
}
void CAVPlayer::SeekTo(int nPos)
{
if (m_pVLC_Player)
{
libvlc_media_player_set_position(m_pVLC_Player, nPos/(float)100.0);
}
}
void CAVPlayer::SeekForward()
{
int nPos = GetPos();
//SeekTo(ceil(nPos * 1.1));
SeekTo(nPos + 10);
}
void CAVPlayer::SeekBackward()
{
int nPos = GetPos();
//SeekTo(floor(nPos * 0.9));
SeekTo(nPos - 10);
}
void CAVPlayer::SetHWND( HWND hwnd )
{
if (::IsWindow(hwnd))
{
m_hWnd = hwnd;
}
}
HWND CAVPlayer::GetHWND()
{
return m_hWnd;
}
void CAVPlayer::SetCallback( pfnPosChanged pfn )
{
m_pfn = pfn;
}
pfnPosChanged CAVPlayer::GetCallback()
{
return m_pfn;
}
BOOL CAVPlayer::IsOpen()
{
return NULL != m_pVLC_Player;
}
BOOL CAVPlayer::IsPlaying()
{
if (m_pVLC_Player)
{
return libvlc_media_player_is_playing(m_pVLC_Player);
}
return FALSE;
}
__int64 CAVPlayer::GetTime()
{
if (m_pVLC_Player)
{
return libvlc_media_player_get_time(m_pVLC_Player);
}
return 0;
}
void OnVLC_EndReached( const libvlc_event_t *event, void *data )
{
}
void OnVLC_PositionChanged( const libvlc_event_t *event, void *data )
{
switch (event->type)
{
case libvlc_MediaPlayerPositionChanged:
{
float fPos = event->u.media_player_position_changed.new_position;
CAVPlayer *pAVPlayer = (CAVPlayer *) data;
if (pAVPlayer)
{
pfnPosChanged pfn = pAVPlayer->GetCallback();
if (pfn)
{
pfn(pAVPlayer, int(fPos * 100));
}
}
}
break;
case libvlc_MediaPlayerSnapshotTaken:
break;
default:
break;
}
}
int CAVPlayer::ScreenShot(CString strPath){
if (m_pVLC_Player)
{
int res=libvlc_video_take_snapshot(m_pVLC_Player,0,strPath.GetBuffer(0),300,200);
if(-1 == res){
AfxMessageBox("读取视频流错误!");
return -1;
}
}
return 0;
}
void CAVPlayer::VideoRecord(CString strPath){
if (m_pVLC_Player)
{
}
}
本文出自 “日知其所无” 博客,谢绝转载!