summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/TranscodeStatusTable.php
blob: 2220fc72bd7a64fb9b3081cfdef5adf3d409c696 (plain)
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
<?php
/**
 * TranscodeStatusTable outputs a "transcode" status table to the ImagePage
 *
 * If logged in as autoconfirmed users can reset transcode states
 * via the transcode api entry point
 *
 */
class TranscodeStatusTable {
	/**
	 * @param $file File
	 * @return string
	 */
	public static function getHTML( $file ){
		global $wgUser, $wgOut;

		// Add transcode table css and javascript:
		$wgOut->addModules( array( 'ext.tmh.transcodetable' ) );

		$o = '<h2 id="transcodestatus">' . wfMessage( 'timedmedia-status-header' )->escaped() . '</h2>';
		// Give the user a purge page link
		$o.= Linker::link(
			$file->getTitle(),
			wfMessage('timedmedia-update-status')->escaped(),
			array(),
			array( 'action'=> 'purge' )
		);

		$o.= Xml::openElement( 'table', array( 'class' => 'wikitable mw-filepage-transcodestatus' ) ) . "\n"
			. '<tr>'
			. '<th>' . wfMessage( 'timedmedia-transcodeinfo' )->escaped() . '</th>'
			. '<th>' . wfMessage( 'timedmedia-transcodebitrate' )->escaped() . '</th>'
			. '<th>' . wfMessage( 'timedmedia-direct-link' )->escaped() . '</th>';

		if( $wgUser->isAllowed( 'transcode-reset' ) ) {
			$o.= '<th>' . wfMessage( 'timedmedia-actions' )->escaped() . '</th>';
		}

		$o.= '<th>' . wfMessage( 'timedmedia-status' )->escaped() . '</th>';
		$o.= '<th>' . wfMessage( 'timedmedia-transcodeduration' )->escaped() . '</th>';
		$o.= "</tr>\n";

		$o.= self::getTranscodeRows( $file );

		$o.=  Xml::closeElement( 'table' );
		return $o;
	}

	/**
	 * Get the video or audio codec for the defined transcode,
	 * for grouping/sorting purposes.
	 */
	public static function codecFromTranscodeKey( $key ) {
		if ( isset( WebVideoTranscode::$derivativeSettings[$key] ) ) {
			$settings = WebVideoTranscode::$derivativeSettings[$key];
			if ( isset( $settings['videoCodec'] ) ) {
				return $settings['videoCodec'];
			} else if ( isset( $settings['audioCodec'] ) ) {
				return $settings['audioCodec'];
			} else {
				// this this shouldn't happen...
				// fall through
			}
		} else {
			// derivative type no longer defined or invalid def?
			// fall through
		}
		return $key;
	}

	/**
	 * @param $file File
	 * @return string
	 */
	public static function getTranscodeRows( $file ){
		global $wgUser, $wgLang;
		$o='';

		$transcodeRows = WebVideoTranscode::getTranscodeState( $file );

		uksort( $transcodeRows, function( $a, $b ) {
			$formatOrder = array( 'vp9', 'vp8', 'h264', 'theora', 'opus', 'vorbis', 'aac' );

			$aFormat = self::codecFromTranscodeKey( $a );
			$bFormat = self::codecFromTranscodeKey( $b );
			$aIndex = array_search( $aFormat, $formatOrder );
			$bIndex = array_search( $bFormat, $formatOrder );

			if ( $aIndex === false && $bIndex === false ) {
				return -strnatcmp( $a, $b );
			} else if ( $aIndex === false ) {
				return 1;
			} else if ( $bIndex === false ) {
				return -1;
			} else if ( $aIndex === $bIndex ) {
				return -strnatcmp( $a, $b );
			} else {
				return ( $aIndex - $bIndex );
			}
		} );

		foreach( $transcodeRows as $transcodeKey => $state ){
			$o.='<tr>';
			// Encode info:
			$o.='<td>' . wfMessage('timedmedia-derivative-' . $transcodeKey )->escaped() . '</td>';
			$o.='<td>' . self::getTranscodeBitrate( $file, $state ) . '</td>';

			// Download file
			$o.='<td>';
			$o.= ( !is_null( $state['time_success'] ) ) ?
				'<a href="'.self::getSourceUrl( $file, $transcodeKey ) .'" title="'.wfMessage
				('timedmedia-download' )->escaped() .'"><div class="download-btn"><span>' .
				wfMessage('timedmedia-download' )->escaped(). '</span></div></a></td>' :
				wfMessage('timedmedia-not-ready' )->escaped();
			$o.='</td>';

			// Check if we should include actions:
			if( $wgUser->isAllowed( 'transcode-reset' ) ){
				// include reset transcode action buttons
				$o.='<td class="mw-filepage-transcodereset"><a href="#" data-transcodekey="' .
					htmlspecialchars( $transcodeKey ). '">' . wfMessage('timedmedia-reset')->escaped() .
					'</a></td>';
			}

			// Status:
			$o.='<td>' . self::getStatusMsg( $file, $state ) . '</td>';
			$o.='<td>' . self::getTranscodeDuration( $file, $state ) . '</td>';

			$o.='</tr>';
		}
		return $o;
	}

