429 lines
13 KiB
C
429 lines
13 KiB
C
|
|
//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*)¶ms))
|
||
|
|
// {
|
||
|
|
// 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*)¶ms))
|
||
|
|
// {
|
||
|
|
// 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*)¶ms);
|
||
|
|
//
|
||
|
|
// // 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;
|
||
|
|
//
|
||
|
|
//}
|