[Bugfix][T106][bug-view-340]Fixed the problem that audio resource synchronization issues causing ramdump

Only Configure: Yes
Affected branch: master
Affected module: audio
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I945f55ce336a6f1e51148823d63228e4eeb83c41
diff --git a/ap/app/include/audio_res_ctrl.h b/ap/app/include/audio_res_ctrl.h
index e36ffeb..fc3a5ed 100755
--- a/ap/app/include/audio_res_ctrl.h
+++ b/ap/app/include/audio_res_ctrl.h
@@ -44,6 +44,10 @@
 	REL_AUDIO_IND,      //ÇëÇóijҵÎñÄ£¿éÊÍ·ÅaudioµÄ½á¹ûÓ¦´ð£¬Èôʧ°Ü£¬¶ÏÑÔ£»»»ÑÔÖ®£¬±ØÐë³É¹¦
 	IDLE_AUDIO_INFO,     //Ö÷¿ØÍ¨ÖªËùÓÐÉêÇë¹ýAUDIO×ÊÔ´µÄÒµÎñÄ£¿é£¬¸æÖªµ±Ç°¿ÕÏУ¬ÒÔ½â¾öÒµÎñÄ£¿é´¦ÓÚ¹ÒÆð̬»ò֮ǰ±»Ç¿ÐÐÊͷŵÄÒµÎñÄ£¿é£¬ÈçMP3ÒôÀÖ²¥·ÅµÈ£»
 	EXIT_AUDIO_IND, //֪ͨÖ÷¿Ø£¬ÒµÎñÄ£¿éÍ˳öÒôƵ×ÊÔ´¾ºÕù
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+	EXIT_AUDIO_REQ, 
+	EXIT_AUDIO_RSP,
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 };
 
 
diff --git a/ap/app/zte_comm/zte_audio_ctrl/audio_res_ctrl.c b/ap/app/zte_comm/zte_audio_ctrl/audio_res_ctrl.c
index d7769ce..9c4237d 100755
--- a/ap/app/zte_comm/zte_audio_ctrl/audio_res_ctrl.c
+++ b/ap/app/zte_comm/zte_audio_ctrl/audio_res_ctrl.c
@@ -332,17 +332,17 @@
     slog(RTC_PRINT, SLOG_NORMAL, "%s:%s: LINE(%d), moduleId(%x), type(%x), using_type(%d), releasing_type(%d)\n",  __FILE__, __FUNCTION__, __LINE__, rel_ind_data->moduleId, rel_ind_data->type, using_type, releasing_type);
 }
 
-static void  unregister_exist_app(MSG_BUF *msg)
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+static void  unregister_exist_app(int moduleId, int type)
 {
-	T_audio_ExitInd *exitIndData = (T_audio_ExitInd*)(msg->aucDataBuf);
 	T_exist_applist  *tmp = NULL;
 	T_exist_applist  *node = exist_apphead;
 
-	slog(RTC_PRINT, SLOG_NORMAL, "unregister_exist_app: moduleId(%x) type(%d) \n", exitIndData->moduleId, exitIndData->type);
+	slog(RTC_PRINT, SLOG_NORMAL, "unregister_exist_app: moduleId(%x) type(%d) \n", moduleId, type);
 	
 	while (node != NULL)
     {    	
-        if (node->src_id == exitIndData->moduleId && node->type == exitIndData->type)
+        if (node->tempSrcId == moduleId && node->type == type)
         {
         	if(NULL != tmp)
         	{
@@ -353,7 +353,7 @@
 				exist_apphead = node->next;
 			}
             free(node);
-			slog(RTC_PRINT, SLOG_NORMAL, "unregister_exist_app: %d moduleId(%x) type(%d) \n", __LINE__,exitIndData->moduleId, exitIndData->type);
+			slog(RTC_PRINT, SLOG_NORMAL, "unregister_exist_app: %d moduleId(%x) type(%d) \n", __LINE__,moduleId, type);
             return;
         }
 		tmp = node;
@@ -361,6 +361,17 @@
     }
 }
 
+static void  unregister_exist_app_proc(MSG_BUF *msg)
+{
+	T_audio_ExitInd *exitIndData = (T_audio_ExitInd*)(msg->aucDataBuf);
+	unregister_exist_app(exitIndData->moduleId, exitIndData->type);
+	if(EXIT_AUDIO_REQ == msg->usMsgCmd) {
+		if (0 > platform_send_msg(MODULE_ID_CODEC, exitIndData->moduleId, EXIT_AUDIO_RSP, 0, NULL))
+			softap_assert("unregister_exist_app_proc function: send USE_AUDIO_RSP msg error\n");
+    	}
+}
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
+
 /*******************************************************************************
  *                             È«¾Öº¯Êý¶¨Òå                                   *
  ******************************************************************************/
