-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
VideoPlayer.vue
134 lines (125 loc) · 3.25 KB
/
VideoPlayer.vue
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
<template>
<v-container fill-height fluid class="pa-0">
<div ref="videoContainer">
<video ref="videoPlayer" :poster="poster" autoplay></video>
</div>
</v-container>
</template>
<script lang="ts">
import Vue from 'vue';
import { stringify } from 'qs';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import shaka from 'shaka-player/dist/shaka-player.ui';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import muxjs from 'mux.js';
import 'shaka-player/dist/controls.css';
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
muxjs: any;
}
}
export default Vue.extend({
props: {
item: {
type: Object,
required: true
},
poster: {
type: String,
default: ''
}
},
data() {
return {
source: '',
player: null as any,
ui: null as any
};
},
watch: {
async source(newSource) {
if (this.player) {
try {
await this.player.load(newSource);
} catch (e) {
console.error('Error code', e.code, 'object', e);
}
}
}
},
async mounted() {
try {
const response = await this.$api.mediaInfo.getPostedPlaybackInfo({
itemId: this.$route.params.itemId,
userId: this.$auth.user.Id,
deviceProfileDto: {
DeviceProfile: this.$playbackProfile
}
});
let mediaSource;
if (response?.data?.MediaSources) {
mediaSource = response.data.MediaSources[0];
} else {
throw new Error("This item can't be played.");
}
if (mediaSource.SupportsDirectStream) {
const directOptions: Record<string, any> = {
Static: true,
mediaSourceId: mediaSource.Id,
deviceId: this.$store.state.deviceProfile.deviceId,
api_key: this.$store.state.user.accessToken
};
if (mediaSource.ETag) {
directOptions.Tag = mediaSource.ETag;
}
if (mediaSource.LiveStreamId) {
directOptions.LiveStreamId = mediaSource.LiveStreamId;
}
const params = stringify(directOptions);
this.source = `${this.$axios.defaults.baseURL}/Videos/${mediaSource.Id}/stream.${mediaSource.Container}?${params}`;
} else if (
mediaSource.SupportsTranscoding &&
mediaSource.TranscodingUrl
) {
this.source = this.$axios.defaults.baseURL + mediaSource.TranscodingUrl;
}
window.muxjs = muxjs;
shaka.polyfill.installAll();
if (shaka.Player.isBrowserSupported()) {
// Everything looks good!
this.player = new shaka.Player(this.$refs.videoPlayer);
this.ui = new shaka.ui.Overlay(
this.player,
this.$refs.videoContainer,
this.$refs.videoPlayer
);
} else {
this.$nuxt.error({
message: this.$t('browserNotSupported') as string
});
}
} catch (error) {
this.$nuxt.error({
statusCode: 404,
message: error
});
}
},
beforeDestroy() {
if (this.player) {
window.muxjs = undefined;
this.player.unload();
this.player.destroy();
}
}
});
</script>
<style scoped>
video {
width: 100vw;
height: 100vh;
}
</style>