Browse Source

Z991239-1387 #comment other: 优化本地图片播放器主动停止播放功能

陈礼鹏80274480 4 years ago
parent
commit
7a6e43303e

+ 1 - 2
Module/mod_localmediaplay/mod_localmediaplay.cpp

@@ -524,8 +524,7 @@ ErrorCodeEnum CLocalMediaPlayEntity::__OnStart(ErrorCodeEnum preOperationError)
 	if (0 == err){
 		Dbg("create queryMedia thread success, %d.", m_scanThreadId);
 	}
-	else
-	{
+	else{
 		Dbg("create queryMedia thread failed.");
 	}
 #endif // RVC_OS_WIN

+ 27 - 47
Other/libpictureplayer/CPicturePlayer.cpp

@@ -41,14 +41,20 @@ static int picture_playing_thread(void* arg)
 	int iRet = -1;
 
 	CPicturePlayer* is = (CPicturePlayer*)arg;
-	printf("is->m_nplay_cnt is %d\n", is->m_nplay_cnt);
-
 	SDL_Surface* surface = SDL_GetWindowSurface(is->m_window);
-	for (size_t i = 0; i < is->m_nplay_cnt; i++)
+
+	int iPlayCount = 0;
+	for (; iPlayCount < is->m_nplay_cnt; iPlayCount++)
 	{
+		if (is->m_busrstop){
+			break;
+		}
 		int index = 0;
 		for (; index < is->m_nfile_cnt; index++)
 		{
+			if (is->m_busrstop) {
+				break;
+			}
 			SDL_Surface* image = SDL_LoadBMP(is->m_strfile_names[index]);
 			//SDL_Surface* image = IMG_Load(is->m_strfile_names[index]);
 			if (NULL == image){
@@ -74,7 +80,17 @@ static int picture_playing_thread(void* arg)
 		}
 	}
 
-	SDL_QuitRequested();
+	if (iPlayCount == is->m_nplay_cnt) {
+		is->m_pHostApi->PicDebug("%d times picture playing task finished, picture_playing_thread exit.", iPlayCount);
+	}
+	else
+	{
+		is->m_pHostApi->PicDebug("user stop picture playing task, picture_playing_thread exit.");
+	}
+
+	is->ExitPicturePlayingThread();
+
+	iRet = 0;
 
 	return iRet;
 }
@@ -103,6 +119,7 @@ CPicturePlayer::CPicturePlayer(CPicHostApi* pHostApi, const char* picopath)
 	}
 	m_show_width = 0;
 	m_show_height = 0;
+	m_busrstop = false;
 }
 
 CPicturePlayer::~CPicturePlayer()
@@ -166,7 +183,7 @@ int CPicturePlayer::Init(rvc_picture_player_param_t* tparam)
 	idispaly_height = idispaly_height;
 
 	//1.创建播放窗体
-	m_window = SDL_CreateWindow("rvc picture player",
+	m_window = SDL_CreateWindow("图片播放器",
 		idispalycx,// 不关心窗口X坐标
 		idispalycy,// 不关心窗口Y坐标
 		idispaly_width,
@@ -200,11 +217,9 @@ int CPicturePlayer::DeInit()
 	int iRet = -1;
 	rvc_strfree(m_stricopath);
 	m_stricopath = NULL;
-
-	if (NULL != m_pHostApi){
-		free(m_pHostApi);
-		m_pHostApi = NULL;
-	}
+	m_show_width = 0;
+	m_show_height = 0;
+	m_busrstop = false;
 
 	iRet = 0;
 
@@ -228,42 +243,6 @@ bool CPicturePlayer::StartPicPlay()
 	m_bplaying = true;
 	bRet = true;
 
-	SDL_Event event;
-	while (1)
-	{
-		SDL_PumpEvents();
-		// SDL event队列为空,则在while循环中播放图像。否则从队列头部取一个event,退出当前函数,在上级函数中处理event
-		while (!SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT))
-		{
-			SDL_PumpEvents();
-		}
-
-		switch (event.type) {
-		case SDL_KEYDOWN:
-			if (event.key.keysym.sym == SDLK_ESCAPE)
-			{
-				ExitPicturePlayingThread();
-				break;
-			}
-
-			switch (event.key.keysym.sym) {
-			case SDLK_SPACE:        // 空格键:暂停
-				break;
-			case SDL_WINDOWEVENT:
-				break;
-			default:
-				break;
-			}
-			break;
-
-		case SDL_QUIT:
-			ExitPicturePlayingThread();
-			break;
-		default:
-			break;
-		}
-	}
-
 	return bRet;
 }
 
