Update MediaClient

This commit is contained in:
2026-03-28 11:39:04 +11:00
parent 24dc6c7cd0
commit f3266566eb
1284 changed files with 462406 additions and 0 deletions

View File

@@ -0,0 +1,443 @@
/***************************************************************************************
*
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
*
* By downloading, copying, installing or using the software you agree to this license.
* If you do not agree to this license, do not download, install,
* copy or use the software.
*
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
*
* Redistribution and use in binary forms, with or without modification, are permitted.
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************************/
#include "sys_inc.h"
#include "rtsp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
/**********************************************************/
HQUEUE * g_queue;
int g_flag = 0;
pthread_t g_tid = 0;
/**********************************************************/
typedef struct
{
CRtspClient rtsp;
CVideoDecoder decoder;
BOOL decoder_init;
} MyPlayer;
typedef struct
{
int event;
MyPlayer * player;
} EVENT_PARAMS;
/**********************************************************/
void video_decoder_cb(AVFrame * frame, void * userdata)
{
MyPlayer * p_player = (MyPlayer *) userdata;
}
/**
* @desc : rtsp notify callback
*
* @params :
* event : event type
* puser : user parameter
*/
int rtsp_notify_cb(int event, void * puser)
{
printf("%s, event = %d\r\n", __FUNCTION__, event);
MyPlayer * p_player = (MyPlayer *) puser;
CRtspClient * p_rtsp = &p_player->rtsp;
if (RTSP_EVE_CONNSUCC == event)
{
int vcodec = p_rtsp->video_codec();
if (vcodec != VIDEO_CODEC_NONE)
{
char codec_str[20] = {'\0'};
switch (vcodec)
{
case VIDEO_CODEC_H264:
strcpy(codec_str, "H264");
break;
case VIDEO_CODEC_H265:
strcpy(codec_str, "H265");
break;
case VIDEO_CODEC_MP4:
strcpy(codec_str, "MP4");
break;
case VIDEO_CODEC_JPEG:
strcpy(codec_str, "JPEG");
break;
}
printf("video codec is %s\r\n", codec_str);
}
int acodec = p_rtsp->audio_codec();
if (acodec != AUDIO_CODEC_NONE)
{
char codec_str[20] = {'\0'};
switch (acodec)
{
case AUDIO_CODEC_G711A:
strcpy(codec_str, "G711A");
break;
case AUDIO_CODEC_G711U:
strcpy(codec_str, "G711U");
break;
case AUDIO_CODEC_G722:
strcpy(codec_str, "G722");
break;
case AUDIO_CODEC_G726:
strcpy(codec_str, "G726");
break;
case AUDIO_CODEC_OPUS:
strcpy(codec_str, "OPUS");
break;
case AUDIO_CODEC_AAC:
strcpy(codec_str, "AAC");
break;
}
printf("audio codec is %s\r\n", codec_str);
printf("audio sample rate is %d\r\n", p_rtsp->get_audio_samplerate());
printf("audio channels is %d\r\n", p_rtsp->get_audio_channels());
}
}
EVENT_PARAMS params;
params.event = event;
params.player = p_player;
if (!hqBufPut(g_queue, (char *) &params))
{
printf("hqBufPut failed\r\n");
}
return 0;
}
/**
* @desc : rtsp audio data callback
*
* @params :
* pdata : audio data buffer
* len : audio data buffer length
* ts : timestamp
* seq : sequential
* puser : user parameter
*/
int rtsp_audio_cb(uint8 * pdata, int len, uint32 ts, uint16 seq, void * puser)
{
MyPlayer * p_player = (MyPlayer *) puser;
printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
return 0;
}
/**
* @desc : rtsp video data callback
*
* @params :
* pdata : video data buffer
* len : video data buffer length
* ts : timestamp
* seq : sequential
* puser : user parameter
*/
int rtsp_video_cb(uint8 * pdata, int len, uint32 ts, uint16 seq, void * puser)
{
MyPlayer * p_player = (MyPlayer *) puser;
CRtspClient * p_rtsp = &p_player->rtsp;
CVideoDecoder * p_decoder = &p_player->decoder;
printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
if (!p_player->decoder_init)
{
p_player->decoder_init = p_decoder->init(p_rtsp->video_codec());
p_decoder->setCallback(video_decoder_cb, p_player);
}
if (p_player->decoder_init)
{
p_decoder->decode(pdata, len, ts);
}
return 0;
}
/**
* @desc : RTCP raw data callback
*
* @params :
* pdata : RTCP raw data
* len : data length
* type : AV_VIDEO_CH, AV_AUDIO_CH, AV_METADATA_CH, AV_BACK_CH
* puser : user parameter
*/
int rtsp_rtcp_cb(uint8 * pdata, int len, int type, void * puser)
{
MyPlayer * p_player = (MyPlayer *) puser;
printf("%s, len = %d, type = %d\r\n", __FUNCTION__, len, type);
return 0;
}
/**
* @desc : rtsp meta data callback
*
* @params :
* pdata : meta data buffer
* len : meta data buffer length
* ts : timestamp
* seq : sequential
* puser : user parameter
*/
int rtsp_metadata_cb(uint8 * pdata, int len, uint32 ts, uint16 seq, void * puser)
{
MyPlayer * p_player = (MyPlayer *) puser;
printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
return 0;
}
void rtsp_setup(MyPlayer * player)
{
CRtspClient * p_rtsp = &player->rtsp;
p_rtsp->set_notify_cb(rtsp_notify_cb, player);
p_rtsp->set_audio_cb(rtsp_audio_cb);
p_rtsp->set_video_cb(rtsp_video_cb);
p_rtsp->set_rtcp_cb(rtsp_rtcp_cb);
#ifdef METADATA
p_rtsp->set_metadata_cb(rtsp_metadata_cb);
#endif
// p_rtsp->set_rtp_over_udp(1); // rtp over udp
// p_rtsp->set_rtp_multicast(1); // force multicast rtp via rtsp
#ifdef BACKCHANNEL
// p_rtsp->set_bc_flag(1); // enable audio backchannel
// p_rtsp->set_bc_data_flag(1); // enable send audio data
#endif
#ifdef REPLAY
// p_rtsp->set_replay_flag(1); // enable replay
// p_rtsp->set_replay_range(time(NULL) - 3600, time(NULL)); // set the replay timestamp range
#endif
#ifdef OVER_HTTP
// p_rtsp->set_rtsp_over_http(1, 80); // rtsp over http
#endif
#ifdef OVER_WEBSOCKET
// p_rtsp->set_rtsp_over_ws(1, 80); // rtsp over websocket
#endif
p_rtsp->set_rx_timeout(10); // No data within 10s, receiving timeout
// p_rtsp->set_channel(AV_VIDEO_CH, 0); // not setup video channel
// p_rtsp->set_channel(AV_AUDIO_CH, 0); // not setup audio channel
// p_rtsp->set_channel(AV_METADATA_CH, 0); // not setup metadata channel
// p_rtsp->set_channel(AV_BACK_CH, 0); // not setup audio back channel
}
void rtsp_reconn(MyPlayer * p_player)
{
char url[256], user[64], pass[64];
CRtspClient * p_rtsp = &p_player->rtsp;
strcpy(url, p_rtsp->get_url());
strcpy(user, p_rtsp->get_user());
strcpy(pass, p_rtsp->get_pass());
printf("rtsp_reconn, url = %s, user = %s, pass = %s\r\n", url, user, pass);
p_rtsp->rtsp_close();
rtsp_setup(p_player);
p_rtsp->rtsp_start(url, user, pass);
}
void * rtsp_notify_handler(void * argv)
{
EVENT_PARAMS params;
while (g_flag)
{
if (hqBufGet(g_queue, (char *) &params))
{
if (params.event == -1 || params.player == NULL)
{
break;
}
if (RTSP_EVE_STOPPED == params.event ||
RTSP_EVE_CONNFAIL == params.event ||
RTSP_EVE_NOSIGNAL == params.event ||
RTSP_EVE_NODATA == params.event)
{
rtsp_reconn(params.player);
usleep(100*1000);
}
}
}
g_tid = 0;
printf("%s exit\r\n", __FUNCTION__);
return NULL;
}
#define RTSP_CLN_NUM 1
int main(int argc, char * argv[])
{
if (argc < 2)
{
printf("usage: %s url {user} {pass}\r\n", argv[0]);
return -1;
}
log_init("rtsptest.log");
log_set_level(HT_LOG_DBG);
network_init();
// allocate system BUFFER and rtsp parser BUFFER
sys_buf_init(RTSP_CLN_NUM * 8);
rtsp_parse_buf_init(RTSP_CLN_NUM * 8);
#if defined(OVER_HTTP) || defined(OVER_WEBSOCKET)
// allocate http message parser BUFFER
http_msg_buf_init(RTSP_CLN_NUM * 8);
#endif
// create event queue
g_queue = hqCreate(RTSP_CLN_NUM * 4, sizeof(EVENT_PARAMS), HQ_PUT_WAIT | HQ_GET_WAIT);
if (NULL == g_queue)
{
printf("create queue failed\r\n");
return -1;
}
// create event handler thread
g_flag = 1;
g_tid = sys_os_create_thread((void *)rtsp_notify_handler, NULL);
if (g_tid == 0)
{
printf("create rtsp notify handler thread failed\r\n");
return -1;
}
MyPlayer * player = new MyPlayer[RTSP_CLN_NUM];
for (int i = 0; i < RTSP_CLN_NUM; i++)
{
player->decoder_init = FALSE;
rtsp_setup(&player[i]);
char * p_user = NULL;
char * p_pass = NULL;
if (argc >= 3)
{
p_user = argv[2];
}
if (argc >= 4)
{
p_pass = argv[3];
}
BOOL ret = player[i].rtsp.rtsp_start(argv[1], p_user, p_pass);
printf("rtsp %d start ret = %d\r\n", i, ret);
usleep(100 * 1000);
}
for (;;)
{
if (getchar() == 'q')
{
break;
}
usleep(1000*1000); // 1s
}
for (int i = 0; i < RTSP_CLN_NUM; i++)
{
player[i].rtsp.rtsp_close();
}
delete[] player;
g_flag = 0;
EVENT_PARAMS params;
params.event = -1;
params.player = NULL;
hqBufPut(g_queue, (char *) &params);
// waiting for event handler thread to exit
while (g_tid)
{
usleep(10*1000);
}
hqDelete(g_queue);
g_queue = NULL;
// free memory resources
rtsp_parse_buf_deinit();
sys_buf_deinit();
#if defined(OVER_HTTP) || defined(OVER_WEBSOCKET)
http_msg_buf_deinit();
#endif
// close log
log_close();
return 0;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RtspTest", "RtspTest.vcxproj", "{79A3B043-27AD-491F-96D9-A4E0158ED10B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Debug|x64.ActiveCfg = Debug|x64
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Debug|x64.Build.0 = Debug|x64
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Debug|x86.ActiveCfg = Debug|Win32
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Debug|x86.Build.0 = Debug|Win32
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Release|x64.ActiveCfg = Release|x64
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Release|x64.Build.0 = Release|x64
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Release|x86.ActiveCfg = Release|Win32
{79A3B043-27AD-491F-96D9-A4E0158ED10B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5955BC95-5A87-4157-822A-C8554BF7374E}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,187 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\MediaClient\media\avcodec_mutex.cpp" />
<ClCompile Include="..\MediaClient\media\media_codec.cpp" />
<ClCompile Include="..\MediaClient\media\media_parse.cpp" />
<ClCompile Include="..\MediaClient\media\media_util.cpp" />
<ClCompile Include="..\MediaClient\media\video_decoder.cpp" />
<ClCompile Include="..\MediaClient\rtp\h264_util.cpp" />
<ClCompile Include="..\MediaClient\rtp\h265_util.cpp" />
<ClCompile Include="RtspTest.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{79A3B043-27AD-491F-96D9-A4E0158ED10B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>RtspTest</RootNamespace>
<ProjectName>RtspTest</ProjectName>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<TargetName>$(ProjectName)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<LibraryPath>C:\Projects\ANLS\MediaClient\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<LibraryPath>c;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Label="Vcpkg">
<VcpkgEnabled>false</VcpkgEnabled>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;HT_STATIC;METADATA;REPLAY;OVER_HTTP;OVER_WEBSOCKET;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\MediaClient;..\MediaClient\bm;..\MediaClient\http;..\MediaClient\rtsp;..\MediaClient\rtp;..\MediaClient\media;..\MediaClient\openssl\include;..\MediaClient\ffmpeg\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>..\MediaClient\openssl\lib\x86;..\MediaClient\ffmpeg\lib\x86;$(SolutionDir)$(Platform)\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>RtspClientLibrary.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;HT_STATIC;METADATA;REPLAY;OVER_HTTP;OVER_WEBSOCKET;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\MediaClient;..\MediaClient\bm;..\MediaClient\http;..\MediaClient\rtsp;..\MediaClient\rtp;..\MediaClient\media;..\MediaClient\openssl\include;..\MediaClient\ffmpeg\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>..\MediaClient\openssl\lib\x64;..\MediaClient\ffmpeg\lib\x64;$(SolutionDir)$(Platform)\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>RtspClientLibrary.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;HT_STATIC;METADATA;REPLAY;OVER_HTTP;OVER_WEBSOCKET;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\MediaClient;..\MediaClient\bm;..\MediaClient\http;..\MediaClient\rtsp;..\MediaClient\rtp;..\MediaClient\media;..\MediaClient\openssl\include;..\MediaClient\ffmpeg\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>..\MediaClient\openssl\lib\x86;..\MediaClient\ffmpeg\lib\x86;$(SolutionDir)$(Platform)\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>RtspClientLibrary.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;HT_STATIC;METADATA;REPLAY;OVER_HTTP;OVER_WEBSOCKET;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\MediaClient;..\MediaClient\bm;..\MediaClient\http;..\MediaClient\rtsp;..\MediaClient\rtp;..\MediaClient\media;..\MediaClient\openssl\include;..\MediaClient\ffmpeg\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>..\MediaClient\openssl\lib\x64;..\MediaClient\ffmpeg\lib\x64;$(SolutionDir)$(Platform)\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>RtspClientLibrary.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,428 @@
//typedef struct
//{
// CRtspClient rtsp;
// CVideoDecoder decoder;
// BOOL decoder_init;
//} ANSRTSPPlayer;
//typedef struct
//{
// int event;
// ANSRTSPPlayer* player;
//} EVENT_PARAMS;
//class ANSRTSP_API ANSRTSP
//{
//protected:
// ANSRTSPPlayer* _rtspClient;
// std::string _uri;
// std::string _username;
// std::string _password;
// static HQUEUE* g_queue;
// static int g_flag;
// static pthread_t g_tid;
// std::mutex mtx; // Mutex to protect frame conversion
// SwsContext* swsCtx = nullptr; // Shared SwsContext
// int lastWidth = 0, lastHeight = 0;
// AVPixelFormat lastPixFmt = AV_PIX_FMT_NONE;
//
// void initSwsContext(int width, int height, AVPixelFormat pixFmt);
// static int rtsp_notify_cb(int event, void* puser);
// static int rtsp_audio_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser);
// static int rtsp_video_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser);
// static int rtsp_rtcp_cb(uint8* pdata, int len, int type, void* puser);
// static int rtsp_metadata_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser);
// static void rtsp_setup(ANSRTSPPlayer* player);
// static void rtsp_reconn(ANSRTSPPlayer* player);
// static void* rtsp_notify_handler(void* argv);
// static void video_decoder_cb(AVFrame* frame, void* userdata);
// cv::Mat avframeToCvmat(const AVFrame* frame);
// void playVideo(AVFrame* frame);
//public:
// ANSRTSP();
// ~ANSRTSP();
// bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
// bool Start();
// bool Stop();
// void Destroy();
//
//public:
// bool _licenseValid{ false };
// std::string _licenseKey;
//};
//HQUEUE* ANSRTSP::g_queue = nullptr;
//int ANSRTSP::g_flag = 0;
//pthread_t ANSRTSP::g_tid = 0;
//
//cv::Mat ANSRTSP::avframeToCvmat(const AVFrame* frame) {
// std::lock_guard<std::mutex> lock(mtx); // Ensure thread safety
//
// if (!frame || !frame->data[0] || frame->width <= 0 || frame->height <= 0) {
// std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
// return cv::Mat();
// }
//
// // Initialize or reinitialize the context if needed
// initSwsContext(frame->width, frame->height, static_cast<AVPixelFormat>(frame->format));
//
// cv::Mat image(frame->height, frame->width, CV_8UC3);
// uint8_t* dst[1] = { image.data }; // Destination pointers
// int dstStride[1] = { static_cast<int>(image.step[0]) }; // Destination stride
//
// int result = sws_scale(swsCtx, frame->data, frame->linesize, 0, frame->height, dst, dstStride);
// if (result < 0) {
// std::cerr << "Failed to scale the frame." << std::endl;
// return cv::Mat(); // Return an empty matrix if scaling fails
// }
//
// return image;
//}
//void ANSRTSP::video_decoder_cb(AVFrame* frame, void* userdata)
//{
// ANSRTSP* p_player = (ANSRTSP*)userdata;
// p_player->playVideo(frame);
//}
//void ANSRTSP::playVideo(AVFrame* frame) {
// cv::Mat image = avframeToCvmat(frame);
// if (image.dims > 0) {
// cv::imshow("Image", image);
// if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
// {
// std::cout << "End .\n";
// }
// }
// // Update here or do whatever we need
//}
//int ANSRTSP::rtsp_notify_cb(int event, void* puser)
//{
// printf("%s, event = %d\r\n", __FUNCTION__, event);
//
// ANSRTSPPlayer* p_player = (ANSRTSPPlayer*)puser;
// CRtspClient* p_rtsp = &p_player->rtsp;
//
// if (RTSP_EVE_CONNSUCC == event)
// {
// int vcodec = p_rtsp->video_codec();
// if (vcodec != VIDEO_CODEC_NONE)
// {
// char codec_str[20] = { '\0' };
//
// switch (vcodec)
// {
// case VIDEO_CODEC_H264:
// strcpy_s(codec_str, "H264");
// break;
//
// case VIDEO_CODEC_H265:
// strcpy_s(codec_str, "H265");
// break;
//
// case VIDEO_CODEC_MP4:
// strcpy_s(codec_str, "MP4");
// break;
//
// case VIDEO_CODEC_JPEG:
// strcpy_s(codec_str, "JPEG");
// break;
// }
//
// printf("video codec is %s\r\n", codec_str);
// }
//
// int acodec = p_rtsp->audio_codec();
// if (acodec != AUDIO_CODEC_NONE)
// {
// char codec_str[20] = { '\0' };
//
// switch (acodec)
// {
// case AUDIO_CODEC_G711A:
// strcpy_s(codec_str, "G711A");
// break;
//
// case AUDIO_CODEC_G711U:
// strcpy_s(codec_str, "G711U");
// break;
//
// case AUDIO_CODEC_G722:
// strcpy_s(codec_str, "G722");
// break;
//
// case AUDIO_CODEC_G726:
// strcpy_s(codec_str, "G726");
// break;
//
// case AUDIO_CODEC_OPUS:
// strcpy_s(codec_str, "OPUS");
// break;
//
// case AUDIO_CODEC_AAC:
// strcpy_s(codec_str, "AAC");
// break;
// }
//
// printf("audio codec is %s\r\n", codec_str);
// printf("audio sample rate is %d\r\n", p_rtsp->get_audio_samplerate());
// printf("audio channels is %d\r\n", p_rtsp->get_audio_channels());
// }
// }
//
// EVENT_PARAMS params;
// params.event = event;
// params.player = p_player;
//
// if (!hqBufPut(g_queue, (char*)&params))
// {
// printf("hqBufPut failed\r\n");
// }
//
// return 0;
//}
//int ANSRTSP::rtsp_audio_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser)
//{
// ANSRTSPPlayer* p_player = (ANSRTSPPlayer*)puser;
// //printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
// return 0;
//}
//int ANSRTSP::rtsp_video_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser)
//{
// ANSRTSPPlayer* p_player = (ANSRTSPPlayer*)puser;
// CRtspClient* p_rtsp = &p_player->rtsp;
// CVideoDecoder* p_decoder = &p_player->decoder;
// //printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
// if (!p_player->decoder_init)
// {
// p_player->decoder_init = p_decoder->init(p_rtsp->video_codec());
// p_decoder->setCallback(video_decoder_cb, p_player);
// }
//
// if (p_player->decoder_init)
// {
// p_decoder->decode(pdata, len, ts);
// }
//
// return 0;
//}
//int ANSRTSP::rtsp_rtcp_cb(uint8* pdata, int len, int type, void* puser)
//{
// ANSRTSPPlayer* p_player = (ANSRTSPPlayer*)puser;
//
// printf("%s, len = %d, type = %d\r\n", __FUNCTION__, len, type);
//
// return 0;
//}
//int ANSRTSP::rtsp_metadata_cb(uint8* pdata, int len, uint32 ts, uint16 seq, void* puser)
//{
// ANSRTSPPlayer* p_player = (ANSRTSPPlayer*)puser;
// //printf("%s, len = %d, ts = %u, seq = %d\r\n", __FUNCTION__, len, ts, seq);
// return 0;
//}
//void ANSRTSP::rtsp_setup(ANSRTSPPlayer* player)
//{
// CRtspClient* p_rtsp = &player->rtsp;
//
// p_rtsp->set_notify_cb(rtsp_notify_cb, player);
// p_rtsp->set_audio_cb(rtsp_audio_cb);
// p_rtsp->set_video_cb(rtsp_video_cb);
// p_rtsp->set_rtcp_cb(rtsp_rtcp_cb);
//#ifdef METADATA
// p_rtsp->set_metadata_cb(rtsp_metadata_cb);
//#endif
// // p_rtsp->set_rtp_over_udp(1); // rtp over udp
// // p_rtsp->set_rtp_multicast(1); // force multicast rtp via rtsp
//#ifdef BACKCHANNEL
// // p_rtsp->set_bc_flag(1); // enable audio backchannel
// // p_rtsp->set_bc_data_flag(1); // enable send audio data
//#endif
//#ifdef REPLAY
// // p_rtsp->set_replay_flag(1); // enable replay
// // p_rtsp->set_replay_range(time(NULL) - 3600, time(NULL)); // set the replay timestamp range
//#endif
//#ifdef OVER_HTTP
// // p_rtsp->set_rtsp_over_http(1, 80); // rtsp over http
//#endif
//#ifdef OVER_WEBSOCKET
// // p_rtsp->set_rtsp_over_ws(1, 80); // rtsp over websocket
//#endif
//
// p_rtsp->set_rx_timeout(10); // No data within 10s, receiving timeout
// // p_rtsp->set_channel(AV_VIDEO_CH, 0); // not setup video channel
// // p_rtsp->set_channel(AV_AUDIO_CH, 0); // not setup audio channel
// // p_rtsp->set_channel(AV_METADATA_CH, 0); // not setup metadata channel
// // p_rtsp->set_channel(AV_BACK_CH, 0); // not setup audio back channel
//
//}
//void ANSRTSP::rtsp_reconn(ANSRTSPPlayer* player)
//{
// char url[256], user[64], pass[64];
// CRtspClient* p_rtsp = &player->rtsp;
//
// strcpy_s(url, p_rtsp->get_url());
// strcpy_s(user, p_rtsp->get_user());
// strcpy_s(pass, p_rtsp->get_pass());
//
// printf("rtsp_reconn, url = %s, user = %s, pass = %s\r\n", url, user, pass);
//
// p_rtsp->rtsp_close();
//
// rtsp_setup(player);
//
// p_rtsp->rtsp_start(url, user, pass);
//}
//void* ANSRTSP::rtsp_notify_handler(void* argv)
//{
// EVENT_PARAMS params;
//
// while (g_flag)
// {
// if (hqBufGet(g_queue, (char*)&params))
// {
// if (params.event == -1 || params.player == NULL)
// {
// break;
// }
//
// if (RTSP_EVE_STOPPED == params.event ||
// RTSP_EVE_CONNFAIL == params.event ||
// RTSP_EVE_NOSIGNAL == params.event ||
// RTSP_EVE_NODATA == params.event)
// {
// rtsp_reconn(params.player);
// usleep(100 * 1000);
// }
// }
// }
//
// g_tid = 0;
//
// printf("%s exit\r\n", __FUNCTION__);
//
// return NULL;
//}
//
//ANSRTSP::ANSRTSP() {
// g_queue = nullptr;
// g_flag = 0;
// g_tid = 0;
//}
//ANSRTSP::~ANSRTSP() {
// if (swsCtx != nullptr) {
// sws_freeContext(swsCtx);
// swsCtx = nullptr;
// }
// Destroy();
//}
//void ANSRTSP::initSwsContext(int width, int height, AVPixelFormat pixFmt) {
// if (swsCtx != nullptr && (width != lastWidth || height != lastHeight || pixFmt != lastPixFmt)) {
// sws_freeContext(swsCtx);
// swsCtx = nullptr;
// }
//
// if (swsCtx == nullptr) {
// swsCtx = sws_getContext(width, height, pixFmt, width, height, AV_PIX_FMT_BGR24,
// SWS_LANCZOS | SWS_ACCURATE_RND, nullptr, nullptr, nullptr);
// if (swsCtx == nullptr) {
// throw std::runtime_error("Failed to create SwsContext");
// }
// lastWidth = width;
// lastHeight = height;
// lastPixFmt = pixFmt;
// }
//}
//bool ANSRTSP::Init(std::string licenseKey, std::string username, std::string password, std::string url) {
// log_init("rtsptest.log");
// log_set_level(HT_LOG_DBG);
// network_init();
// // allocate system BUFFER and rtsp parser BUFFER
// sys_buf_init(200);
// rtsp_parse_buf_init(200);
//
//#if defined(OVER_HTTP) || defined(OVER_WEBSOCKET)
// // allocate http message parser BUFFER
// http_msg_buf_init(200);
//#endif
//
// // create event queue
// g_queue = hqCreate(1 * 4, sizeof(EVENT_PARAMS), HQ_PUT_WAIT | HQ_GET_WAIT);
// if (NULL == g_queue)
// {
// printf("create queue failed\r\n");
// return false;
// }
//
// // create event handler thread
// g_flag = 1;
// g_tid = sys_os_create_thread((void*)rtsp_notify_handler, NULL);
// if (g_tid == 0)
// {
// printf("create rtsp notify handler thread failed\r\n");
// return false;
// }
//
// _rtspClient = new ANSRTSPPlayer[1];
// rtsp_setup(&_rtspClient[0]); // Setup Client
// _uri = url;
// _username = username;
// _password = password;
// return true;
//}
//bool ANSRTSP::Start() {
// BOOL ret = _rtspClient[0].rtsp.rtsp_start(_uri.c_str(), _username.c_str(), _password.c_str());// ("rtsp://118.70.125.152:5656/cam/realmonitor?channel=1&subtype=0", "root", "abcd1234");
// //printf("rtsp %d start ret = %d\r\n", i, ret);
// usleep(100 * 1000);
// return ret;
//}
//bool ANSRTSP::Stop() {
// return _rtspClient[0].rtsp.rtsp_close();
//}
//void ANSRTSP::Destroy() {
// g_flag = 0;
//
// EVENT_PARAMS params;
//
// params.event = -1;
// params.player = NULL;
//
// hqBufPut(g_queue, (char*)&params);
//
// // waiting for event handler thread to exit
// while (g_tid)
// {
// usleep(10 * 1000);
// }
//
// hqDelete(g_queue);
// g_queue = NULL;
//
// // free memory resources
// rtsp_parse_buf_deinit();
// sys_buf_deinit();
//
//#if defined(OVER_HTTP) || defined(OVER_WEBSOCKET)
// http_msg_buf_deinit();
//#endif
//
// // close log
// log_close();
//
//}
//int RSTPTest() {
// ANSRTSP rtspClient;
// //rtsp://anscamuser:SonTay2020@anssontay.tplinkdns.com:554/Streaming/Channels/401
// //rtspClient.Init("","root", "abcd1234", "rtsp://118.70.125.152:5656/cam/realmonitor?channel=1&subtype=0");
// rtspClient.Init("","anscamuser", "SonTay2020", "rtsp://anssontay.tplinkdns.com:554/Streaming/Channels/401");
//
// rtspClient.Start();
// for (;;)
// {
// if (getchar() == 'q')
// {
// break;
// }
// std::cout << "Waiting ..." << std::endl;
//
// usleep(1000 * 1000); // 1s
// }
// rtspClient.Stop();
// rtspClient.Destroy();
// return 0;
//
//}