网上有很多RTSP的客户端接口,基本上是根据live55实现的,本篇分享的也是基于live555的封装和实现,经过实际的项目验证,稳定可靠。可以直接连接海康大华的网络相机,获取视频和音频数据。
本文章主要分享封装的接口,以及编译好的库,不分享具体的代码,有需要的可以发邮件512146329@qq.com联系
编译库的下载地址:(62条消息) RTSP客户端接收库(支持Linux、Windows)-C文档类资源-CSDN文库
接口定义:#ifndef __Devin_RTSP_CLIENT_API_H__
#define __Devin_RTSP_CLIENT_API_H__
#ifdef _WIN32
#define Devin_API __declspec(dllexport)
#define Devin_APICALL __stdcall
#define WIN32_LEAN_AND_MEAN
#else
#define Devin_API
#define Devin_APICALL
#endif
enum
{
Devin_ConnectSuccess = 1,
Devin_PlaySuccess = 2,
Devin_NoErr = 0,
Devin_GetDescriptionFailed = -1,
Devin_CreateMediaSessionFailed = -2,
Devin_SessionNoMediaSubsessions = -3,
Devin_SetupFailed = -4,
Devin_CreateMediaSinkFailed = -5,
Devin_PlayFailed = -6,
Devin_PARAMERROR = -7,
Devin_ConnectFailed = -8,
Devin_CreateRTSPClientFailed = -9,
Devin_ConnectTimeout = -10,
Devin_PlayTimeout = -11,
Devin_MallocError = -12,
Devin_ChannelFull = -13,
Devin_FindHandleFailed = -14,
Devin_TeardownSuecss = -15,
Devin_TeardownFailed = -16
};
typedef int Devin_Error;
typedef struct Devin_Handle{
void *param;
}Devin_Handle, *PDevin_Handle;
//typedef unsigned int Devin_Handle;
#define DEVIN_VIDEO_CODEC_H264 0x1C
#define DEVIN_VIDEO_CODEC_H265 0xAE
#define DEVIN_VIDEO_CODEC_MJPEG 0x08
#define DEVIN_VIDEO_CODEC_MPEG4 0x0D
#define DEVIN_AUDIO_CODEC_AAC 0x15002
#define DEVIN_AUDIO_CODEC_G711U 0x10006
#define DEVIN_AUDIO_CODEC_G711A 0x10007
#define DEVIN_AUDIO_CODEC_G726 0x1100B
#define DEVIN_EVENT_CODEC_ERROR 0x63657272
#define DEVIN_EVENT_CODEC_EXIT 0x65786974
#define DEVIN_VIDEO_FRAME_FLAG 0x00000001
#define DEVIN_AUDIO_FRAME_FLAG 0x00000002
#if 0
#define DEVIN_EVENT_FRAME_FLAG 0x00000004
#define DEVIN_RTP_FRAME_FLAG 0x00000008
#define DEVIN_SDP_FRAME_FLAG 0x00000010
#define DEVIN_MEDIA_INFO_FLAG 0x00000020
#define DEVIN_SNAP_FRAME_FLAG 0x00000040
#endif
#define DEVIN_VIDEO_FRAME_I 0x01
#define DEVIN_VIDEO_FRAME_P 0x02
#define DEVIN_VIDEO_FRAME_B 0x03
#define DEVIN_VIDEO_FRAME_J 0x04
#define DEVIN_VIDEO_FRAME_SPS 0x05
#define DEVIN_VIDEO_FRAME_PPS 0x06
#define DEVIN_VIDEO_FRAME_SEI 0x07
typedef enum __DEVIN_RTP_CONNECT_TYPE
{
DEVIN_RTP_OVER_TCP = 0x01,
DEVIN_RTP_OVER_UDP,
DEVIN_RTP_OVER_MULTICAST
}DEVIN_RTP_CONNECT_TYPE;
typedef struct __DEVIN_AV_Frame
{
unsigned int u32AVFrameFlag;
unsigned int u32AVFrameLen;
unsigned int u32VFrameType;
unsigned char *pBuffer;
unsigned int u32TimestampSec;
unsigned int u32TimestampUsec;
} DEVIN_AV_Frame;
typedef struct
{
unsigned int codec;
unsigned int type;
unsigned char fps;
unsigned short width;
unsigned short height;
unsigned int reserved1;
unsigned int reserved2;
unsigned int sample_rate;
unsigned int channels;
unsigned int bits_per_sample;
unsigned int length;
unsigned int timestamp_usec;
unsigned int timestamp_sec;
float bitrate;
float losspacket;
}DEVIN_FRAME_INFO;
typedef int (Devin_APICALL *RTSPSourceCallBack)(PDevin_Handle _handle, int _frameType, char *pBuf, DEVIN_FRAME_INFO* _frameInfo, void *userPtr);
#ifdef __cplusplus
extern "C"
{
#endif
Devin_API int Devin_APICALL DevinRTSP_OpenStream(PDevin_Handle *handle, char *_url, RTSPSourceCallBack _callback, DEVIN_RTP_CONNECT_TYPE _connType, unsigned int _mediaType, void *userPtr, int heartbeatType, int _verbosity);
Devin_API int Devin_APICALL EasyRTSP_CloseStream(PDevin_Handle handle);
#ifdef __cplusplus
}
#endif
#endif
使用例程:
// DevinRtspClient-test.cpp : �������̨Ӧ�ó������ڵ㡣 // #ifdef _WIN32 #include "stdafx.h" #include#ifdef _DEBUG #pragma comment (lib, "..\Debug\libDevinRtspClient.lib") #else #pragma comment (lib, "..\Release\libDevinRtspClient.lib") #endif #endif #include #include #include #include //#include #include "RTSPClient.h" FILE* fVideo = NULL; int fTransType = 1; //0 : TCP 1 : UDP bool fSaveFile = true; //true : save file false : don't save int Devin_APICALL _RTSPSourceCallBack(Devin_Handle* _handle, int _frameType, char *_pBuf, DEVIN_FRAME_INFO* _frameInfo, void *userPtr) { #if 1 if (_frameType == DEVIN_VIDEO_FRAME_FLAG) { if (_frameInfo->codec == DEVIN_VIDEO_CODEC_H264) { #if 1 if(fSaveFile) { if(fVideo == NULL) { char filename[100] = {0}; sprintf(filename, "./video_%s.H264", fTransType?"udp":"tcp"); fVideo = ::fopen(filename,"wb"); } ::fwrite(_pBuf, 1, _frameInfo->length, fVideo); } #endif if (_frameInfo->type == DEVIN_VIDEO_FRAME_I) { char sps[2048] = { 0 }; char pps[2048] = { 0 }; char* IFrame = NULL; unsigned int spsLen,ppsLen,iFrameLen = 0; spsLen = _frameInfo->reserved1; // SPS ppsLen = _frameInfo->reserved2 - _frameInfo->reserved1; // PPS iFrameLen = _frameInfo->length - spsLen - ppsLen; // IDR memcpy(sps, _pBuf, spsLen); //SPS memcpy(pps, _pBuf+spsLen, ppsLen); //PPS IFrame = _pBuf + spsLen + ppsLen; //IDR printf("Get I H264 video/H264, SPS/PPS/IDR Len:%d/%d/%d, fps:%d, ttimestamp:%u.%un", spsLen, ppsLen, iFrameLen, _frameInfo->fps, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec); }else if (_frameInfo->type == DEVIN_VIDEO_FRAME_P){ printf("Get P H264 video/H264, len:%06d, ttimestamp:%u.%06un", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec); } }else if (_frameInfo->codec == DEVIN_VIDEO_CODEC_MJPEG){ printf("video/MJPEG, len:%04d, ttimestamp:%u.%un", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec); } }else if(_frameType == DEVIN_AUDIO_FRAME_FLAG){ if (_frameInfo->codec == DEVIN_AUDIO_CODEC_AAC) { printf("Get audio/AAC , len:%06d, ttimestamp:%u.%06un", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec); } } #endif return 0; } int main(int argc, char* argv[]) { PDevin_Handle handle = {NULL}; //unsigned int mediaType = DEVIN_VIDEO_FRAME_FLAG | DEVIN_AUDIO_FRAME_FLAG; unsigned int mediaType = DEVIN_VIDEO_FRAME_FLAG; int ret = DevinRTSP_OpenStream(&handle, (char *)"rtsp://admin:admin@192.168.1.67:554/snl/live/1/1", _RTSPSourceCallBack, DEVIN_RTP_OVER_TCP, mediaType, NULL, 0x00, 0); while(1) usleep(10000); EasyRTSP_CloseStream(handle); }



