status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
{
    status_t err = UNKNOWN_ERROR;
    sp<IMediaPlayer> p;
    { // scope for the lock
        Mutex::Autolock _l(mLock);

        if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
                (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
            ALOGE("attachNewPlayer called in state %d", mCurrentState);
            return INVALID_OPERATION;
        }

        clear_l();
        p = mPlayer;
        mPlayer = player;
        if (player != 0) {
            mCurrentState = MEDIA_PLAYER_INITIALIZED;
            err = NO_ERROR;
        } else {
            ALOGE("Unable to create media player");
        }
    }

    if (p != 0) {
        p->disconnect();
    }

    return err;
}
int reset(VideoState **ps) {
	VideoState *is = *ps;

	if (is) {
	    is->quit = 1;
	    /*
	     * If the video has finished playing, then both the picture and
	     * audio queues are waiting for more data.  Make them stop
	     * waiting and terminate normally.
	     */
	    if (is->audioq.initialized == 1) {
	    	SDL_CondSignal(is->audioq.cond);
	    }

	    if (is->videoq.initialized == 1) {
	    	SDL_CondSignal(is->videoq.cond);
	    }

	    if (is->parse_tid) {
	    	pthread_join(*(is->parse_tid), NULL);
	    }

	    if (is->video_tid) {
	    	pthread_join(*(is->video_tid), NULL);
	    }

	    clear_l(&is);

		return NO_ERROR;
	}

	return INVALID_OPERATION;
}
int reset(State **ps)
{
	State *state = *ps;
	
    pthread_mutex_lock(lock);

    if (state) {
    	state->abort_request = 1;
    	pthread_join(state->decoder_thread, NULL);
    }
    
    pthread_mutex_unlock(lock);
	
    clear_l(ps);
    
    return 0;
}
status_t MediaPlayer::reset()
{
    LOGV("reset");
    Mutex::Autolock _l(mLock);
    mLoop = false;
    if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
    mPrepareSync = false;
    if (mPlayer != 0) {
        status_t ret = mPlayer->reset();
        if (ret != NO_ERROR) {
            LOGE("reset() failed with return code (%d)", ret);
            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
        } else {
            mCurrentState = MEDIA_PLAYER_IDLE;
        }
        return ret;
    }
    clear_l();
    return NO_ERROR;
}
status_t MediaPlayer::reset_l()
{
    mLoop = false;
    if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
    mPrepareSync = false;
    if (mPlayer != 0) {
        status_t ret = mPlayer->reset();
        if (ret != NO_ERROR) {
            ALOGE("reset() failed with return code (%d)", ret);
            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
        } else {
            mCurrentState = MEDIA_PLAYER_IDLE;
        }
        // setDataSource has to be called again to create a
        // new mediaplayer.
        mPlayer = 0;
        return ret;
    }
    clear_l();
    return NO_ERROR;
}