-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfxos-camera.js
3381 lines (2908 loc) · 78.1 KB
/
fxos-camera.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.FXOSCamera = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/* globals define */
;(function(define){define(function(require,exports,module){
/**
* Locals
*/
var textContent = Object.getOwnPropertyDescriptor(Node.prototype,
'textContent');
var innerHTML = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
var removeAttribute = Element.prototype.removeAttribute;
var setAttribute = Element.prototype.setAttribute;
var noop = function() {};
var base = {
properties: {
GaiaComponent: true,
attributeChanged: noop,
attached: noop,
detached: noop,
created: noop,
createdCallback: function() {
if (this.dirObserver) { addDirObserver(); }
injectLightCss(this);
this.created();
},
/**
* It is very common to want to keep object
* properties in-sync with attributes,
* for example:
*
* el.value = 'foo';
* el.setAttribute('value', 'foo');
*
* So we support an object on the prototype
* named 'attrs' to provide a consistent
* way for component authors to define
* these properties. When an attribute
* changes we keep the attr[name]
* up-to-date.
*
* @param {String} name
* @param {String||null} from
* @param {String||null} to
*/
attributeChangedCallback: function(name, from, to) {
var prop = toCamelCase(name);
if (this._attrs && this._attrs[prop]) { this[prop] = to; }
this.attributeChanged(name, from, to);
},
attachedCallback: function() {
if (this.dirObserver) {
this.setInnerDirAttributes = setInnerDirAttributes.bind(null, this);
document.addEventListener('dirchanged', this.setInnerDirAttributes);
}
this.attached();
},
detachedCallback: function() {
if (this.dirObserver) {
document.removeEventListener('dirchanged', this.setInnerDirAttributes);
}
if (document.l10n && this.onDOMRetranslated) {
document.l10n.ready.then(() => document.removeEventListener(
'DOMRetranslated', this.onDOMRetranslated));
}
this.detached();
},
/**
* A convenient method for setting up
* a shadow-root using the defined template.
*
* @return {ShadowRoot}
*/
setupShadowRoot: function() {
if (!this.template) { return; }
var node = document.importNode(this.template.content, true);
this.createShadowRoot().appendChild(node);
if (this.dirObserver) { setInnerDirAttributes(this); }
return this.shadowRoot;
},
/**
* A convenient method for triggering l10n for component's shadow DOM.
*/
setupShadowL10n: function() {
if (!document.l10n) { return this.localizeShadow(this.shadowRoot); }
this.onDOMRetranslated = this.localizeShadow.bind(null, this.shadowRoot);
document.l10n.ready.then(() => {
document.addEventListener('DOMRetranslated', this.onDOMRetranslated);
this.localizeShadow(this.shadowRoot);
});
},
/**
* Localizes the shadowRoot subtree.
*/
localizeShadow: function(shadowRoot) {
if (!document.l10n) { return; }
document.l10n.translateFragment(shadowRoot);
},
/**
* Sets an attribute internally
* and externally. This is so that
* we can style internal shadow-dom
* content.
*
* @param {String} name
* @param {String} value
*/
setAttr: function(name, value) {
var internal = this.shadowRoot.firstElementChild;
setAttribute.call(internal, name, value);
setAttribute.call(this, name, value);
},
/**
* Removes an attribute internally
* and externally. This is so that
* we can style internal shadow-dom
* content.
*
* @param {String} name
* @param {String} value
*/
removeAttr: function(name) {
var internal = this.shadowRoot.firstElementChild;
removeAttribute.call(internal, name);
removeAttribute.call(this, name);
}
},
descriptors: {
textContent: {
set: function(value) {
textContent.set.call(this, value);
if (this.lightStyle) { this.appendChild(this.lightStyle); }
},
get: function() {
return textContent.get();
}
},
innerHTML: {
set: function(value) {
innerHTML.set.call(this, value);
if (this.lightStyle) { this.appendChild(this.lightStyle); }
},
get: innerHTML.get
}
}
};
/**
* Register a new component.
*
* @param {String} name
* @param {Object} props
* @return {constructor}
* @public
*/
exports.register = function(name, props) {
var baseProto = getBaseProto(props.extends);
var template = props.template || baseProto.templateString;
// Components are extensible by default but can be declared
// as non extensible as an optimization to avoid
// storing the template strings
var extensible = props.extensible = props.hasOwnProperty('extensible')?
props.extensible : true;
// Clean up
delete props.extends;
// Pull out CSS that needs to be in the light-dom
if (template) {
// Stores the string to be reprocessed when
// a new component extends this one
if (extensible && props.template) {
props.templateString = props.template;
}
var output = processCss(template, name);
props.template = document.createElement('template');
props.template.innerHTML = output.template;
props.lightCss = output.lightCss;
props.globalCss = props.globalCss || '';
props.globalCss += output.globalCss;
}
// Inject global CSS into the document,
// and delete as no longer needed
injectGlobalCss(props.globalCss);
delete props.globalCss;
// Merge base getter/setter attributes with the user's,
// then define the property descriptors on the prototype.
var descriptors = mixin(props.attrs || {}, base.descriptors);
// Store the orginal descriptors somewhere
// a little more private and delete the original
props._attrs = props.attrs;
delete props.attrs;
// Create the prototype, extended from base and
// define the descriptors directly on the prototype
var proto = createProto(baseProto, props);
Object.defineProperties(proto, descriptors);
// Register the custom-element and return the constructor
try {
return document.registerElement(name, { prototype: proto });
} catch (e) {
if (e.name !== 'NotSupportedError') {
throw e;
}
}
};
/**
* The default base prototype to use
* when `extends` is undefined.
*
* @type {Object}
*/
var defaultPrototype = createProto(HTMLElement.prototype, base.properties);
/**
* Returns a suitable prototype based
* on the object passed.
*
* @private
* @param {HTMLElementPrototype|undefined} proto
* @return {HTMLElementPrototype}
*/
function getBaseProto(proto) {
if (!proto) { return defaultPrototype; }
proto = proto.prototype || proto;
return !proto.GaiaComponent ?
createProto(proto, base.properties) : proto;
}
/**
* Extends the given proto and mixes
* in the given properties.
*
* @private
* @param {Object} proto
* @param {Object} props
* @return {Object}
*/
function createProto(proto, props) {
return mixin(Object.create(proto), props);
}
/**
* Detects presence of shadow-dom
* CSS selectors.
*
* @private
* @return {Boolean}
*/
var hasShadowCSS = (function() {
var div = document.createElement('div');
try { div.querySelector(':host'); return true; }
catch (e) { return false; }
})();
/**
* Regexs used to extract shadow-css
*
* @type {Object}
*/
var regex = {
shadowCss: /(?:\:host|\:\:content)[^{]*\{[^}]*\}/g,
':host': /(?:\:host)/g,
':host()': /\:host\((.+)\)(?: \:\:content)?/g,
':host-context': /\:host-context\((.+)\)([^{,]+)?/g,
'::content': /(?:\:\:content)/g
};
/**
* Extracts the :host and ::content rules
* from the shadow-dom CSS and rewrites
* them to work from the <style scoped>
* injected at the root of the component.
*
* @private
* @return {String}
*/
function processCss(template, name) {
var globalCss = '';
var lightCss = '';
if (!hasShadowCSS) {
template = template.replace(regex.shadowCss, function(match) {
var hostContext = regex[':host-context'].exec(match);
if (hostContext) {
globalCss += match
.replace(regex['::content'], '')
.replace(regex[':host-context'], '$1 ' + name + '$2')
.replace(/ +/g, ' '); // excess whitespace
} else {
lightCss += match
.replace(regex[':host()'], name + '$1')
.replace(regex[':host'], name)
.replace(regex['::content'], name);
}
return '';
});
}
return {
template: template,
lightCss: lightCss,
globalCss: globalCss
};
}
/**
* Some CSS rules, such as @keyframes
* and @font-face don't work inside
* scoped or shadow <style>. So we
* have to put them into 'global'
* <style> in the head of the
* document.
*
* @private
* @param {String} css
*/
function injectGlobalCss(css) {
if (!css) {return;}
var style = document.createElement('style');
style.innerHTML = css.trim();
headReady().then(function() {
document.head.appendChild(style);
});
}
/**
* Resolves a promise once document.head is ready.
*
* @private
*/
function headReady() {
return new Promise(function(resolve) {
if (document.head) { return resolve(); }
window.addEventListener('load', function fn() {
window.removeEventListener('load', fn);
resolve();
});
});
}
/**
* The Gecko platform doesn't yet have
* `::content` or `:host`, selectors,
* without these we are unable to style
* user-content in the light-dom from
* within our shadow-dom style-sheet.
*
* To workaround this, we clone the <style>
* node into the root of the component,
* so our selectors are able to target
* light-dom content.
*
* @private
*/
function injectLightCss(el) {
if (hasShadowCSS) { return; }
var stylesheet = el.querySelector('style');
if (!stylesheet) {
stylesheet = document.createElement('style');
stylesheet.setAttribute('scoped', '');
stylesheet.appendChild(document.createTextNode(el.lightCss));
el.appendChild(stylesheet);
}
el.lightStyle = stylesheet;
}
/**
* Convert hyphen separated
* string to camel-case.
*
* Example:
*
* toCamelCase('foo-bar'); //=> 'fooBar'
*
* @private
* @param {String} string
* @return {String}
*/
function toCamelCase(string) {
return string.replace(/-(.)/g, function replacer(string, p1) {
return p1.toUpperCase();
});
}
/**
* Observer (singleton)
*
* @type {MutationObserver|undefined}
*/
var dirObserver;
/**
* Workaround for bug 1100912: applies a `dir` attribute to all shadowRoot
* children so that :-moz-dir() selectors work on shadow DOM elements.
*
* In order to keep decent performances, the `dir` is the component dir if
* defined, or the document dir otherwise. This won't work if the component's
* direction is defined by CSS or inherited from a parent container.
*
* This method should be removed when bug 1100912 is fixed.
*
* @private
* @param {WebComponent}
*/
function setInnerDirAttributes(component) {
var dir = component.dir || document.dir;
Array.from(component.shadowRoot.children).forEach(element => {
if (element.nodeName !== 'STYLE') {
element.dir = dir;
}
});
}
/**
* Observes the document `dir` (direction) attribute and when it changes:
* - dispatches a global `dirchanged` event;
* - forces the `dir` attribute of all shadowRoot children.
*
* Components can listen to this event and make internal changes if needed.
*
* @private
*/
function addDirObserver() {
if (dirObserver) { return; }
dirObserver = new MutationObserver(onChanged);
dirObserver.observe(document.documentElement, {
attributeFilter: ['dir'],
attributes: true
});
function onChanged(mutations) {
document.dispatchEvent(new Event('dirchanged'));
}
}
/**
* Copy the values of all properties from
* source object `target` to a target object `source`.
* It will return the target object.
*
* @private
* @param {Object} target
* @param {Object} source
* @returns {Object}
*/
function mixin(target, source) {
for (var key in source) {
target[key] = source[key];
}
return target;
}
});})(typeof define=='function'&&define.amd?define
:(function(n,w){'use strict';return typeof module=='object'?function(c){
c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){
return w[n];},m.exports,m);w[n]=m.exports;};})('gaia-component',this));
},{}],2:[function(require,module,exports){
'use strict';
/**
* Dependencies
*/
var Viewfinder = require('./lib/viewfinder');
var MozCamera = require('./lib/moz-camera');
var component = require('gaia-component');
/**
* Mini logger.
*
* @type {Funciton}
*/
var debug = 0 ? (...args) => console.log('[FXOSCamera]', ...args) : () => {};
/**
* Private internal key.
*
* @type {Symbol}
*/
var internal = 0 ? 'internal' : Symbol();
/**
* Shorthand
*
* @type {Function}
*/
var on = (el, name, fn) => el.addEventListener(name, fn);
var off = (el, name, fn) => el.removeEventListener(name, fn);
/**
* Public class.
*
* @type {Object}
*/
var FXOSCameraPrototype = {
extensible: false,
created() {
debug('created');
this.setupShadowRoot();
this[internal] = new Internal(this);
},
attached() {
debug('attached');
this[internal].attached();
},
detached() {
debug('detached');
this[internal].detached();
},
start() {
return this[internal].start();
},
stop() {
return this[internal].stop();
},
get(value) {
return this[internal].get(value);
},
set(key, value) {
return this[internal].set(key, value);
},
takePicture(filePath, options) {
return this[internal].takePicture(filePath, options);
},
startRecording(filePath, options) {
return this[internal].startRecording(filePath, options);
},
stopRecording() {
return this[internal].stopRecording();
},
focus(point) {
return this[internal].focus(point);
},
attrs: {
maxFileSize: {
set(value) { this[internal].setMaxFileSize(value); }
},
flush: {
get() { return !!this[internal].flush; },
set(value) {
value = !!value || value === '';
if (this.flush === value) return;
if (value) this.el.setAttribute('flush', '');
else this.el.removeAttribute('flush');
this[internal].flush = value;
},
},
started: { get() { return this[internal].started.promise; }},
stopped: { get() { return this[internal].stopped.promise; }}
},
template: `<div class="inner">
<div class="frame">
<div class="wrapper">
<video></video>
<content></content>
</div>
</div>
</div>
<style>
:host {
position: relative;
display: block;
width: 100%;
height: 100%;
}
.inner {
position: absolute;
top: 0;
left: 0;
display: flex;
width: 100%;
height: 100%;
justify-content: center;
overflow: hidden;
}
/**
* 1. Should never overflow the viewport.
*/
.frame {
display: flex;
position: relative;
max-width: 100%; /* 1 */
max-height: 100%; /* 1 */
justify-content: center;
align-items: center;
}
.wrapper {
flex-shrink: 0;
}
/**
* .shutter
*/
.wrapper.shutter {
animation: 400ms shutter-animation;
}
video {
width: 100%;
height: 100%;
outline: none;
}
</style>`,
globalCss: `
@keyframes shutter-animation {
0% { opacity: 1; }
1% { opacity: 0.25; }
100% { opacity: 1 }
}`
};
/**
* Private class.
*
* @constructor
*/
function Internal(el) {
var shadow = el.shadowRoot;
this.el = el;
this.els = {
inner: shadow.querySelector('.inner'),
frame: shadow.querySelector('.frame'),
wrapper: shadow.querySelector('.wrapper'),
video: shadow.querySelector('video')
};
this.viewfinder = new Viewfinder(this);
this.started = new Deferred();
this.pending = {};
// defaults
this.mode = 'picture';
this.type = 'back';
// bind helps removeEventListener()
this.onVisibilityChange = this.onVisibilityChange.bind(this);
this.onFocusChanged = this.onFocusChanged.bind(this);
this.onFacesChanged = this.onFacesChanged.bind(this);
this.onShutter = this.onShutter.bind(this);
}
Internal.prototype = {
/**
* As soon as the component is
* attached to the DOM we start
* up the camera.
*
* @private
*/
attached() {
this.start();
on(document, 'visibilitychange', this.onVisibilityChange);
},
/**
* As soon as the component is
* detached from the DOM we stop
* the camera.
*
* @private
*/
detached() {
this.stop();
off(document, 'visibilitychange', this.onVisibilityChange);
},
/**
* Start the camera.
*
* The viewfinder will be displayed
* and the camera will be setup ready
* to capture.
*
* @return {Promise}
*/
start() {
if (this._started) return this._started;
debug('starting ...');
delete this._stopped;
this.stopped = new Deferred();
return this._started = this.viewfinder.hide({ instant: true })
.then(() => this.load())
.then(() => this.viewfinder.show())
.then(() => {
debug('started');
this.started.resolve();
})
.catch(this.started.reject);
},
/**
* Stop the camera.
*
* The viewfinder will stop streaming
* and the camera will be released.
*
* @return {Promise}
*/
stop() {
if (this._stopped) return this._stopped;
debug('stopping ...');
delete this._started;
this.started = new Deferred();
return this._stopped = this.release()
.then(() => {
debug('torndown');
delete this._loaded;
})
.then(this.stopped.resolve)
.catch(err => {
this.stopped.reject(err);
throw err;
});
},
/**
* Resolves when the camera has
* fully started and any hardware
* aquisition is complete.
*
* @return {Promise}
*/
loaded() {
return Promise.all([
this.started.promise,
this._loaded
]);
},
/**
* Load the currently chosen camera type.
*
* 1. Acquired hardware is released
* 2. Viewfinder updated to match the latest preview-size
* 3. Camera set to stream into <video>
*
* We before we resolve the original Promise
* we check to make sure that the Camera
* type wasn't changed whilst the operation
* was in progress, if it has: repeat.
*
* @return {Promise}
*/
load() {
debug('load');
if (this.loading) return this.loading;
// don't reload unnecessarily
if (this.camera && this.camera.type === this.type) {
return Promise.resolve(this.camera);
}
var loaded = this.release()
.then(() => {
debug('loading ...');
// TODO: We could pass an emitter
// instead of callbacks here ...?
this.camera = new MozCamera({
type: this.type,
mode: this.mode,
onFocusChanged: this.onFocusChanged,
onFacesChanged: this.onFacesChanged,
onError: e => this.onError(e),
onShutter: this.onShutter
});
return this.camera._ready;
})
.then(() => {
debug('loaded', this.camera.type, this.type);
delete this.loading;
// If the camera was changed since the call
// to loadCamera() we need to .setCamera()
// again to get the hardware in sync.
if (this.type !== this.camera.type) {
debug('type changed during load');
return this.load();
}
this.viewfinder.update(this.camera);
return this.camera.streamInto(this.viewfinder.els.video);
});
return this._loaded = this.loading = loaded;
},
/**
* Set the camera 'type'.
*
* @param {String} type ['front'|'back']
*/
setCamera(camera) {
debug('set camera', camera);
if (!this.knownType(camera)) return Promise.reject(error(4, camera));
this.type = camera;
this.viewfinder.hide();
return this.load()
.then(() => this.viewfinder.show());
},
/**
* Test if given type is valid.
*
* @param {String} type
* @return {Boolean}
*/
knownType(type) {
return !!~this.getCameras().indexOf(type);
},
/**
* Get list of available camera 'types'.
*
* @return {Array}
*/
getCameras() {
return navigator.mozCameras.getListOfCameras();
},
/**
* Test if given camera mode is known.
*
* @param {String} type
* @return {Boolean}
*/
knownMode(type) {
return !!{
'video': 1,
'picture': 1
}[type];
},
/**
* Set the mode.
*
* @param {String} value
* @param {Object} [options]
* @param {Boolean} [options.hide]
*/
setMode(value, options={}) {
debug('set mode', value);
if (!this.knownMode(value)) return Promise.reject(error(3, value));
var hide = options.hide !== false;
this.mode = value;
return this.loaded()
.then(() => hide && this.viewfinder.hide())
.then(() => {
debug('setting mode', this.mode);
return this.camera.configure({ mode: this.mode });
})
// If the camera was 'destroyed' before
// we could configure, we can try again.
// Any other error gets thrown back up
// the promise chain for the user to catch.
.catch(err => {
if (err.message !== 'destroyed') throw err;
return this.setMode(this.mode, { hide: false });
})
.then(() => this.viewfinder.update(this.camera))
.then(() => hide && this.viewfinder.show());
},
/**
* Set the maximum file size the
* Camera should record up to.
*
* When in video mode the camera hardware
* will automatically stop recording
* if/when this size is reached.
*
* In picture mode, the picture will
* no be taken if there is not enough
* space.
*
* @param {[type]} value [description]
*/
setMaxFileSize(value) {
this.maxFileSize = value;
return this.loaded()
.then(() => this.camera.setMaxFileSize(value));
},
/**
* Take a picture.
*
* Throws if called when picture
* taking is in progress.
*
* @param {String} filePath
* @param {Object} [options]
* @param {Object} [options.position]
* @param {Object} [options.rotation]
* @return {Promise<Picture>}
*/
takePicture(filePath, options) {
return this.loaded()
.then(() => {
if (typeof filePath !== 'string') throw error(1);
return this.camera.takePicture(filePath, options);
});
},
/**
* Starts recording.
*
* @param {String} filePath
* @param {Object} options
* @param {Object} [options.position]
* @param {Object} [options.rotation]
* @return {Promise}
*/
startRecording(filePath, options) {
return this.loaded()
.then(() => {
if (typeof filePath !== 'string') throw error(1);
return this.camera.startRecording(filePath, options);
});
},
/**
* Stops recording and returns a `Video`.
*
* @return {Promise<Video>}
*/
stopRecording() {
return this.loaded()
.then(() => this.camera.stopRecording());