Fix double stop in ANSVideoPlayer

This commit is contained in:
2026-04-22 10:10:16 +10:00
parent 97d814936d
commit 57cc8e0a56
14 changed files with 492 additions and 70 deletions

View File

@@ -48,40 +48,6 @@ cv::Mat JpegStringToMat(const std::string& jpegStr) {
return emptyImage;
}
}
int VideoTestClient() {
int width = 0;
int height = 0;
int64_t pts = 0;
ANSVIDEOPLAYER* filePlayerClient;
std::string testVideoFile = "C:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\SimFire.mp4";
CreateANSVideoPlayerHandle(&filePlayerClient, "", testVideoFile.c_str());
StartVideoPlayer(&filePlayerClient);
cv::namedWindow("Image", cv::WINDOW_NORMAL); // Create a resizable window.
cv::resizeWindow("Image", 1920, 1080); // Set initial size of the window.
std::string jpegImage;
int index = 0;
while (true) {
index++;
GetVideoPlayerStrImage(&filePlayerClient, width, height, pts, jpegImage);
if (jpegImage.empty()) {
sleep(1);
continue; // Skip the rest of the loop if image is empty
}
cv::Mat image = JpegStringToMat(jpegImage);
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(width, height));
cv::imshow("Image", resizedImage); // Show the resized image inside the window.
if (cv::waitKey(30) == 27) {
break;
}
}
cv::destroyAllWindows(); // Destroy all OpenCV windows
StopVideoPlayer(&filePlayerClient);
ReleaseANSVideoPlayerHandle(&filePlayerClient);
return 0;
}
int FilePlayerTestClient() {
int width = 0;
@@ -1473,6 +1439,203 @@ int OpenCVFunctionTest() {
}
int VideoTestClient() {
int width = 0;
int height = 0;
int64_t pts = 0;
ANSVIDEOPLAYER* filePlayerClient;
std::string testVideoFile = "C:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\SimFire.mp4";
CreateANSVideoPlayerHandle(&filePlayerClient, "", testVideoFile.c_str());
StartVideoPlayer(&filePlayerClient);
cv::namedWindow("Image", cv::WINDOW_NORMAL); // Create a resizable window.
cv::resizeWindow("Image", 1920, 1080); // Set initial size of the window.
std::string jpegImage;
int index = 0;
while (true) {
index++;
GetVideoPlayerStrImage(&filePlayerClient, width, height, pts, jpegImage);
if (jpegImage.empty()) {
sleep(1);
continue; // Skip the rest of the loop if image is empty
}
cv::Mat image = JpegStringToMat(jpegImage);
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(width, height));
cv::imshow("Image", resizedImage); // Show the resized image inside the window.
if (cv::waitKey(30) == 27) {
break;
}
}
cv::destroyAllWindows(); // Destroy all OpenCV windows
StopVideoPlayer(&filePlayerClient);
ReleaseANSVideoPlayerHandle(&filePlayerClient);
return 0;
}
int VideoPlayerClientTest() {
int width = 0;
int height = 0;
int64_t pts = 0;
ANSVIDEOPLAYER* videoClient;
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
CreateANSVideoPlayerHandle(&videoClient, "", testVideoFile.c_str());
StartVideoPlayer(&videoClient);
cv::namedWindow("Image", cv::WINDOW_NORMAL); // Create a resizable window.
cv::resizeWindow("Image", 1920, 1080); // Set initial size of the window (landscape).
int index = 0;
while (true) {
index++;
std::cout << "Index=" << index << std::endl;
if ((index == 200) || (index == 800) || (index == 1200)) { StopVideoPlayer(&videoClient); }
if ((index == 400) || (index == 1000) || (index == 1500)) { StartVideoPlayer(&videoClient); }
if ((index == 1800) || (index == 2200) || (index == 2500)) { StopVideoPlayer(&videoClient); }
if ((index == 2000) || (index == 2300) || (index == 2700)) { StartVideoPlayer(&videoClient); }
if (index > 20000) break;
auto start = std::chrono::system_clock::now();
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
GetVideoPlayerCVImage(&videoClient, width, height, pts,&image);
auto end1 = std::chrono::system_clock::now();
auto elapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start);
if (elapsed1.count() > 0)std::cout << "Time to get image:" << elapsed1.count() << "ms" << std::endl;
// ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1));
continue; // Skip processing if the image is empty
}
// High-quality downscale for display: INTER_LANCZOS4 preserves sharpness and edges
cv::Mat displayImage;
if (image->cols > 1920) {
double scale = 1920.0 / image->cols;
cv::resize(*image, displayImage, cv::Size(), scale, scale, cv::INTER_LANCZOS4);
}
else {
displayImage = *image;
}
cv::imshow("Image", displayImage);
ANSCV_ReleaseImage_S(&image);
//std::cout << "Index="<<index<<". Size: " << width << "x" << height << ". Timestamp: " << pts << std::endl;
if (cv::waitKey(30) == 27) {
std::cout << "Break" << std::endl;
break;
}
}
cv::destroyAllWindows(); // Destroy all OpenCV windows
StopVideoPlayer(&videoClient);
ReleaseANSVideoPlayerHandle(&videoClient);
return 0;
}
int VideoPlayerClientDoubleDestroy() {
ANSVIDEOPLAYER* videoClient;
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
std::cout << "create Video Player" << std::endl;
CreateANSVideoPlayerHandle(&videoClient, "", testVideoFile.c_str());
std::cout<< "Start 1" << std::endl;
StartVideoPlayer(&videoClient);
std::cout<< "Stop 1" << std::endl;
StopVideoPlayer(&videoClient);
std::cout<< "Start 2" << std::endl;
StartVideoPlayer(&videoClient);
std::cout<< "Stop 2" << std::endl;
StopVideoPlayer(&videoClient);
std::cout<<"released"<<std::endl;
ReleaseANSVideoPlayerHandle(&videoClient);
return 0;
}
int FilePlayerClientCVTest() {
int width = 0;
int height = 0;
int64_t pts = 0;
ANSFILEPLAYER* filePlayerClient;
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
CreateANSFilePlayerHandle(&filePlayerClient, "", testVideoFile.c_str());
StartFilePlayer(&filePlayerClient);
cv::namedWindow("Image", cv::WINDOW_NORMAL);
cv::resizeWindow("Image", 1920, 1080);
int index = 0;
while (true) {
index++;
std::cout << "Index=" << index << std::endl;
if ((index == 200) || (index == 800) || (index == 1200)) { StopFilePlayer(&filePlayerClient); }
if ((index == 400) || (index == 1000) || (index == 1500)) { StartFilePlayer(&filePlayerClient); }
if ((index == 1800) || (index == 2200) || (index == 2500)) { StopFilePlayer(&filePlayerClient); }
if ((index == 2000) || (index == 2300) || (index == 2700)) { StartFilePlayer(&filePlayerClient); }
if (index > 20000) break;
auto start = std::chrono::system_clock::now();
cv::Mat* image = nullptr;
GetFilePlayerCVImage(&filePlayerClient, width, height, pts, &image);
auto end1 = std::chrono::system_clock::now();
auto elapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start);
if (elapsed1.count() > 0) std::cout << "Time to get image:" << elapsed1.count() << "ms" << std::endl;
if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
cv::Mat displayImage;
if (image->cols > 1920) {
double scale = 1920.0 / image->cols;
cv::resize(*image, displayImage, cv::Size(), scale, scale, cv::INTER_LANCZOS4);
}
else {
displayImage = *image;
}
cv::imshow("Image", displayImage);
ANSCV_ReleaseImage_S(&image);
if (cv::waitKey(30) == 27) {
std::cout << "Break" << std::endl;
break;
}
}
cv::destroyAllWindows();
StopFilePlayer(&filePlayerClient);
ReleaseANSFilePlayerHandle(&filePlayerClient);
return 0;
}
int FilePlayerClientDoubleDestroy() {
ANSFILEPLAYER* filePlayerClient;
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
std::cout << "create File Player" << std::endl;
CreateANSFilePlayerHandle(&filePlayerClient, "", testVideoFile.c_str());
std::cout << "Start 1" << std::endl;
StartFilePlayer(&filePlayerClient);
std::cout << "Stop 1" << std::endl;
StopFilePlayer(&filePlayerClient);
std::cout << "Start 2" << std::endl;
StartFilePlayer(&filePlayerClient);
std::cout << "Stop 2" << std::endl;
StopFilePlayer(&filePlayerClient);
std::cout << "released" << std::endl;
ReleaseANSFilePlayerHandle(&filePlayerClient);
return 0;
}
int main()
{
ANSCENTER::ANSOPENCV::InitCameraNetwork();
@@ -1481,15 +1644,18 @@ int main()
// resolved inside ANSCV.dll (which is linked against libavcodec etc.),
// so this works without the unit test having to link FFmpeg itself.
//ANSCV_PrintFFmpegLicense_S();
//FilePlayerClientDoubleDestroy();
FilePlayerClientCVTest();
//VideoPlayerClientTest();
//VideoPlayerClientDoubleDestroy();
// VideoPlayerClientTest();
//OpenCVFunctionTest();
//GenerateVideo();
//VideoTestClient();
// TestGetImage();
//PureOpenCV();
// RSTPTestClient();
RSTPTestCVClient();
//RSTPTestCVClient();
//TestCreateImageFromJpegStringFile();
//TestCreateImageFromFile();
//for (int i = 0; i < 100; i++) {