‘liusuyi’
2023-10-24 5addedcb92fcd56239825f301502aabbeaf5e325
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
import "./libs/request.min.js";
import "./janusvideoroom.js";
import "./libs/EventEmitter.min.js";
 
class WebRTCStreamerJanusElement extends HTMLElement {
    static get observedAttributes() {
        return ['videostream','audiostream','options', 'webrtcurl'];
    }  
    
    constructor() {
        super(); 
        this.shadowDOM = this.attachShadow({mode: 'open'});
        this.shadowDOM.innerHTML = `
                    <style>@import "styles.css"</style>
                    <body></body>
                    `;
    }
    connectedCallback() {
        this.connectStream();
    }
    disconnectedCallback() {
        this.disconnectStream();
    }
    attributeChangedCallback(attrName, oldVal, newVal) {
        this.connectStream();
    }
    
    writeStatus(name, status) {
        var textNode = this.shadowDOM.createElement("h3");
        textNode.innerHTML = name + " " + status;
        this.shadowDOM.body.appendChild(textNode);
    }
        
    disconnectStream() {
        if (this.janus) {
            this.janus.leave();
            this.janus = null;
        }
    }
    connectStream() {
        this.disconnectStream();
        
        let webrtcurl = this.getAttribute("webrtcurl") || webrtcConfig.url;
        let videostream = this.getAttribute("videostream") || webrtcConfig.defaultvideostream;
        let audiostream = this.getAttribute("audiostream") || webrtcConfig.defaultaudiostream;
        let options = this.getAttribute("options") || webrtcConfig.options;
        
        
        var bus = new EventEmitter();
        bus.addListener('state', writeStatus);        
    
        this.janus = new JanusVideoRoom(janusRoomConfig.url, webrtcurl, bus);
        this.janus.join(janusRoomConfig.roomId, {video:videostream}, videostream);
    }
    
    
}
 
customElements.define('webrtc-streamer-janus', WebRTCStreamerJanusElement);