//--------------------------------------------------------
void ofQuickTimePlayer::start(){

	//--------------------------------------
	#ifdef OF_VIDEO_PLAYER_QUICKTIME
	//--------------------------------------

	if (bLoaded == true && bStarted == false){
		SetMovieActive(moviePtr, true);

		//------------------ set the movie rate to default
		//------------------ and preroll, so the first frames come correct

		TimeValue timeNow 	= 	GetMovieTime(moviePtr, 0);
		Fixed playRate 		=	GetMoviePreferredRate(moviePtr); 		//Not being used!

		PrerollMovie(moviePtr, timeNow, X2Fix(speed));
		SetMovieRate(moviePtr,  X2Fix(speed));
		setLoopState(currentLoopState);

		// get some pixels in there right away:
		MoviesTask(moviePtr,0);
		#if defined(TARGET_OSX) && defined(__BIG_ENDIAN__)
			convertPixels(offscreenGWorldPixels, pixels.getPixels(), width, height);
		#endif
		bHavePixelsChanged = true;

		bStarted = true;
		bPlaying = true;
	}

	//--------------------------------------
	#endif
	//--------------------------------------
}
//--------------------------------------------------------
void ofVideoPlayer::start(){

	//--------------------------------------
	#ifdef OF_VIDEO_PLAYER_QUICKTIME
	//--------------------------------------

	if (bLoaded == true && bStarted == false){
		SetMovieActive(moviePtr, true);

		//------------------ set the movie rate to default
		//------------------ and preroll, so the first frames come correct

		TimeValue timeNow 	= 	GetMovieTime(moviePtr, 0);
		Fixed playRate 		=	GetMoviePreferredRate(moviePtr); 		//Not being used!

		PrerollMovie(moviePtr, timeNow, X2Fix(speed));
		SetMovieRate(moviePtr,  X2Fix(speed));
		setLoopState(OF_LOOP_NORMAL);

		// get some pixels in there right away:
		MoviesTask(moviePtr,0);
		convertPixels(offscreenGWorldPixels, pixels, width, height);
		bHavePixelsChanged = true;
		if (bUseTexture == true){
			tex.loadData(pixels, width, height, GL_RGB);
		}

		bStarted = true;
		bPlaying = true;
	}

	//--------------------------------------
	#endif
	//--------------------------------------
}
////////////////////////////////////////////////////////////////////////////////
// virtual
bool LLMediaImplQuickTime::updateMedia()
{
	if ( ! mMovieHandle )
		return false;

	if ( ! mMovieController )
		return false;

	if ( ! mGWorldHandle )
		return false;

	// service QuickTime
	MoviesTask( mMovieHandle, 0 );
	MCIdle( mMovieController );

	// update state machine (deals with transport controls for example)
	processState();

	// special code for looping - need to rewind at the end of the movie

	if ( isLooping() )
	{
		// QT call to see if we are at the end - can't do with controller
		if ( IsMovieDone( mMovieHandle ) )
		{
			// go back to start
			rewind();

			// kick off new play
			MCDoAction( mMovieController, mcActionPrerollAndPlay, (void*)GetMoviePreferredRate( mMovieHandle ) );

			// set the volume
			MCDoAction( mMovieController, mcActionSetVolume, (void*)mCurVolume );
		}
	}

	return true;
}
bool LLMediaImplQuickTime::processState()
{
	// start stream
	if ( nextCommand() == LLMediaBase::COMMAND_START )
	{
		// valid when we are in these states
		if ( getStatus() == LLMediaBase::STATUS_NAVIGATING|| getStatus() == LLMediaBase::STATUS_STOPPED || getStatus() == LLMediaBase::STATUS_PAUSED )
		{
			// it appears that the movie must be in a loaded state before we do this command
			if ( GetMovieLoadState( mMovieHandle ) >= kMovieLoadStatePlaythroughOK )
			{
				MCDoAction( mMovieController, mcActionPrerollAndPlay, (void*)GetMoviePreferredRate( mMovieHandle ) );

				MCDoAction( mMovieController, mcActionSetVolume, (void*)mCurVolume );

				setStatus( LLMediaBase::STATUS_STARTED );

				clearCommand();
			}
		}
	}
	else
	if ( nextCommand() == LLMediaBase::COMMAND_STOP )
	{
		// valid when we are in these states
		if ( getStatus() == LLMediaBase::STATUS_NAVIGATING || getStatus() == LLMediaBase::STATUS_STARTED || getStatus() == LLMediaBase::STATUS_PAUSED )
		{
			// it appears that the movie must be in a loaded state before we do this command
			if ( GetMovieLoadState( mMovieHandle ) >= kMovieLoadStatePlaythroughOK )
			{
				// stop playing
				Fixed rate = X2Fix( 0.0 );
				MCDoAction( mMovieController, mcActionPlay, (void*)rate );

				// go back to start
				rewind();

				setStatus( LLMediaBase::STATUS_STOPPED );
				clearCommand();
			};
		};
	}
	else
	if ( nextCommand() == LLMediaBase::COMMAND_PAUSE )
	{
		// valid when we are in these states
		if ( getStatus() == LLMediaBase::STATUS_NAVIGATING || getStatus() == LLMediaBase::STATUS_STARTED || getStatus() == LLMediaBase::STATUS_STOPPED )
		{
			// it appears that the movie must be in a loaded state before we do this command
			if ( GetMovieLoadState( mMovieHandle ) >= kMovieLoadStatePlaythroughOK )
			{
				// stop playing
				Fixed rate = X2Fix( 0.0 );
				MCDoAction( mMovieController, mcActionPlay, (void*)rate );

				setStatus( LLMediaBase::STATUS_PAUSED );
				clearCommand();
			};
		};
	};

	return true;
}
////////////////////////////////////////////////////////////////////////////////
// private
bool LLMediaImplQuickTime::load( const std::string url )
{
	if ( url.empty() )
		return false;

	//In case std::string::c_str() makes a copy of the url data,
	//make sure there is memory to hold it before allocating memory for handle.
	//if fails, NewHandleClear(...) should return NULL.
	const char* url_string = url.c_str() ;
	Handle handle = NewHandleClear( ( Size )( url.length() + 1 ) );
    if ( NULL == handle )
		return false;
	if(noErr != MemError() || NULL == *handle)
	{
		return false ;
	}

	BlockMove( url_string, *handle, ( Size )( url.length() + 1 ) );

	//std::cout << "LLMediaImplQuickTime::load( " << url << " )" << std::endl;

	// TODO: supposed to use NewMovieFromDataParams now
	OSErr err = NewMovieFromDataRef( &mMovieHandle, newMovieActive | newMovieDontInteractWithUser | newMovieAsyncOK | newMovieIdleImportOK, nil, handle, URLDataHandlerSubType );
	DisposeHandle( handle );
	if ( noErr != err )
		return false;

	// do pre-roll actions (typically fired for streaming movies but not always)
	PrePrerollMovie( mMovieHandle, 0, GetMoviePreferredRate( mMovieHandle ), moviePrePrerollCompleteCallback, ( void * )this );

	// get movie rect (and check for min/max)
	Rect movie_rect;
	setMovieBoxEnhanced( &movie_rect );

	// make a new movie controller
	mMovieController = NewMovieController( mMovieHandle, &movie_rect, mcNotVisible | mcTopLeftMovie );

#if defined(__APPLE__) || defined(MACOSX)
	setMediaDepth( 4 );
#else
	setMediaDepth( 3 );
#endif

	// tell manager about the media size
	setMediaSize( movie_rect.right - movie_rect.left, movie_rect.bottom - movie_rect.top);

	// movie controller
	MCSetActionFilterWithRefCon( mMovieController, mcActionFilterCallBack, ( long )this );

	SetMoviePlayHints( mMovieHandle, hintsAllowDynamicResize, hintsAllowDynamicResize );

	// function that gets called when a frame is drawn
	SetMovieDrawingCompleteProc( mMovieHandle, movieDrawingCallWhenChanged, movieDrawingCompleteCallback, ( long )this );

	// emit an event to say that a media source was loaded
	LLMediaEvent event( this );
	mEventEmitter.update( &LLMediaObserver::onMediaLoaded, event );

	// set up inital state
	sizeChanged();

	return true;
}