addDynamicXCode(
final DiskManagerFileInfo source )
{
final TranscodeProfile profile = dynamic_transcode_profile;
IPCInterface ipc = upnpav_ipc;
if ( profile == null || ipc == null ){
return;
}
try{
TranscodeFileImpl transcode_file = allocateFile( profile, false, source, false );
AzureusContentFile acf = (AzureusContentFile)transcode_file.getTransientProperty( UPNPAV_FILE_KEY );
if ( acf != null ){
return;
}
final String tf_key = transcode_file.getKey();
synchronized( acf_map ){
acf = acf_map.get( tf_key );
}
if ( acf != null ){
return;
}
final DiskManagerFileInfo stream_file =
new DiskManagerFileInfoStream(
new DiskManagerFileInfoStream.StreamFactory()
{
private List<Object> current_requests = new ArrayList<Object>();
public StreamDetails
getStream(
Object request )
throws IOException
{
try{
TranscodeJobImpl job = getManager().getTranscodeManager().getQueue().add(
(TranscodeTarget)DeviceUPnPImpl.this,
profile,
source,
false,
true,
TranscodeTarget.TRANSCODE_UNKNOWN );
synchronized( this ){
current_requests.add( request );
}
while( true ){
InputStream is = job.getStream( 1000 );
if ( is != null ){
return( new StreamWrapper( is, job ));
}
int state = job.getState();
if ( state == TranscodeJobImpl.ST_FAILED ){
throw( new IOException( "Transcode failed: " + job.getError()));
}else if ( state == TranscodeJobImpl.ST_CANCELLED ){
throw( new IOException( "Transcode failed: job cancelled" ));
}else if ( state == TranscodeJobImpl.ST_COMPLETE ){
throw( new IOException( "Job complete but no stream!" ));
}
synchronized( this ){
if ( !current_requests.contains( request )){
break;
}
}
System.out.println( "waiting for stream" );
}
IOException error = new IOException( "Stream request cancelled" );
job.failed( error );
throw( error );
}catch( IOException e ){
throw( e );
}catch( Throwable e ){
throw( new IOException( "Failed to add transcode job: " + Debug.getNestedExceptionMessage(e)));
}finally{
synchronized( this ){
current_requests.remove( request );
}
}
}
public void
destroyed(
Object request )
{
synchronized( this ){
current_requests.remove( request );
}
}
},
transcode_file.getCacheFile());
acf = new AzureusContentFile()
{
public DiskManagerFileInfo
getFile()
{
return( stream_file );
}
public Object
getProperty(
String name )
{
// TODO: duration etc
if ( name.equals( MY_ACF_KEY )){
return( new Object[]{ DeviceUPnPImpl.this, tf_key });
}else if ( name.equals( PT_PERCENT_DONE )){
return( new Long(1000));
}else if ( name.equals( PT_ETA )){
return( new Long(0));
}
return( null );
}
};
synchronized( acf_map ){
acf_map.put( tf_key, acf );
}
transcode_file.setTransientProperty( UPNPAV_FILE_KEY, acf );
syncCategories( transcode_file, true );
synchronized( this ){
if ( dynamic_xcode_map == null ){
dynamic_xcode_map = new HashMap<String,AzureusContentFile>();
}
dynamic_xcode_map.put( tf_key, acf );
}
ipc.invoke( "addContent", new Object[]{ acf });
}catch( Throwable e ){
Debug.out( e );
}