|
@@ -1,7 +1,10 @@
|
1
|
|
-/* global $, APP */
|
|
1
|
+/* global $, JitsiMeetJS, APP */
|
2
|
2
|
import * as KeyCodes from "../keycode/keycode";
|
3
|
|
-import {EVENT_TYPES, API_EVENT_TYPE}
|
|
3
|
+import {EVENT_TYPES, REMOTE_CONTROL_EVENT_TYPE, PERMISSIONS_ACTIONS}
|
4
|
4
|
from "../../service/remotecontrol/Constants";
|
|
5
|
+import RemoteControlParticipant from "./RemoteControlParticipant";
|
|
6
|
+
|
|
7
|
+const ConferenceEvents = JitsiMeetJS.events.conference;
|
5
|
8
|
|
6
|
9
|
/**
|
7
|
10
|
* Extract the keyboard key from the keyboard event.
|
|
@@ -44,34 +47,113 @@ function getModifiers(event) {
|
44
|
47
|
* It listens for mouse and keyboard events and sends them to the receiver
|
45
|
48
|
* party of the remote control session.
|
46
|
49
|
*/
|
47
|
|
-export default class Controller {
|
|
50
|
+export default class Controller extends RemoteControlParticipant {
|
48
|
51
|
/**
|
49
|
52
|
* Creates new instance.
|
50
|
53
|
*/
|
51
|
54
|
constructor() {
|
52
|
|
- this.enabled = false;
|
|
55
|
+ super();
|
|
56
|
+ this.controlledParticipant = null;
|
|
57
|
+ this.requestedParticipant = null;
|
|
58
|
+ this.stopListener = this._handleRemoteControlStoppedEvent.bind(this);
|
53
|
59
|
}
|
54
|
60
|
|
55
|
61
|
/**
|
56
|
|
- * Enables / Disables the remote control
|
57
|
|
- * @param {boolean} enabled the new state.
|
|
62
|
+ * Requests permissions from the remote control receiver side.
|
|
63
|
+ * @param {string} userId the user id of the participant that will be
|
|
64
|
+ * requested.
|
58
|
65
|
*/
|
59
|
|
- enable(enabled) {
|
60
|
|
- this.enabled = enabled;
|
|
66
|
+ requestPermissions(userId) {
|
|
67
|
+ if(!this.enabled) {
|
|
68
|
+ return Promise.reject(new Error("Remote control is disabled!"));
|
|
69
|
+ }
|
|
70
|
+ return new Promise((resolve, reject) => {
|
|
71
|
+ let permissionsReplyListener = (participant, event) => {
|
|
72
|
+ let result = null;
|
|
73
|
+ try {
|
|
74
|
+ result = this._handleReply(participant, event);
|
|
75
|
+ } catch (e) {
|
|
76
|
+ reject(e);
|
|
77
|
+ }
|
|
78
|
+ if(result !== null) {
|
|
79
|
+ this.requestedParticipant = null;
|
|
80
|
+ APP.conference.removeConferenceListener(
|
|
81
|
+ ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
82
|
+ permissionsReplyListener);
|
|
83
|
+ resolve(result);
|
|
84
|
+ }
|
|
85
|
+ };
|
|
86
|
+ APP.conference.addConferenceListener(
|
|
87
|
+ ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
88
|
+ permissionsReplyListener);
|
|
89
|
+ this.requestedParticipant = userId;
|
|
90
|
+ this._sendRemoteControlEvent(userId, {
|
|
91
|
+ type: EVENT_TYPES.permissions,
|
|
92
|
+ action: PERMISSIONS_ACTIONS.request
|
|
93
|
+ }, e => {
|
|
94
|
+ APP.conference.removeConferenceListener(
|
|
95
|
+ ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
96
|
+ permissionsReplyListener);
|
|
97
|
+ this.requestedParticipant = null;
|
|
98
|
+ reject(e);
|
|
99
|
+ });
|
|
100
|
+ });
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ /**
|
|
104
|
+ * Handles the reply of the permissions request.
|
|
105
|
+ * @param {JitsiParticipant} participant the participant that has sent the
|
|
106
|
+ * reply
|
|
107
|
+ * @param {object} event the remote control event.
|
|
108
|
+ */
|
|
109
|
+ _handleReply(participant, event) {
|
|
110
|
+ const remoteControlEvent = event.event;
|
|
111
|
+ const userId = participant.getId();
|
|
112
|
+ if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE
|
|
113
|
+ && remoteControlEvent.type === EVENT_TYPES.permissions
|
|
114
|
+ && userId === this.requestedParticipant) {
|
|
115
|
+ if(remoteControlEvent.action === PERMISSIONS_ACTIONS.grant) {
|
|
116
|
+ this.controlledParticipant = userId;
|
|
117
|
+ this._start();
|
|
118
|
+ return true;
|
|
119
|
+ } else if(remoteControlEvent.action === PERMISSIONS_ACTIONS.deny) {
|
|
120
|
+ return false;
|
|
121
|
+ } else {
|
|
122
|
+ throw new Error("Unknown reply received!");
|
|
123
|
+ }
|
|
124
|
+ } else {
|
|
125
|
+ //different message type or another user -> ignoring the message
|
|
126
|
+ return null;
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ /**
|
|
131
|
+ * Handles remote control stopped.
|
|
132
|
+ * @param {JitsiParticipant} participant the participant that has sent the
|
|
133
|
+ * event
|
|
134
|
+ * @param {object} event the the remote control event.
|
|
135
|
+ */
|
|
136
|
+ _handleRemoteControlStoppedEvent(participant, event) {
|
|
137
|
+ if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE
|
|
138
|
+ && event.event.type === EVENT_TYPES.stop
|
|
139
|
+ && participant.getId() === this.controlledParticipant) {
|
|
140
|
+ this._stop();
|
|
141
|
+ }
|
61
|
142
|
}
|
62
|
143
|
|
63
|
144
|
/**
|
64
|
145
|
* Starts processing the mouse and keyboard events.
|
65
|
|
- * @param {JQuery.selector} area the selector which will be used for
|
66
|
|
- * attaching the listeners on.
|
67
|
146
|
*/
|
68
|
|
- start(area) {
|
|
147
|
+ _start() {
|
69
|
148
|
if(!this.enabled)
|
70
|
149
|
return;
|
71
|
|
- this.area = area;
|
|
150
|
+ APP.conference.addConferenceListener(
|
|
151
|
+ ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
152
|
+ this.stopListener);
|
|
153
|
+ this.area = $("#largeVideoWrapper");
|
72
|
154
|
this.area.mousemove(event => {
|
73
|
155
|
const position = this.area.position();
|
74
|
|
- this._sendRemoteControlEvent({
|
|
156
|
+ this._sendRemoteControlEvent(this.controlledParticipant, {
|
75
|
157
|
type: EVENT_TYPES.mousemove,
|
76
|
158
|
x: (event.pageX - position.left)/this.area.width(),
|
77
|
159
|
y: (event.pageY - position.top)/this.area.height()
|
|
@@ -85,7 +167,7 @@ export default class Controller {
|
85
|
167
|
this._onMouseClickHandler.bind(this, EVENT_TYPES.mousedblclick));
|
86
|
168
|
this.area.contextmenu(() => false);
|
87
|
169
|
this.area[0].onmousewheel = event => {
|
88
|
|
- this._sendRemoteControlEvent({
|
|
170
|
+ this._sendRemoteControlEvent(this.controlledParticipant, {
|
89
|
171
|
type: EVENT_TYPES.mousescroll,
|
90
|
172
|
x: event.deltaX,
|
91
|
173
|
y: event.deltaY
|
|
@@ -99,7 +181,11 @@ export default class Controller {
|
99
|
181
|
/**
|
100
|
182
|
* Stops processing the mouse and keyboard events.
|
101
|
183
|
*/
|
102
|
|
- stop() {
|
|
184
|
+ _stop() {
|
|
185
|
+ APP.conference.removeConferenceListener(
|
|
186
|
+ ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
187
|
+ this.stopListener);
|
|
188
|
+ this.controlledParticipant = null;
|
103
|
189
|
this.area.off( "mousemove" );
|
104
|
190
|
this.area.off( "mousedown" );
|
105
|
191
|
this.area.off( "mouseup" );
|
|
@@ -116,7 +202,7 @@ export default class Controller {
|
116
|
202
|
* @param {Event} event the mouse event.
|
117
|
203
|
*/
|
118
|
204
|
_onMouseClickHandler(type, event) {
|
119
|
|
- this._sendRemoteControlEvent({
|
|
205
|
+ this._sendRemoteControlEvent(this.controlledParticipant, {
|
120
|
206
|
type: type,
|
121
|
207
|
button: event.which
|
122
|
208
|
});
|
|
@@ -128,25 +214,10 @@ export default class Controller {
|
128
|
214
|
* @param {Event} event the key event.
|
129
|
215
|
*/
|
130
|
216
|
_onKeyPessHandler(type, event) {
|
131
|
|
- this._sendRemoteControlEvent({
|
|
217
|
+ this._sendRemoteControlEvent(this.controlledParticipant, {
|
132
|
218
|
type: type,
|
133
|
219
|
key: getKey(event),
|
134
|
220
|
modifiers: getModifiers(event),
|
135
|
221
|
});
|
136
|
222
|
}
|
137
|
|
-
|
138
|
|
- /**
|
139
|
|
- * Sends remote control event to the controlled participant.
|
140
|
|
- * @param {Object} event the remote control event.
|
141
|
|
- */
|
142
|
|
- _sendRemoteControlEvent(event) {
|
143
|
|
- if(!this.enabled)
|
144
|
|
- return;
|
145
|
|
- try{
|
146
|
|
- APP.conference.sendEndpointMessage("",
|
147
|
|
- {type: API_EVENT_TYPE, event});
|
148
|
|
- } catch (e) {
|
149
|
|
- // failed to send the event.
|
150
|
|
- }
|
151
|
|
- }
|
152
|
223
|
}
|