Skip to content

Commit 1cf983a

Browse files
committed
refactor: conference, dynamic-resolution
1 parent 6741153 commit 1cf983a

File tree

21 files changed

+264
-1583
lines changed

21 files changed

+264
-1583
lines changed

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["eamodio.gitlens"]
3+
}

frontend/contents/js/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @homepage codejs.co.kr
66
*/
77

8-
$(function() {
8+
$(function () {
99
const browserVersion = DetectRTC.browser.version;
1010
const isFirefox = DetectRTC.browser.isFirefox;
1111
const isChrome = DetectRTC.browser.isChrome;
@@ -46,7 +46,7 @@ $(function() {
4646
}
4747

4848
// 캠 체크, 체크 텀이 필요함
49-
setTimeout(function() {
49+
setTimeout(function () {
5050
if (checkPage && !DetectRTC.hasWebcam) {
5151
showNeedCamMessage();
5252
}

frontend/contents/js/lib/eventemitter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,6 @@ EventEmitter.prototype.emit = function(type) {
155155
} else {
156156
return false;
157157
}
158-
};
158+
};
159+
160+
export default EventEmitter;

frontend/contents/js/lib/inherit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ const inherit = (function(Parent, Child) {
1313
Child.super = Parent.prototype;
1414
};
1515
})();
16+
17+
export default inherit;

frontend/views/examples/conference/index.ejs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
</div>
2626

2727

28-
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
29-
<script src="js/modules/peer-handler.js"></script>
30-
<script src="js/modules/media-handler.js"></script>
31-
<script src="js/main.js"></script>
28+
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.4.0/socket.io.js"></script>
29+
<script type="module" src="js/main.js"></script>
3230
<% include ../../partials/footer %>

frontend/views/examples/conference/js/main.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import PeerHandler from './modules/peer-handler.js';
2+
import MediaHandler from './modules/media-handler.js';
3+
14
/*!
25
*
36
* WebRTC Lab
@@ -13,18 +16,24 @@ $(function () {
1316
const animationTime = 500;
1417
const isSafari = DetectRTC.browser.isSafari;
1518
const isMobile = DetectRTC.isMobileDevice;
16-
const mediaOption = {
19+
const mediaConstraints = {
1720
audio: true,
1821
video: {
19-
mandatory: {
20-
maxWidth: 1920,
21-
maxHeight: 1080,
22-
maxFrameRate: 30,
22+
width: {
23+
ideal: 1280,
24+
min: 640,
25+
max: 1920,
26+
},
27+
height: {
28+
ideal: 720,
29+
min: 360,
30+
max: 1080,
31+
},
32+
frameRate: {
33+
ideal: 25,
2334
},
24-
optional: [
25-
{ googNoiseReduction: true }, // Likely removes the noise in the captured video stream at the expense of computational effort.
26-
{ facingMode: 'user' }, // Select the front/user facing camera or the rear/environment facing camera if available (on Phone)
27-
],
35+
// Select the front/user facing camera or the rear/environment facing camera if available (on Phone)
36+
facingMode: 'user',
2837
},
2938
};
3039

@@ -41,7 +50,7 @@ $(function () {
4150
const $uniqueToken = $('#unique-token');
4251

4352
async function getUserMedia() {
44-
const stream = await peerHandler.getUserMedia(mediaOption, isOffer);
53+
const stream = await peerHandler.getUserMedia(mediaConstraints, isOffer);
4554
onLocalStream(stream);
4655
}
4756

@@ -51,14 +60,12 @@ $(function () {
5160
function onDetectUser() {
5261
console.log('onDetectUser');
5362

54-
$waitWrap.html(
55-
[
56-
'<div class="room-info">',
57-
'<p>당신을 기다리고 있어요. 참여 하실래요?</p>',
58-
'<button id="btn-join">Join</button>',
59-
'</div>',
60-
].join('\n')
61-
);
63+
$waitWrap.html(`
64+
<div class="room-info">
65+
<p>당신을 기다리고 있어요. 참여 하실래요?</p>
66+
<button id="btn-join">Join</button>
67+
</div>
68+
`);
6269

6370
$('#btn-join').click(function () {
6471
$(this).attr('disabled', true);
@@ -228,6 +235,10 @@ $(function () {
228235
$this.toggleClass('active');
229236
mediaHandler[$this.hasClass('active') ? 'muteAudio' : 'unmuteAudio']();
230237
});
238+
239+
$('#btn-change-resolution').click(function () {
240+
peerHandler.changeResolution();
241+
});
231242
}
232243

233244
initialize();

frontend/views/examples/conference/js/modules/media-handler.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ function MediaHandler() {
8686
this.unmuteAudio = unmuteAudio;
8787
this.playForIOS = playForIOS;
8888
}
89+
90+
export default MediaHandler;

frontend/views/examples/conference/js/modules/peer-handler.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import EventEmitter from '/js/lib/eventemitter.js';
2+
import inherit from '/js/lib/inherit.js';
3+
14
/**
25
* PeerHandler
36
* @param options
@@ -12,8 +15,6 @@ function PeerHandler(options) {
1215
const RTCSessionDescription =
1316
window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
1417
const RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
15-
const browserVersion = DetectRTC.browser.version;
16-
const isEdge = DetectRTC.browser.isEdge && browserVersion >= 15063; // 15버전 이상
1718

1819
const that = this;
1920
const send = options.send;
@@ -31,22 +32,14 @@ function PeerHandler(options) {
3132
};
3233

3334
let localStream = null;
35+
let resolution = {
36+
width: 1280,
37+
height: 720,
38+
};
3439
let peer = null; // offer or answer peer
3540
let peerConnectionOptions = {
3641
optional: [{ DtlsSrtpKeyAgreement: 'true' }],
3742
};
38-
let mediaConstraints = {
39-
mandatory: {
40-
OfferToReceiveAudio: true,
41-
OfferToReceiveVideo: true,
42-
},
43-
};
44-
45-
// edge is not supported
46-
if (isEdge) {
47-
peerConnectionOptions = {};
48-
mediaConstraints = {};
49-
}
5043

5144
/**
5245
* getUserMedia
@@ -78,19 +71,20 @@ function PeerHandler(options) {
7871
addTrack(peer, localStream); // addTrack 제외시 recvonly로 SDP 생성됨
7972
}
8073

81-
peer.createOffer(
82-
(SDP) => {
74+
peer
75+
.createOffer()
76+
.then((SDP) => {
8377
peer.setLocalDescription(SDP);
8478
console.log('Sending offer description', SDP);
8579

8680
send({
8781
to: 'all',
8882
sdp: SDP,
8983
});
90-
},
91-
onSdpError,
92-
mediaConstraints
93-
);
84+
})
85+
.catch((error) => {
86+
console.error('Error setLocalDescription', error);
87+
});
9488
}
9589

9690
/**
@@ -238,6 +232,31 @@ function PeerHandler(options) {
238232
}
239233
}
240234

235+
/**
236+
* 전송중인 영상 해상도를 다이나믹하게 변경합니다.
237+
*/
238+
function changeResolution() {
239+
localStream.getVideoTracks().forEach((track) => {
240+
console.log('changeResolution', track, track.getConstraints(), track.applyConstraints);
241+
242+
if (resolution.height > 90) {
243+
resolution = {
244+
width: 160,
245+
height: 90,
246+
};
247+
} else {
248+
resolution = {
249+
width: 1280,
250+
height: 720,
251+
};
252+
}
253+
254+
track.applyConstraints(resolution).then(() => {
255+
console.log('changeResolution result', track.getConstraints());
256+
});
257+
});
258+
}
259+
241260
/**
242261
* signaling
243262
* @param data
@@ -278,6 +297,9 @@ function PeerHandler(options) {
278297
*/
279298
this.getUserMedia = getUserMedia;
280299
this.signaling = signaling;
300+
this.changeResolution = changeResolution;
281301
}
282302

283303
inherit(EventEmitter, PeerHandler);
304+
305+
export default PeerHandler;

frontend/views/examples/dynamic-resolution/css/main.css

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)