EasyRTC Documentation

Documentation

Source: desktop_capture_iframe_version.js

1
2
//  This file is a modification of Muaz Khan's getScreenId.js. It uses loads an iframe 
3
//  pointed at Muaz Khan's page, and then communicates with that Iframe. You can also point
4
//  it at other urls.
5
//  
6
//  Technically, it is possible to desktop capture without iframes and for a production system.
7
//  However, this solution get will get you running with the minimal effort on your side.
8
//  
9
//  //
10
//  
11
// // Last time updated at July 29, 2014, 08:32:23
12
// Latest file can be found here: https://cdn.webrtc-experiment.com/getScreenId.js
13
14
// Muaz Khan         - www.MuazKhan.com
15
// MIT License       - www.WebRTC-Experiment.com/licence
16
// Documentation     - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/getScreenId.js
17
// Modified by Eric Davies Sept 1/ 2014.
18
// 
19
// ______________
20
// getScreenId.js
21
22
/**
23
* Provides a method for window/screen capture using an iframe.
24
* This requires that your users install Muah Khans desktop extension.
25
* Read the source code for more details.
26
* @class Easyrtc_IframeCapture
27
*/
28
29
30
31
(function() {
32
/** Create a local media stream for desktop capture.
33
* This will fail if a desktop capture extension is not installed.
34
* not granting permission.
35
* @function
36
* @memberOf Easyrtc_IframeCapture
37
* @param {function(Object)} successCallback - will be called with localmedia stream on success. 
38
* @param {function(String,String)} errorCallback - is called with an error code and error description.
39
* @param {String} streamName - an optional name for the media source so you can use multiple cameras and screen share simultaneously.
40
* @param {String} iframeUrl - an optional url for the iframe. The default is to use Muaz Khan's.
41
* @example
42
*       easyrtc.initMediaSource(
43
*          function(mediastream){
44
*              easyrtc.setVideoObjectSrc( document.getElementById("mirrorVideo"), mediastream);
45
*          },
46
*          function(errorCode, errorText){
47
*               easyrtc.showError(errorCode, errorText);
48
*          });
49
*
50
*/
51
var iframeUrl =  'https://www.webrtc-experiment.com/getSourceId/';
52
53
easyrtc.initDesktopStream= function(successCallback, failureCallback, streamName) {
54
// for Firefox:
55
// sourceId == 'firefox'
56
// screen_constraints = {...}
57
58
59
60
if (!!navigator.mozGetUserMedia) {
61
easyrtc._presetMediaConstraints = {
62
video: {
63
mozMediaSource: 'window',
64
mediaSource: 'window',
65
maxWidth: 1920,
66
maxHeight: 1080,
67
minAspectRatio: 1.77
68
},
69
audio: false
70
};
71
easyrtc.initMediaSource(successCallback, failureCallback, streamName);
72
return;
73
}
74
75
postMessage();
76
77
var cb = function(event) {
78
if (!event.data) return;
79
80
if (event.data.chromeMediaSourceId) {
81
window.removeEventListener("message", cb);
82
if (event.data.chromeMediaSourceId === 'PermissionDeniedError') {
83
failureCallback(easyrtc.errCodes.MEDIA_ERR, 'permission-denied');
84
} else {
85
easyrtc._presetMediaConstraints = {
86
video: {
87
mandatory: {
88
chromeMediaSource:'desktop',
89
chromeMediaSourceId: event.data.chromeMediaSourceId,
90
maxWidth: 1920,
91
maxHeight: 1080,
92
minAspectRatio: 1.77
93
}
94
},
95
audio: false
96
}
97
easyrtc.initMediaSource(successCallback, failureCallback, streamName);
98
}
99
}
100
101
if (event.data.chromeExtensionStatus) {
102
console.log("extension status is ", event.data.chromeExtensionStatus);  
103
}
104
};
105
easyrtc.desktopCaptureInstalled = null;
106
window.addEventListener('message', cb);
107
};
108
109
110
var iframe = document.createElement('iframe');
111
112
function postMessage() {
113
if (!iframe.isLoaded) {
114
setTimeout(postMessage, 100);
115
return;
116
}
117
118
iframe.contentWindow.postMessage({
119
captureSourceId: true
120
}, '*');
121
}
122
123
iframe.onload = function() {
124
iframe.isLoaded = true;
125
};
126
127
iframe.src = iframeUrl;
128
129
iframe.style.display = 'none';
130
(document.body || document.documentElement).appendChild(iframe);
131
})();