/* --------------------------------------------------------------------------*/
int player_stop_async(int pid)
{
    player_cmd_t *cmd;
    int r = PLAYER_SUCCESS;
    play_para_t *player_para;
    player_status sta;

    player_para = player_open_pid_data(pid);

    if (player_para == NULL) {
        return PLAYER_NOT_VALID_PID;
    }

    sta = get_player_state(player_para);
    log_print("[player_stop]player_status=%x\n", sta);
    if (PLAYER_THREAD_IS_STOPPED(sta)) {
        player_close_pid_data(pid);
        log_print("[player_stop]pid=%d thread is already stopped\n", pid);
        return PLAYER_SUCCESS;
    }
    cmd = message_alloc();
    if (cmd) {
        cmd->ctrl_cmd = CMD_STOP;
        ffmpeg_interrupt(player_para->thread_mgt.pthread_id);
        r = send_message(player_para, cmd);
    } else {
        r = PLAYER_NOMEM;
    }

    player_close_pid_data(pid);

    return r;
}
/* --------------------------------------------------------------------------*/
int player_stop(int pid)
{
    player_cmd_t *cmd;
    int r = PLAYER_SUCCESS;
    play_para_t *player_para;
    player_status sta;

    log_print("[player_stop:enter]pid=%d\n", pid);

    player_para = player_open_pid_data(pid);
    if (player_para == NULL) {
        return PLAYER_NOT_VALID_PID;
    }

    sta = get_player_state(player_para);
    log_print("[player_stop]player_status=%x\n", sta);
    if (PLAYER_THREAD_IS_STOPPED(sta)) {
        player_close_pid_data(pid);
        log_print("[player_stop]pid=%d thread is already stopped\n", pid);
        return PLAYER_SUCCESS;
    }
    /*if (player_para->pFormatCtx) {
        av_ioctrl(player_para->pFormatCtx, AVIOCTL_STOP, 0, 0);
    }*/

    cmd = message_alloc();
    if (cmd) {
        cmd->ctrl_cmd = CMD_STOP;
        ffmpeg_interrupt(player_para->thread_mgt.pthread_id);
        r = send_message(player_para, cmd);
        r = player_thread_wait_exit(player_para);
        log_print("[player_stop:%d]wait player_theadpid[%d] r = %d\n", __LINE__, player_para->player_id, r);
        clear_all_message(player_para);
        ffmpeg_uninterrupt(player_para->thread_mgt.pthread_id);
    } else {
        r = PLAYER_NOMEM;
    }

    player_close_pid_data(pid);
    log_print("[player_stop:exit]pid=%d\n", pid);

    return r;
}
int main(int argc,char *argv[])
{
	play_control_t *pCtrl = NULL;	
	int pid;
	int pos = 0;
	int speed = 0;
	int tmpneedexit = 0;
	int ret = -1;
	media_info_t minfo;	
	char tmpcommand[TMP_COMMAND_MAX];
	EMU_STEP tmpstep = EMU_STEP_MENU; 
	
	pCtrl = (play_control_t*)malloc(sizeof(play_control_t));  
	memset(pCtrl,0,sizeof(play_control_t)); 	
	memset(&minfo,0,sizeof(media_info_t));
	if(argc<2)
	{
		printf("usage:player file\n");
		return -1;
	}
	player_init();		
	set_display_axis(0);		//move osd out of screen to set video layer out
		
	player_register_update_callback(&pCtrl->callback_fn,&update_player_info,1000);
	printf("player callback register....\n");
	
	pCtrl->file_name = strdup(argv[1]);
	
	//pCtrl->nosound = 1;   // if disable audio...,must call this api
	pCtrl->video_index = -1;// MUST
	pCtrl->audio_index = -1;// MUST
    pCtrl->sub_index = -1;/// MUST 
    pCtrl->hassub = 1;  // enable subtitle

	//just open a buffer,just for p2p,http,etc...
    //pCtrl->auto_buffing_enable = 1;
//	pCtrl->buffing_min = 0.001;
//	pCtrl->buffing_middle = 0.02;
//	pCtrl->buffing_max = 0.9;
	
	pCtrl->t_pos = -1;	// start position, if live streaming, need set to -1
	pCtrl->need_start = 0; // if 0,you can omit player_start_play API.just play video/audio immediately. if 1,need call "player_start_play" API;
	
	pid=player_start(pCtrl,0);
	if(pid<0)
	{
		printf("player start failed!error=%d\n",pid);
		return -1;
	}	
	signal(SIGSEGV, signal_handler);
    //SYS_disable_osd0();	
	while((!tmpneedexit)&&(!PLAYER_THREAD_IS_STOPPED(player_get_state(pid)))){
		 switch (tmpstep) {                
    		case EMU_STEP_PAUSE:   
				player_pause(pid);                
				tmpstep = EMU_STEP_MENU;
    			break;
    		case EMU_STEP_RESUME:
                player_resume(pid);
                tmpstep = EMU_STEP_MENU;    				
    			break;
    		case EMU_STEP_SEEK:   
    			printf("will  seek position:100\n");
    			pos = 100;    			
    			player_timesearch(pid,pos);
    			tmpstep = EMU_STEP_MENU;    		
    			break;    		
    		 		
    			break;
    		case EMU_STEP_STOP:
                player_stop(pid);				
                tmpstep = EMU_STEP_MENU;    			
    			break; 
    		case EMU_STEP_FF:
    			printf("please input fastforward speed:\n");
    			speed = 1;    			
    			player_forward(pid,speed);
    			tmpstep = EMU_STEP_MENU;    		
    			break;
    		case EMU_STEP_RR:
    			printf("please input fastrewind speed:");
    			speed = 1;    			
    			player_backward(pid,speed);
    			tmpstep = EMU_STEP_MENU; 
    			break;  
    		case EMU_STEP_SETLOOP:
    			player_loop(pid);
    			tmpstep = EMU_STEP_MENU;    			
    			break;  
    		case EMU_STEP_EXIT:
    			player_exit(pid);
    			tmpneedexit = 1;
    			break;    
			case EMU_STEP_START:
				player_start_play(pid);
				//SYS_set_tsync_enable(0);///< f no sound,can set to be 0
				tmpstep = EMU_STEP_MENU; 		
				break;
			case EMU_STEP_GETAVMEDIAINFO:
				if(pid>=0){
					if(player_get_state(pid) > PLAYER_INITOK) {
						ret = player_get_media_info(pid,&minfo);
						if(ret==0)
							_media_info_dump(&minfo);					
					}
				}
				tmpstep = EMU_STEP_MENU; 	
				break;	
			case EMU_STEP_MENU:
				do {
					printf(SCREEN_SPLITER);
					printf("       	     player benchmark tool for android            v2.0\n");
					printf(SCREEN_SPLITER);
					printf("* Please choose one option                                 *\r\n");	
					printf("* 0   show main menu                                       *\r\n");	
					printf("* a   start play                                           *\r\n");	
					printf("* s   get media info                                       *\r\n");				
					printf("* 1   Pause play                                           *\r\n");
					printf("* 2   Resume play                                          *\r\n");
					printf("* 3   Stop play                                            *\r\n");			   
					printf("* 4   Fastforward                                          *\r\n");  
					printf("* 5   Fastrewind                                       	   *\r\n");  
					printf("* 6   Seek                                             	   *\r\n"); 
					printf("* 7   Set repeat                                           *\r\n"); 	                  
					printf("* 8   Quit tools                                           *\r\n");  					
					printf(SCREEN_SPLITER); 
					printf("please input you choice:");
					memset(tmpcommand,0,TMP_COMMAND_MAX);
					
					scanf ("%s",tmpcommand);				    
				    if (strcmp(tmpcommand,"1")==0){
				        tmpstep = EMU_STEP_PAUSE;
				    } 
				    if (strcmp(tmpcommand,"0")==0){
				    	sleep(1);
				        tmpstep = EMU_STEP_MENU;
				    }
				    else if (strcmp(tmpcommand,"2")==0){
				    	tmpstep = EMU_STEP_RESUME;
				    } 
				    else if (strcmp(tmpcommand,"3")==0){
				    	tmpstep = EMU_STEP_STOP;
				    }                     
				    else if (strcmp(tmpcommand,"4")==0){
				        tmpstep = EMU_STEP_FF;
				    } 
				    else if (strcmp(tmpcommand,"5")==0){
				        tmpstep = EMU_STEP_RR;
				    } 
				    else if (strcmp(tmpcommand,"6")==0) {
				        tmpstep = EMU_STEP_SEEK;
				    } 
				    else if (strcmp(tmpcommand,"7")==0) {
				        tmpstep = EMU_STEP_SETLOOP;
				    } 
				    else if (strcmp(tmpcommand,"8")==0) {
				    	
				        tmpstep = EMU_STEP_EXIT;
				    }
					else if (strcmp(tmpcommand,"a")==0) {
				        tmpstep = EMU_STEP_START;
				    }
				    else if (strcmp(tmpcommand,"s")==0){
				    	tmpstep = EMU_STEP_GETAVMEDIAINFO;
				    }
				    
				}while (0);
				
				break;
    	}	
		usleep(100*1000);
        signal(SIGCHLD, SIG_IGN);        
		signal(SIGTSTP, SIG_IGN);        
		signal(SIGTTOU, SIG_IGN);        
		signal(SIGTTIN, SIG_IGN);        
		signal(SIGHUP, signal_handler);        
		signal(SIGTERM, signal_handler);        
		signal(SIGSEGV, signal_handler);        
		signal(SIGINT, signal_handler);        
		signal(SIGQUIT, signal_handler);		
	}	
	set_display_axis(1);	//recover osd
	free(pCtrl->file_name);
	free(pCtrl);       
    printf("...........player exit,~,byeybe...........\n");	
	return 0;
}