void LLViewerMediaImpl::play()
{
	LLPluginClassMedia* plugin = getMediaPlugin();

	// first stop any previously playing media
	// stop();

	// plugin->addObserver( this );
	if (!plugin)
	{
	 	if(!initializePlugin(mMimeType))
		{
			// Plugin failed initialization... should assert or something
			return;
		}
		plugin = getMediaPlugin();
	}
	
	// updateMovieImage(mTextureId, true);

	plugin->loadURI( mMediaURL );
	if(/*plugin->pluginSupportsMediaTime()*/ true)
	{
		start();
	}
}
void LLViewerMediaImpl::navigateTo(const std::string& url, const std::string& mime_type,  bool rediscover_type)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if(rediscover_type)
	{

		LLURI uri(url);
		std::string scheme = uri.scheme();

		if(scheme.empty() || ("http" == scheme || "https" == scheme))
		{
			if(mime_type.empty())
			{
				LLHTTPClient::getHeaderOnly( url, new LLMimeDiscoveryResponder(this));
			}
			else if(initializeMedia(mime_type) && (plugin = getMediaPlugin()))
			{
				plugin->loadURI( url );
			}
		}
		else if("data" == scheme || "file" == scheme || "about" == scheme)
		{
			// FIXME: figure out how to really discover the type for these schemes
			// We use "data" internally for a text/html url for loading the login screen
			if(initializeMedia("text/html") && (plugin = getMediaPlugin()))
			{
				plugin->loadURI( url );
			}
		}
		else
		{
			// This catches 'rtsp://' urls
			if(initializeMedia(scheme) && (plugin = getMediaPlugin()))
			{
				plugin->loadURI( url );
			}
		}
	}
	else if (plugin)
	{
		plugin->loadURI( url );
	}
	else if(initializeMedia(mime_type) && (plugin = getMediaPlugin()))
	{
		plugin->loadURI( url );
	}
	else
	{
		LL_WARNS("Media") << "Couldn't navigate to: " << url << " as there is no media type for: " << mime_type << LL_ENDL;
		return;
	}
	mMediaURL = url;

}
////////////////////////////////////////////////////////////////////////////////
// virtual
void
LLViewerMediaImpl::copy()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
		plugin->copy();
}
bool LLViewerMediaImpl::initializePlugin(const std::string& media_type)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		// Save the previous media source's last set size before destroying it.
		mMediaWidth = plugin->getSetWidth();
		mMediaHeight = plugin->getSetHeight();
	}
	
	// Always delete the old media impl first.
	destroyMediaSource();
	
	// and unconditionally set the mime type
	mMimeType = media_type;

	LLPluginClassMedia* media_source = newSourceFromMediaType(media_type, this, mMediaWidth, mMediaHeight);
	
	if (media_source)
	{
		media_source->setDisableTimeout(gSavedSettings.getBOOL("DebugPluginDisableTimeout"));
		media_source->setLoop(mMediaLoop);
		media_source->setAutoScale(mMediaAutoScale);
		media_source->setBrowserUserAgent(LLViewerMedia::getCurrentUserAgent());
		
		mPluginBase = media_source;
		return true;
	}

	return false;
}
void LLViewerMediaImpl::getTextureSize(S32 *texture_width, S32 *texture_height)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if(plugin && plugin->textureValid())
	{
		S32 real_texture_width = plugin->getBitsWidth();
		S32 real_texture_height = plugin->getBitsHeight();

		{
			// The "texture width" coming back from the plugin may not be a power of two (thanks to webkit).
			// It will be the correct "data width" to pass to setSubImage
			int i;
			
			for(i = 1; i < real_texture_width; i <<= 1)
				;
			*texture_width = i;

			for(i = 1; i < real_texture_height; i <<= 1)
				;
			*texture_height = i;
		}
			
	}
	else
	{
		*texture_width = 0;
		*texture_height = 0;
	}
}
////////////////////////////////////////////////////////////////////////////////
// virtual
void
LLViewerMediaImpl::paste()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
		plugin->paste();
}
void LLViewerMediaImpl::seek(F32 time)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->seek(time);
	}
}
void LLViewerMediaImpl::onMouseCaptureLost()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->mouseEvent(LLPluginClassMedia::MOUSE_EVENT_UP, LEFT_BUTTON, mLastMouseX, mLastMouseY, 0);
	}
}
void LLViewerMediaImpl::setVolume(F32 volume)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->setVolume(volume);
	}
}
void LLViewerMediaImpl::navigateHome()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->loadURI( mHomeURL );
	}
}
void LLViewerMediaImpl::start()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->start();
	}
}
void LLViewerMediaImpl::navigateStop()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->browse_stop();
	}

}
void LLViewerMediaImpl::stop()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		plugin->stop();
		// destroyMediaSource();
	}
}
////////////////////////////////////////////////////////////////////////////////
// virtual
BOOL
LLViewerMediaImpl::canPaste() const
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
		return plugin->canPaste();
	else
		return FALSE;
}
bool LLViewerMediaImpl::canNavigateBack()
{
	bool result = false;
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		result = plugin->getHistoryBackAvailable();
	}
	return result;
}
void LLViewerMediaImpl::setSize(int width, int height)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	mMediaWidth = width;
	mMediaHeight = height;
	if (plugin)
	{
		plugin->setSize(width, height);
	}
}
void LLViewerMediaImpl::mouseLeftDoubleClick(S32 x, S32 y)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	scaleMouse(&x, &y);
	mLastMouseX = x;
	mLastMouseY = y;
	if (plugin)
	{
		plugin->mouseEvent(LLPluginClassMedia::MOUSE_EVENT_DOUBLE_CLICK, LEFT_BUTTON, x, y, 0);
	}
}
void LLViewerMediaImpl::mouseMove(S32 x, S32 y)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	scaleMouse(&x, &y);
	mLastMouseX = x;
	mLastMouseY = y;
	if (plugin)
	{
		plugin->mouseEvent(LLPluginClassMedia::MOUSE_EVENT_MOVE, LEFT_BUTTON, x, y, 0);
	}
}
bool LLViewerMediaImpl::isMediaPaused()
{
	bool result = false;
	LLPluginClassMedia* plugin = getMediaPlugin();

	if(plugin)
	{
		if(plugin->getStatus() == MEDIA_PAUSED)
			result = true;
	}
	
	return result;
}
bool LLViewerMediaImpl::isMediaPlaying()
{
	bool result = false;
	LLPluginClassMedia* plugin = getMediaPlugin();
	
	if(plugin)
	{
		EMediaStatus status = plugin->getStatus();
		if(status == MEDIA_PLAYING || status == MEDIA_LOADING)
			result = true;
	}
	
	return result;
}
void LLViewerMediaImpl::focus(bool focus)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (plugin)
	{
		// call focus just for the hell of it, even though this apopears to be a nop
		plugin->focus(focus);
		if (focus)
		{
			// spoof a mouse click to *actually* pass focus
			// Don't do this anymore -- it actually clicks through now.
//			plugin->mouseEvent(LLPluginClassMedia::MOUSE_EVENT_DOWN, 1, 1, 0);
//			plugin->mouseEvent(LLPluginClassMedia::MOUSE_EVENT_UP, 1, 1, 0);
		}
	}
}
bool LLViewerMediaImpl::handleUnicodeCharHere(llwchar uni_char)
{
	bool result = false;
	LLPluginClassMedia* plugin = getMediaPlugin();
	
	if (plugin)
	{
		// only accept 'printable' characters, sigh...
		if (uni_char >= 32 // discard 'control' characters
			&& uni_char != 127) // SDL thinks this is 'delete' - yuck.
		{
			LLSD native_key_data = LLSD::emptyMap(); 
			
			plugin->textInput(wstring_to_utf8str(LLWString(1, uni_char)), gKeyboard->currentMask(FALSE), native_key_data);
		}
	}
	
	return result;
}
bool LLViewerMediaImpl::handleKeyHere(KEY key, MASK mask)
{
	bool result = false;
	LLPluginClassMedia* plugin = getMediaPlugin();
	
	if (plugin)
	{
		// FIXME: THIS IS SO WRONG.
		// Menu keys should be handled by the menu system and not passed to UI elements, but this is how LLTextEditor and LLLineEditor do it...
		if( MASK_CONTROL & mask )
		{
			if( 'C' == key )
			{
				plugin->copy();
				result = true;
			}
			else
			if( 'V' == key )
			{
				plugin->paste();
				result = true;
			}
			else
			if( 'X' == key )
			{
				plugin->cut();
				result = true;
			}
		}
		
		if(!result)
		{
			
			LLSD native_key_data = LLSD::emptyMap(); 
			
			result = plugin->keyEvent(LLPluginClassMedia::KEY_EVENT_DOWN ,key, mask, native_key_data);
			// Since the viewer internal event dispatching doesn't give us key-up events, simulate one here.
			(void)plugin->keyEvent(LLPluginClassMedia::KEY_EVENT_UP ,key, mask, native_key_data);
		}
	}
	
	return result;
}
void LLViewerMediaImpl::setVisible(bool visible)
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	mVisible = visible;
	
	if(mVisible)
	{
		if(plugin && plugin->isPluginExited())
		{
			destroyMediaSource();
		}
		
		if(!plugin)
		{
			createMediaSource();
		}
	}
	
	if(plugin)
	{
		plugin->setPriority(mVisible?LLPluginClassBasic::PRIORITY_NORMAL:LLPluginClassBasic::PRIORITY_SLEEP);
	}
}
/*LLViewerMediaTexture*/LLViewerTexture* LLViewerMediaImpl::updatePlaceholderImage()
{
	if(mTextureId.isNull())
	{
		// The code that created this instance will read from the plugin's bits.
		return NULL;
	}
	
	LLViewerMediaTexture* placeholder_image = (LLViewerMediaTexture*)LLViewerTextureManager::getFetchedTexture( mTextureId );
	LLPluginClassMedia* plugin = getMediaPlugin();

	placeholder_image->getLastReferencedTimer()->reset();

	if (mNeedsNewTexture 
		|| placeholder_image->getUseMipMaps()
		|| ! placeholder_image->mIsMediaTexture
		|| (placeholder_image->getWidth() != plugin->getTextureWidth())
		|| (placeholder_image->getHeight() != plugin->getTextureHeight())
		|| (mTextureUsedWidth != plugin->getWidth())
		|| (mTextureUsedHeight != plugin->getHeight())
		)
	{
		llinfos << "initializing media placeholder" << llendl;
		llinfos << "movie image id " << mTextureId << llendl;

		int texture_width = plugin->getTextureWidth();
		int texture_height = plugin->getTextureHeight();
		int texture_depth = plugin->getTextureDepth();
		
		// MEDIAOPT: check to see if size actually changed before doing work
		placeholder_image->destroyGLTexture();
		// MEDIAOPT: apparently just calling setUseMipMaps(FALSE) doesn't work?
		placeholder_image->reinit(FALSE);	// probably not needed

		// MEDIAOPT: seems insane that we actually have to make an imageraw then
		// immediately discard it
		LLPointer<LLImageRaw> raw = new LLImageRaw(texture_width, texture_height, texture_depth);
		raw->clear(0x0f, 0x0f, 0x0f, 0xff);
		int discard_level = 0;

		// ask media source for correct GL image format constants
		placeholder_image->setExplicitFormat(plugin->getTextureFormatInternal(),
											 plugin->getTextureFormatPrimary(),
											 plugin->getTextureFormatType(),
											 plugin->getTextureFormatSwapBytes());

		placeholder_image->createGLTexture(discard_level, raw);

		// placeholder_image->setExplicitFormat()
		placeholder_image->setUseMipMaps(FALSE);

		// MEDIAOPT: set this dynamically on play/stop
		placeholder_image->mIsMediaTexture = true;
		mNeedsNewTexture = false;
				
		// If the amount of the texture being drawn by the media goes down in either width or height, 
		// recreate the texture to avoid leaving parts of the old image behind.
		mTextureUsedWidth = plugin->getWidth();
		mTextureUsedHeight = plugin->getHeight();
	}
	
	return placeholder_image;
}
void LLViewerMediaImpl::update()
{
	LLPluginClassMedia* plugin = getMediaPlugin();
	if (!plugin)
	{
		return;
	}
	
	plugin->idle();
	
	if (plugin->isPluginExited())
	{
		destroyMediaSource();
		return;
	}

	if (!plugin->textureValid())
	{
		return;
	}
	
	if(mSuspendUpdates || !mVisible)
	{
		return;
	}
	
	LLViewerTexture* placeholder_image = updatePlaceholderImage();
		
	if(placeholder_image)
	{
		LLRect dirty_rect;
		if (plugin->getDirty(&dirty_rect))
		{
			// Constrain the dirty rect to be inside the texture
			S32 x_pos = llmax(dirty_rect.mLeft, 0);
			S32 y_pos = llmax(dirty_rect.mBottom, 0);
			S32 width = llmin(dirty_rect.mRight, placeholder_image->getWidth()) - x_pos;
			S32 height = llmin(dirty_rect.mTop, placeholder_image->getHeight()) - y_pos;
			
			if(width > 0 && height > 0)
			{

				U8* data = plugin->getBitsData();

				// Offset the pixels pointer to match x_pos and y_pos
				data += ( x_pos * plugin->getTextureDepth() * plugin->getBitsWidth() );
				data += ( y_pos * plugin->getTextureDepth() );
				
				placeholder_image->setSubImage(
						data, 
						plugin->getBitsWidth(), 
						plugin->getBitsHeight(),
						x_pos, 
						y_pos, 
						width, 
						height,
						TRUE);		// force a fast update (i.e. don't call analyzeAlpha, etc.)

			}
			
			plugin->resetDirty();
		}
	}
}