Blame view

static/plugins/webshim-gh-pages/js-webshim/dev/shims/usermedia-shim.js 4.45 KB
831eac332   zhuzhenchao   add file
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
  webshim.register('usermedia-shim', function($, webshim, window, document, undefined, options){
  	"use strict";
  	var addMediaAPI;
  	var id = 0;
  	var streamCb = {};
  	var hasSwf = swfmini.hasFlashPlayerVersion('11.3');
  	var mediaelement = webshim.mediaelement;
  	var flashEvents = {
  		NotSupportedError: 1,
  		PermissionDeniedError: 1,
  		//not implemented yet
  		ConstraintNotSatisfiedError: 1,
  		onUserSuccess: 1
  	};
  	var noSource = function(){
  		return !$.prop(this, 'currentSrc') && !mediaelement.srces(this).length;
  	};
  
  	function wsGetUserMedia(constraints, successCb, failCb){
  		if(hasSwf){
  			if(!webshim.mediaelement.createSWF){
  				webshim.loader.loadList(['swfmini-embed']);
  				webshim.mediaelement.loadSwf = true;
  				webshim.reTest(['mediaelement-jaris'], true);
  				webshim.ready('mediaelement-jaris', function(){
  					createMediaRequest(constraints, successCb, failCb);
  				});
  			} else {
  				createMediaRequest(constraints, successCb, failCb);
  			}
  		} else {
  			failCb({name: 'NOT_SUPPORTED_ERROR'});
  		}
  	}
  
  	function createMediaRequest(constraints, successCb, failCb){
  		var src;
  		var media = getMediaCandidate();
  		if(!media){return;}
  
  		id++;
  		src = 'webshimstream:stream'+id;
  
  		streamCb[src] = {
  			src: src,
  			success: successCb,
  			fail: failCb
  		};
  
  		addMediaAPI();
  		/*
  		 { width: 650 },
  		 { width: { min: 650 }},
  		 { frameRate: 60 },
  		 { width: { max: 800 }},
  		 { facingMode: "user" }
  		 */
  		mediaelement.createSWF(media, {srcProp: src, streamrequest: true, type: 'jarisplayer/stream'});
  	}
  
  
  
  	function getMediaCandidate(){
  		var $media = $('video');
  		$media = $media.filter('.ws-usermedia');
  		if(!$media.length){
  			$media = $media.end();
  		}
  		if($media.length != 1){
  			$media = $media.filter(noSource);
  		}
  		if($media.length != 1){
  			webshim.error('for getUserMedia an empty video element has to be already in the DOM. If you provide multiple empty videos. please mark your suggested video using the "ws-usermedia" class.');
  		}
  		return $media[0];
  	}
  
  
  	function LocalMediaStream(data, api, id){
  		data._cTNow = Date.now();
  		data._cTID = false;
  		webshim.defineProperties(this, {
  			_swf: {
  				value: api,
  				enumerable: false
  			},
  			_data: {
  				value: data,
  				enumerable: false
  			},
  			_wsStreamId: {
  				value: id,
  				enumerable: false
  			}
  		});
  	}
  
  
  
  	LocalMediaStream.prototype = {
  		stop: function(){
  			if(this._data._cTID){
  				clearInterval(this._data._cTID);
  			}
  			mediaelement.queueSwfMethod(this._data._elem, 'api_detach', [], this._data);
  			this._data.ended = true;
  			$(this._data._elem).trigger('ended');
  		}
  	};
  
  
  	webshim.usermedia = {
  		attach: function(elem, canPlaySrc, data){
  			var $media;
  			if(data._usermedia == canPlaySrc.srcProp){
  				mediaelement.queueSwfMethod(data._elem, 'api_attach', [], data);
  				$media = $(data._elem).trigger('loadstart');
  				data._cTID = setInterval(function(){
  					if(data.ended){
  						clearInterval(data._cTID);
  					} else if(!data.paused){
  						data.currentTime = (Date.now() - data._cTNow) / 1000;
  						$media.triggerHandler('timeupdate');
  					}
  				}, 250);
  				if(!data.paused){
  					mediaelement.queueSwfMethod(data._elem, 'api_play', [], data);
  				}
  			} else {
  				webshim.error('something went wrong');
  			}
  		},
  		request: function(elem, canPlaySrc, data){
  			data._usermedia = canPlaySrc.srcProp;
  			if(!options.inline && !$(elem).hasClass('ws-inlineusermedia')){
  				$(data.api).css({position: 'fixed', top: 0, left: 0, width: '100%', height: 150, zIndex: '999999'});
  			} else {
  				$(data.api).css({position: 'relative', zIndex: '999999'});
  			}
  		}
  	};
  
  
  
  	addMediaAPI = function(){
  		if(!webshim.mediaelement.createSWF){return;}
  		addMediaAPI = $.noop;
  		var revert = function(data){
  			setTimeout(function(){
  				$(data.api).css({position: '', top: '', left: '', width: '', height: '', zIndex: ''});
  				if($.prop(data._elem, 'controls')){
  					$.prop(data._elem, 'controls', true);
  				}
  			}, 50);
  		};
  		var fail = function(jaris, data){
  			revert(data);
  			streamCb[data._usermedia].fail({name: jaris.type});
  		};
  		$.extend(mediaelement.onEvent, {
  			NotSupportedError: fail,
  			PermissionDeniedError: fail,
  			//not implemented yet
  			ConstraintNotSatisfiedError: fail,
  			onUserSuccess: function(jaris, data){
  				revert(data);
  				streamCb[data._usermedia].success(new LocalMediaStream(data, data.api, data._usermedia));
  			}
  		});
  	};
  	webshim.ready('mediaelement-jaris', addMediaAPI);
  
  	webshim.getUserMedia = wsGetUserMedia;
  	navigator.wsGetUserMedia = wsGetUserMedia;
  	webshim.isReady('usermedia-api', true);
  });