r56380 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56379‎ | r56380 | r56381 >
Date:18:12, 15 September 2009
Author:dale
Status:deferred
Tags:
Comment:
some strict php fixes and error fixes
Modified paths:
  • /trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/FLV/Util/AMFSerialize.php (modified) (history)
  • /trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/MvFlv.php (modified) (history)
  • /trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/mvFlvServer.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/MvFlv.php
@@ -3,88 +3,91 @@
44 include_once 'FLV/FLV.php';
55
66 define('MAX_FLV_TS', 16777.216);//it appears tag times restart after 16777.216 seconds;
7 -define('KEY_FRAME_DISTANCE', 2); //max keyframe distance
 7+define('KEY_FRAME_DISTANCE', 2); //max keyframe distance
88
9 -//define('META_KFDATA_EXT', '.KfMeta'); //file extension for cached keframe metadata
 9+//define('META_KFDATA_EXT', '.KfMeta'); //file extension for cached keframe metadata
1010 define('META_DATA_EXT', '.meta'); //basic cached meta
1111
1212 class MyFLV extends FLV {
13 -
 13+
1414 /**
15 - * On audio-only files the frame index will use this as minimum gap
 15+ * On audio-only files the frame index will use this as minimum gap
1616 */
1717 private $audioFrameGap = 3;
18 -
 18+
1919 private $origMetaOfs = 0;
2020 private $origMetaSize = 0;
2121 private $origMetaData;
2222 private $compMetaData;
23 -
 23+
2424 //added segment vars:
2525 private $fullMeta=null;
2626 private $mDuration=null;
2727 var $wrapTimeCount=0;
28 -
 28+
2929 function getKeyFrameByteTimes( $start_time_sec, $end_time_sec=null ){
30 - //print "play $start_time_sec to $end_time_sec";
 30+ //print "play $start_time_sec to $end_time_sec";
3131 //@@todo cache byte offsets in memcache if available
3232 if( $end_time_sec)
3333 $this->mDuration = $end_time_sec - $start_time_sec;
34 - //print "SET metaDuration to: " . $this->metaDuration . "\n";
 34+ //print "SET metaDuration to: " . $this->metaDuration . "\n";
3535 //print_r($fullMeta);
36 - $meta =& $this->getKeyFrameMetaData();
 36+ $meta = $this->getKeyFrameMetaData();
3737 //die;
38 - $start_byte=$end_byte=null;
39 - if($start_time_sec==0 && $end_time_sec==null)$this->play();
 38+ $start_byte=$end_byte=null;
 39+
 40+ if($start_time_sec==0 && $end_time_sec==null)
 41+ $this->play();
 42+
4043 $start_time_ms = $start_time_sec*1000;
41 - $end_time_ms = ($end_time_sec==null)?null:$end_time_sec*1000;
42 -
43 - for($i=0; $i < count($meta['times']); $i++){
 44+ $end_time_ms = ($end_time_sec==null)?null:$end_time_sec*1000;
 45+
 46+ for($i=0; $i < count($meta['times']); $i++){
4447 //set to the keyframe right before a keyframe of the requested start time
4548 if($meta['times'][$i] >= $start_time_ms && $start_byte==null){
4649 $start_byte=(isset($meta['times'][$i-1]))?$meta['filepositions'][$i-1]:$meta['filepositions'][$i];
47 - if($end_time_ms==null)break;
 50+ if($end_time_ms==null)break;
4851 }
49 - //set to the keyframe right after the keyframe of the end time:
 52+ //set to the keyframe right after the keyframe of the end time:
5053 if($end_time_ms!=null){
5154 if($meta['times'][$i] >= $end_time_ms){
5255 $end_byte= $meta['filepositions'][$i];
5356 break;
54 - }
55 - }
56 - }
 57+ }
 58+ }
 59+ }
