Files
libreoffice/avmedia/source/viewer/mediawindowbase_impl.cxx

552 lines
15 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2004-08-23 08:04:42 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2004-08-23 08:04:42 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2004-08-23 08:04:42 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2004-08-23 08:04:42 +00:00
*
* This file is part of OpenOffice.org.
2004-08-23 08:04:42 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2004-08-23 08:04:42 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2004-08-23 08:04:42 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2004-08-23 08:04:42 +00:00
*
************************************************************************/
#include "mediawindowbase_impl.hxx"
#include <avmedia/mediaitem.hxx>
2004-08-23 08:04:42 +00:00
#include "mediamisc.hxx"
#include "mediawindow.hrc"
#include <rtl/oustringostreaminserter.hxx>
#include <sal/log.hxx>
#include <osl/file.hxx>
2004-08-23 08:04:42 +00:00
#include <tools/urlobj.hxx>
#include <ucbhelper/content.hxx>
2004-08-23 08:04:42 +00:00
#include <comphelper/processfactory.hxx>
#include <comphelper/storagehelper.hxx>
2004-08-23 08:04:42 +00:00
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XComponent.hdl>
#include <com/sun/star/media/XManager.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/document/XStorageBasedDocument.hpp>
2004-08-23 08:04:42 +00:00
#define MEDIA_TIMER_TIMEOUT 100
using namespace ::com::sun::star;
namespace avmedia { namespace priv {
// -----------------------
// - MediaWindowBaseImpl -
// -----------------------
MediaWindowBaseImpl::MediaWindowBaseImpl( MediaWindow* pMediaWindow )
: mpTempFileURL(0)
, mpMediaWindow( pMediaWindow )
2004-08-23 08:04:42 +00:00
{
}
// ---------------------------------------------------------------------
MediaWindowBaseImpl::~MediaWindowBaseImpl()
{
uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
}
// -------------------------------------------------------------------------
uno::Reference< media::XPlayer > MediaWindowBaseImpl::createPlayer( const ::rtl::OUString& rURL )
{
uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
uno::Reference< media::XPlayer > xPlayer;
if( xFactory.is() )
{
try
{
uno::Reference< ::com::sun::star::media::XManager > xManager(
xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_MANAGER_SERVICE_NAME )) ),
2004-08-23 08:04:42 +00:00
uno::UNO_QUERY );
if( xManager.is() )
{
xPlayer = uno::Reference< ::com::sun::star::media::XPlayer >(
xManager->createPlayer( rURL ), uno::UNO_QUERY );
}
else
SAL_WARN(
"avmedia",
("failed to create media player service "
AVMEDIA_MANAGER_SERVICE_NAME));
2004-08-23 08:04:42 +00:00
}
catch( const uno::Exception &e )
2004-08-23 08:04:42 +00:00
{
SAL_WARN(
"avmedia",
"couldn't create media player " AVMEDIA_MANAGER_SERVICE_NAME
", exception '" << e.Message << '\'');
2004-08-23 08:04:42 +00:00
}
}
return xPlayer;
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::cleanupTempFile()
{
if (mpTempFileURL)
{
::osl::File::remove(*mpTempFileURL);
delete mpTempFileURL;
mpTempFileURL = 0;
}
}
2004-08-23 08:04:42 +00:00
bool
MediaWindowBaseImpl::initPackageURL(::rtl::OUString const & rURL,
uno::Reference<frame::XModel> const& xModel)
2004-08-23 08:04:42 +00:00
{
uno::Reference<document::XStorageBasedDocument> const xSBD(
xModel, uno::UNO_QUERY);
if (!xSBD.is())
{
SAL_WARN("avmedia", "cannot get model");
return false;
}
uno::Reference<embed::XStorage> const xStorage(
xSBD->getDocumentStorage());
if (!xStorage.is())
{
SAL_WARN("avmedia", "cannot get storage");
return false;
}
::comphelper::LifecycleProxy proxy;
uno::Reference<io::XInputStream> xInStream;
try {
uno::Reference<io::XStream> const xStream(
::comphelper::OStorageHelper::GetStreamAtPackageURL(
xStorage, rURL, embed::ElementModes::READ, proxy));
xInStream = (xStream.is()) ? xStream->getInputStream() : 0;
}
catch (container::NoSuchElementException const&)
{
SAL_INFO("avmedia", "not found: '" << ::rtl::OUString(rURL) << "'");
return false;
}
catch (uno::Exception const& e)
{
SAL_WARN("avmedia", "exception: '" << e.Message << "'");
return false;
}
if (!xInStream.is())
2004-08-23 08:04:42 +00:00
{
SAL_WARN("avmedia", "no stream?");
return false;
}
mpTempFileURL = new ::rtl::OUString;
::osl::FileBase::RC const err =
::osl::FileBase::createTempFile(0, 0, mpTempFileURL);
if (::osl::FileBase::E_None != err)
{
SAL_INFO("avmedia", "cannot create temp file");
delete mpTempFileURL;
mpTempFileURL = 0;
return false;
}
2004-08-23 08:04:42 +00:00
try
{
::ucbhelper::Content tempContent(*mpTempFileURL,
uno::Reference<ucb::XCommandEnvironment>());
tempContent.writeStream(xInStream, true); // copy stream to file
}
catch (uno::Exception const& e)
{
SAL_WARN("avmedia", "exception: '" << e.Message << "'");
return false;
}
return true;
}
static char const s_PkgScheme[] = "vnd.sun.star.Package:";
void MediaWindowBaseImpl::setURL( const ::rtl::OUString& rURL,
uno::Reference<frame::XModel> const& xModel )
{
if( rURL != getURL() )
{
2004-08-23 08:04:42 +00:00
if( mxPlayer.is() )
mxPlayer->stop();
if( mxPlayerWindow.is() )
{
mxPlayerWindow->setVisible( false );
mxPlayerWindow.clear();
}
mxPlayer.clear();
cleanupTempFile();
2004-08-23 08:04:42 +00:00
bool bSuccess(true);
if (0 == rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
rURL.getStr(), rURL.getLength(),
s_PkgScheme, SAL_N_ELEMENTS(s_PkgScheme) - 1))
{
bSuccess = initPackageURL(rURL, xModel);
2004-08-23 08:04:42 +00:00
maFileURL = (bSuccess) ? rURL : ::rtl::OUString();
}
else
{
INetURLObject aURL( rURL );
if (aURL.GetProtocol() != INET_PROT_NOT_VALID)
maFileURL = aURL.GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS);
else
maFileURL = rURL;
}
if (bSuccess)
{
mxPlayer = createPlayer(
(mpTempFileURL) ? *mpTempFileURL : maFileURL );
}
2004-08-23 08:04:42 +00:00
onURLChanged();
}
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::onURLChanged()
{
}
// ---------------------------------------------------------------------
const ::rtl::OUString& MediaWindowBaseImpl::getURL() const
{
return maFileURL;
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::isValid() const
{
return( getPlayer().is() );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::stopPlayingInternal( bool bStop )
{
if( isPlaying() )
{
bStop ? mxPlayer->stop() : mxPlayer->start();
2004-08-23 08:04:42 +00:00
}
}
// ---------------------------------------------------------------------
MediaWindow* MediaWindowBaseImpl::getMediaWindow() const
{
return mpMediaWindow;
}
// ---------------------------------------------------------------------
uno::Reference< media::XPlayer > MediaWindowBaseImpl::getPlayer() const
{
return mxPlayer;
}
// ---------------------------------------------------------------------
uno::Reference< media::XPlayerWindow > MediaWindowBaseImpl::getPlayerWindow() const
{
return mxPlayerWindow;
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference< media::XPlayerWindow >& rxPlayerWindow )
{
mxPlayerWindow = rxPlayerWindow;
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::cleanUp()
{
if( mxPlayer.is() )
{
2004-08-23 08:04:42 +00:00
mxPlayer->stop();
uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
if( xComponent.is() )
xComponent->dispose();
mxPlayer.clear();
2004-08-23 08:04:42 +00:00
}
cleanupTempFile();
2004-08-23 08:04:42 +00:00
mpMediaWindow = NULL;
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::hasPreferredSize() const
{
return( mxPlayerWindow.is() );
}
// ---------------------------------------------------------------------
Size MediaWindowBaseImpl::getPreferredSize() const
{
Size aRet;
if( mxPlayer.is() )
{
awt::Size aPrefSize( mxPlayer->getPreferredPlayerWindowSize() );
aRet.Width() = aPrefSize.Width;
aRet.Height() = aPrefSize.Height;
}
return aRet;
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
{
return( mxPlayerWindow.is() ? mxPlayerWindow->setZoomLevel( eLevel ) : false );
}
// -------------------------------------------------------------------------
::com::sun::star::media::ZoomLevel MediaWindowBaseImpl::getZoom() const
{
return( mxPlayerWindow.is() ? mxPlayerWindow->getZoomLevel() : ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE );
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::start()
{
return( mxPlayer.is() ? ( mxPlayer->start(), true ) : false );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::stop()
{
if( mxPlayer.is() )
mxPlayer->stop();
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::isPlaying() const
{
return( mxPlayer.is() && mxPlayer->isPlaying() );
}
// ---------------------------------------------------------------------
double MediaWindowBaseImpl::getDuration() const
{
return( mxPlayer.is() ? mxPlayer->getDuration() : 0.0 );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setMediaTime( double fTime )
{
if( mxPlayer.is() )
mxPlayer->setMediaTime( fTime );
}
// ---------------------------------------------------------------------
double MediaWindowBaseImpl::getMediaTime() const
{
return( mxPlayer.is() ? mxPlayer->getMediaTime() : 0.0 );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setStopTime( double fTime )
{
if( mxPlayer.is() )
mxPlayer->setStopTime( fTime );
}
// ---------------------------------------------------------------------
double MediaWindowBaseImpl::getStopTime() const
{
return( mxPlayer.is() ? mxPlayer->getStopTime() : 0.0 );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setRate( double fRate )
{
if( mxPlayer.is() )
mxPlayer->setRate( fRate );
}
// ---------------------------------------------------------------------
double MediaWindowBaseImpl::getRate() const
{
return( mxPlayer.is() ? mxPlayer->getRate() : 0.0 );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setPlaybackLoop( bool bSet )
{
if( mxPlayer.is() )
mxPlayer->setPlaybackLoop( bSet );
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::isPlaybackLoop() const
{
return( mxPlayer.is() ? mxPlayer->isPlaybackLoop() : false );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setMute( bool bSet )
{
if( mxPlayer.is() )
mxPlayer->setMute( bSet );
}
// ---------------------------------------------------------------------
bool MediaWindowBaseImpl::isMute() const
{
return( mxPlayer.is() ? mxPlayer->isMute() : false );
}
// ---------------------------------------------------------------------
void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB )
{
if( mxPlayer.is() )
mxPlayer->setVolumeDB( nVolumeDB );
}
// ---------------------------------------------------------------------
sal_Int16 MediaWindowBaseImpl::getVolumeDB() const
{
return( mxPlayer.is() ? mxPlayer->getVolumeDB() : 0 );
}
// -------------------------------------------------------------------------
void MediaWindowBaseImpl::updateMediaItem( MediaItem& rItem ) const
{
if( isPlaying() )
rItem.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW : MEDIASTATE_PLAY );
else
rItem.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP : MEDIASTATE_PAUSE );
rItem.setDuration( getDuration() );
rItem.setTime( getMediaTime() );
rItem.setLoop( isPlaybackLoop() );
rItem.setMute( isMute() );
rItem.setVolumeDB( getVolumeDB() );
rItem.setZoom( getZoom() );
rItem.setURL( getURL(), 0 );
2004-08-23 08:04:42 +00:00
}
// -------------------------------------------------------------------------
void MediaWindowBaseImpl::executeMediaItem( const MediaItem& rItem )
{
const sal_uInt32 nMaskSet = rItem.getMaskSet();
// set URL first
if( nMaskSet & AVMEDIA_SETMASK_URL )
setURL( rItem.getURL(), rItem.getModel() );
2004-08-23 08:04:42 +00:00
// set different states next
if( nMaskSet & AVMEDIA_SETMASK_TIME )
setMediaTime( ::std::min( rItem.getTime(), getDuration() ) );
if( nMaskSet & AVMEDIA_SETMASK_LOOP )
setPlaybackLoop( rItem.isLoop() );
if( nMaskSet & AVMEDIA_SETMASK_MUTE )
setMute( rItem.isMute() );
if( nMaskSet & AVMEDIA_SETMASK_VOLUMEDB )
setVolumeDB( rItem.getVolumeDB() );
if( nMaskSet & AVMEDIA_SETMASK_ZOOM )
setZoom( rItem.getZoom() );
// set play state at last
if( nMaskSet & AVMEDIA_SETMASK_STATE )
{
switch( rItem.getState() )
{
case( MEDIASTATE_PLAY ):
case( MEDIASTATE_PLAYFFW ):
{
if( !isPlaying() )
start();
}
break;
case( MEDIASTATE_PAUSE ):
{
if( isPlaying() )
stop();
}
break;
case( MEDIASTATE_STOP ):
{
if( isPlaying() )
{
setMediaTime( 0.0 );
stop();
setMediaTime( 0.0 );
}
}
break;
}
}
}
} // namespace priv
} // namespace avemdia
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */