Search in sources :

Example 61 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class TranscodeProviderVuze method transcode.

@Override
public TranscodeProviderJob transcode(final TranscodeProviderAdapter _adapter, TranscodeProviderAnalysis analysis, boolean direct_input, DiskManagerFileInfo input, TranscodeProfile profile, URL output) throws TranscodeException {
    try {
        PluginInterface av_pi = plugin_interface.getPluginManager().getPluginInterfaceByID("azupnpav");
        if (av_pi == null) {
            throw (new TranscodeException("Media Server plugin not found"));
        }
        final TranscodeProviderJob[] xcode_job = { null };
        URL source_url = null;
        TranscodePipe pipe = null;
        if (direct_input) {
            if (input instanceof DiskManagerFileInfoURL) {
                ((DiskManagerFileInfoURL) input).download();
            }
            if (input.getDownloaded() == input.getLength()) {
                File file = input.getFile();
                if (file.exists() && file.length() == input.getLength()) {
                    source_url = file.toURI().toURL();
                }
            }
            if (source_url == null) {
                manager.log("Failed to use direct input as source file doesn't exist/incomplete");
            }
        }
        if (source_url == null) {
            if (input instanceof DiskManagerFileInfoURL) {
                source_url = ((DiskManagerFileInfoURL) input).getURL();
            } else {
                IPCInterface av_ipc = av_pi.getIPC();
                String url_str = (String) av_ipc.invoke("getContentURL", new Object[] { input });
                if (url_str == null || url_str.length() == 0) {
                    // see if we can use the file directly
                    File source_file = input.getFile();
                    if (source_file.exists()) {
                        pipe = new TranscodePipeFileSource(source_file, new TranscodePipe.errorListener() {

                            @Override
                            public void error(Throwable e) {
                                _adapter.failed(new TranscodeException("File access error", e));
                                if (xcode_job[0] != null) {
                                    xcode_job[0].cancel();
                                }
                            }
                        });
                        source_url = new URL("http://127.0.0.1:" + pipe.getPort() + "/");
                    } else {
                        throw (new TranscodeException("Source file doesn't exist"));
                    }
                } else {
                    source_url = new URL(url_str);
                    pipe = new TranscodePipeStreamSource(source_url.getHost(), source_url.getPort());
                    source_url = UrlUtils.setHost(source_url, "127.0.0.1");
                    source_url = UrlUtils.setPort(source_url, pipe.getPort());
                }
            }
        }
        final TranscodePipe f_pipe = pipe;
        try {
            final IPCInterface ipc = plugin_interface.getIPC();
            final Object context;
            final TranscodeProviderAdapter adapter;
            if (output.getProtocol().equals("tcp")) {
                adapter = _adapter;
                context = ipc.invoke("transcodeToTCP", new Object[] { ((TranscodeProviderAnalysisImpl) analysis).getResult(), source_url, profile.getName(), output.getPort() });
            } else {
                final File file = new File(output.toURI());
                File parent_dir = file.getParentFile();
                if (parent_dir.exists()) {
                    if (!parent_dir.canWrite()) {
                        throw (new TranscodeException("Folder '" + parent_dir.getAbsolutePath() + "' isn't writable"));
                    }
                } else {
                    if (!parent_dir.mkdirs()) {
                        throw (new TranscodeException("Failed to create folder '" + parent_dir.getAbsolutePath() + "'"));
                    }
                }
                adapter = new TranscodeProviderAdapter() {

                    @Override
                    public void updateProgress(int percent, int eta_secs, int width, int height) {
                        _adapter.updateProgress(percent, eta_secs, width, height);
                    }

                    @Override
                    public void streamStats(long connect_rate, long write_speed) {
                        _adapter.streamStats(connect_rate, write_speed);
                    }

                    @Override
                    public void failed(TranscodeException error) {
                        try {
                            file.delete();
                        } finally {
                            _adapter.failed(error);
                        }
                    }

                    @Override
                    public void complete() {
                        _adapter.complete();
                    }
                };
                context = ipc.invoke("transcodeToFile", new Object[] { ((TranscodeProviderAnalysisImpl) analysis).getResult(), source_url, profile.getName(), file });
            }
            new AEThread2("xcodeStatus", true) {

                @Override
                public void run() {
                    try {
                        boolean in_progress = true;
                        while (in_progress) {
                            in_progress = false;
                            if (f_pipe != null) {
                                adapter.streamStats(f_pipe.getConnectionRate(), f_pipe.getWriteSpeed());
                            }
                            try {
                                Map status = (Map) ipc.invoke("getTranscodeStatus", new Object[] { context });
                                long state = (Long) status.get("state");
                                if (state == 0) {
                                    int percent = (Integer) status.get("percent");
                                    Integer i_eta = (Integer) status.get("eta_secs");
                                    int eta = i_eta == null ? -1 : i_eta;
                                    Integer i_width = (Integer) status.get("new_width");
                                    int width = i_width == null ? 0 : i_width;
                                    Integer i_height = (Integer) status.get("new_height");
                                    int height = i_height == null ? 0 : i_height;
                                    adapter.updateProgress(percent, eta, width, height);
                                    if (percent == 100) {
                                        adapter.complete();
                                    } else {
                                        in_progress = true;
                                        Thread.sleep(1000);
                                    }
                                } else if (state == 1) {
                                    adapter.failed(new TranscodeException("Transcode cancelled"));
                                } else {
                                    Boolean perm_fail = (Boolean) status.get("error_is_perm");
                                    TranscodeException error = new TranscodeException("Transcode failed", (Throwable) status.get("error"));
                                    if (perm_fail != null && perm_fail) {
                                        error.setDisableRetry(true);
                                    }
                                    adapter.failed(error);
                                }
                            } catch (Throwable e) {
                                adapter.failed(new TranscodeException("Failed to get status", e));
                            }
                        }
                    } finally {
                        if (f_pipe != null) {
                            f_pipe.destroy();
                        }
                    }
                }
            }.start();
            xcode_job[0] = new TranscodeProviderJob() {

                @Override
                public void pause() {
                    if (f_pipe != null) {
                        f_pipe.pause();
                    }
                }

                @Override
                public void resume() {
                    if (f_pipe != null) {
                        f_pipe.resume();
                    }
                }

                @Override
                public void cancel() {
                    try {
                        ipc.invoke("cancelTranscode", new Object[] { context });
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }

                @Override
                public void setMaxBytesPerSecond(int max) {
                    if (f_pipe != null) {
                        f_pipe.setMaxBytesPerSecond(max);
                    }
                }
            };
            return (xcode_job[0]);
        } catch (Throwable e) {
            if (pipe != null) {
                pipe.destroy();
            }
            throw (e);
        }
    } catch (TranscodeException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new TranscodeException("transcode failed", e));
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) URL(java.net.URL) AEThread2(com.biglybt.core.util.AEThread2) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) File(java.io.File) IPCInterface(com.biglybt.pif.ipc.IPCInterface) HashMap(java.util.HashMap) Map(java.util.Map)

