快速用VLC的SDK开发RTSP流教程

http://www.paobuke.com/develop/c/pbk1090.html


使用VLC的SDK开发播放器,不管是播放文件还是RTSP流都是相当简单的事,只需要调用几个简单的函数就可以实现播放了,甚至你连窗口都可以不建,SDK会自动创建窗口。

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "stdafx.h"
#include
#include
#include "include/rmrtspcltsdk.h"
#include "pubdef.h"
using namespace std;
#include
#include
#pragma comment(lib, "libvlc.lib")
#pragma comment(lib, "libvlccore.lib")
libvlc_instance_t * g_vlc_ins = 0;
namespace RM{
     namespace RTSPCLTSDK
     {
         class   RtspClientState
         {
         public :
             RtspClientState()
             {
                 pEndCB      = 0;
                 vlc_ins     = g_vlc_ins;
                 vlc_player  = 0;
                 vlc_media   = 0;
                 m_bOpen     = false ;
                 m_bPlaying  = false ;
                 m_hWnd      = 0;
             }
             ~RtspClientState(){
                 ;
             }
             bool    ReInit(CRtspClient* p);
             bool    m_bOpen;
             bool    m_bPlaying;
             string  m_strURL;
             bool    m_bAutoPlay;
             bool    m_bEnableSound;
             libvlc_instance_t *     vlc_ins    ; //= NULL;
             libvlc_media_player_t * vlc_player ; //= NULL;
             libvlc_media_t *        vlc_media  ; //= NULL;
             CRtspClient::PLAYEND_CB         pEndCB;
             void *               pEndData;
             HWND    m_hWnd;
         };
         CRtspClient::CRtspClient()
         {
             _pState = new RtspClientState();
         }
         CRtspClient::~CRtspClient(){
             Stop();
             delete _pState;
         }
         CRtspClient*    CRtspClient::createInstance()
         {
             return new CRtspClient();
         }
         void    CRtspClient::freeInstance(CRtspClient* p)
         {
             if (p){
                 delete p;
             }
         }
         bool CRtspClient::Play()
         {
             if (_pState->m_bPlaying) return true ;
             if ( !_pState->vlc_player || !_pState->vlc_media )
             {
                 if (!_pState->ReInit( this )) return false ;
             }
             {
                 libvlc_media_player_set_hwnd(_pState->vlc_player,( void *) _pState->m_hWnd);
                 libvlc_media_player_play(_pState->vlc_player);
                 _pState->m_bPlaying = true ;
                 return true ;
             }
             return false ;
         }
         bool CRtspClient::Stop()
         {
             _pState->m_bPlaying = false ;
             if ( _pState->vlc_player )
                 libvlc_media_player_stop( _pState->vlc_player);
             if (_pState->vlc_media)
                 libvlc_media_release(_pState->vlc_media);
             if (_pState->vlc_player)
                 libvlc_media_player_release(_pState->vlc_player);
             _pState->vlc_media = 0;
             _pState->vlc_player = 0;
             _pState->m_bOpen = false ;
             return true ;
         }
         bool CRtspClient::Pause( bool    bResume)
         {
             if ( _pState->vlc_player )
             {
                 if (bResume)
                 {
                     libvlc_media_player_pause( _pState->vlc_player);
                     _pState->m_bPlaying = false ;
                 } else {
                     libvlc_media_player_play( _pState->vlc_player);
                     _pState->m_bPlaying = true ;
                 }
                 return true ;
             }
             return false ;
         }
         bool CRtspClient::Seek(unsigned long ul)
         {
             if ( _pState->vlc_player )
             {
                 ::libvlc_media_player_set_time( _pState->vlc_player, ul );
                 return true ;
             }
             return false ;
         }
         unsigned long CRtspClient::GetPlayedTime() const
         {
             if (_pState->vlc_player)
             {
                 libvlc_time_t t =   ::libvlc_media_player_get_time(_pState->vlc_player);
                 if ( t == -1 )
                 {
                     printf ( "No media now -- GetPlayedTimed\n" );
                     return -1;
                 } else {
                     return t;
                 }
             }
             return 0;
         }
         unsigned long CRtspClient::GetTotalTime() const
         {
             if (_pState->vlc_media)
                 return (unsigned long )libvlc_media_get_duration(_pState->vlc_media);
             return 0;
         }
         bool    RtspClientState::ReInit(CRtspClient* p )
         {
             if (!vlc_player)
                 vlc_player = libvlc_media_player_new(vlc_ins);
             if (!vlc_media)
                 vlc_media  = libvlc_media_new_path(vlc_ins, this ->m_strURL.c_str());
             if ( !vlc_media || !vlc_player ) {
                 ::OutputDebugStringA( "init player or media failed!\n" );
                 return false ;
             }
             //libvlc_media_parse(vlc_media);
             libvlc_media_parse_async( vlc_media);
             libvlc_media_player_set_media(vlc_player, vlc_media);
             return true ;
         }
         bool    CRtspClient::Init( unsigned long hWnd, const char * RtspUrl , bool enableSound, bool autoPlay)
         {
             if (!RtspUrl) return false ;
             if (_pState->m_bOpen) Stop();
             _pState->m_hWnd = ( HWND )hWnd;
             _pState->m_strURL = RtspUrl;
             _pState->m_bAutoPlay = autoPlay;
             _pState->m_bEnableSound = enableSound;
             if (!_pState->ReInit( this )) return false ;
             _pState->m_bOpen = true ;
             if (autoPlay) return Play();
             return true ;
         }
         void    CRtspClient::SetNewHwnd( unsigned long hWnd)
         {
             if (_pState->vlc_player)
                 libvlc_media_player_set_hwnd(_pState->vlc_player,( void *) hWnd);
         }
         void    CRtspClient::Refersh()
         {
         }
         void    CRtspClient::SetEndCB( PLAYEND_CB   cb , void * pData)
         {
             _pState->pEndCB = cb;
             _pState->pEndData = pData;
         }
     }
}
const char * vlc_args[] =
{
     "-I" ,
     "dummy" ,
     "--ignore-config" ,
     "--extraintf=logger" ,
     "--verbose=2" ,
};
void DllEntry( HINSTANCE dll)
{
     g_vlc_ins = libvlc_new( sizeof (vlc_args)/ sizeof (vlc_args[0]), vlc_args );
}
void DllExit()
{
     ::libvlc_release(g_vlc_ins);
}

你需要准备的是安装VLC,可以到官网上下载最新版本,直接安装之后在程序目录有一个APP/SDK/里面的include,lib复制出来,另外 有一个插件目录plugins,也要在发布程序时复制到程序根目录就可以了

vlc-sdk-file需要的文件

通过这个代码片断你应该了解如何调用VLC的SDK来实现简单的播放器开发了。


你可能感兴趣的:(开源软件)