-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTvUdpRepeater.java
More file actions
91 lines (78 loc) · 3.31 KB
/
TvUdpRepeater.java
File metadata and controls
91 lines (78 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import android.media.tv.TvInputManager;
import android.media.tv.TvInputInfo;
import android.media.tv.TvView;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
public class TvUdpRepeater extends AppCompatActivity {
private Context context;
private TvInputManager tvInputManager;
private static final int BASE_PORT = 5000; // Port UDP awal
private static final String BROADCAST_IP = "255.255.255.255";
private ListView channelListView;
private VideoView videoView;
private List<String> channelList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
channelListView = findViewById(R.id.channelListView);
videoView = findViewById(R.id.videoView);
this.context = this;
this.tvInputManager = (TvInputManager) getSystemService(Context.TV_INPUT_SERVICE);
startStreaming();
}
public void startStreaming() {
List<TvInputInfo> tvInputs = tvInputManager.getTvInputList();
int portOffset = 0;
for (TvInputInfo input : tvInputs) {
if (input.isPassthroughInput()) continue;
final int udpPort = BASE_PORT + portOffset;
portOffset++;
channelList.add("Channel " + portOffset + " - Port: " + udpPort);
new Thread(() -> streamToUdp(input, udpPort)).start();
}
runOnUiThread(() -> {
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, channelList);
channelListView.setAdapter(adapter);
});
}
private void streamToUdp(TvInputInfo input, int udpPort) {
try {
DatagramSocket socket = new DatagramSocket();
InetAddress address = InetAddress.getByName(BROADCAST_IP);
// Dummy data untuk simulasi (gantilah dengan real stream)
byte[] buffer = ("Streaming channel from " + input.getId()).getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, udpPort);
while (true) {
socket.send(packet);
Thread.sleep(1000 / 30); // 30 FPS simulasi
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void enableHotspotForwarding() {
try {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null && wifiManager.isWifiEnabled()) {
Runtime.getRuntime().exec("iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE");
Runtime.getRuntime().exec("iptables -A FORWARD -i wlan0 -o wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT");
Runtime.getRuntime().exec("iptables -A FORWARD -i wlan1 -o wlan0 -j ACCEPT");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}