@@ -272,7 +251,8 @@ int CPicturePlayer::StopPicPlay()
 {
 	int iRet = -1;
 
-	SDL_QuitRequested();
+	m_busrstop = true;
+	m_pHostApi->PicDebug("stop picture play, set usr stop flag true.");
 
 	iRet = 0;
 

+ 1 - 0
Other/libpictureplayer/CPicturePlayer.h

@@ -85,6 +85,7 @@ public:
 	char m_strfile_names[MAX_FILECOUNT][MAX_PATH];
 	int m_show_width;
 	int m_show_height;
+	bool m_busrstop;
 
 private:
 	bool m_bplaying;

+ 52 - 23
Other/libpictureplayer/libpictureplayer.cpp

@@ -2,14 +2,19 @@
 #include "CPicturePlayer.h"
 #include <unistd.h>
 #include <string.h>
+#include <pthread.h>
+
+
+void* PicturePlayingFunc(void* param);
 
 class libpictureplayer_impl
 {
-private:
+public:
 	CPicPlayConfig m_stPlayConfig;
 	CPicHostApi* m_pHostApi;
 	CPicturePlayer* m_Player;
 	bool m_bIsPlay;
+	pthread_t m_PlayThreadId;
 
 public:
 	libpictureplayer_impl(CPicHostApi* pHostApi) {
@@ -18,19 +23,22 @@ public:
 		memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
 		m_Player = new CPicturePlayer(pHostApi, "./rvc_media_player_64px.bmp");
 		if (NULL != m_Player) {
-			pHostApi->PicDebug(" new PicturePlayer success!");
+			pHostApi->PicDebug("new PicturePlayer success!");
 		}
 		else {
-			pHostApi->PicDebug(" new PicturePlayer failed!");
+			pHostApi->PicDebug("new PicturePlayer failed!");
 		}
+		m_PlayThreadId = 0;
 	}
 
 	~libpictureplayer_impl()
 	{
+		printf("~libpictureplayer_impl()\n");
 		m_pHostApi = NULL;
 		delete m_Player;
 		m_Player = NULL;
 		m_bIsPlay = false;
+		m_PlayThreadId = 0;
 	}
 
 	bool isStop() 
@@ -102,7 +110,9 @@ public:
 			}
 		}
 		pParam->nfile_cnt = uValidCount;
-		m_pHostApi->PicDebug("pParam uFilesCount = %d", uValidCount);
+		if (NULL != m_pHostApi) {
+			m_pHostApi->PicDebug("pParam uFilesCount = %d", uValidCount);
+		}
 		if (uValidCount > 0) {
 			iRet = 0;
 		}
@@ -112,6 +122,7 @@ public:
 
 	bool StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
 	{
+		bool bRet = false;
 		m_stPlayConfig.nWndX = nWndX;
 		m_stPlayConfig.nWndY = nWndY;
 		m_stPlayConfig.nWndWidth = nWndWidth;
@@ -120,7 +131,7 @@ public:
 		if (0 != iRet)
 		{
 			m_pHostApi->PicDebug("Load ImgConfiguration failed!");
-			return false;
+			return bRet;
 		}
 		else
 		{
@@ -128,27 +139,16 @@ public:
 			m_pHostApi->PicDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
 		}
 
-		rvc_picture_player_param_t tplayer_param = {0};
-		tplayer_param.icx = nWndX;
-		tplayer_param.icy = nWndY;
-		tplayer_param.uwindow_width = nWndWidth;
-		tplayer_param.uwindow_height = nWndHeight;
-
-		if (0 != GetPicturePlayingParams(&tplayer_param)){
-			return false;
+		int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, this);
+		if (0 == err) {
+			m_pHostApi->PicDebug("create picture play thread[%lu] success.", m_PlayThreadId);
+			bRet = true;
 		}
-
-		if (0 == m_Player->Init(&tplayer_param)) 
-		{
-			m_pHostApi->PicDebug("Init Picture Player Success.");
-			if (m_Player->StartPicPlay()) 
-			{
-				m_bIsPlay = true;
-				m_pHostApi->PicDebug("Start Picture Play Success.");
-			}
+		else {
+			m_pHostApi->PicDebug("create picture play thread failed.");
 		}
 
-		return true;
+		return bRet;
 	}
 
 	bool StopPlay()
@@ -209,4 +209,33 @@ bool Clibpictureplayer::checkIsPlay(void* pthreadid)
 void Clibpictureplayer::Close()
 {
 	m_pImpl->StopPlay();
+}
+
+
+void* PicturePlayingFunc(void* param) 
+{
+	printf("enter PicturePlayingFunc \n");
+	libpictureplayer_impl* is = (libpictureplayer_impl*)param;
+
+	rvc_picture_player_param_t tplayer_param = { 0 };
+	tplayer_param.icx = is->m_stPlayConfig.nWndX;
+	tplayer_param.icy = is->m_stPlayConfig.nWndY;
+	tplayer_param.uwindow_width = is->m_stPlayConfig.nWndWidth;
+	tplayer_param.uwindow_height = is->m_stPlayConfig.nWndHeight;
+
+	if (0 == is->GetPicturePlayingParams(&tplayer_param)) {
+		if (0 == is->m_Player->Init(&tplayer_param))
+		{
+			is->m_pHostApi->PicDebug("Init Picture Player Success.");
+			if (is->m_Player->StartPicPlay())
+			{
+				is->m_bIsPlay = true;
+				is->m_pHostApi->PicDebug("Start Picture Play Success.");
+			}
+		}
+	}
+	else {
+		is->m_pHostApi->PicDebug("Get Picture Playing Params failed!");
+	}
+	printf("leave PicturePlayingFunc \n");
 }