Skip to content

Commit

Permalink
update 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
augok committed Sep 24, 2024
1 parent 4d2c538 commit 7bde4d9
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 108 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2023 augok
Copyright 2024 augok

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
35 changes: 6 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,26 @@ https://webp2p.augok.com/p2pdemo/?url=https://m3u8.link.address/video.m3u8

## Quick Start

Load the latest CDN hosted version of JavaScript library.
Project upgrade uses module loading.

Add these tags to your document's `<head>`:

```html
<script src="https://unpkg.com/webp2p-hls@latest/p2pl.js"></script>
<script src="https://unpkg.com/webp2p-hls@latest/p2ph.js"></script>
<script src="https://unpkg.com/webp2p-hls@latest/p2phls.js"></script>
```
Support the latest version of hls.js.

Then there's a free, CDN hosted version of hls.js that anyone can use. Add these tags to your document's `<head>`:

```html
<script src="https://unpkg.com/hls.min@0.0.2/index.js"></script>
<script src="https://unpkg.com/hls.js@1.5.15/dist/hls.min.js"></script>
```

## Example by DPlayer

```html
<script src="https://unpkg.com/webp2p-hls@latest/p2pl.js"></script>
<script src="https://unpkg.com/webp2p-hls@latest/p2ph.js"></script>
<script src="https://unpkg.com/webp2p-hls@latest/p2phls.js"></script>
<script src="https://unpkg.com/hls.min@0.0.2/index.js"></script>
<script src="https://unpkg.com/hls.js@1.5.15/dist/hls.min.js"></script>
<!-- unpkg : use the latest version of DPlayer.js -->
<script src="https://unpkg.com/dplayer/dist/DPlayer.min.js"></script>
```