5760 return array($start_byte, $end_byte);
5861 }
59 - function playTimeReq( $start_time_sec, $end_time_sec=null ){
60 - //print "play $start_time_sec to $end_time_sec";
 62+ function playTimeReq( $start_time_sec, $end_time_sec=null ){
 63+ //print "play $start_time_sec to $end_time_sec";
6164 //@@todo cache byte offsets in memcache if available
6265 if( $end_time_sec)
6366 $this->mDuration = $end_time_sec - $start_time_sec;
64 - //print "SET metaDuration to: " . $this->metaDuration . "\n";
 67+ //print "SET metaDuration to: " . $this->metaDuration . "\n";
6568 //print_r($fullMeta);
66 - $meta =& $this->getKeyFrameMetaData();
 69+ $meta =& $this->getKeyFrameMetaData();
6770 //die;
68 - $start_byte=$end_byte=null;
 71+ $start_byte=$end_byte=null;
6972 if($start_time_sec==0 && $end_time_sec==null)$this->play();
7073 $start_time_ms = $start_time_sec*1000;
71 - $end_time_ms = ($end_time_sec==null)?null:$end_time_sec*1000;
72 -
73 - for($i=0; $i < count($meta['times']); $i++){
 74+ $end_time_ms = ($end_time_sec==null)?null:$end_time_sec*1000;
 75+
 76+ for($i=0; $i < count($meta['times']); $i++){
7477 //set to the keyframe right before a keyframe of the requested start time
7578 if($meta['times'][$i]>=$start_time_ms && $start_byte==null){
7679 $start_byte=(isset($meta['times'][$i-1]))?$meta['filepositions'][$i-1]:$meta['filepositions'][$i];
77 - if($end_time_ms==null)break;
 80+ if($end_time_ms==null)break;
7881 }
79 - //set to the keyframe right after the keyframe of the end time:
 82+ //set to the keyframe right after the keyframe of the end time:
8083 if($end_time_ms!=null){
8184 if($meta['times'][$i]>=$end_time_ms){
8285 $end_byte= $meta['filepositions'][$i];
8386 break;
84 - }
85 - }
86 - }
87 - $this->play($start_byte, $end_byte);
88 - }
 87+ }
 88+ }
 89+ }
 90+ $this->play($start_byte, $end_byte);
 91+ }
8992 function computeMetaData()
9093 {
9194 ini_set("max_execution_time","0");//computeMetaData can take some time
@@ -95,69 +98,69 @@
9699 $this->compMetaData['keyframes']['filepositions'] = array();
97100 $this->compMetaData['keyframes']['times'] = array();
98101 $this->compMetaData['lasttimestamp'] = -1;
99 -
 102+
100103 $this->origMetaOfs = 0;
101104 $this->origMetaSize = 0;
102105 $this->origMetaData = null;
103 -
 106+
104107 $skipTagTypes = array();
105108 while ($tag = $this->getTag( $skipTagTypes ))
106109 {
107110 // pre-calculate the timestamp as seconds
108 - $ts = $tag->timestamp/1000;
109 - if ($tag->timestamp > 0 && $tag->type==FLV_Tag::TYPE_VIDEO ){
 111+ $ts = $tag->timestamp/1000;
 112+ if ($tag->timestamp > 0 && $tag->type==FLV_Tag::TYPE_VIDEO ){
110113 $ts = (MAX_FLV_TS*$this->wrapTimeCount) + $ts;
111114 if($ts < $this->compMetaData['lasttimestamp']){
112115 $this->wrapTimeCount++;
113116 $ts = MAX_FLV_TS + $ts;
114 - }
 117+ }
115118 //print "set end time to $ts \n";
116 - $this->compMetaData['lasttimestamp'] = $ts;
117 - }
118 -
 119+ $this->compMetaData['lasttimestamp'] = $ts;
 120+ }
 121+
119122 switch ($tag->type)
120123 {
121 - case FLV_Tag::TYPE_VIDEO :
 124+ case FLV_Tag::TYPE_VIDEO :
122125 //Optimization, extract the frametype without analyzing the tag body
123126 if ((ord($tag->body[0]) >> 4) == FLV_Tag_Video::FRAME_KEYFRAME)
124127 {
125128 $this->compMetaData['keyframes']['filepositions'][] = $this->getTagOffset();
126129 $this->compMetaData['keyframes']['times'][] = $ts;
127130 }
128 -
 131+
129132 if ( !in_array(FLV_TAG::TYPE_VIDEO, $skipTagTypes) )
130133 {
131134 $this->compMetaData['width'] = $tag->width;
132135 $this->compMetaData['height'] = $tag->height;
133136 $this->compMetaData['videocodecid'] = $tag->codec;
134 - //Processing one video tag is enough
 137+ //Processing one video tag is enough
135138 array_push( $skipTagTypes, FLV_Tag::TYPE_VIDEO );
136 - }
 139+ }
137140 break;
138 -
 141+
139142 case FLV_Tag::TYPE_AUDIO :
140 -
141 - //Save audio frame positions when there is no video
 143+
 144+ //Save audio frame positions when there is no video
142145 /*if (!$flv->hasVideo && $ts - $oldTs > $this->audioFrameGap)
143146 {
144147 $this->compMetaData['keyframes']['filepositions'][] = $this->getTagOffset();
145148 $this->compMetaData['keyframes']['times'][] = $ts;
146149 $oldTs = $ts;
147150 }
148 -
149 - if ( !in_array( FLV_Tag::TYPE_AUDIO, $skipTagTypes) )
 151+
 152+ if ( !in_array( FLV_Tag::TYPE_AUDIO, $skipTagTypes) )
150153 {
151154 $this->compMetaData['audiocodecid'] = $tag->codec;
152155 $this->compMetaData['audiofreqid'] = $tag->frequency;
153156 $this->compMetaData['audiodepthid'] = $tag->depth;
154157 $this->compMetaData['audiomodeid'] = $tag->mode;
155 -
 158+
156159 //Processing one audio tag is enough
157160 array_push( $skipTagTypes, FLV_Tag::TYPE_AUDIO );
158161 } */
159 -
 162+
160163 break;
161 -
 164+
162165 case FLV_Tag::TYPE_DATA :
163166 if ($tag->name == 'onMetaData')
164167 {
@@ -167,19 +170,19 @@
168171 }
169172 break;
170173 }
171 -
 174+