Example 62 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class TranscodeProviderVuze method analyse.

@Override
public TranscodeProviderAnalysis analyse(final TranscodeProviderAdapter _adapter, DiskManagerFileInfo input, TranscodeProfile profile) throws TranscodeException {
    try {
        URL source_url = null;
        File source_file = null;
        long input_length = input.getLength();
        if (input_length > 0 && input_length == input.getDownloaded()) {
            File file = input.getFile();
            if (file.exists() && file.length() == input_length) {
                source_file = file;
            }
        }
        TranscodePipe pipe = null;
        if (source_file == null) {
            if (input instanceof DiskManagerFileInfoURL) {
                source_url = ((DiskManagerFileInfoURL) input).getURL();
            } else {
                for (int i = 0; i < 10; i++) {
                    PluginInterface av_pi = plugin_interface.getPluginManager().getPluginInterfaceByID("azupnpav");
                    if (av_pi == null) {
                        throw (new TranscodeException("Media Server plugin not found"));
                    }
                    IPCInterface av_ipc = av_pi.getIPC();
                    String url_str = (String) av_ipc.invoke("getContentURL", new Object[] { input });
                    if (url_str != null && url_str.length() > 0) {
                        source_url = new URL(url_str);
                        pipe = new TranscodePipeStreamSource(source_url.getHost(), source_url.getPort());
                        source_url = UrlUtils.setHost(source_url, "127.0.0.1");
                        source_url = UrlUtils.setPort(source_url, pipe.getPort());
                    }
                    if (source_url != null) {
                        break;
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (Throwable e) {
                            break;
                        }
                    }
                }
            }
        }
        if (source_file == null && source_url == null) {
            throw (new TranscodeException("File doesn't exist"));
        }
        final TranscodePipe f_pipe = pipe;
        try {
            final IPCInterface ipc = plugin_interface.getIPC();
            final Object analysis_context;
            if (source_url != null) {
                analysis_context = ipc.invoke("analyseContent", new Object[] { source_url, profile.getName() });
            } else {
                analysis_context = ipc.invoke("analyseContent", new Object[] { source_file, profile.getName() });
            }
            final Map<String, Object> result = new HashMap<>();
            final TranscodeProviderAnalysisImpl analysis = new TranscodeProviderAnalysisImpl() {

                @Override
                public void cancel() {
                    try {
                        ipc.invoke("cancelAnalysis", new Object[] { analysis_context });
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }

                @Override
                public boolean foundVideoStream() {
                    return (getLongProperty(PT_VIDEO_WIDTH) > 0);
                }

                @Override
                public boolean getBooleanProperty(int property) {
                    if (property == PT_TRANSCODE_REQUIRED) {
                        return (getBooleanProperty("xcode_required", true));
                    } else {
                        Debug.out("Unknown property: " + property);
                        return (false);
                    }
                }

                @Override
                public void setBooleanProperty(int property, boolean value) {
                    if (property == PT_FORCE_TRANSCODE) {
                        result.put("force_xcode", value);
                    } else {
                        Debug.out("Unknown property: " + property);
                    }
                }

                @Override
                public long getLongProperty(int property) {
                    if (property == PT_DURATION_MILLIS) {
                        long duration = getLongProperty("duration_millis", 0);
                        long audio_duration = getLongProperty("audio_duration_millis", 0);
                        if (duration <= 0 && audio_duration > 0) {
                            duration = audio_duration;
                        }
                        if (audio_duration > 0 && audio_duration < duration) {
                            duration = audio_duration;
                        }
                        return (duration);
                    } else if (property == PT_VIDEO_WIDTH) {
                        return (getLongProperty("video_width", 0));
                    } else if (property == PT_VIDEO_HEIGHT) {
                        return (getLongProperty("video_height", 0));
                    } else if (property == PT_SOURCE_SIZE) {
                        return (getLongProperty("source_size", 0));
                    } else if (property == PT_ESTIMATED_XCODE_SIZE) {
                        return (getLongProperty("estimated_transcoded_size", 0));
                    } else {
                        Debug.out("Unknown property: " + property);
                        return (0);
                    }
                }

                protected boolean getBooleanProperty(String name, boolean def) {
                    Boolean b = (Boolean) result.get(name);
                    if (b != null) {
                        return (b);
                    }
                    return (def);
                }

                protected long getLongProperty(String name, long def) {
                    Long l = (Long) result.get(name);
                    if (l != null) {
                        return (l);
                    }
                    return (def);
                }

                @Override
                public Map<String, Object> getResult() {
                    return (result);
                }
            };
            new AEThread2("analysisStatus", true) {

                @Override
                public void run() {
                    try {
                        while (true) {
                            try {
                                Map status = (Map) ipc.invoke("getAnalysisStatus", new Object[] { analysis_context });
                                long state = (Long) status.get("state");
                                if (state == 0) {
                                    // running
                                    Thread.sleep(50);
                                } else if (state == 1) {
                                    _adapter.failed(new TranscodeException("Analysis cancelled"));
                                    break;
                                } else if (state == 2) {
                                    _adapter.failed(new TranscodeException("Analysis failed", (Throwable) status.get("error")));
                                    break;
                                } else {
                                    result.putAll((Map<String, Object>) status.get("result"));
                                    _adapter.complete();
                                    break;
                                }
                            } catch (Throwable e) {
                                _adapter.failed(new TranscodeException("Failed to get status", e));
                                break;
                            }
                        }
                    } finally {
                        if (f_pipe != null) {
                            f_pipe.destroy();
                        }
                    }
                }
            }.start();
            return (analysis);
        } catch (Throwable e) {
            if (pipe != null) {
                pipe.destroy();
            }
            throw (e);
        }
    } catch (TranscodeException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new TranscodeException("analysis failed", e));
    }
}
Also used : HashMap(java.util.HashMap) PluginInterface(com.biglybt.pif.PluginInterface) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) URL(java.net.URL) AEThread2(com.biglybt.core.util.AEThread2) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) File(java.io.File) IPCInterface(com.biglybt.pif.ipc.IPCInterface) HashMap(java.util.HashMap) Map(java.util.Map)

Example 63 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class NetworkAdminSpeedTestSchedulerImpl method scheduleTest.

@Override
public synchronized NetworkAdminSpeedTestScheduledTest scheduleTest(int type) throws NetworkAdminException {
    if (currentTest != null) {
        throw (new NetworkAdminException("Test already scheduled"));
    }
    if (type == TEST_TYPE_BT) {
        PluginInterface plugin = PluginInitializer.getDefaultInterface();
        currentTest = new NetworkAdminSpeedTestScheduledTestImpl(plugin, new NetworkAdminSpeedTesterBTImpl(plugin));
        currentTest.getTester().setMode(type);
        currentTest.addListener(new NetworkAdminSpeedTestScheduledTestListener() {

            @Override
            public void stage(NetworkAdminSpeedTestScheduledTest test, String step) {
            }

            @Override
            public void complete(NetworkAdminSpeedTestScheduledTest test) {
                synchronized (NetworkAdminSpeedTestSchedulerImpl.this) {
                    currentTest = null;
                }
            }
        });
    } else {
        throw (new NetworkAdminException("Unknown test type"));
    }
    return (currentTest);
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface)

Example 64 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class NetworkAdminSpeedTesterBTImpl method initialise.

protected static void initialise() {
    PluginInterface plugin = PluginInitializer.getDefaultInterface();
    speedTestAttrib = plugin.getTorrentManager().getPluginAttribute(NetworkAdminSpeedTesterBTImpl.class.getName() + ".test.attrib");
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface)