```js
import { P2Phls } from "https://unpkg.com/webp2p-hls@latest/p2phls.js";
// url is link address.
var p2phls = new P2Phls(url);

Expand All @@ -71,12 +63,7 @@ var player = new DPlayer({
type: 'customHls',
customType: {
'customHls': function(video, player) {
var hls = new Hls({
liveSyncDurationCount: 7,
loader: p2phls.getLoader()
});
p2phls.initHlsPlayer(hls);

var hls = p2phls.getHlsPlayer();
hls.loadSource(video.src);
hls.attachMedia(video);
}
Expand All @@ -87,16 +74,6 @@ var player = new DPlayer({

## API

**isP2P()**

Determine whether to support P2P.

```js
// p2phls is new P2Phls(url) object
p2phls.isP2P()
// return true or false
```

**isHls()**

Determine whether to support Hls.
Expand Down
3 changes: 3 additions & 0 deletions js/p2pa.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion js/p2ph.js

This file was deleted.

2 changes: 1 addition & 1 deletion js/p2phls.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion js/p2pl.js

This file was deleted.

57 changes: 21 additions & 36 deletions p2pdemo/hls.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<title>Hls Demo</title>
<script src="https://unpkg.com/jquery@1.12.4/dist/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2pl.js"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2ph.js"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2phls.js"></script>
<script src="https://unpkg.com/hls.min@0.0.2/index.js" type="text/javascript" charset="utf-8"></script>
<script src="https://unpkg.com/hls.js@1.5.15/dist/hls.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="./css/index.css?2" />
<style>
#videohls{
Expand Down Expand Up @@ -59,7 +56,8 @@ <h1>P2P Hls Demo</h1>
</div>
<div class="action-button">创建新连接</div>
</div>
<script>
<script type="module">
import { P2Phls } from "../js/p2phls.js";
$(".action-button").on('click',function(){
window.open(document.location.href, '_blank');
});
Expand All @@ -69,9 +67,6 @@ <h1>P2P Hls Demo</h1>
alert("请在链接中添加url参数以及相应的m3u8播放地址");
}else{
var p2phls = new P2Phls(urls);
if(!p2phls.isP2P()){
alert("此浏览器不支持p2p加速,建议使用chrome浏览器");
}
if (p2phls.isHls()) {
var videoContainer = document.querySelector('#video');
var video = document.createElement("video");
Expand All @@ -83,37 +78,27 @@ <h1>P2P Hls Demo</h1>
video.setAttribute("controls", "");
videoContainer.appendChild(video);

if(p2phls.isP2P()){
var hls = new Hls({
liveSyncDurationCount: 7,
loader: p2phls.getLoader()
});
p2phls.initHlsPlayer(hls);
}else{
var hls = new Hls();
}
var hls = p2phls.getHlsPlayer();
hls.loadSource(urls);
hls.attachMedia(video);

if (p2phls.isP2P()) {
p2phls.on('bytechange', function(data){
var httpMb = data.http / 1048576;
var p2pMb = data.p2p / 1048576;
var totalMb = httpMb + p2pMb;
var p2pr = p2pMb / totalMb * 100;
$('.httpmb').text(httpMb.toFixed(1));
if(p2pMb) {
$('.p2pmb').text(p2pMb.toFixed(1));
$('.p2pr').text(p2pr.toFixed(1));
} else {
$('.p2pmb').text('0');
$('.p2pr').text('0');
}
});
p2phls.on('peers', function(num,peer){
num > 0 ? $('.peernum').text(num):$('.peernum').text('0');
});
}
p2phls.on('bytechange', function(data){
var httpMb = data.http / 1048576;
var p2pMb = data.p2p / 1048576;
var totalMb = httpMb + p2pMb;
var p2pr = p2pMb / totalMb * 100;
$('.httpmb').text(httpMb.toFixed(1));
if(p2pMb) {
$('.p2pmb').text(p2pMb.toFixed(1));
$('.p2pr').text(p2pr.toFixed(1));
} else {
$('.p2pmb').text('0');
$('.p2pr').text('0');
}
});
p2phls.on('peers', function(num,peer){
num > 0 ? $('.peernum').text(num):$('.peernum').text('0');
});
}else{
alert("此浏览器不支持加速器解码,将使用普通模式播放");
$('#video').html('<video autoplay x5-playsinline="" playsinline="" webkit-playsinline="" x-webkit-airplay="allow" preload="true" controls src="' + urls + '" style="width:100%;height:100%;"></video>');
Expand Down
63 changes: 24 additions & 39 deletions p2pdemo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
<title>P2P Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/jquery@1.12.4/dist/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2pl.js"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2ph.js"></script>
<script type='text/javascript' src="https://unpkg.com/webp2p-hls@latest/p2phls.js"></script>
<script src="https://unpkg.com/hls.min@0.0.2/index.js" type="text/javascript" charset="utf-8"></script>
<script src="https://unpkg.com/dplayer@1.27.0/dist/DPlayer.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://unpkg.com/hls.js@1.5.15/dist/hls.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://unpkg.com/dplayer@1.27.1/dist/DPlayer.min.js" type="text/javascript" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="./css/index.css?2" />
</head>
<body>
Expand Down Expand Up @@ -50,7 +47,8 @@ <h1>P2P Demo</h1>
</div>
<div class="action-button">创建新连接</div>
</div>
<script>
<script type="module">
import { P2Phls } from "../js/p2phls.js";
$(".action-button").on('click',function(){
window.open(document.location.href, '_blank');
});
Expand All @@ -60,10 +58,7 @@ <h1>P2P Demo</h1>
alert("请在链接中添加url参数以及相应的m3u8播放地址");
}else{
var p2phls = new P2Phls(urls);
if(!p2phls.isP2P()){
alert("此浏览器不支持p2p加速,建议使用chrome浏览器");
}
if (p2phls.isHls() && p2phls.isP2P()) {
if (p2phls.isHls()) {
var player = new DPlayer({
container: document.getElementById('video'),
volume: 1,
Expand All @@ -74,16 +69,8 @@ <h1>P2P Demo</h1>
type: 'customHls',
customType: {
'customHls': function(video, player) {
if(p2phls.isP2P()){
var hls = new Hls({
liveSyncDurationCount: 7,
loader: p2phls.getLoader()
});
p2phls.initHlsPlayer(hls);
}else{
var hls = new Hls();
}
hls.loadSource(video.src);
var hls = p2phls.getHlsPlayer();
hls.loadSource(urls);
hls.attachMedia(video);
}
}
Expand All @@ -92,25 +79,23 @@ <h1>P2P Demo</h1>
player.on('loadstart', function() {
$('#video').find('.dplayer-menu .dplayer-menu-item').not(":eq(0)").remove();
});
if (p2phls.isP2P()) {
p2phls.on('bytechange', function(data){
var httpMb = data.http / 1048576;
var p2pMb = data.p2p / 1048576;
var totalMb = httpMb + p2pMb;
var p2pr = p2pMb / totalMb * 100;
$('.httpmb').text(httpMb.toFixed(1));
if(p2pMb) {
$('.p2pmb').text(p2pMb.toFixed(1));
$('.p2pr').text(p2pr.toFixed(1));
} else {
$('.p2pmb').text('0');
$('.p2pr').text('0');
}
});
p2phls.on('peers', function(num,peer){
num > 0 ? $('.peers').text(num):$('.peers').text('0');
});
}
p2phls.on('bytechange', function(data){
var httpMb = data.http / 1048576;
var p2pMb = data.p2p / 1048576;
var totalMb = httpMb + p2pMb;
var p2pr = p2pMb / totalMb * 100;
$('.httpmb').text(httpMb.toFixed(1));
if(p2pMb) {
$('.p2pmb').text(p2pMb.toFixed(1));
$('.p2pr').text(p2pr.toFixed(1));
} else {
$('.p2pmb').text('0');
$('.p2pr').text('0');
}
});
p2phls.on('peers', function(num,peer){
num > 0 ? $('.peers').text(num):$('.peers').text('0');
});
}else{
alert("此浏览器不支持加速器解码,将使用普通模式播放");
$('#video').html('<video autoplay x5-playsinline="" playsinline="" webkit-playsinline="" x-webkit-airplay="allow" preload="true" controls src="' + urls + '" style="width:100%;height:100%;"></video>');
Expand Down

0 comments on commit 7bde4d9

Please sign in to comment.