EasyRTC Documentation

Documentation

Source: desktop_capture_no_iframe_version.js

1
//
2
// This code was taken from: https://github.com/muaz-khan/WebRTC-Experiment/tree/master/Pluginfree-Screen-Sharing
3
// and modified to fit with EasyRTC.
4
//
5
6
// todo: need to check exact chrome browser because opera/node-webkit also uses chromium framework
7
var isChrome = !!navigator.webkitGetUserMedia;
8
9
// DetectRTC.js - github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC
10
// Below code is taken from RTCMultiConnection-v1.8.js (http://www.rtcmulticonnection.org/changes-log/#v1.8)
11
// and modified.
12
var DetectRTC = {};
13
14
15
(function() {
16
17
/**
18
* Provides a method for window/screen capture using an iframe.
19
* Please read the comments in the source code about setting the 
20
* chrome extension that this requires to be installed in each browser.
21
* @class Easyrtc_No_IframeCapture
22
*/
23
24
var screenCallback;
25
26
DetectRTC.screen = {
27
supported: false,
28
getSourceId: function(callback) {
29
if (!callback)
30
throw '"callback" parameter is mandatory.';
31
screenCallback = callback;
32
window.postMessage('desktopcapture-get-sourceId', '*');
33
},
34
isChromeExtensionAvailable: function(callback) {
35
if (!callback) {
36
return;
37
}
38
39
if (DetectRTC.screen.supported) {
40
callback(true);
41
}
42
43
// ask extension if it is available
44
window.postMessage('desktopcapture-are-you-there', '*');
45
46
setTimeout(function() {
47
callback(DetectRTC.screen.supported);
48
}, 2000);
49
},
50
onMessageCallback: function(data) {
51
// "cancel" button is clicked
52
if (data == 'PermissionDeniedError') {
53
DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
54
if (screenCallback) {
55
return screenCallback('PermissionDeniedError');
56
}
57
else {
58
throw new Error('PermissionDeniedError');
59
}
60
}
61
62
// extension notified his presence
63
if (data == 'desktopcapture-loaded') {
64
DetectRTC.screen.supported = true;
65
}
66
67
// extension shared temp sourceId
68
if (data.sourceId) {
69
DetectRTC.screen.sourceId = data.sourceId;
70
if (screenCallback) {
71
screenCallback(null);
72
}
73
}
74
}
75
};
76
77
// check if desktop-capture extension installed.
78
if (window.postMessage && isChrome) {
79
DetectRTC.screen.isChromeExtensionAvailable(function(){});
80
}
81
})();
82
83
window.addEventListener('message', function(event) {
84
if (event.origin != window.location.origin) {
85
return;
86
}
87
88
DetectRTC.screen.onMessageCallback(event.data);
89
});
90
91
/**
92
* Check if desktop capture installed
93
* @function
94
* @memberOf Easyrtc_No_IframeCapture
95
* @return boolean
96
*/ 
97
easyrtc.isDesktopCaptureInstalled = function() {
98
return DetectRTC.screen.supported;
99
}
100
101
/** Create a local media stream for desktop capture.
102
* This will fail if a desktop capture extension is not installed.
103
* not granting permission.
104
* @function
105
* @memberOf Easyrtc_No_IframeCapture
106
* @param {function(HTMLMediaStream)} successCallback - will be called with localmedia stream on success.
107
* @param {function(String,String)} errorCallback - is called with an error code and error description.
108
* @param {String} streamName - an optional name for the media source so you can use multiple cameras and screen share simultaneously.
109
* @param {String} iframeUrl - an optional url for the iframe. The default is to use Muaz Khan's.
110
* @example
111
*       easyrtc.initDesktopStream(
112
*          function(mediastream){
113
*              easyrtc.setVideoObjectSrc( document.getElementById("mirrorVideo"), mediastream);
114
*          },
115
*          function(errorCode, errorText){
116
*               easyrtc.showError(errorCode, errorText);
117
*          });
118
*
119
*/
120
easyrtc.initDesktopStream = function(successCallback, failureCallback, streamName) {
121
if (!easyrtc.isDesktopCaptureInstalled()) {
122
failureCallback(easyrtc.errCodes.DEVELOPER_ERR, "Desktop capture plugin not installed").
123
return;
124
}
125
126
DetectRTC.screen.getSourceId(function(error) {
127
if( error) {
128
failureCallback(easyrtc.errCodes.MEDIA_ERR, error);
129
}
130
else if (DetectRTC.screen.sourceId) {
131
easyrtc._presetMediaConstraints = {
132
video: {
133
mandatory: {
134
chromeMediaSource: 'desktop',
135
chromeMediaSourceId: DetectRTC.screen.sourceId,
136
maxWidth: 1920,
137
maxHeight: 1080,
138
minAspectRatio: 1.77
139
}
140
},
141
audio: false
142
}
143
easyrtc.initMediaSource(successCallback, failureCallback, streamName);
144
}
145
else {
146
failureCallback(easyrtc.errCodes.MEDIA_CANCELLED, "Desktop capture plugin not installed");
147
}
148
});
149
}
150
151
/**
152
* This method builds a function that can be attached to a button to install an extension.
153
* The install will only work on a {@link https://support.google.com/webmasters/answer/34592?hl=en|Google Verified Website}
154
* with a `link` tag pointing to the extension, which is required by chrome for
155
* {@link https://developer.chrome.com/webstore/inline_installation|Inline Installations}.
156
*
157
* @function
158
* @memberOf Easyrtc_No_IframeCapture
159
* @example
160
*
161
* <link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/bemabaogbdfpbkkganibcmhbgjogabfj" id="custom-app-id" />
162
*
163
* easyrtc.chromeInstall("custom-app_id", function() {
164
*         // success
165
*     },
166
*     function(errorCode, errorText) {
167
*         // failure
168
*     });
169
*
170
* @param  {String} extensionId The id of the `link` tag pointing to your extension.
171
* @param  {Function} successCallback Function to call on success.
172
* @param  {Function} failureCallback Function to call on failure.  Will pass argument `errorCode` and `errorMessage`.
173
*/
174
easyrtc.chromeInstaller = function(extensionId, successCallback, failureCallback) {
175
return function() {
176
var el, url;
177
if( !navigator.webkitGetUserMedia ||
178
!window.chrome ||
179
!chrome.webstore ||
180
!chrome.webstore.install ) {
181
failureCallback(easyrtc.errCodes.DEVELOPER_ERR, "Can't install plugin on non-chrome browsers");
182
}
183
else {
184
try {
185
var el = document.querySelector('head link#' + extensionId);
186
187
if ( ! el) throw new Error("Can't find a `link` element in `head` with id `"+extensionId+"`");
188
189
// get the chrome extension url from the link's href attribute
190
var url = el.attributes.href.value;
191
192
chrome.webstore.install(url, successCallback, function(error) {
193
failureCallback(easyrtc.errCodes.DEVELOPER_ERR, error);
194
});
195
196
}
197
catch (error) {
198
failureCallback(easyrtc.errCodes.DEVELOPER_ERR, error.message);
199
}
200
}
201
}
202
}