Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebTorrent: Fit video/audio media to window size #3005

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions components/brave_webtorrent/extension/components/mediaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as React from 'react'

// Constants
import { TorrentObj } from '../constants/webtorrentState'
import { TorrentObj, File } from '../constants/webtorrentState'

const SUPPORTED_VIDEO_EXTENSIONS = ['m4v', 'mkv', 'mov', 'mp4', 'ogv', 'webm']

Expand All @@ -20,6 +20,26 @@ const getExtension = (filename: string) => {
return filename.substring(ix + 1)
}

const getSelectedFile = (torrent: TorrentObj, ix: number) => {
return torrent.files
? torrent.files[ix]
: null
}

const fileIsVideo = (file: File | null) => {
if (!file) return false
const fileExt = getExtension(file.name)
if (!fileExt) return false
return SUPPORTED_VIDEO_EXTENSIONS.includes(fileExt)
}

const fileIsAudio = (file: File | null) => {
if (!file) return false
const fileExt = getExtension(file.name)
if (!fileExt) return false
return SUPPORTED_AUDIO_EXTENSIONS.includes(fileExt)
}

interface Props {
torrent: TorrentObj
ix: number
Expand All @@ -31,22 +51,32 @@ export default class MediaViewer extends React.PureComponent<Props, {}> {
elem.play().catch(err => console.error('Autoplay failed', err))
}

componentWillReceiveProps () {
// Set page background to black when video or audio is being viewed
const { torrent, ix } = this.props
const file = getSelectedFile(torrent, ix)
const isMedia = fileIsAudio(file) || fileIsVideo(file)
if (isMedia) {
document.body.style.backgroundColor = 'rgb(0, 0, 0)'
} else {
document.body.style.backgroundColor = null
}
}

componentWillUnmount () {
document.body.style.backgroundColor = null
}

render () {
const { torrent, ix } = this.props

const file = torrent.files ? torrent.files[ix] : undefined
const file = getSelectedFile(torrent, ix)
const isAudio = fileIsAudio(file)
const isVideo = fileIsVideo(file)
const fileURL = torrent.serverURL && torrent.serverURL + '/' + ix

const fileExt = file && getExtension(file.name)
const isVideo = fileExt
? SUPPORTED_VIDEO_EXTENSIONS.includes(fileExt)
: false
const isAudio = fileExt
? SUPPORTED_AUDIO_EXTENSIONS.includes(fileExt)
: false

let content
if (!file || !torrent.serverURL) {
if (!file || !fileURL) {
content = <div className='loading'>Loading Media</div>
} else if (isVideo) {
content = (
Expand Down
14 changes: 13 additions & 1 deletion components/styles/webtorrent.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
body {
#root {
min-height: 100%;
padding: 100px;
font-family: Muli, sans-serif;
}
Expand Down Expand Up @@ -71,3 +72,14 @@ a {
height: 32px;
margin: 0 0 18px;
}

#video, #audio {
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
max-height: 100%;
max-width: 100%;
margin: auto;
}