-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.qml
More file actions
223 lines (200 loc) · 7.32 KB
/
main.qml
File metadata and controls
223 lines (200 loc) · 7.32 KB
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
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.15
import QtMultimedia 5.12
import QtQml 2.15
import QtQuick.Controls 1.4 as Controls1_4
import Qml.WebSocket 1.0
import Qml.VideoFrame 1.0
import "qrc:/protoo.js" as Protoo
Window {
id: mainWin
visible: true
width: 1080
height: 720
title: qsTr("Hello World")
property string roomId: "10000"
property string uid: "1000"
TextField {
id: txtUri
x: 100
width: 373
height: 40
text: "wss://test.zhuanxin.com:33443/?uid=1000&roomid=10000&appid=0"
placeholderText: qsTr("wss uri")
onTextChanged: {
let url = Qt.resolvedUrl(text);
mainWin.roomId = url.queryItemValue("roomid");
mainWin.uid = url.queryItemValue("uid");
}
}
Button {
id: btnConnect
height: txtUri.height
anchors.left: txtUri.right
text: qsTr("connect")
onClicked: {
wss.connect(txtUri.text);
}
}
QmlWebSocket {
id: wss
onClose: {
console.error("wss close");
}
onError: {
console.error("wss error, %s", errorMessage);
}
function notification(msg) {
switch (msg.method) {
case "list": {
const { roomid, users } = msg.data;
for (let otherUser of users) {
if (otherUser.uid != mainWin.uid) {
if (otherUser.mediaStreams.length == 0) {
} else {
const tab = tabs.createTab(otherUser.uid);
tab.item.mid = otherUser.mediaStreams[0].mid;
}
}
}
break;
}
case "publish": {
const { uid, mediaStream } = msg.data;
const tab = tabs.createTab(uid);
tab.item.mid = mediaStream.mid;
break;
}
}
}
onMessage: {
try {
console.info("weoskcet recv, %s", msg);
const raw = JSON.parse(msg);
if (raw.notification) {
notification(raw);
} else {
Protoo.setResponse(raw);
}
}
catch (error) {
console.error('parse() | invalid JSON: %s', error.message);
}
}
onOpen: {
console.info("wss open");
}
}
Controls1_4.TabView {
anchors.top: txtUri.bottom
width: mainWin.width
height: mainWin.height - y
id: tabs
function createTab(title) {
const tab = addTab("", tabComponent);
tab.active = true;
tab.title = title;
return tab;
}
Component.onCompleted: createTab("local")
Component {
id: tabComponent
Rectangle {
anchors.fill: parent
property string mid: ""
VideoOutput {
id: video
width: 320
height: 180
anchors.bottom: parent.bottom
source: QmlVideoFrame {
id: videoFrame
width: video.width
height: video.height
onMessage: {
if (type == "rtp") {
packet.addRtp(msg);
} else if (type == "rtcp") {
console.info("rtcp, %s", msg);
}
}
}
}
Packet {
id: packet
width: parent.width
height: parent.height - video.height
childWidth: parent.width - video.width - publish.width
childHeight: video.height
}
Button {
id: publish
anchors.left: video.right
anchors.top: video.top
visible: true
text: "publish"
onClicked: {
videoFrame.direction = "send";
const offer = videoFrame.createOffer();
const request = Protoo.createRequest("publish", {
description: { type: "offer", sdp: offer },
codec: "av1x",
});
console.info("publish send, %s", JSON.stringify(request));
wss.send(JSON.stringify(request));
Protoo.getResponse(request).then(function (data) {
console.info("publish success, %s", JSON.stringify(data));
videoFrame.setRemoteDescription(data.description.sdp);
}).catch(function (err) {
console.error("publish failed, %s", err.message);
});
}
}
Button {
id: subscribe
anchors.left: publish.left
anchors.top: publish.top
visible: false
text: "subscribe"
onClicked: {
videoFrame.direction = "recv";
const request = Protoo.createRequest("subscribe", {
mid: mid,
constraints: { video: true, audio: true },
});
console.info("subscribe send, %s", JSON.stringify(request));
wss.send(JSON.stringify(request));
Protoo.getResponse(request).then(function (data) {
console.info("subscribe success, %s", JSON.stringify(data));
videoFrame.setRemoteDescription(data.description.sdp);
let answer = videoFrame.createAnswer();
const request2 = Protoo.createRequest("subscribe", {
mid: data.mid,
description: { type: "answer", sdp: answer },
});
console.info("subscribe send 2, %s", JSON.stringify(request2));
wss.send(JSON.stringify(request2));
Protoo.getResponse(request2).then(function (data) {
console.info("subscribe success 2");
}).catch(function (err){
console.error("subscribe failed 2, %s", err.message);
});
}).catch(function (err) {
console.error("subscribe failed, %s", err.message);
});
}
}
onMidChanged: {
if (mid) {
publish.visible = false;
subscribe.visible = true;
} else {
publish.visible = true;
subscribe.visible = false;
}
}
}
}
}
}