	/**
	 * @param $file File
	 * @param $transcodeKey string
	 * @return string
	 */
	public static function getSourceUrl( $file, $transcodeKey ){
		return WebVideoTranscode::getTranscodedUrlForFile( $file, $transcodeKey );
	}

	/**
	 * @param $file File
	 * @param $state
	 * @return string
	 */
	public static function getTranscodeDuration( $file, $state ) {
		global $wgLang;
		if( !is_null( $state['time_success'] ) ) {
			$startTime = wfTimestamp( TS_UNIX, $state['time_startwork'] );
			$endTime = wfTimestamp( TS_UNIX, $state['time_success'] );
			$delta = $endTime - $startTime;
			$duration = $wgLang->formatTimePeriod( $delta );
			return $duration;
		} else {
			return '';
		}
	}

	/**
	 * @param $file File
	 * @param $state
	 * @return string
	 */
	public static function getTranscodeBitrate( $file, $state ) {
		global $wgLang;
		if( !is_null( $state['time_success'] ) ) {
			return $wgLang->formatBitrate( $state['final_bitrate'] );
		} else {
			return '';
		}
	}

	/**
	 * @param $file File
	 * @param $state
	 * @return string
	 */
	public static function getStatusMsg( $file, $state ){
		global $wgContLang;
		// Check for success:
		if( !is_null( $state['time_success'] ) ) {
			return wfMessage( 'timedmedia-completed-on',
				$wgContLang->timeAndDate( $state[ 'time_success' ] ) )->escaped();
		}
		// Check for error:
		if( !is_null( $state['time_error'] ) ){
			$attribs = array();
			if( !is_null( $state['error'] ) ){
				$attribs = array(
					'class' => 'mw-tmh-pseudo-error-link',
					'data-error' => $state['error'],
				);
			}

			return Html::rawElement( 'span', $attribs,
				wfMessage( 'timedmedia-error-on',
					$wgContLang->timeAndDate( $state['time_error'] ) )->escaped()
			);
		}

		//$db = wfGetDB( DB_SLAVE );
		// Check for started encoding
		if( !is_null( $state['time_startwork'] ) ){
			$timePassed = time() - wfTimestamp ( TS_UNIX, $state['time_startwork'] );
			// Get the rough estimate of time done: ( this is not very costly considering everything else
			// that happens in an action=purge video page request )
			/*$filePath = WebVideoTranscode::getTargetEncodePath( $file, $state['key'] );
			if( is_file( $filePath ) ){
				$targetSize = WebVideoTranscode::getProjectedFileSize( $file, $state['key'] );
				if( $targetSize === false ){
					$doneMsg = wfMessage( 'timedmedia-unknown-target-size', $wgLang->formatSize( filesize( $filePath ) ) )->escaped();
				} else {
					$doneMsg = wfMessage('timedmedia-percent-done', round( filesize( $filePath ) / $targetSize, 2 ) )->escaped();
				}
			}	*/
			// Predicting percent done is not working well right now ( disabled for now )
			$doneMsg = '';
			return wfMessage(
				'timedmedia-started-transcode',
				TimedMediaHandler::getTimePassedMsg( $timePassed ), $doneMsg
			)->escaped();
		}
		// Check for job added ( but not started encoding )
		if( !is_null( $state['time_addjob'] ) ){
			$timePassed = time() - wfTimestamp ( TS_UNIX, $state['time_addjob'] );
			return wfMessage(
				'timedmedia-in-job-queue',
				TimedMediaHandler::getTimePassedMsg( $timePassed )
			)->escaped();
		}
		// Return unknown status error:
		return wfMessage('timedmedia-status-unknown')->escaped();
	}
}