Browse Source

Signed-off-by: caner <5658514@qq.com>

caner 3 years ago
parent
commit
839cab7622
3 changed files with 42 additions and 7 deletions
  1. 2 1
      README.md
  2. 38 5
      SRSWebRtcPlayer.ts
  3. 2 1
      package.json

+ 2 - 1
README.md

@@ -4,4 +4,5 @@ srs webrtc video demo
 1. index_only_one.vue
     拉取单流
 2. index_mute
-    拉去多流
+    拉取多流
+3. ts 增加历史流,倍数播放

+ 38 - 5
SRSWebRtcPlayer.ts

@@ -2,11 +2,12 @@
  * srs webrtc palyer
  * auth Caner
  */
+ import MD5 from 'js-md5'
 class WebRtcPlayer {
     private Peer: any
     private TIMER: any
-    constructor(option: { HOST: string; TOKEN: string; UUID: string; PROFILE: number; PORT: number; DOM: HTMLVideoElement }) {
-        this.initWebRtc(option.HOST, option.PORT, option.TOKEN, option.UUID, option.PROFILE, option.DOM).then(res => {
+    constructor(option: { HOST: string; TOKEN: string; UUID: string; PROFILE: number; PORT: number; DOM: HTMLVideoElement, starttime?: number; endtime?: number }) {
+        this.initWebRtc(option.HOST, option.PORT, option.TOKEN, option.UUID, option.PROFILE, option.DOM, option.starttime, option.endtime).then(res => {
             this.Peer = res
         }).catch(() => {
             this.Peer = null
@@ -28,7 +29,7 @@ class WebRtcPlayer {
      * @param DOM video节点
      * @returns 
      */
-    private async initWebRtc(HOST: string, PORT: number, TOKEN: string, UUID: string, PROFILE: number, DOM: HTMLVideoElement) {
+    private async initWebRtc(HOST: string, PORT: number, TOKEN: string, UUID: string, PROFILE: number, DOM: HTMLVideoElement, STIME?: number, ETIME?: number) {
         try {
             const Peer = new RTCPeerConnection() as any
             Peer.addTransceiver('video', { direction: 'recvonly' })
@@ -59,11 +60,18 @@ class WebRtcPlayer {
             }
 
             // SDP SRS params
-            const params = {
+            const STREAM = STIME && ETIME ? MD5(UUID + PROFILE + Math.floor(new Date().getTime() * Math.random() * 100)) : UUID + PROFILE
+            const params = STIME && ETIME ? {
                 api: `http://${HOST}:${PORT}/rtc/v1/play/?token=${TOKEN}&uuid=${UUID}&stream=${UUID + PROFILE}&profile=${PROFILE}`,
                 clientip: null,
                 sdp: offer.sdp,
-                streamurl: `webrtc://${HOST}/live/${UUID + PROFILE}?token=${TOKEN}&uuid=${UUID}&stream=${UUID + PROFILE}&profile=${PROFILE}`,
+                streamurl: `webrtc://${HOST}/replay/${STREAM}?token=${TOKEN}&starttime=${STIME}&endtime=${ETIME}&uuid=${UUID}&stream=${STREAM}&profile=${PROFILE}`,
+                tid: Number(Math.floor(new Date().getTime() * Math.random() * 100)).toString(16).slice(0, 7)
+            } : {
+                api: `http://${HOST}:${PORT}/rtc/v1/play/?token=${TOKEN}&uuid=${UUID}&stream=${UUID + PROFILE}&profile=${PROFILE}`,
+                clientip: null,
+                sdp: offer.sdp,
+                streamurl: `webrtc://${HOST}/live/${STREAM}?token=${TOKEN}&uuid=${UUID}&stream=${STREAM}&profile=${PROFILE}`,
                 tid: Number(Math.floor(new Date().getTime() * Math.random() * 100)).toString(16).slice(0, 7)
             }
             console.log('params', params)
@@ -157,6 +165,31 @@ class WebRtcPlayer {
         })
     }
 
+    /**
+     * 倍数播放
+     * @param URL 
+     * @param TOKEN 
+     * @param uuid 
+     * @param stream 
+     */
+    public async speedPlay(URL: string, TOKEN: string, uuid: string, stream: string) {
+        window.fetch(URL, {
+            method: 'POST',
+            headers: { 'Content-Type': 'application/json' },
+            body: JSON.stringify({
+                code: 'replay.play',
+                token: TOKEN,
+                body: {
+                    uuid: uuid, // 摄像头id
+                    stream: stream, // 播放视频生成的stream
+                    startTime: 1667836800000, // 播放开始时间,默认0,即当前时间
+                    endTime: 1667923199000, // 播放结束时间。默认0,即一直播放,不主动结束
+                    speed: 4.0 // 速度 0.25、0.5、1、2、4。
+                }
+            })
+        })
+    }
+
     /**
      * 关闭webrtc
      */

+ 2 - 1
package.json

@@ -9,7 +9,8 @@
     "preview": "vite preview"
   },
   "dependencies": {
-    "axios": "^1.1.3"
+    "axios": "^1.1.3",
+    "js-md5": "^0.7.3"
   },
   "devDependencies": {}
 }