devices/devicevideo/ordevicevideo_device.cxx

devices/devicevideo/ordevicevideo_device.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declaration
#include "ordevicevideo_device.h"
//--- FBX load/save tags
#define FBX_CHANNEL_TAG "Channels"
#define FBX_VERSION_TAG "Version"
//--- Device strings
#define ORDEVICEVIDEO__CLASS ORDEVICEVIDEO__CLASSNAME
#define ORDEVICEVIDEO__NAME ORDEVICEVIDEO__CLASSSTR
#define ORDEVICEVIDEO__LABEL "OR - Video Device"
#define ORDEVICEVIDEO__DESC "OR - Video Manip/Switcher Device"
//--- FiLMBOX implementation and registration
FBDeviceImplementation ( ORDEVICEVIDEO__CLASS );
FBRegisterDevice ( ORDEVICEVIDEO__NAME,
ORDEVICEVIDEO__CLASS,
ORDEVICEVIDEO__LABEL,
ORDEVICEVIDEO__DESC,
"object_video.png" ); // Icon filename (default=Open Reality icon)
static int gCount = 0;
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ORDeviceVideo::FBCreate()
{
gCount++;
// Configure device
mVideoSwitcher = NULL;
// Existing media
int i;
int lCnt = mSystem.Scene->Components.GetCount();
for(i=0;i<lCnt;i++)
{
FBComponent* lComponent = mSystem.Scene->Components[i];
if( lComponent->Is( FBVideoSwitcher::TypeInfo ) )
{
FBVideoSwitcher* lVSwitch = (FBVideoSwitcher*)lComponent;
if( lVSwitch->IsSDKSwitcher() )
{
FBString lDevName = (const char*)lVSwitch->DeviceOwner->Name;
FBString lThisName = (const char*)this->Name;
if( lDevName == lThisName )
{
mVideoSwitcher = lVSwitch;
break;
}
}
}
}
if( !mVideoSwitcher )
{
char lBuffer[256];
sprintf( lBuffer, "Video Switcher (%d)", gCount );
mVideoSwitcher = new FBVideoSwitcher(lBuffer);
}
mVideoSwitcher->DeviceOwner = this;
mCurrentIndex = 0;
mModePlayback = kORPlaybackFreeRunning;
// Create connectors
mNodeFrame_IN = AnimationNodeInCreate ( 2, "Frame Global (In )", ANIMATIONNODE_TYPE_NUMBER );
mNodeFrame_OUT = AnimationNodeOutCreate( 3, "Frame Global (Out)", ANIMATIONNODE_TYPE_NUMBER );
mNodeFrameLocal_IN = AnimationNodeInCreate ( 2, "Frame Local (In )", ANIMATIONNODE_TYPE_NUMBER );
mNodeFrameLocal_OUT = AnimationNodeOutCreate( 3, "Frame Local (Out)", ANIMATIONNODE_TYPE_NUMBER );
mNodeIndex_IN = AnimationNodeInCreate ( 4, "Index (In )", ANIMATIONNODE_TYPE_NUMBER );
mNodeIndex_OUT = AnimationNodeOutCreate( 5, "Index (Out)", ANIMATIONNODE_TYPE_NUMBER );
// Add videoclip callback
mVideoSwitcher->OnClipEnd.Add( this, (FBCallback) &ORDeviceVideo::VideoClipEndCallback );
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ORDeviceVideo::FBDestroy()
{
gCount--;
// Remove videoswitcher callback
mVideoSwitcher->OnClipEnd.Remove( this, (FBCallback) &ORDeviceVideo::VideoClipEndCallback );
mVideoSwitcher->DeviceOwner = NULL;
delete mVideoSwitcher;
mVideoSwitcher = NULL;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
unsigned long ORDeviceVideo::GetTotalFrames()
{
unsigned long lTotal=0;
int i;
for(i=0;i<mClips.GetCount();i++)
{
if(mClips[i]->GetVideo())
{
lTotal+= mClips[i]->GetVideo()->LastFrame + 1;
}
}
return lTotal;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
FBVideoClip* ORDeviceVideo::GetCurrentVideo()
{
if( mCurrentIndex < mClips.GetCount() && mCurrentIndex >= 0 )
{
return mClips[mCurrentIndex]->GetVideo();
}
return NULL;
}
int ORDeviceVideo::AddNewVideoClip()
{
return mClips.Add( new ORVideoEntry( NULL ) );
}
/************************************************
* Device operation.
************************************************/
bool ORDeviceVideo::DeviceOperation( kDeviceOperations pOperation )
{
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpStop: return Stop();
case kOpReset: return Reset();
case kOpDone: return Done();
}
return FBDevice::DeviceOperation( pOperation );
}
/************************************************
* Initialization of device.
************************************************/
bool ORDeviceVideo::Init()
{
return true;
}
/************************************************
* Device is put online.
************************************************/
bool ORDeviceVideo::Start()
{
int i;
for( i=0; i<mClips.GetCount(); i++ )
{
if( !mClips[i]->GetVideo() )
{
FBMessageBox( "Video Device Error", "Some clips have not been assigned media", "OK", NULL, NULL, 1 );
return false;
}
}
// Reset video switcher params
mCurrentIndex = 0;
mVideoSwitcher->ResetVideoSwitcher();
if( mClips.GetCount() > 0 )
{
mVideoSwitcher->SetNextClip( mClips[0]->GetVideo(), kFBPlayModePlay );
}
return true;
}
/************************************************
* Device is stopped (offline).
************************************************/
bool ORDeviceVideo::Stop()
{
mCurrentIndex = 0;
return false;
}
/************************************************
* Removal of device.
************************************************/
bool ORDeviceVideo::Done()
{
return false;
}
/************************************************
* Reset of device.
************************************************/
bool ORDeviceVideo::Reset()
{
Stop();
return Start();
}
/************************************************
* Animation node notify.
************************************************/
bool ORDeviceVideo::AnimationNodeNotify(FBAnimationNode* pAnimationNode ,FBEvaluateInfo* pEvaluateInfo)
{
double lFrame, lFrameLocal, lIndex;
// Read in from connectors
mNodeFrame_IN ->ReadData ( &lFrame, pEvaluateInfo );
mNodeFrameLocal_IN ->ReadData ( &lFrameLocal, pEvaluateInfo );
mNodeIndex_IN ->ReadData ( &lIndex, pEvaluateInfo );
// Write out to other side.
mNodeFrame_OUT ->WriteData( &lFrame, pEvaluateInfo );
mNodeFrameLocal_OUT ->WriteData( &lFrameLocal, pEvaluateInfo );
mNodeIndex_OUT ->WriteData( &lIndex, pEvaluateInfo );
return true;
}
/************************************************
* Device Evaluation Notify.
************************************************/
bool ORDeviceVideo::DeviceEvaluationNotify(kTransportMode pMode, FBEvaluateInfo* pEvaluateInfo)
{
switch(pMode)
{
case kStop:
case kPlay:
{
if( mVideoSwitcher )
{
switch( mModePlayback )
{
case kORPlaybackFreeRunning:
{
mVideoSwitcher->PrepareNextFrame();
}
break;
case kORPlaybackGlobalFrame:
{
double lFrame;
mNodeFrame_IN->ReadData( &lFrame, pEvaluateInfo );
int lIntFrame,i;
bool lFound = false;
int lCount=0,lNextCount=0;
int lFrameCnt;
lIntFrame = (int) lFrame;
if( GetLoopMode() )
{
int lTotalFrame = GetTotalFrames();
if( lTotalFrame != 0 )
lIntFrame %= lTotalFrame;
}
lFrameCnt = mClips.GetCount();
for( i=0; i<lFrameCnt && !lFound; i++ )
{
lNextCount = lCount + mClips[i]->GetVideo()->LastFrame + 1;
if( lIntFrame >= lCount && lIntFrame < lNextCount )
{
mVideoSwitcher->SwapCurrent( mClips[i]->GetVideo() );
mClips[i]->GetVideo()->CurrentFrame = lIntFrame-lCount;
mCurrentIndex = i;
lFound = true;
}
lCount = lNextCount;
}
}
break;
case kORPlaybackLocalFrame:
{
double lFrame;
double lIndex;
mNodeIndex_IN->ReadData( &lIndex, pEvaluateInfo );
mNodeFrameLocal_IN ->ReadData( &lFrame, pEvaluateInfo );
int i=(int)lIndex;
int lClipCount = mClips.GetCount();
if( GetLoopMode() )
{
if( i<0 ) i = 0;
if( lClipCount != 0 ) i %= lClipCount;
}
else
{
if( i<0 ) i = 0;
if( i >= lClipCount ) i=lClipCount-1;
}
if( i >= 0 && i < lClipCount )
{
mVideoSwitcher->SwapCurrent( mClips[i]->GetVideo() );
mClips[i]->GetVideo()->CurrentFrame = lFrame;
mCurrentIndex = i;
}
}
break;
}
}
AckOneSampleReceived();
}
break;
}
return true;
}
/************************************************
* Device IO Notify.
************************************************/
void ORDeviceVideo::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
switch (pAction)
{
case kIOPlayModeWrite:
case kIOStopModeWrite:
{
}
break;
case kIOStopModeRead:
case kIOPlayModeRead:
{
}
break;
}
}
typedef FBArrayTemplate<FBVideoClip*> FBVideoArray;
static void GetVideoMediaList( FBVideoArray &pArray )
{
static FBSystem sSystem;
int z;
int lZMax = sSystem.Scene->VideoClips.GetCount();
for( z=0; z<lZMax; z++ )
{
pArray.Add( sSystem.Scene->VideoClips[z] );
}
}
/************************************************
* Store data in FBX.
************************************************/
#define FBX_CLIPLIST_TAG "Playlist"
bool ORDeviceVideo::FbxStore(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
// store clip list
if (pStoreWhat & kAttributes)
{
pFbxObject->FieldWriteBegin( FBX_CLIPLIST_TAG );
{
// Device configuration
pFbxObject->FieldWriteI( GetLoopMode() ? 1:0 );
pFbxObject->FieldWriteI( (int) GetPlaybackMode() );
// Clip list.
FBVideoArray lVideoMedia;
GetVideoMediaList( lVideoMedia );
int lCnt = mClips.GetCount();
int i;
FBVideoClip* lVideo = NULL;
int lIndex = -1;
// # of clips
pFbxObject->FieldWriteI( lCnt );
for( i=0; i<lCnt; i++ )
{
// Name & looping information
pFbxObject->FieldWriteC( mClips[i]->GetName() );
pFbxObject->FieldWriteI( mClips[i]->GetLoopCount() );
pFbxObject->FieldWriteI( mClips[i]->GetLoopsLeft() );
// Video reference stuff
lVideo = mClips[i]->GetVideo();
if( lVideo )
{
lIndex = lVideoMedia.Find( lVideo );
}
else
{
lIndex = -1;
}
pFbxObject->FieldWriteI( lIndex );
}
}
pFbxObject->FieldWriteEnd();
}
return true;
}
/************************************************
* Retrieve data from FBX.
************************************************/
bool ORDeviceVideo::FbxRetrieve(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
// retrieve clip list
if (pStoreWhat & kAttributes)
{
pFbxObject->FieldReadBegin( FBX_CLIPLIST_TAG );
{
// Device info.
SetLoopMode ( pFbxObject->FieldReadI() != 0 );
SetPlaybackMode ( (ORPlaybackMode) pFbxObject->FieldReadI() );
// Clip playlist info.
FBVideoArray lVideoMedia;
GetVideoMediaList( lVideoMedia );
int lCnt = pFbxObject->FieldReadI();
int i;
int lVideoIx, lClipIx;
int lLoopCnt, lLoopLeft;
FBString lName;
for( i=0; i<lCnt; i++ )
{
lName = pFbxObject->FieldReadC();
lLoopCnt = pFbxObject->FieldReadI();
lLoopLeft = pFbxObject->FieldReadI();
lVideoIx = pFbxObject->FieldReadI();
lClipIx = AddNewVideoClip();
mClips[lClipIx]->SetName ( lName );
mClips[lClipIx]->SetLoopCount ( lLoopCnt );
mClips[lClipIx]->SetLoopsLeft ( lLoopLeft );
if( lVideoIx != -1 )
{
mClips[lClipIx]->SetVideo( lVideoMedia[lVideoIx] );
}
}
pFbxObject->FieldReadEnd();
}
}
return true;
}
/************************************************
* Clip end callback.
************************************************/
void ORDeviceVideo::VideoClipEndCallback( HISender pSender, HKEvent pEvent )
{
FBEventVideoSwitcher lEvent( pEvent );
FBClipEnd lEndType = lEvent.EndType;
int lNextIndex;
int lCurBase = mCurrentIndex;
int lCount;
lCount = mClips.GetCount();
if( mCurrentIndex >= (lCount-1) && GetLoopMode() )
{
lCurBase = -1;
}
lNextIndex = lCurBase + 1;
// Index less than # of clips
if( lCurBase < (lCount-1) )
{
ORVideoEntry* lCurClp = mClips[mCurrentIndex];
ORVideoEntry* lNxtClp = mClips[lNextIndex];
int lLoopsLeft = lCurClp->GetLoopsLeft();
if( lLoopsLeft > 0 )
{
lCurClp->SetLoopsLeft( --lLoopsLeft );
mVideoSwitcher->SetNextClip( lCurClp->GetVideo(), kFBPlayModePlay );
}
else
{
lCurClp->SetLoopsLeft( lCurClp->GetLoopCount() );
if( lNextIndex >= (lCount) )
{
lNextIndex = -1;
}
if( lEndType == kFBClipEndEnd && lNextIndex != -1 )
{
mVideoSwitcher->SetNextClip( lNxtClp->GetVideo(), kFBPlayModePlay );
if( lCurBase == -1 )
{
mVideoSwitcher->PlayMode = kFBPlayModePlay;
mCurrentIndex = -1;
}
mCurrentIndex ++;
}
}
}
}
/************************************************
* Get system time or local time for playback.
************************************************/
bool ORDeviceVideo::GetUsingSystemTime()
{
if( mVideoSwitcher )
{
return mVideoSwitcher->UsingSystemTime;
}
return true;
}
/************************************************
* Set using the system time for time playback.
************************************************/
void ORDeviceVideo::SetUsingSystemTime( bool pState )
{
mVideoSwitcher->UsingSystemTime = pState;
}
ORPlaybackMode ORDeviceVideo::GetPlaybackMode()
{
return mModePlayback;
}
void ORDeviceVideo::SetPlaybackMode( ORPlaybackMode pMode )
{
mModePlayback = pMode;
switch( pMode )
{
case kORPlaybackFreeRunning: this->SetUsingSystemTime(true); break;
case kORPlaybackGlobalFrame: break;
case kORPlaybackLocalFrame: break;
}
}