Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.
This repository was archived by the owner on May 7, 2023. It is now read-only.

Can't read torrent information  #19

Description

package com.oasis.streamio;

import android.net.Uri;
import android.view.View;
import android.widget.Toast;
import androidx.fragment.app.FragmentActivity;
import com.github.se_bastiaan.torrentstream.Torrent;
import com.github.se_bastiaan.torrentstream.StreamStatus;
import com.github.se_bastiaan.torrentstream.TorrentOptions;
import com.github.se_bastiaan.torrentstream.TorrentOptions.Builder;
import com.github.se_bastiaan.torrentstreamserver.TorrentServerListener;
import com.github.se_bastiaan.torrentstreamserver.TorrentStreamNotInitializedException;
import com.github.se_bastiaan.torrentstreamserver.TorrentStreamServer;
import com.oasis.streamio.databinding.ActivityMainBinding;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class StreamModel implements TorrentServerListener {
	private String torrentUri;
	private PlayerModel playerModel;
	private MainActivity activity;
	private ActivityMainBinding binding;
	private Uri streamUri;
	private TorrentStreamServer torrentStreamServer;

	public StreamModel(PlayerModel playerModel, String torrentUri) {
		this.playerModel = playerModel;
		this.torrentUri = torrentUri;
		this.binding = playerModel.getBindView();
		this.activity = (MainActivity) playerModel.getActivity();

		String ipAddress = "127.0.0.1";

		try {
			InetAddress inetAddress = Util.getIpAddress(activity);
			if (inetAddress != null)
				ipAddress = inetAddress.getHostAddress();
		} catch (UnknownHostException exception) {
			Toast.makeText(activity, exception.toString(), Toast.LENGTH_LONG).show();
		}
		final TorrentOptions options = new TorrentOptions.Builder().saveLocation(activity.getFilesDir())
				.removeFilesAfterStop(false).build();
		torrentStreamServer = TorrentStreamServer.getInstance();
		torrentStreamServer.setTorrentOptions(options);
		torrentStreamServer.setServerHost(ipAddress);
		torrentStreamServer.setServerPort(9000);
		torrentStreamServer.startTorrentStream();
		try {
			torrentStreamServer.startStream(torrentUri);
		} catch (TorrentStreamNotInitializedException | IOException e) {
			Toast.makeText(activity, e.toString(), Toast.LENGTH_LONG).show();
		}

		torrentStreamServer.addListener(this);
	}

	@Override
	public void onStreamPrepared(Torrent torrent) {
		Toast.makeText(activity, "onStreamPrepared", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onStreamStarted(Torrent torrent) {
		Toast.makeText(activity, "OnStreamStarted", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onStreamError(Torrent torrent, Exception exception) {
		Toast.makeText(activity, torrentUri, Toast.LENGTH_LONG).show();

		((MainActivity) playerModel.getActivity()).showError(exception.toString(), view -> {
			torrentStreamServer.startTorrentStream();
		});
	}

	@Override
	public void onStreamReady(Torrent torrent) {
		binding.loading.setProgress(100);
	}

	@Override
	public void onStreamProgress(Torrent torrent, StreamStatus status) {
		if (status.bufferProgress <= 100 && binding.loading.getProgress() < 100
				&& binding.loading.getProgress() != status.bufferProgress) {
			Toast.makeText(activity, "Progress: " + status.bufferProgress, Toast.LENGTH_LONG).show();
			binding.loading.setProgress(status.bufferProgress);
		}
	}

	@Override
	public void onStreamStopped() {
		Toast.makeText(activity, "OnStreamStopped", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onServerReady(final String streamUrl) {
		streamUri = Uri.parse(streamUrl);
		binding.customContainer.setVisibility(View.GONE);
		playerModel.initializePlayer(streamUri);
		playerModel.addListener();
	}

	public Uri getStreamUri() {
		return streamUri;
	}

	public void stopStream() {
		torrentStreamServer.stopStream();
	}

	public void stopServer() {
		torrentStreamServer.stopTorrentStream();
	}
}

Screenshot_20220729-211103

The magnet URI is
magnet:?xt=urn:btih:3138B17E66FB66C36280D6165395EFAA98EA09D3&dn=Sniper.+The+White+Raven+%282022%29+%5B720p%5D+%5BYTS.MX%5D&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.ch%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions