資源簡(jiǎn)介
此代碼封裝了H264編解碼和RTSP傳輸協(xié)議庫(kù),經(jīng)過(guò)測(cè)試,此庫(kù)可以直接調(diào)用,從本地的攝像頭采集圖像,經(jīng)過(guò)H264編碼、在本地計(jì)算機(jī)上建立RTSP Server,網(wǎng)絡(luò)內(nèi)的其它計(jì)算機(jī)可以通過(guò)RTSP Client進(jìn)行訪問(wèn)拉流、也可以通過(guò)VLC軟件進(jìn)行直接訪問(wèn)。
代碼片段和文件信息
//?RtspService.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?“RtspService.h“
#include?“MediaStreamH264.h“
#include?“MediaStreamMP4ALatm.h“
#ifdef?__cplusplus
extern?“C“?{
#endif
#include?“l(fā)ibavcodec/avcodec.h“
#include?“l(fā)ibavformat/avformat.h“
#include?“l(fā)ibswscale/swscale.h“
#include?“l(fā)ibavutil/avutil.h“
#include?“l(fā)ibavutil/opt.h“
#include?“l(fā)ibavutil/frame.h“
#ifdef?__cplusplus
}
#endif
RtspService_t*?g_pRtspService?=?NULL;
AVCodecContext?*c=?NULL;
SwsContext?*?scxt?=?NULL;
CRITICAL_SECTION?crtsection;
char?g_pBmpData[1024*1024*3];
int??g_nBmpSize?=?0;
int?g_nWidth?g_nHeight;
bool?g_exitcodecthread?=?false;
void?H264CodecThread(LPVOID?lpParam);
//1.
int?StreamServerInit(int?width?int?height)
{
InitializeCriticalSection(&crtsection);
g_nWidth?=?width;
g_nHeight?=?height;
av_register_all();
avcodec_register_all();
AVCodec?*pCodecH264;?//編碼器
//查找h264編碼器
pCodecH264?=?avcodec_find_encoder(AV_CODEC_ID_H264);
if(!pCodecH264)
{
fprintf(stderr?“h264?codec?not?found\n“);
return?0;
}
c=?avcodec_alloc_context3(pCodecH264);
//?c->bit_rate?=?3000000;//?put?sample?parameters?
c->bit_rate?=?400000;
c->rc_max_rate?=?400000;
c->rc_min_rate?=?400000;
c->rc_buffer_size?=?200000;
c->width?=g_nWidth;
c->height?=?g_nHeight;?
//?frames?per?second?
AVRational?rate;
rate.num?=?1;
rate.den?=?25;????//幀率
c->time_base=?rate;//(AVRational){125};
c->gop_size?=?250;?//?emit?one?intra?frame?every?ten?frames?
c->max_b_frames=0;
c->thread_count?=?1;
c->pix_fmt?=?AV_PIX_FMT_YUV420P;//PIX_FMT_RGB24;
av_opt_set(c->priv_data?“preset“?“superfast“?0);
av_opt_set(c->priv_data?“tune“?“zerolatency“?0);
//打開(kāi)編碼器
if(avcodec_open2(cpCodecH264NULL)<0)
{
fprintf(stderr?“不能打開(kāi)編碼庫(kù)\n“);
return?-1;
}
//初始化SwsContext
scxt?=?sws_getContext(g_nWidthg_nHeightAV_PIX_FMT_GRAY8g_nWidthg_nHeightAV_PIX_FMT_YUV420PSWS_POINTNULLNULLNULL);
if?(NULL?==?scxt)
{
return?-2;
}
g_exitcodecthread?=?false;
DWORD?ThreadId;
HANDLE?handle?=?CreateThread(NULL?NULL?(LPTHREAD_START_ROUTINE)H264CodecThread?NULL?NULL?&ThreadId);
return?0;
}
//2-2
void?H264CodecThread(LPVOID?lpParam)
{
????AVframe?*m_pRGBframe?=??new?AVframe[1];??//RGB幀數(shù)據(jù)??
????AVframe?*m_pYUVframe?=?new?AVframe[1];??//YUV幀數(shù)據(jù)s
????m_pYUVframe->pts?=?0;
????AVCodecContext?*in_c=?NULL;
????uint8_t?*?yuv_buff;
????int?size?=?g_nWidth?*?g_nHeight;
????yuv_buff?=?(uint8_t?*)?malloc((size?*?3)?/?2);?//?size?for?YUV?420?
????//將rgb圖像數(shù)據(jù)填充rgb幀
????char?*?rgb_buff?=?new?char[1024?*?1024?*?3];
????//圖象編碼
????int?outbuf_size=2000000;
????uint8_t?*?outbuf=?(uint8_t*)malloc(outbuf_size);?
????int?u_size?=?0;
????AVPacket?avpkt;
????while(true)
????{
EnterCriticalSection(&crtsection);
if(g_exitcodecthread)
{????
LeaveCriticalSection(&crtsection);
break;
}
if?(g_nBmpSize?>?0)
{
memcpy(rgb_buffg_pBmpData?g_n
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1034??2017-09-11?11:45??VideoStreamServerDLL(備份)\clear.bat
?????文件???11362830??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\avcodec-57.dll
?????文件????2232846??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\avformat-57.dll
?????文件?????482318??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\avutil-55.dll
?????文件?????396288??2017-11-10?14:52??VideoStreamServerDLL(備份)\Debug\D3DRender.dll
?????文件?????122880??2017-11-10?14:52??VideoStreamServerDLL(備份)\Debug\libfaac.dll
?????文件?????112142??2017-09-11?11:58??VideoStreamServerDLL(備份)\Debug\libgcc_s_dw2-1.dll
?????文件????1408750??2017-09-11?11:58??VideoStreamServerDLL(備份)\Debug\libiconv-2.dll
?????文件?????143296??2017-11-10?14:52??VideoStreamServerDLL(備份)\Debug\libvlc.dll
?????文件????2631616??2017-11-10?14:52??VideoStreamServerDLL(備份)\Debug\libvlccore.dll
?????文件????1030120??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\libx264-148.dll
?????文件????1460483??2017-11-10?14:52??VideoStreamServerDLL(備份)\Debug\libx264.dll
?????文件?????116750??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\swresample-2.dll
?????文件?????525838??2017-09-11?11:46??VideoStreamServerDLL(備份)\Debug\swscale-4.dll
?????文件?????142552??2017-09-11?11:58??VideoStreamServerDLL(備份)\Debug\tbb_Debug.dll
?????文件?????332800??2017-11-13?18:09??VideoStreamServerDLL(備份)\Debug\VideoStream.dll
?????文件????????938??2017-11-13?11:34??VideoStreamServerDLL(備份)\Debug\VideoStream.exp
?????文件????1304656??2017-11-13?11:34??VideoStreamServerDLL(備份)\Debug\VideoStream.ilk
?????文件???????2280??2017-11-13?11:34??VideoStreamServerDLL(備份)\Debug\VideoStream.lib
?????文件????2124800??2017-11-13?11:34??VideoStreamServerDLL(備份)\Debug\VideoStream.pdb
?????文件??????87552??2017-11-13?11:46??VideoStreamServerDLL(備份)\Release\VideoStream.dll
?????文件????????968??2017-11-13?11:46??VideoStreamServerDLL(備份)\Release\VideoStream.exp
?????文件???????2280??2017-11-13?11:46??VideoStreamServerDLL(備份)\Release\VideoStream.lib
?????文件????1264640??2017-11-13?11:46??VideoStreamServerDLL(備份)\Release\VideoStream.pdb
?????文件???????1660??2017-09-11?11:45??VideoStreamServerDLL(備份)\sunday\aac.h
?????文件???????8352??2017-11-13?11:32??VideoStreamServerDLL(備份)\sunday\Debug\Bitstream.obj
?????文件??????20446??2017-11-13?11:34??VideoStreamServerDLL(備份)\sunday\Debug\cl.command.1.tlog
?????文件?????285986??2017-11-13?11:34??VideoStreamServerDLL(備份)\sunday\Debug\CL.read.1.tlog
?????文件??????11296??2017-11-13?11:34??VideoStreamServerDLL(備份)\sunday\Debug\CL.write.1.tlog
?????文件??????????2??2017-11-13?11:34??VideoStreamServerDLL(備份)\sunday\Debug\li
............此處省略300個(gè)文件信息
評(píng)論
共有 條評(píng)論