/* --------------------------------------------------------------------------*/
int player_exit(int pid)
{
    int ret = PLAYER_SUCCESS;
    play_para_t *para;

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

    para = player_open_pid_data(pid);
    if (para != NULL) {
        log_print("[player_exit]player_state=0x%x\n", get_player_state(para));
        if (get_player_state(para) != PLAYER_EXIT) {
            player_stop(pid);
        }

        ret = player_thread_wait_exit(para);
        log_print("[player_exit]player thread already exit: %d\n", ret);
        ffmpeg_uninterrupt(para->thread_mgt.pthread_id);
        FREE(para);
        para = NULL;
    }
    player_close_pid_data(pid);
    player_release_pid(pid);
    log_print("[player_exit:exit]pid=%d\n", pid);

    return ret;
}
/* --------------------------------------------------------------------------*/
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 player_mate_sleep(play_para_t *player)
{
    struct player_mate *mate = (struct player_mate *)(player->player_mate);
    int ret = 0;
    if (!mate) {
        return -1;
    }
    if(player->playctrl_info.temp_interrupt_ffmpeg){
	     player->playctrl_info.temp_interrupt_ffmpeg=0;	
	     log_print("ffmpeg_uninterrupt tmped by player mate!\n");	 
            ffmpeg_uninterrupt(player->thread_mgt.pthread_id);
    }
    mate->mate_should_sleep = 1;	 
    while (mate->mate_isrunng) {
        pthread_mutex_lock(&mate->pthread_mutex);
        ret = pthread_cond_signal(&mate->pthread_cond);
        pthread_mutex_unlock(&mate->pthread_mutex);
        if (mate->mate_isrunng) {
            player_thread_wait(player, 1000);
        }
    }
    return ret;
}