172175 //Does this actually help with memory allocation?
173176 unset($tag);
174177 }
175 -
 178+
176179 if (! empty($this->compMetaData['keyframes']['times']))
177180 $this->compMetaData['lastkeyframetimestamp'] = $this->compMetaData['keyframes']['times'][ count($this->compMetaData['keyframes']['times'])-1 ];
178 -
 181+
179182 $this->compMetaData['duration'] = $this->compMetaData['lasttimestamp'];
180 -
 183+
181184 return $this->compMetaData;
182185 }
183 -
 186+
184187 function setMetaData( $metadata, $origMetaOfs = 0, $origMetaSize = 0 )
185188 {
186189 $this->compMetaData = $metadata;
@@ -191,50 +194,50 @@
192195 {
193196 if($this->fullMeta)return $this->fullMeta;
194197 if(is_file($this->fname . META_DATA_EXT)){
195 - $this->fullMeta = unserialize(file_get_contents ($this->fname . META_DATA_EXT));
 198+ $this->fullMeta = unserialize(file_get_contents ($this->fname . META_DATA_EXT));
196199 }else{
197200 $this->computeMetaData();
198201 if (! is_array($this->origMetaData)){
199202 $this->fullMeta = $this->compMetaData;
200203 }else{
201204 $this->fullMeta = array_merge( $this->origMetaData, $this->compMetaData );
202 - }
203 - //free non-merged arrays:
 205+ }
 206+ //free non-merged arrays:
204207 unset($this->origMetaData);
205 - unset($this->compMetaData);
 208+ unset($this->compMetaData);
206209 //convert floats to int
207210 foreach($this->fullMeta['keyframes']['times'] as $inx=>& $ts){
208211 $ts = (int)($ts*1000);
209 - }
 212+ }
210213 if(!file_put_contents($this->fname . META_DATA_EXT, serialize($this->fullMeta))){
211214 throw( new FLV_FileException('Unable to write out cached keyframe output to '.$this->fname) );
212 - }
213 - }
 215+ }
 216+ }
214217 return $this->fullMeta;
215218 }
216219 function getKeyFrameMetaData(){
217 - $meta = & $this->getMetaData();
 220+ $meta = $this->getMetaData();
218221 return $meta['keyframes'];
219222 }
220223 function getSegmentMetaData(){
221224 //pull base cached metadata
222225 //update duration & return new array
223 - $meta = & $this->getMetaData();
224 - $meta['keyframes'] = array();
225 - //@@todo update all the other metadata to match segment outupt
 226+ $meta = $this->getMetaData();
 227+ $meta['keyframes'] = array();
 228+ //@@todo update all the other metadata to match segment outupt
226229 return $meta;
227 - }
 230+ }