Example 65 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class NetworkAdminUDPTester method testInbound.

@Override
public InetAddress testInbound(InetAddress bind_ip, int bind_port) throws NetworkAdminException {
    PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler(bind_port);
    PRUDPPacketHandler packet_handler = handler.getHandler();
    HashMap data_to_send = new HashMap();
    PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
    String upnp_str = null;
    if (pi_upnp != null) {
        UPnPPlugin upnp = (UPnPPlugin) pi_upnp.getPlugin();
        /*
			UPnPMapping mapping = upnp.getMapping( true, port );

			if ( mapping == null ) {

				new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );

				// give UPnP a chance to work

				try {
					Thread.sleep( 500 );

				}
				catch (Throwable e) {

					Debug.printStackTrace( e );
				}
			}
			*/
        UPnPPluginService[] services = upnp.getServices();
        if (services.length > 0) {
            upnp_str = "";
            for (int i = 0; i < services.length; i++) {
                UPnPPluginService service = services[i];
                upnp_str += (i == 0 ? "" : ",") + service.getInfo();
            }
        }
    }
    if (upnp_str != null) {
        data_to_send.put("upnp", upnp_str);
    }
    NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();
    String as = net_asn.getAS();
    String asn = net_asn.getASName();
    if (as.length() > 0) {
        data_to_send.put("as", as);
    }
    if (asn.length() > 0) {
        data_to_send.put("asn", asn);
    }
    data_to_send.put("locale", MessageText.getCurrentLocale().toString());
    Random random = new Random();
    data_to_send.put("id", new Long(random.nextLong()));
    try {
        packet_handler.setExplicitBindAddress(bind_ip);
        Throwable last_error = null;
        long timeout = 5000;
        long timeout_inc = 5000;
        try {
            for (int i = 0; i < 3; i++) {
                data_to_send.put("seq", new Long(i));
                try {
                    // connection ids for requests must always have their msb set...
                    // apart from the original darn udp tracker spec....
                    long connection_id = 0x8000000000000000L | random.nextLong();
                    NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                    request_packet.setPayload(data_to_send);
                    if (listener != null) {
                        listener.reportProgress("Sending outbound packet and waiting for reply probe (timeout=" + timeout + ")");
                    }
                    NetworkAdminNATUDPReply reply_packet = (NetworkAdminNATUDPReply) packet_handler.sendAndReceive(null, request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT), timeout, PRUDPPacketHandler.PRIORITY_IMMEDIATE);
                    Map reply = reply_packet.getPayload();
                    byte[] ip_bytes = (byte[]) reply.get("ip_address");
                    if (ip_bytes == null) {
                        throw (new NetworkAdminException("IP address missing in reply"));
                    }
                    byte[] reason = (byte[]) reply.get("reason");
                    if (reason != null) {
                        throw (new NetworkAdminException(new String(reason, "UTF8")));
                    }
                    return (InetAddress.getByAddress(ip_bytes));
                } catch (Throwable e) {
                    last_error = e;
                    timeout += timeout_inc;
                }
            }
            if (last_error != null) {
                throw (last_error);
            }
            throw (new NetworkAdminException("Timeout"));
        } finally {
            try {
                data_to_send.put("seq", new Long(99));
                long connection_id = 0x8000000000000000L | random.nextLong();
                NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                request_packet.setPayload(data_to_send);
                if (listener != null) {
                    listener.reportProgress("Sending completion event");
                }
                packet_handler.send(request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT));
            } catch (Throwable e) {
            }
        }
    } catch (NetworkAdminException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new NetworkAdminException("Inbound test failed", e));
    } finally {
        packet_handler.setExplicitBindAddress(null);
        handler.release();
    }
}
Also used : NetworkAdminException(com.biglybt.core.networkmanager.admin.NetworkAdminException) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) PluginInterface(com.biglybt.pif.PluginInterface) NetworkAdminASN(com.biglybt.core.networkmanager.admin.NetworkAdminASN) UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) PRUDPReleasablePacketHandler(com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler) Random(java.util.Random) PRUDPPacketHandler(com.biglybt.net.udp.uc.PRUDPPacketHandler) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PluginInterface (com.biglybt.pif.PluginInterface)120 URL (java.net.URL)15 IPCInterface (com.biglybt.pif.ipc.IPCInterface)14 UIManager (com.biglybt.pif.ui.UIManager)13 PluginManager (com.biglybt.pif.PluginManager)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Download (com.biglybt.pif.download.Download)10 MenuItemListener (com.biglybt.pif.ui.menus.MenuItemListener)10 File (java.io.File)10 Map (java.util.Map)10 DownloadManager (com.biglybt.core.download.DownloadManager)9 UIFunctions (com.biglybt.ui.UIFunctions)9 ArrayList (java.util.ArrayList)9 PluginException (com.biglybt.pif.PluginException)8 MenuItem (com.biglybt.pif.ui.menus.MenuItem)8 MenuManager (com.biglybt.pif.ui.menus.MenuManager)8 DHTPlugin (com.biglybt.plugin.dht.DHTPlugin)8 DHT (com.biglybt.core.dht.DHT)7 SelectionEvent (org.eclipse.swt.events.SelectionEvent)7