xiangpei
2024-06-04 c87c4fe5aa3987d61b10d57208232a94eec83d7c
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
239
240
241
242
243
244
245
246
247
<template>
    <div class="c">
        <div class="bg">
            <div class="main">
                <div class="list">
                    <div class="card list-con" v-for="(item,index) in curriculum" :key="index">
                        <img src="@/assets/img/teach/list-card-bg.jpg" style="height:220px;width:350px" alt=""
                            v-if="!item.start">
                        <video :id="`video-${item.id}`" v-show="item.start" height="220px" width="350px"></video>
                        <div class="list-con-t">
                            <p>上课时间</p>
                            <span>{{ item.time }}</span>
                        </div>
                        <div class="list-con-op">
                            <el-button colorType="default">资料上传</el-button>
                            <el-button colorType="deepBlue" @click="handleStart(item)">开始上课</el-button>
                        </div>
                    </div>
                </div>
                <div class="flex" style="justify-content:center;margin-top:20px;">
                    <el-pagination background layout="prev, pager, next" :total="1000">
                    </el-pagination>
                </div>
            </div>
        </div>
 
        <s-dialog v-model="dialog" title="请选择授课方式">
            <template>
                <div style="width:300px; padding:20px; justify-content:space-between" class="flex">
                    <el-button colorType="default">语音授课</el-button>
                    <el-button colorType="deepBlue" @click="selectScreen">视频授课</el-button>
                </div>
            </template>
        </s-dialog>
 
        <el-dialog title="选择授课屏幕" :visible.sync="dialogScreenVisible" width="60%">
            <div v-for="(source, index) in sources" :key="source.id" class="sourceDiv"
                :class="{ defaultSelect: index === selectIndex }" @click="selectSource(index, source)">
                <video width="220" height="140" :id="`video-${source.id}`">
                    您的浏览器不支持 video 标签。
                </video>
                {{ source.name }}
            </div>
 
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogScreenVisible = false; screenConfirm()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
 
<script>
import { desktopCapturer } from 'electron';
 
export default {
    data () {
        return {
            dialog: false,
            dialogScreenVisible: false,
            sources: null,
            selectIndex: 0,
            currentSource: null,
            currentItem: null,
            curriculum: [
                {
                    id: 1,
                    start: false,
                    time: '12:30 - 13:30'
                },
                {
                    id: 2,
                    start: false,
                    time: '12:30 - 13:30'
                },
                {
                    id: 3,
                    start: false,
                    time: '12:30 - 13:30'
                },
                {
                    id: 4,
                    start: false,
                    time: '12:30 - 13:30'
                },
                {
                    id: 5,
                    start: false,
                    time: '12:30 - 13:30'
                },
                {
                    id: 6,
                    start: false,
                    time: '12:30 - 13:30'
                },
            ],
 
        }
    },
    methods: {
        handleStart (i) {
            this.dialog = true;
            this.currentItem = i
        },
        selectSource (index, source) {
            this.selectIndex = index
            this.currentSource = source
        },
        screenConfirm () {
            this.getSource(this.currentSource, `video-${this.currentItem.id}`, true)
        },
        async selectScreen () {
            this.dialog = false
            const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] })
            this.sources = sources
            this.getSources(sources)
        },
        async getSources (sources, containerId) {
            const _this = this
            await sources.forEach(source => {
                _this.getSource(source, containerId)
            })
        },
        async getSource (source, containerId, isPush) {
            const constraints = {
                audio: false,
                video: {
                    mandatory: {
                        chromeMediaSource: 'desktop',
                        chromeMediaSourceId: source.id,
                        minWidth: 1440,
                        maxWidth: 1920,
                        minHeight: 900,
                        maxHeight: 1080
                    }
                }
            }
            await this.getUserMedia(source, containerId, constraints, isPush)
        },
        async getUserMedia (source, containerId, constraints, isPush) {
            const _this = this
 
            await navigator.mediaDevices.getUserMedia(constraints)
                .then(function (mediaStream) {
                    if (!isPush) {
                        _this.dialogScreenVisible = true
                    } else {
                        _this.dialogScreenVisible = false
                        _this.currentItem.start = true
                    }
                    var video = document.getElementById(containerId ? containerId : `video-${source.id}`);
 
                    video.srcObject = mediaStream;
                    video.onloadedmetadata = async function (e) {
                        video.play();
                        if (isPush) {
                            let stream = new MediaStream()
                            stream.addTrack(mediaStream.getVideoTracks()[0])
 
                            let audio1 = await getDisplayMediaSource('microphone')
                            // if (audio1) {
 
                            //   stream.addTrack(audio1.getAudioTracks()[0])
 
                            //   // _this.sdk.ontrack()
                            //   // console.log(audio1.getAudioTracks()[0])
                            //   // mediaStream.addTrack(audio1.getAudioTracks()[0])
                            // }
                            let audio2 = await getDisplayMediaSource('system')
                            // if (audio2) {
                            //   stream.addTrack(audio2.getAudioTracks()[0])
                            //   // 将获取的音频流数据添加到视频流
                            //   // mediaStream.addTrack(audio2.getAudioTracks()[0])
                            // }
                            stream.addTrack(...mergeAudioStreams(audio1, audio2))
                            startPublish(`teacher/${_this.currentItem.id}`, stream)
                            //     // SrsRtcPublisherAsync().publish('webrtc://192.168.3.115/live/livestream', mediaStream)
                        }
 
                    };
                })
                .catch(function (err) { console.log(err.name + ": " + err.message); }); // 总是在最后检查错误
 
        },
    }
}
</script>
 
<style scoped lang="scss">
.flex {
    display: flex;
}
 
.card {
    background: white;
    box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.2);
    padding: 20px;
    box-sizing: border-box;
    margin-bottom: 20px;
    border-radius: 10px;
}
 
.list {
    margin-top: 50px;
    width: 1262px;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
 
    &-con {
        &-t {
            &>p {
                margin-top: 10px;
                margin-bottom: 0px;
            }
 
            &>span {
                color: #777;
            }
        }
 
        &-op {
            justify-content: space-between;
            display: flex;
        }
    }
}
 
.sourceDiv {
    display: inline-flex;
    margin-right: 10px;
    flex-direction: column;
    padding: 5px;
}
 
.sourceDiv.defaultSelect {
    background-color: cornsilk;
}
 
.sourceDiv:hover {
    background-color: aliceblue;
}
 
.sourceDiv:active {
    background-color: cornsilk;
}
</style>