Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.64 KB | None | 0 0
  1. package com.webmons.disono.p2pPlayer;
  2.  
  3. //adb logcat -b crash
  4. //adb logcat -c
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import android.widget.LinearLayout;
  13.  
  14. import com.dueeeke.videocontroller.StandardVideoController;
  15. import com.dueeeke.videoplayer.exo.ExoMediaPlayerFactory;
  16. import com.dueeeke.videoplayer.ijk.IjkPlayerFactory;
  17. import com.dueeeke.videoplayer.player.AndroidMediaPlayerFactory;
  18. import com.dueeeke.videoplayer.player.VideoView;
  19.  
  20. import com.cdnbye.sdk.ChannelIdCallback;
  21. import com.cdnbye.sdk.P2pEngine;
  22. import com.cdnbye.sdk.P2pConfig;
  23. import com.cdnbye.sdk.P2pStatisticsListener;
  24. import com.cdnbye.sdk.LogLevel;
  25.  
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.util.List;
  30. import java.util.logging.Logger;
  31. import java.util.concurrent.TimeUnit;
  32.  
  33. import android.util.DisplayMetrics;
  34.  
  35. import android.content.Context;
  36. import android.content.Intent;
  37. import android.content.IntentFilter;
  38. import android.content.BroadcastReceiver;
  39. import android.content.pm.ActivityInfo;
  40.  
  41.  
  42. import android.view.Window;
  43. import android.view.WindowManager;
  44. import android.view.KeyEvent;
  45. import android.view.View;
  46. import android.view.ViewGroup;
  47. import android.view.ViewGroup.LayoutParams;
  48.  
  49. //import androidx.appcompat.app.AppCompatActivity;
  50.  
  51. public class P2PActivity extends Activity {
  52.  
  53. private Activity activity;
  54.  
  55. public static final String BROADCAST_LISTENER = "com.webmons.disono.p2pPlayer.Listener";
  56.  
  57. private VideoView videoView;
  58. private StandardVideoController controller;
  59.  
  60. private String VOD = "";
  61. private String LIVE = "";
  62.  
  63. private Button replayBtn;
  64. private Button switchBtn;
  65. private Button vodBtn;
  66. private Button liveBtn;
  67. private String currentUrl = VOD;
  68. // private String currentUrl = LIVE;
  69.  
  70. private double totalHttpDownloaded = 0;
  71. private double totalP2pDownloaded = 0;
  72. private double totalP2pUploaded = 0;
  73.  
  74. private String _url;
  75.  
  76. private LinearLayout lla;
  77. private LinearLayout llb;
  78. private LinearLayout llc;
  79. private TextView peerIdLL;
  80. private LinearLayout lld;
  81.  
  82. @Override
  83. protected void onCreate(Bundle savedInstanceState) {
  84. requestWindowFeature(Window.FEATURE_NO_TITLE);
  85.  
  86. super.onCreate(savedInstanceState);
  87.  
  88. activity = this;
  89.  
  90. Intent intent = getIntent();
  91. _url = intent.getStringExtra("url");
  92.  
  93. VOD = _url;
  94. LIVE = _url;
  95. currentUrl = _url;
  96.  
  97. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  98. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  99. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  100.  
  101. setContentView(getResources().getIdentifier("p2p_player", "layout", getPackageName()));
  102. TextView versionV = findViewById(getResources().getIdentifier("version", "id", getPackageName()));
  103. versionV.setText("Versão: " + P2pEngine.Version);
  104.  
  105. lla = (LinearLayout) findViewById(getResources().getIdentifier("lla", "id", getPackageName()));
  106. llb = (LinearLayout) findViewById(getResources().getIdentifier("llb", "id", getPackageName()));
  107. llc = (LinearLayout) findViewById(getResources().getIdentifier("llc", "id", getPackageName()));
  108. peerIdLL = (TextView) findViewById(getResources().getIdentifier("peerId", "id", getPackageName()));
  109. lld = (LinearLayout) findViewById(getResources().getIdentifier("lld", "id", getPackageName()));
  110.  
  111. _broadcastRCV();
  112.  
  113. P2pConfig config = new P2pConfig.Builder()
  114. .logEnabled(false)
  115. .logLevel(LogLevel.WARN)
  116. .announce("https://tracker.cdnbye.com/v1")
  117. .wsSignalerAddr("wss://signal.cdnbye.com")
  118. .downloadTimeout(10_000, TimeUnit.MILLISECONDS)
  119. .dcDownloadTimeout(6_000, TimeUnit.MILLISECONDS)
  120. .localPort(52019)
  121. .diskCacheLimit(1024*1024*1024)
  122. .memoryCacheLimit(60*1024*1024)
  123. .p2pEnabled(true)
  124. .withTag("unknown")
  125. .webRTCConfig(null)
  126. .maxPeerConnections(10)
  127. .channelId(new ChannelIdCallback() {
  128. @Override
  129. public String onChannelId(String urlString) {
  130. String m3u8 = urlString.split(".m3u8")[0];
  131. String[] parts = m3u8.split("/");
  132. return parts[parts.length-1];
  133. }
  134. })
  135. .build();
  136. P2pEngine engine = P2pEngine.initEngine(getApplicationContext(), "my_key", config);
  137. engine.addP2pStatisticsListener(new P2pStatisticsListener() {
  138. @Override
  139. public void onHttpDownloaded(long value) {
  140. totalHttpDownloaded += (double)value;
  141. refreshRatio();
  142. checkIfConnected();
  143. }
  144.  
  145. @Override
  146. public void onP2pDownloaded(long value) {
  147. totalP2pDownloaded += (double)value;
  148. runOnUiThread(new Runnable() {
  149. @Override
  150. public void run() {
  151. TextView offloadV = findViewById(getResources().getIdentifier("offload", "id", getPackageName()));
  152. String text = String.format("Offload: %.2fMB", totalP2pDownloaded/1024);
  153. offloadV.setText(text);
  154.  
  155. refreshRatio();
  156. }
  157. });
  158. checkIfConnected();
  159. }
  160.  
  161. @Override
  162. public void onP2pUploaded(long value) {
  163. totalP2pUploaded += (double)value;
  164. runOnUiThread(new Runnable() {
  165. @Override
  166. public void run() {
  167. TextView uploadV = findViewById(getResources().getIdentifier("upload", "id", getPackageName()));
  168. String text = String.format("Envio: %.2fMB", totalP2pUploaded/1024);
  169. uploadV.setText(text);
  170. }
  171. });
  172. checkIfConnected();
  173. }
  174.  
  175. @Override
  176. public void onPeers(List<String> peers) {
  177. runOnUiThread(new Runnable() {
  178. @Override
  179. public void run() {
  180. TextView peersV = findViewById(getResources().getIdentifier("peers", "id", getPackageName()));
  181. String text = String.format("Conexões: %d", peers.size());
  182. peersV.setText(text);
  183. }
  184. });
  185. checkIfConnected();
  186. }
  187. });
  188.  
  189. startPlay(currentUrl);
  190.  
  191. replayBtn = findViewById(getResources().getIdentifier("replay", "id", getPackageName()));
  192. switchBtn = findViewById(getResources().getIdentifier("switcher", "id", getPackageName()));
  193.  
  194. //vodBtn = findViewById(getResources().getIdentifier("vod", "id", getPackageName()));
  195. //liveBtn = findViewById(getResources().getIdentifier("live", "id", getPackageName()));
  196.  
  197. replayBtn.setOnClickListener(new View.OnClickListener() {
  198. @Override
  199. public void onClick(View view) {
  200. // 清空数据
  201. clearData();
  202. startPlay(currentUrl);
  203. }
  204. });
  205.  
  206. switchBtn.setOnClickListener(new View.OnClickListener() {
  207. @Override
  208. public void onClick(View view) {
  209. if (currentUrl.equals(VOD)) {
  210. currentUrl = LIVE;
  211. } else {
  212. currentUrl = VOD;
  213. }
  214. // 清空数据
  215. clearData();
  216. startPlay(currentUrl);
  217. }
  218. });
  219. // vodBtn.setOnClickListener(new View.OnClickListener() {
  220. // @Override
  221. // public void onClick(View view) {
  222. // // 清空数据
  223. // clearData();
  224. // currentUrl = VOD;
  225. // startPlay(currentUrl);
  226. // }
  227. // });
  228.  
  229. // liveBtn.setOnClickListener(new View.OnClickListener() {
  230. // @Override
  231. // public void onClick(View view) {
  232. // // 清空数据
  233. // clearData();
  234. // currentUrl = LIVE;
  235. // startPlay(currentUrl);
  236. // }
  237. // });
  238. }
  239.  
  240. BroadcastReceiver br = new BroadcastReceiver() {
  241.  
  242. @Override
  243. public void onReceive(Context context, Intent intent) {
  244. if (intent != null) {
  245. String method = intent.getStringExtra("method");
  246.  
  247. if (method != null) {
  248. switch (method) {
  249. case "pause":
  250. if (videoView.isPlaying()) {
  251. videoView.pause();
  252. }
  253.  
  254. break;
  255. case "resume":
  256. if (!videoView.isPlaying()) {
  257. videoView.resume();
  258. }
  259.  
  260. break;
  261. case "release":
  262. videoView.release();
  263. break;
  264.  
  265. case "hideDetails":
  266. lla.setVisibility(LinearLayout.GONE);
  267. llb.setVisibility(LinearLayout.GONE);
  268. llc.setVisibility(LinearLayout.GONE);
  269. peerIdLL.setVisibility(View.GONE);
  270. lld.setVisibility(LinearLayout.GONE);
  271.  
  272. DisplayMetrics displayMetrics = new DisplayMetrics();
  273. getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  274. int height = displayMetrics.heightPixels;
  275. int width = displayMetrics.widthPixels;
  276.  
  277. if(videoView == null){
  278. videoView = findViewById(getResources().getIdentifier("player", "id", getPackageName()));
  279. }
  280.  
  281. LayoutParams params = videoView.getLayoutParams();
  282. params.height = height - 30;
  283. params.width = width - 30;
  284. videoView.setLayoutParams(params);
  285. break;
  286.  
  287. case "showDetails":
  288. lla.setVisibility(LinearLayout.VISIBLE);
  289. llb.setVisibility(LinearLayout.VISIBLE);
  290. llc.setVisibility(LinearLayout.VISIBLE);
  291. peerIdLL.setVisibility(View.VISIBLE);
  292. lld.setVisibility(LinearLayout.VISIBLE);
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. };
  299.  
  300. private void _broadcastRCV() {
  301. IntentFilter filter = new IntentFilter(VideoPlayerP2P.BROADCAST_METHODS);
  302.  
  303. activity.registerReceiver(br, filter);
  304. }
  305.  
  306. private void _sendBroadCast(String methodName) {
  307. Intent intent = new Intent();
  308. intent.setAction(BROADCAST_LISTENER);
  309. intent.putExtra("method", methodName);
  310. activity.sendBroadcast(intent);
  311. }
  312.  
  313. private void _sendBroadCast(String methodName, JSONObject object) {
  314. Intent intent = new Intent();
  315. intent.setAction(BROADCAST_LISTENER);
  316. intent.putExtra("method", methodName);
  317. intent.putExtra("data", object.toString());
  318. activity.sendBroadcast(intent);
  319. }
  320.  
  321.  
  322. private void startPlay(String url) {
  323. String parsedUrl = P2pEngine.getInstance().parseStreamUrl(url);
  324. videoView = findViewById(getResources().getIdentifier("player", "id", getPackageName()));
  325.  
  326. videoView.setFocusable(true);
  327. videoView.setFocusableInTouchMode(true);
  328.  
  329. videoView.release();
  330. videoView.setUrl(parsedUrl); //设置视频地址
  331.  
  332. // 使用IjkPlayer解码
  333. //videoView.setPlayerFactory(IjkPlayerFactory.create(this));
  334. // 使用ExoPlayer解码
  335. videoView.setPlayerFactory(ExoMediaPlayerFactory.create());
  336. // 使用MediaPlayer解码
  337. //videoView.setPlayerFactory(AndroidMediaPlayerFactory.create(this));
  338.  
  339. if (controller == null) {
  340. controller = new StandardVideoController(this);
  341. }
  342.  
  343. videoView.setVideoController(controller); //设置控制器,如需定制可继承BaseVideoController
  344. videoView.start();
  345. _sendBroadCast("onPlay");
  346. }
  347.  
  348. private void checkIfConnected() {
  349. runOnUiThread(new Runnable() {
  350. @Override
  351. public void run() {
  352. TextView connectedV = findViewById(getResources().getIdentifier("connected", "id", getPackageName()));
  353. String text = String.format("Conectado: %s", P2pEngine.getInstance().isConnected()?"Sim":"Não");
  354. connectedV.setText(text);
  355.  
  356. TextView peerIdV = findViewById(getResources().getIdentifier("peerId", "id", getPackageName()));
  357. String text2 = String.format("Seu ID: %s", P2pEngine.getInstance().getPeerId());
  358. peerIdV.setText(text2);
  359. }
  360. });
  361. }
  362.  
  363. private void refreshRatio() {
  364. runOnUiThread(new Runnable() {
  365. @Override
  366. public void run() {
  367. double ratio = 0;
  368. if (totalHttpDownloaded+totalP2pDownloaded != 0) {
  369. ratio = totalP2pDownloaded/(totalHttpDownloaded+totalP2pDownloaded);
  370. }
  371. TextView ratioV = findViewById(getResources().getIdentifier("ratio", "id", getPackageName()));
  372. String text = String.format("Uso P2P: %.0f%%", ratio*100);
  373. ratioV.setText(text);
  374. }
  375. });
  376. }
  377.  
  378. private void clearData() {
  379. totalHttpDownloaded = 0;
  380. totalP2pDownloaded = 0;
  381. totalP2pUploaded = 0;
  382. checkIfConnected();
  383. refreshRatio();
  384. }
  385.  
  386. @Override
  387. protected void onPause() {
  388. super.onPause();
  389. videoView.pause();
  390. _sendBroadCast("onPause");
  391. }
  392.  
  393. @Override
  394. protected void onResume() {
  395. super.onResume();
  396. videoView.resume();
  397. _sendBroadCast("onResume");
  398. }
  399.  
  400. @Override
  401. protected void onDestroy() {
  402. super.onDestroy();
  403. videoView.release();
  404. _sendBroadCast("onDestroy");
  405. }
  406.  
  407. @Override
  408. public boolean onKeyDown(int keyCode, KeyEvent event) {
  409. JSONObject obj = new JSONObject();
  410. try {
  411. obj.put("keyCode", keyCode);
  412. if (keyCode == KeyEvent.KEYCODE_BACK) {
  413. obj.put("isBackCode", true);
  414. } else {
  415. obj.put("isBackCode", false);
  416. }
  417. } catch (JSONException e) {
  418. e.printStackTrace();
  419. }
  420. _sendBroadCast("onKeyDown", obj);
  421. return super.onKeyDown(keyCode, event);
  422. }
  423.  
  424. @Override
  425. public boolean onKeyUp(int keyCode, KeyEvent event) {
  426. JSONObject obj = new JSONObject();
  427. try {
  428. obj.put("keyCode", keyCode);
  429. if (keyCode == KeyEvent.KEYCODE_BACK) {
  430. obj.put("isBackCode", true);
  431. } else {
  432. obj.put("isBackCode", false);
  433. }
  434. } catch (JSONException e) {
  435. e.printStackTrace();
  436. }
  437. _sendBroadCast("onKeyUp", obj);
  438. return super.onKeyUp(keyCode, event);
  439. }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement