|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var React = require('react'); |
| 20 | +var filesize = require('filesize'); |
| 21 | + |
| 22 | +var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); |
| 23 | +var Modal = require('matrix-react-sdk/lib/Modal'); |
| 24 | +var sdk = require('matrix-react-sdk') |
| 25 | + |
| 26 | +module.exports = React.createClass({ |
| 27 | + displayName: 'MVideoTile', |
| 28 | + |
| 29 | + thumbScale: function(fullWidth, fullHeight, thumbWidth, thumbHeight) { |
| 30 | + if (!fullWidth || !fullHeight) { |
| 31 | + // Cannot calculate thumbnail height for image: missing w/h in metadata. We can't even |
| 32 | + // log this because it's spammy |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + if (fullWidth < thumbWidth && fullHeight < thumbHeight) { |
| 36 | + // no scaling needs to be applied |
| 37 | + return fullHeight; |
| 38 | + } |
| 39 | + var widthMulti = thumbWidth / fullWidth; |
| 40 | + var heightMulti = thumbHeight / fullHeight; |
| 41 | + if (widthMulti < heightMulti) { |
| 42 | + // width is the dominant dimension so scaling will be fixed on that |
| 43 | + return widthMulti; |
| 44 | + } |
| 45 | + else { |
| 46 | + // height is the dominant dimension so scaling will be fixed on that |
| 47 | + return heightMulti; |
| 48 | + } |
| 49 | + }, |
| 50 | + |
| 51 | + render: function() { |
| 52 | + var content = this.props.mxEvent.getContent(); |
| 53 | + var cli = MatrixClientPeg.get(); |
| 54 | + |
| 55 | + var videoStyle = { |
| 56 | + maxHeight: "500px", |
| 57 | + maxWidth: "100%", |
| 58 | + }; |
| 59 | + |
| 60 | + var height = null; |
| 61 | + var width = null; |
| 62 | + var poster = null; |
| 63 | + var preload = "metadata"; |
| 64 | + if (content.info) { |
| 65 | + var scale = this.thumbScale(content.info.w, content.info.h, 480, 360); |
| 66 | + if (scale) { |
| 67 | + width = Math.floor(content.info.w * scale); |
| 68 | + height = Math.floor(content.info.h * scale); |
| 69 | + } |
| 70 | + |
| 71 | + if (content.info.thumbnail_url) { |
| 72 | + poster = cli.mxcUrlToHttp(content.info.thumbnail_url); |
| 73 | + preload = "none"; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + return ( |
| 80 | + <span className="mx_MVideoTile"> |
| 81 | + <video className="mx_MVideoTile" src={cli.mxcUrlToHttp(content.url)} alt={content.body} |
| 82 | + controls preload={preload} autoplay="false" loop |
| 83 | + height={height} width={width} poster={poster}> |
| 84 | + </video> |
| 85 | + </span> |
| 86 | + ); |
| 87 | + }, |
| 88 | +}); |
0 commit comments