228231 function play( $from = 0, $play_to=null )
229232 {
230233 fseek($this->fp, 0);
231 -
 234+
232235 // get original file header just in case it has any special flag
233236 echo fread($this->fp, $this->bodyOfs + 4);
234 -
 237+
235238 // output the metadata if available
236 - $meta = $this->getSegmentMetaData();
 239+ $meta = $this->getSegmentMetaData();
237240 //$meta = $this->getMetaData();
238 -
 241+
239242 if (! empty($meta))
240243 {
241244 //serialize the metadata as an AMF stream
@@ -252,25 +255,25 @@
253256 $out.= pack('N', 0);
254257 //StreamID
255258 $out.= pack('Cn', 0, 0);
256 -
 259+
257260 echo $out;
258261 echo $serMeta;
259 -
 262+
260263 // PrevTagSize for the metadata
261264 echo pack('N', strlen($serMeta) + strlen($out) );
262265 }
263 -
 266+
264267 $chunkSize = 4096;
265268 $skippedOrigMeta = empty($this->origMetaSize);
266 - //seek to offset point:
 269+ //seek to offset point:
267270 fseek($this->fp, $from);
268271 while (! feof($this->fp))
269272 {
270273 // if the original metadata is present and not yet skipped...
271274 if (! $skippedOrigMeta)
272 - {
 275+ {
273276 $pos = ftell($this->fp);
274 -
 277+
275278 // check if we are going to output it in this loop step
276279 if ( $pos <= $this->origMetaOfs &&
277280 $pos + $chunkSize > $this->origMetaOfs )
@@ -278,10 +281,10 @@
279282 // output the bytes just before the original metadata tag
280283 if ($this->origMetaOfs - $pos > 0)
281284 echo fread($this->fp, $this->origMetaOfs - $pos);
282 -
 285+
283286 // position the file pointer just after the metadata tag
284287 fseek($this->fp, $this->origMetaOfs + $this->origMetaSize);
285 -
 288+
286289 $skippedOrigMeta = true;
287290 continue;
288291 }
Index: trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/FLV/Util/AMFSerialize.php
@@ -46,7 +46,7 @@
4747 */
4848 function serialize( $var, $skipMark = false )
4949 {
50 - $number='';
 50+ $number = '';
5151 // process objects as hashed arrays
5252 if ( is_object( $var ) )
5353 $var = (array)$var;
@@ -94,7 +94,6 @@
9595 return ( $skipMark ? '':"\x01" ) . ( $var ? "\x01" : "\x00" );
9696
9797 } else if ( is_numeric( $var ) ) {
98 -
9998 $number .= pack( 'd', $var );
10099
101100 // reverse bytes if we are in little-endian hardware
@@ -121,7 +120,8 @@
122121 if ( $this->isLittleEndian )
123122 $tz = strrev( $tz );
124123 }
125 -
 124+ if(!isset($tz))
 125+ $tz='';
126126 return ( $skipMark ? '':"\x0B" ) . $this->serialize( (float)$ms, true ) . pack( 'n', $tz );
127127 }
128128
Index: trunk/extensions/MetavidWiki/skins/mv_embed/flvServer/mvFlvServer.php
@@ -1,15 +1,15 @@
22 <?php
33 /*
4 - * mv_flvServer.php
 4+ * mv_flvServer.php
55 *
66 * @date Jul 25, 2008
7 - *
 7+ *
88 * this should enable serving segments of flv files
9 - *
 9+ *
1010 * called with: mv_flvServer.php?file=myClip.flv&t=0:01:00/0:02:00
11 - *
 11+ *
1212 * flash does not like get arguments in media files? so best to use mod-rewrite
13 - * something like:
 13+ * something like:
1414 * /flvserver/my_file/start_time/end_time
1515 */
1616 define('BASE_LOCAL_CLIP_PATH', '/metavid/video_archive');
@@ -24,12 +24,11 @@
2525 if(!function_exists('filter_input')){
2626 die('you version of php lacks <b>filter_input()</b> function</br>');
2727 }
28 -
2928 do_request();
3029 function do_request(){
3130 $file_req = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_STRING);
3231 $time_req = filter_input(INPUT_GET, 't', FILTER_SANITIZE_STRING);
33 -
 32+
3433 //try to grab the file from /filename format
3534 if($file_req==''){
3635 if(isset($_SERVER['PHP_SELF'])){
@@ -43,12 +42,15 @@
4443 $file_req = str_replace(array('../','./'), '', $file_req);
4544 if($file_req=='')die('error: missing file name');
4645 $file_loc =BASE_LOCAL_CLIP_PATH.'/'.$file_req;
47 - if(!is_file($file_loc))die('error: requested file not found');
48 -
49 - //if 't' is empty no seeking necessary:
50 - if($time_req==''){
 46+ if(!is_file($file_loc))
 47+ die('error: requested file not found:'. $file_loc);
 48+
 49+ //if 't' is empty no seeking necessary:
 50+ if( $time_req == '' ){
 51+ header('Content-type: '.FLASH_VIDEO_CONTENT_TYPE);
 52+ header('Content-Length: ' . filesize($file_loc) );
5153 output_full_file($file_loc);
52 - }else{
 54+ }else{
5355 //@@todo support more time request formats
5456 if(strpos( $time_req,'/')!==false){
5557 list($start_time_ntp, $end_time_ntp)=explode('/',$time_req);
@@ -57,12 +59,13 @@
5860 }else{
5961 $start_sec = npt2seconds($time_req);
6062 $end_sec = null;
61 - if($start_sec==0)output_full_file($file_loc);
 63+ if( $start_sec==0 )
 64+ output_full_file($file_loc);
6265 }
6366 if($start_sec > $end_sec)die('error: requested invalid time range');
6467 //print "DO: $start_sec $end_sec \n";
6568 require_once('MvFlv.php');
66 - //open up the metavid Flv object:
 69+ //open up the metavid Flv object:
6770 $flv = new MyFLV();
6871 try {
6972 $flv->open( $file_loc );
@@ -71,15 +74,15 @@
7275 }
7376 header('Content-type: '.FLASH_VIDEO_CONTENT_TYPE);
7477 $one_year = 60*60*24*365;
75 - header("Expires: " . gmdate( "D, d M Y H:i:s", time() + $one_year ) . " GM");
 78+ header("Expires: " . gmdate( "D, d M Y H:i:s", time() + $one_year ) . " GM");
7679 //small meta file to parse no big deal:
7780 list($start_byte, $end_byte) = $flv->getKeyFrameByteTimes($start_sec, $end_sec);
78 - //send out content length:
 81+ //send out content length:
7982 header('Content-Length: ' . ($end_byte - $start_byte) );
80 - $flv->play($start_byte, $end_byte);
 83+ $flv->play($start_byte, $end_byte);
8184 //$end = microtime(true);
82 - //file_put_contents('/tmp/time.log', "<hr/>EXTRACT METADATA PROCESS TOOK " . number_format(($end-$start), 2) . " seconds<br/>");
83 - $flv->close();
 85+ //file_put_contents('/tmp/time.log', "<hr/>EXTRACT METADATA PROCESS TOOK " . number_format(($end-$start), 2) . " seconds<br/>");
 86+ $flv->close();
8487 }
8588 }
8689
@@ -88,13 +91,13 @@
8992 header('Content-Type: '.FLASH_VIDEO_CONTENT_TYPE);
9093 while (ob_get_level() > 0) {
9194 ob_end_flush();
92 - } //turn off output buffering
 95+ } //turn off output buffering
9396 @readfile($file_loc);
9497 die();
9598 }
9699
97100 function npt2seconds($str_time){
98 - $time_ary = explode(':', $str_time);
 101+ $time_ary = explode(':', $str_time);
99102 $hours=$min=$sec=0;
100103 if(count($time_ary)==3){
101104 $hours=$time_ary[0];
@@ -105,7 +108,7 @@
106109 $sec=$time_ary[1];
107110 }else if(count($time_ary)==1){
108111 $sec=$time_ary[0];
109 - }
 112+ }
110113 return ($hours*3600) + ($min*60) + $sec;
111114 }
112115 ?>

Status & tagging log