| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | use DBI; | 
|---|
| 16 | use DBD::Pg; | 
|---|
| 17 | use DBD::SQLite; | 
|---|
| 18 | use Schedule::At; | 
|---|
| 19 | use Time::Local; | 
|---|
| 20 | use Jcode; | 
|---|
| 21 |  | 
|---|
| 22 | $path = $0; | 
|---|
| 23 | $path =~ s/recwrap.pl$//i; | 
|---|
| 24 | if ($path ne "./"){ | 
|---|
| 25 | push( @INC, "$path"); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | require "foltialib.pl"; | 
|---|
| 29 |  | 
|---|
| 30 | $recch = $ARGV[0] ; | 
|---|
| 31 | if ($recch eq "" ){ | 
|---|
| 32 |  | 
|---|
| 33 | print "usage recwrap.pl  ch length(sec) [bitrate(5)] [TID] [NO] [PID]\n"; | 
|---|
| 34 | exit; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | $recch = $ARGV[0] ; | 
|---|
| 38 | $reclength = $ARGV[1] ; | 
|---|
| 39 | $bitrate  = $ARGV[2] ; | 
|---|
| 40 | $tid  = $ARGV[3] ; | 
|---|
| 41 | $countno  = $ARGV[4] ; | 
|---|
| 42 | $pid = $ARGV[5] ; | 
|---|
| 43 | $stationid = $ARGV[6] ; | 
|---|
| 44 | $usedigital = $ARGV[7] ; | 
|---|
| 45 | $digitalstationband = $ARGV[8] ; | 
|---|
| 46 | $digitalch= $ARGV[9] ; | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | $dbh = DBI->connect($DSN,$DBUser,$DBPass) ||die $DBI::error;; | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | if ($usedigital == 1){ | 
|---|
| 53 | $extension = ".m2t"; | 
|---|
| 54 | }else{ | 
|---|
| 55 | $extension = ".m2p"; | 
|---|
| 56 | } | 
|---|
| 57 | if ($recch == -2 ){ | 
|---|
| 58 | $extension = ".aac"; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | $outputfile = strftime("%Y%m%d-%H%M", localtime(time + 60)); | 
|---|
| 62 | chomp($outputfile); | 
|---|
| 63 |  | 
|---|
| 64 | if ($tid == 0){ | 
|---|
| 65 | if ($usedigital == 1){ | 
|---|
| 66 | $outputfilename = "0--".$outputfile."-".$digitalch.$extension; | 
|---|
| 67 | $mp4newstylefilename = "-0--".$outputfile."-".$digitalch; | 
|---|
| 68 | }else{ | 
|---|
| 69 | $outputfilename = "0--".$outputfile."-".$recch.$extension; | 
|---|
| 70 | $mp4newstylefilename = "-0--".$outputfile."-".$recch; | 
|---|
| 71 | } | 
|---|
| 72 | }else{ | 
|---|
| 73 | if ($countno == 0){ | 
|---|
| 74 | $outputfilename = $tid ."--".$outputfile.$extension; | 
|---|
| 75 | $mp4newstylefilename = "-" . $tid ."--".$outputfile; | 
|---|
| 76 | }else{ | 
|---|
| 77 | $outputfilename = $tid ."-".$countno."-".$outputfile.$extension; | 
|---|
| 78 | $mp4newstylefilename = "-" . $tid ."-".$countno."-".$outputfile; | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | if ($recch == -2 ){ | 
|---|
| 83 |  | 
|---|
| 84 | $sth = $dbh->prepare($stmt{'recwrap.8'}); | 
|---|
| 85 | $sth->execute($stationid); | 
|---|
| 86 | @stationline= $sth->fetchrow_array; | 
|---|
| 87 | $radikostationname = $stationline[3]; | 
|---|
| 88 | my $starttime = time(); | 
|---|
| 89 | my $endepochtime = time() + $reclength; | 
|---|
| 90 |  | 
|---|
| 91 | $oserr = system("$toolpath/perl/digitalradiorecording.pl $radikostationname $reclength $outputfilename"); | 
|---|
| 92 | $oserr = $oserr / 256; | 
|---|
| 93 | &writelog("recwrap DEBUG radiko rec finished. $oserr"); | 
|---|
| 94 |  | 
|---|
| 95 | $now = time(); | 
|---|
| 96 | if ($now < $endepochtime){ | 
|---|
| 97 | &writelog("recwrap radiko rec failed,will be retry. NOW:$now PlanedEnd:$endepochtime"); | 
|---|
| 98 |  | 
|---|
| 99 | my $retrycounter = 0; | 
|---|
| 100 | my $waitsecsbase = 10; | 
|---|
| 101 | my $waitsec ; | 
|---|
| 102 | my $waitsecsrand ; | 
|---|
| 103 | my $recfilepath; | 
|---|
| 104 | while($now < $endepochtime){ | 
|---|
| 105 | if($retrycounter >= 15){ | 
|---|
| 106 | &writelog("recwrap radiko rec failed,Giving up.Max retry counter over."); | 
|---|
| 107 | last; | 
|---|
| 108 | } | 
|---|
| 109 | $waitsecsrand = int(rand($waitsecsbase/2)); | 
|---|
| 110 | $waitsec = $waitsecsbase + $waitsecsrand ; | 
|---|
| 111 | &writelog("recwrap DEBUG radiko rec retry waiting. $waitsec ($waitsecsbase + $waitsecsrand)"); | 
|---|
| 112 | sleep($waitsec); | 
|---|
| 113 | &writelog("recwrap DEBUG retry start."); | 
|---|
| 114 | $recfilepath = "$recfolderpath"."/"."$outputfilename"; | 
|---|
| 115 | if (-e $recfilepath ){ | 
|---|
| 116 | unlink("$recfilepath"); | 
|---|
| 117 | &writelog("recwrap DEBUG delete $recfilepath"); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | $oserr = system("$toolpath/perl/digitalradiorecording.pl $radikostationname $reclength $outputfilename N"); | 
|---|
| 121 | $oserr = $oserr / 256; | 
|---|
| 122 | &writelog("recwrap DEBUG radiko rec retry finished. $oserr"); | 
|---|
| 123 |  | 
|---|
| 124 | $retrycounter++; | 
|---|
| 125 | if ($waitsecsbase < 600 ){ | 
|---|
| 126 | $waitsecsbase = $waitsecsbase * 2; | 
|---|
| 127 | } | 
|---|
| 128 | $now = time(); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | $sth = $dbh->prepare($stmt{'recwrap.1'}); | 
|---|
| 135 | $sth->execute($outputfilename, $pid); | 
|---|
| 136 | &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.1'}"); | 
|---|
| 137 | &changefilestatus($pid,$FILESTATUSTRANSCODEMP4BOX); | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 | $sth = $dbh->prepare($stmt{'recwrap.2'}); | 
|---|
| 141 | $sth->execute($outputfilename); | 
|---|
| 142 | &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.2'}"); | 
|---|
| 143 |  | 
|---|
| 144 |  | 
|---|
| 145 | }else{ | 
|---|
| 146 |  | 
|---|
| 147 | if ($usedigital == 1){ | 
|---|
| 148 |  | 
|---|
| 149 | &writelog("recwrap RECSTART DIGITAL $digitalstationband $digitalch $reclength $stationid 0 $outputfilename $tid $countno friio"); | 
|---|
| 150 |  | 
|---|
| 151 | $starttime = time(); | 
|---|
| 152 | $oserr = system("$toolpath/perl/digitaltvrecording.pl $digitalstationband $digitalch $reclength $stationid 0 $outputfilename $tid $countno friio"); | 
|---|
| 153 | $oserr = $oserr / 256; | 
|---|
| 154 |  | 
|---|
| 155 | if ($oserr == 1){ | 
|---|
| 156 | &writelog("recwrap ABORT recfile exist. [$outputfilename] $digitalstationband $digitalch $reclength $stationid 0  $outputfilename $tid $countno"); | 
|---|
| 157 | exit; | 
|---|
| 158 | }elsif ($oserr == 2){ | 
|---|
| 159 | &writelog("recwrap ERR 2:Device busy;retry."); | 
|---|
| 160 | &continuousrecordingcheck; | 
|---|
| 161 | sleep(2); | 
|---|
| 162 | $oserr = system("$toolpath/perl/digitaltvrecording.pl $digitalstationband $digitalch $reclength $stationid N $outputfilename $tid $countno friio"); | 
|---|
| 163 | $oserr = $oserr / 256; | 
|---|
| 164 | if ($oserr == 2){ | 
|---|
| 165 | &writelog("recwrap ERR 2:Device busy;Giving up digital recording."); | 
|---|
| 166 | if ($recunits > 0 ){ | 
|---|
| 167 | }else{ | 
|---|
| 168 | exit; | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | }elsif ($oserr == 3){ | 
|---|
| 172 | &writelog("recwrap ABORT:ERR 3"); | 
|---|
| 173 | exit ; | 
|---|
| 174 | } | 
|---|
| 175 | }else{ | 
|---|
| 176 | if ($recunits > 0 ){ | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 | if ($haveirdaunit == 1){ | 
|---|
| 180 |  | 
|---|
| 181 | if ($recch == 0){ | 
|---|
| 182 |  | 
|---|
| 183 | &writelog("recwrap Call Change STB CH :$pid"); | 
|---|
| 184 | system ("$toolpath/perl/changestbch.pl $pid &"); | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | if($recch == -10){ | 
|---|
| 189 |  | 
|---|
| 190 | &writelog("recwrap Not recordable channel;exit:PID $pid"); | 
|---|
| 191 | exit; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | &writelog("recwrap RECSTART $recch $reclength 0 $outputfilename $bitrate $tid $countno $pid $usedigital $digitalstationband $digitalch"); | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | $starttime = time(); | 
|---|
| 199 |  | 
|---|
| 200 | $oserr = system("$toolpath/perl/tvrecording.pl $recch $reclength 0 $outputfilename $bitrate $tid $countno"); | 
|---|
| 201 | $oserr = $oserr / 256; | 
|---|
| 202 | if ($oserr == 1){ | 
|---|
| 203 | &writelog("recwrap ABORT recfile exist. [$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid"); | 
|---|
| 204 | exit; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | $now = time(); | 
|---|
| 208 | if ($now < $starttime + 100){ | 
|---|
| 209 | $retrycounter = 0; | 
|---|
| 210 | while($now < $starttime + 100){ | 
|---|
| 211 | if($retrycounter >= 5){ | 
|---|
| 212 | &writelog("recwrap WARNING  Giving up recording."); | 
|---|
| 213 | last; | 
|---|
| 214 | } | 
|---|
| 215 | &writelog("recwrap retry recording $now $starttime"); | 
|---|
| 216 |  | 
|---|
| 217 | $starttime = time(); | 
|---|
| 218 | if($outputfilename =~ /.m2t$/){ | 
|---|
| 219 | $outputfilename =~ s/.m2t$/.m2p/; | 
|---|
| 220 | } | 
|---|
| 221 | $oserr = system("$toolpath/perl/tvrecording.pl $recch $reclength N $outputfilename $bitrate $tid $countno"); | 
|---|
| 222 | $now = time(); | 
|---|
| 223 | $oserr = $oserr / 256; | 
|---|
| 224 | if ($oserr == 1){ | 
|---|
| 225 | &writelog("recwrap ABORT recfile exist. in resume process.[$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid"); | 
|---|
| 226 | exit; | 
|---|
| 227 | } | 
|---|
| 228 | $retrycounter++; | 
|---|
| 229 | } | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | &writelog("recwrap RECEND [$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid"); | 
|---|
| 233 |  | 
|---|
| 234 | } | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 |  | 
|---|
| 239 | $sth = $dbh->prepare($stmt{'recwrap.1'}); | 
|---|
| 240 | $sth->execute($outputfilename, $pid); | 
|---|
| 241 | &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.1'}"); | 
|---|
| 242 | &changefilestatus($pid,$FILESTATUSRECEND); | 
|---|
| 243 |  | 
|---|
| 244 |  | 
|---|
| 245 | $sth = $dbh->prepare($stmt{'recwrap.2'}); | 
|---|
| 246 | $sth->execute($outputfilename); | 
|---|
| 247 | &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.2'}"); | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 | if (-e "$toolpath/perl/captureimagemaker.pl"){ | 
|---|
| 251 | &writelog("recwrap Call captureimagemaker $outputfilename"); | 
|---|
| 252 | &changefilestatus($pid,$FILESTATUSCAPTURE); | 
|---|
| 253 | system ("$toolpath/perl/captureimagemaker.pl $outputfilename"); | 
|---|
| 254 | &changefilestatus($pid,$FILESTATUSCAPEND); | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 |  | 
|---|
| 260 | $sth = $dbh->prepare($stmt{'recwrap.3'}); | 
|---|
| 261 | $sth->execute($tid); | 
|---|
| 262 | @psptrcn= $sth->fetchrow_array; | 
|---|
| 263 | if ($psptrcn[0]  == 1 ){ | 
|---|
| 264 | &writelog("recwrap Launch ipodtranscode.pl"); | 
|---|
| 265 | exec ("$toolpath/perl/ipodtranscode.pl"); | 
|---|
| 266 | exit; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | sub continuousrecordingcheck(){ | 
|---|
| 270 | my $now = time() + 60 * 2; | 
|---|
| 271 | &writelog("recwrap DEBUG continuousrecordingcheck() now $now"); | 
|---|
| 272 | my @processes =`ps ax | grep -e recpt1 -e recfriio`; | 
|---|
| 273 |  | 
|---|
| 274 | my $psline = ""; | 
|---|
| 275 | my @processline = ""; | 
|---|
| 276 | my $pid = ""; | 
|---|
| 277 | my @pid; | 
|---|
| 278 | my $sth; | 
|---|
| 279 | foreach (@processes){ | 
|---|
| 280 | if (/recpt1|friiodetect/) { | 
|---|
| 281 | if (/^.[0-9]*\s/){ | 
|---|
| 282 | push(@pid, $&); | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | if (@pid > 0){ | 
|---|
| 288 | my @filenameparts; | 
|---|
| 289 | my $tid = ""; | 
|---|
| 290 | my $startdate = ""; | 
|---|
| 291 | my $starttime = ""; | 
|---|
| 292 | my $startdatetime = ""; | 
|---|
| 293 | my @recfile; | 
|---|
| 294 | my $endtime = ""; | 
|---|
| 295 | my $endtimeepoch = ""; | 
|---|
| 296 | foreach $pid (@pid){ | 
|---|
| 297 |  | 
|---|
| 298 | &writelog("recwrap DEBUG continuousrecordingcheck() PID $pid"); | 
|---|
| 299 |  | 
|---|
| 300 | my @lsofoutput = `/usr/sbin/lsof -p $pid`; | 
|---|
| 301 | my $filename = ""; | 
|---|
| 302 |  | 
|---|
| 303 | foreach (@lsofoutput){ | 
|---|
| 304 | if (/m2t/){ | 
|---|
| 305 | @processline = split(/\s+/,$_); | 
|---|
| 306 | $filename = $processline[8]; | 
|---|
| 307 | $filename =~ s/$recfolderpath\///; | 
|---|
| 308 | &writelog("recwrap DEBUG continuousrecordingcheck()  FILENAME $filename"); | 
|---|
| 309 |  | 
|---|
| 310 | @filenameparts = split(/-/,$filename); | 
|---|
| 311 | $tid = $filenameparts[0]; | 
|---|
| 312 | $startdate = $filenameparts[2]; | 
|---|
| 313 | $starttime = $filenameparts[3]; | 
|---|
| 314 | @filenameparts = split(/\./,$starttime); | 
|---|
| 315 | $startdatetime = $startdate.$filenameparts[0]; | 
|---|
| 316 |  | 
|---|
| 317 | &writelog("recwrap DEBUG continuousrecordingcheck() $stmt{'recwrap.7'}"); | 
|---|
| 318 | $sth = $dbh->prepare($stmt{'recwrap.7'}); | 
|---|
| 319 | &writelog("recwrap DEBUG continuousrecordingcheck() prepare"); | 
|---|
| 320 | $sth->execute($tid, $startdatetime); | 
|---|
| 321 | &writelog("recwrap DEBUG continuousrecordingcheck() execute"); | 
|---|
| 322 | @recfile = $sth->fetchrow_array; | 
|---|
| 323 | &writelog("recwrap DEBUG continuousrecordingcheck() @recfile  $recfile[0] $recfile[1] $recfile[2] $recfile[3] $recfile[4] $recfile[5] $recfile[6] $recfile[7] $recfile[8] $recfile[9] "); | 
|---|
| 324 |  | 
|---|
| 325 | $endtime = $recfile[4]; | 
|---|
| 326 | $endtimeepoch = &foldate2epoch($endtime); | 
|---|
| 327 | &writelog("recwrap DEBUG continuousrecordingcheck() $recfile[0] $recfile[1] $recfile[2] $recfile[3] $recfile[4] $recfile[5] endtimeepoch $endtimeepoch"); | 
|---|
| 328 | if ($endtimeepoch < $now){ | 
|---|
| 329 |  | 
|---|
| 330 | system("kill $pid"); | 
|---|
| 331 | &writelog("recwrap recording process killed $pid/$endtimeepoch/$now"); | 
|---|
| 332 | }else{ | 
|---|
| 333 | &writelog("recwrap No processes killed: $endtimeepoch/$now"); | 
|---|
| 334 | } | 
|---|
| 335 | } | 
|---|
| 336 | } | 
|---|
| 337 | } | 
|---|
| 338 | }else{ | 
|---|
| 339 |  | 
|---|
| 340 | &writelog("recwrap No recording process killed."); | 
|---|
| 341 | } | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 |  | 
|---|
| 345 |  | 
|---|