-
Notifications
You must be signed in to change notification settings - Fork 53
/
MainActivity.java
238 lines (200 loc) · 7.91 KB
/
MainActivity.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.cdnbye.p2pengine;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
import com.cdnbye.sdk.LogLevel;
import com.cdnbye.sdk.P2pConfig;
import com.cdnbye.sdk.P2pEngine;
import com.cdnbye.sdk.P2pStatisticsListener;
import com.cdnbye.sdk.PlayerStatsCallback;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.ui.PlayerView;
//import com.tencent.bugly.crashreport.CrashReport;
public class MainActivity extends Activity {
private final String VOD = "https://www.nmgxwhz.com:65/20200107/17hTnjxI/index.m3u8";
private final String LIVE = "https://wowza.peer5.com/live/smil:bbb_abr.smil/chunklist_b591000.m3u8";
private PlayerView playerView;
private SimpleExoPlayer player;
private Button replayBtn;
private Button switchBtn;
private Button vodBtn;
private Button liveBtn;
private TextView offloadV;
private TextView uploadV;
private TextView peersV;
private TextView connectedV;
private TextView peerIdV;
private TextView ratioV;
// private String currentUrl = VOD;
private String currentUrl = LIVE;
private double totalHttpDownloaded = 0;
private double totalP2pDownloaded = 0;
private double totalP2pUploaded = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// bugly
// CrashReport.initCrashReport(getApplicationContext(), "e40b652a35", true);
setContentView(R.layout.activity_main);
offloadV = findViewById(R.id.offload);
uploadV = findViewById(R.id.upload);
peersV = findViewById(R.id.peers);
connectedV = findViewById(R.id.connected);
peerIdV = findViewById(R.id.peerId);
ratioV = findViewById(R.id.ratio);
playerView = findViewById(R.id.player_view);
TextView versionV = findViewById(R.id.version);
versionV.setText("Version: " + P2pEngine.Version);
P2pConfig config = new P2pConfig.Builder()
.logEnabled(true)
.logLevel(LogLevel.DEBUG)
.p2pEnabled(true)
.withTag("exoplayer")
.channelIdPrefix("cdnbye")
.build();
// Instantiate P2pEngine,which is a singleton
P2pEngine engine = P2pEngine.initEngine(getApplicationContext(), "ZMuO5qHZg", config);
// Recommended while playing living stream
engine.setPlayStats(new PlayerStatsCallback() {
@Override
public long onBufferedDuration() {
return player.getBufferedPosition() - player.getCurrentPosition();
}
});
engine.addP2pStatisticsListener(new P2pStatisticsListener() {
@Override
public void onHttpDownloaded(long value) {
totalHttpDownloaded += (double) value;
refreshRatio();
}
@Override
public void onP2pDownloaded(long value) {
totalP2pDownloaded += (double) value;
String text = String.format("Offload: %.2fMB", totalP2pDownloaded / 1024);
offloadV.setText(text);
refreshRatio();
}
@Override
public void onP2pUploaded(long value) {
totalP2pUploaded += (double) value;
String text = String.format("Upload: %.2fMB", totalP2pUploaded / 1024);
uploadV.setText(text);
}
@Override
public void onPeers(List<String> peers) {
String text = String.format("Peers: %d", peers.size());
peersV.setText(text);
}
@Override
public void onServerConnected(boolean connected) {
String text = String.format("Connected: %s", connected?"Yes":"No");
connectedV.setText(text);
String text2 = String.format("Peer ID: %s", P2pEngine.getInstance().getPeerId());
peerIdV.setText(text2);
}
});
startPlay(currentUrl);
replayBtn = findViewById(R.id.replay);
switchBtn = findViewById(R.id.switcher);
vodBtn = findViewById(R.id.vod);
liveBtn = findViewById(R.id.live);
replayBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 清空数据
clearData();
if (player != null && player.isPlaying()) {
player.stop();
}
startPlay(currentUrl);
}
});
switchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentUrl.equals(VOD)) {
currentUrl = LIVE;
} else {
currentUrl = VOD;
}
// 清空数据
clearData();
startPlay(currentUrl);
}
});
vodBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 清空数据
clearData();
currentUrl = VOD;
startPlay(currentUrl);
}
});
liveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 清空数据
clearData();
currentUrl = LIVE;
startPlay(currentUrl);
}
});
}
private void startPlay(String url) {
if (player != null && player.isPlaying()) {
player.stop(true);
player.release();
}
// Convert original playback address (m3u8) to the address of the local proxy server
String parsedUrl = P2pEngine.getInstance().parseStreamUrl(url);
// Create a data source factory.
DataSource.Factory dataSourceFactory =
new DefaultHttpDataSourceFactory(
Util.getUserAgent(this, "p2p-engine"),
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true /* allowCrossProtocolRedirects */
);
// Create a HLS media source pointing to a playlist uri.
HlsMediaSource hlsMediaSource =
new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(parsedUrl));
// Create a player instance.
player = ExoPlayerFactory.newSimpleInstance(this);
// Attach player to the view.
playerView.setPlayer(player);
// Prepare the player with the HLS media source.
player.prepare(hlsMediaSource);
// Start play when ready
player.setPlayWhenReady(true);
}
private void refreshRatio() {
double ratio = 0;
if (totalHttpDownloaded + totalP2pDownloaded != 0) {
ratio = totalP2pDownloaded / (totalHttpDownloaded + totalP2pDownloaded);
}
String text = String.format("P2P Ratio: %.0f%%", ratio * 100);
ratioV.setText(text);
}
private void clearData() {
totalHttpDownloaded = 0;
totalP2pDownloaded = 0;
totalP2pUploaded = 0;
refreshRatio();
}
@Override
protected void onDestroy() {
super.onDestroy();
player.release();
}
}