@@ -423,7 +434,10 @@
             break;
 
 		case EXIT_AUDIO_IND:
-			unregister_exist_app(&msg);
+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+		case EXIT_AUDIO_REQ:
+			unregister_exist_app_proc(&msg);
+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 			break;
         default:
             slog(RTC_PRINT, SLOG_NORMAL, "%s: %s LINE(%d) other msg: MsgCmd(%d), srcId(%d), dstId(%d)\n", __FILE__, __FUNCTION__, __LINE__, msg.usMsgCmd, msg.src_id, msg.dst_id);
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/include/audio_res_ctrl.h b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/include/audio_res_ctrl.h
index 2ab4da9..453e877 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/include/audio_res_ctrl.h
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/include/audio_res_ctrl.h
@@ -47,6 +47,10 @@
 	REL_AUDIO_IND,      //ÇëÇóijҵÎñÄ£¿éÊÍ·ÅaudioµÄ½á¹ûÓ¦´ð£¬Èôʧ°Ü£¬¶ÏÑÔ£»»»ÑÔÖ®£¬±ØÐë³É¹¦
 	IDLE_AUDIO_INFO,     //Ö÷¿ØÍ¨ÖªËùÓÐÉêÇë¹ýAUDIO×ÊÔ´µÄÒµÎñÄ£¿é£¬¸æÖªµ±Ç°¿ÕÏУ¬ÒÔ½â¾öÒµÎñÄ£¿é´¦ÓÚ¹ÒÆð̬»ò֮ǰ±»Ç¿ÐÐÊͷŵÄÒµÎñÄ£¿é£¬ÈçMP3ÒôÀÖ²¥·ÅµÈ£»
 	EXIT_AUDIO_IND, //֪ͨÖ÷¿Ø£¬ÒµÎñÄ£¿éÍ˳öÒôƵ×ÊÔ´¾ºÕù
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+	EXIT_AUDIO_REQ,
+	EXIT_AUDIO_RSP,
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 };
 
 
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/libsoftap/audio_res_request_api.c b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/libsoftap/audio_res_request_api.c
index 85757ac..6618f5d 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/libsoftap/audio_res_request_api.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/libsoftap/audio_res_request_api.c
@@ -37,7 +37,10 @@
  *							   ºê¶¨Òå								   *
  ******************************************************************************/
 #define AUDIORES_EXIT (0xff)
-#define AUDIORES_EXIT_INTERVAL_NS     10000000
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+#define AUDIORES_EXIT_INTERVAL_SEC         1
+#define AUDIORES_REQUEST_TIMEOUT_SEC       5
+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 
 /*******************************************************************************
  *							   Êý¾ÝÀàÐͶ¨Òå 							   *
@@ -180,7 +183,9 @@
 				paudiores->callbacks->audiores_read_callback(msgbuf.usMsgCmd, (void *)msgbuf.aucDataBuf, paudiores->priv);
 			}
 		}
-		else if(AUDIORES_EXIT == msgbuf.usMsgCmd) {
+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+		else if(AUDIORES_EXIT == msgbuf.usMsgCmd || EXIT_AUDIO_RSP == msgbuf.usMsgCmd) {
+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 			if(NULL != paudiores->callbacks->audiores_destroy_callback) {
 				paudiores->callbacks->audiores_destroy_callback((long)paudiores, paudiores->priv);
 			}
@@ -198,10 +203,15 @@
 		slog(AUDIO_CTRL, SLOG_ERR,"audiores_thread_entry error num(%d): error case:%s",errno,strerror(errno));
 	}
 
-	paudiores->msgq = 0;	
+	paudiores->msgq = 0;
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START	
+	sem_post(&paudiores->semid);
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 	slog(AUDIO_CTRL, SLOG_NORMAL, "audiores_thread_entry(%s) end!\n",name);
 
-	free(paudiores);
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+	//free(paudiores);
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 }
 
 static BOOL audiores_request_by_modid(int type, int queueId, int moduleId, T_audio_UseRsp *RspData)
@@ -407,6 +417,9 @@
 {
 	struct audiores_context_t *paudiores = NULL;
 	T_audio_ExitInd exitIndData = {0}; 
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+	struct timespec ts;
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 	
 	if(AUDIORES_HANDLE_BAD == handle) {
 		slog(AUDIO_CTRL, SLOG_ERR, "Err:The handle is bad!\n");
@@ -427,10 +440,24 @@
 	exitIndData.type = paudiores->type;
 	exitIndData.moduleId = paudiores->modid;
 
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START
+	platform_send_msg(paudiores->modid, MODULE_ID_CODEC, EXIT_AUDIO_REQ, sizeof(exitIndData), (unsigned char *)&exitIndData);
+	if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
+		slog(AUDIO_CTRL, SLOG_NORMAL, "audiores_close: clock_gettime fail.\n");
+	}
+	ts.tv_sec += AUDIORES_REQUEST_TIMEOUT_SEC;
+	ts.tv_nsec += 0;
+	if ((sem_timedwait(&paudiores->semid, &ts)) == -1) {
+		slog(AUDIO_CTRL, SLOG_ERR, "audiores_close: sem_timedwait timeout!errno:%d\n",errno);
 	/*Í˳ö×ÊÔ´¾ºÕù*/
-	platform_send_msg(paudiores->modid, MODULE_ID_CODEC, EXIT_AUDIO_IND, sizeof(exitIndData), (unsigned char *)&exitIndData);
+	//platform_send_msg(paudiores->modid, MODULE_ID_CODEC, EXIT_AUDIO_IND, sizeof(exitIndData), (unsigned char *)&exitIndData);
 
 	/*Í£Ö¹½ÓÊÕÏß³Ì*/
 	ipc_send_message(paudiores->modid, paudiores->modid, AUDIORES_EXIT, 0, NULL, 0);
+		sleep(AUDIORES_EXIT_INTERVAL_SEC);
+	}
+	sem_destroy(&paudiores->semid);
+	slog(AUDIO_CTRL, SLOG_NORMAL, "audiores_close: type(%d)\n ", paudiores->type);
+	free(paudiores);
+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END
 }
-
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.c b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.c
index 4ca23a4..dc1f5fd 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.c
@@ -96,11 +96,27 @@
 		AUDIORES_HANDLE_BAD == pmsmaudio_ctx->audiores_handle) {

 		if(NULL != pmsmaudio_ctx) {

 			slog(MSMSVR_PRINT, SLOG_NORMAL, "audiochn_count=%d handle=0x%x\n", pmsmaudio_ctx->audiochn_count, pmsmaudio_ctx->audiores_handle);

-			if(REL_AUDIO_REQ == msgtype) {		

+			if(REL_AUDIO_REQ == msgtype) {

+				//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+				msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_RELEASE);

+				//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END		

 				audiores_release(pmsmaudio_ctx->audiores_handle);

 			}

 			else if(IDLE_AUDIO_INFO == msgtype) {

-				audiores_request(pmsmaudio_ctx->audiores_handle);

+			//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+				if(MSM_AUDIO_RES_STATE_RELEASE != msm_audio_get_res_state(pmsmaudio_ctx)) {

+					audiores_request(pmsmaudio_ctx->audiores_handle);

+				}

+			}

+			else if(USE_AUDIO_RSP == msgtype) {

+				server_response_data = (T_audio_UseRsp *)data;

+				slog(MSMSVR_PRINT, SLOG_DEBUG, "request result:%d type:%d using_type:%d \n", \

+					server_response_data->result, server_response_data->type, server_response_data->using_type);

+				if(0 != server_response_data->result) {

+					return 0;

+				}

+				msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_HOLD);

+			//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 			}

 		}

 		else {

@@ -111,35 +127,51 @@
 

 	if(!audiochn_valid_check(pmsmaudio_ctx)) {

 		slog(MSMSVR_PRINT, SLOG_DEBUG, "audiochn_valid_check return FALSE!!!\n");

-		if(REL_AUDIO_REQ == msgtype) {		

+		if(REL_AUDIO_REQ == msgtype) {

+			//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+			msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_RELEASE);

+			//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 			audiores_release(pmsmaudio_ctx->audiores_handle);

 		}

 		else if(IDLE_AUDIO_INFO == msgtype) {

 			audiores_request(pmsmaudio_ctx->audiores_handle);

 		}

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+		else if(USE_AUDIO_RSP == msgtype) {

+			server_response_data = (T_audio_UseRsp *)data;

+			slog(MSMSVR_PRINT, SLOG_DEBUG, "msm_audiores_read_callback request result:%d type:%d using_type:%d \n", \

+				server_response_data->result, server_response_data->type, server_response_data->using_type);

+			if(0 != server_response_data->result) {

+				return 0;

+			}

+			msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_HOLD);

+		}

 		return 0;

 	}

-	

-	if(REL_AUDIO_REQ == msgtype) {		

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

+	if(REL_AUDIO_REQ == msgtype) {

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+		msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_RELEASE);

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 		msm_audio_suspend(pmsmaudio_ctx);

-		/*»á´æÔÚpcm»¹Î´¹Ø±Õ£¬¶øÌáǰִÐÐÊÍ·Å£¿*/

 		audiores_release(pmsmaudio_ctx->audiores_handle);

 	}

 	else if(IDLE_AUDIO_INFO == msgtype) {

 		audiores_request(pmsmaudio_ctx->audiores_handle);

 	}

-	else if(USE_AUDIO_RSP == msgtype) {		

+	else if(USE_AUDIO_RSP == msgtype) {

 		server_response_data = (T_audio_UseRsp *)data;

 		slog(MSMSVR_PRINT, SLOG_DEBUG, "msm_audiores_read_callback request result:%d type:%d using_type:%d \n", \

 			server_response_data->result, server_response_data->type, server_response_data->using_type);

-

+ 

 		if(0 != server_response_data->result) {

 			return 0;

 		}

-		

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+		msm_audio_set_res_state(pmsmaudio_ctx, MSM_AUDIO_RES_STATE_HOLD);

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 		msm_audio_resume(pmsmaudio_ctx);

 	}

-	

 	return 0;

 }

 

@@ -160,6 +192,9 @@
 {

 	slog(MSMSVR_PRINT, SLOG_DEBUG, "msm_audiores_release enter.\n");

 	if(AUDIORES_HANDLE_BAD != audiores_handle) {

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+		msm_audio_set_res_state(&msmaudio_ctx, MSM_AUDIO_RES_STATE_RELEASE);

+		//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 		audiores_release(audiores_handle);

 		audiores_close(audiores_handle);	

 	}

@@ -195,7 +230,12 @@
 

 static int msm_audio_resume(struct msm_audio_context *pmsmaudio_ctx)

 {

-	slog(MSMSVR_PRINT, SLOG_DEBUG, "msm_audio_resume enter.\n");		

+	slog(MSMSVR_PRINT, SLOG_DEBUG, "msm_audio_resume enter.\n");

+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+	if(MSM_AUDIO_RES_STATE_RELEASE == msm_audio_get_res_state(pmsmaudio_ctx)) {

+		return;

+	}

+	//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 	msm_ccm_notify_cc_status(0);

 	msm_audio_stream_event_notify(pmsmaudio_ctx, SCAUDIO_EVENT_RESUME);

 

@@ -317,7 +357,22 @@
     pipcmsg->data_len = len;

 	memcpy(pipcmsg->data, data, len);

 }

-

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+void msm_audio_set_res_state(struct msm_audio_context *pmsmaudio_ctx, unsigned int state)

+{

+    pthread_mutex_lock(&pmsmaudio_ctx->audiochn_mutex);

+    pmsmaudio_ctx->audiores_state = state;

+    pthread_mutex_unlock(&pmsmaudio_ctx->audiochn_mutex);

+}

+unsigned int msm_audio_get_res_state(struct msm_audio_context *pmsmaudio_ctx)

+{

+    unsigned int state;

+    pthread_mutex_lock(&pmsmaudio_ctx->audiochn_mutex);

+    state = pmsmaudio_ctx->audiores_state;

+    pthread_mutex_unlock(&pmsmaudio_ctx->audiochn_mutex);

+    return state;

+}

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 void audiochn_add_list(struct msm_client_node *new_audiochn, struct msm_audio_context *pmsmaudio_ctx)

 {

 	if(NULL == new_audiochn) {

diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.h b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.h
index 7941a3e..e676a08 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.h
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/msm_svr/audio/msm_audio.h
@@ -6,7 +6,11 @@
 #include "msm_svrcall_media.h"

 

 #define MSM_AUDIO_HANDLE_INVALID  0

-

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+#define MSM_AUDIO_RES_STATE_UNKNOW    0

+#define MSM_AUDIO_RES_STATE_HOLD    1

+#define MSM_AUDIO_RES_STATE_RELEASE    2

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 struct msm_audio_channel {	

 	MM_HANDLE mm_handle;

 	unsigned int device;

@@ -34,10 +38,18 @@
 	unsigned char audiochn_count;

 

 	long audiores_handle;

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+	/*0-unknow;1-hold;2-release;*/

+	unsigned int audiores_state;

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 };

 

 BOOL msm_audio_file_check(const char *file, const char *mode);

 MSM_AUDIO_RATE_TYPE_E msm_audio_mantate_to_msmrate(int samplerate);

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_START

+void msm_audio_set_res_state(struct msm_audio_context *pmsmaudio_ctx, unsigned int state);

+unsigned int msm_audio_get_res_state(struct msm_audio_context *pmsmaudio_ctx);

+//LYNQ_MODIFY_ZXW_BUG_340_202410260E4E4183_END

 void msm_audio_ipcmsg_set(int fdsvr, int msgreq, void *data, int len, sc_ipc_msg *pipcmsg);

 void audiochn_add_list(struct msm_client_node *new_audiochn, struct msm_audio_context *pmsmaudio_ctx);

 void audiochn_del_list(struct msm_client_node *audioclient, struct msm_audio_context *pmsmaudio_ctx);