2 include_once('config.php');
3 include_once( INSTALL_PATH . "/DBRecord.class.php" );
4 include_once( INSTALL_PATH . "/reclib.php" );
10 public static function simple( $program_id , $autorec = 0, $mode = 0) {
13 $prec = new DBRecord( TBL_PREFIX.PROGRAM_TBL, "id", $program_id );
27 catch( Exception $e ) {
33 public static function custom(
34 $starttime, // 開始時間Datetime型
35 $endtime, // 終了時間Datetime型
36 $channel_id, // チャンネルID
37 $title = "none", // タイトル
38 $description = "none", // 概要
39 $category_id = 0, // カテゴリID
40 $program_id = 0, // 番組ID
47 $start_time = toTimestamp( $starttime );
48 $end_time = toTimestamp( $endtime );
50 if( $start_time < (time() + PADDING_TIME + 10) ) { // 現在時刻より3分先より小さい=すでに開始されている番組
51 $start_time = time() + PADDING_TIME + 10; // 録画開始時間を3分10秒先に設定する
53 $at_start = $start_time - PADDING_TIME;
54 $sleep_time = PADDING_TIME - FORMER_TIME;
55 $rec_start = $start_time - FORMER_TIME;
58 $duration = $end_time - $rec_start;
59 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く
60 throw new Exception( "終わりつつある/終わっている番組です" );
67 $num = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE program_id = '".$program_id."'" );
69 throw new Exception("同一の番組が録画予約されています");
73 $crec = new DBRecord( TBL_PREFIX.CHANNEL_TBL, "id", $channel_id );
76 $tuners = ($crec->type == "GR") ? GR_TUNERS : BS_TUNERS;
77 $battings = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
78 "AND type = '".$crec->type."' ".
79 "AND starttime < '".toDatetime($end_time) ."' ".
80 "AND endtime > '".toDatetime($rec_start)."'"
83 if( $battings >= $tuners ) {
85 if( FORCE_CONT_REC ) {
88 $nexts = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
89 "AND type = '".$crec->type."' ".
90 "AND starttime = '".toDatetime($end_time - FORMER_TIME)."'");
92 $prevs = DBRecord::countRecords( TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
93 "AND type = '".$crec->type."' ".
94 "AND endtime = '".$starttime."'" );
96 // 前後を引いてもチューナー数と同数以上なら重複の解消は無理
97 if( ($battings - $nexts - $prevs) >= $tuners )
98 throw new Exception( "重複予約を解消できません" );
102 // この番組の終わりをちょっとだけ早める
103 $end_time = $end_time - FORMER_TIME - REC_SWITCH_TIME;
104 $duration = $end_time - $rec_start; // durationを計算しなおす
109 $trecs = DBRecord::createRecords(TBL_PREFIX.RESERVE_TBL, "WHERE complete = '0' ".
110 "AND type = '".$crec->type."' ".
111 "AND endtime = '".$starttime."'" );
113 for( $i = 0; $i < count($trecs) ; $i++ ) {
114 if( $battings < $tuners ) break; // 解消終了のハズ?
116 $prev_id = $trecs[$i]->id;
117 $prev_program_id = $trecs[$i]->program_id;
118 $prev_channel_id = $trecs[$i]->channel_id;
119 $prev_title = $trecs[$i]->title;
120 $prev_description = $trecs[$i]->description;
121 $prev_category_id = $trecs[$i]->category_id;
122 $prev_starttime = $trecs[$i]->starttime;
123 $prev_endtime = $trecs[$i]->endtime;
124 $prev_autorec = $trecs[$i]->autorec;
125 $prev_mode = $trecs[$i]->mode;
127 $prev_start_time = toTimestamp($prev_starttime);
129 if( $prev_start_time > (time() + PADDING_TIME + FORMER_TIME) ) {
131 $prev_starttime = toDatetime( $prev_start_time + FORMER_TIME );
133 $prev_endtime = toDatetime( toTimestamp($prev_endtime) - FORMER_TIME - REC_SWITCH_TIME );
138 self::cancel( $prev_id );
141 $prev_starttime, // 開始時間Datetime型
142 $prev_endtime, // 終了時間Datetime型
143 $prev_channel_id, // チャンネルID
145 $prev_description, // 概要
146 $prev_category_id, // カテゴリID
147 $prev_program_id, // 番組ID
148 $prev_autorec, // 自動録画
151 catch( Exception $e ) {
152 throw new Exception( "重複予約を解消できません" );
156 throw new Exception( "重複予約を解消できません" );
160 if( $battings < 0 ) $battings = 0;
164 throw new Exception( "重複予約があります" );
170 // 改めてdurationをチェックしなおす
171 if( $duration < (FORMER_TIME + 60) ) { // 60秒以下の番組は弾く
172 throw new Exception( "終わりつつある/終わっている番組です" );
175 $filename = "".$crec->type.$crec->channel."_".date("YmdHis", $start_time)."_".date("YmdHis", $end_time).$RECORD_MODE[$mode]['suffix'];
178 $rrec = new DBRecord( TBL_PREFIX.RESERVE_TBL );
179 $rrec->channel_disc = $crec->channel_disc;
180 $rrec->channel_id = $crec->id;
181 $rrec->program_id = $program_id;
182 $rrec->type = $crec->type;
183 $rrec->channel = $crec->channel;
184 $rrec->title = $title;
185 $rrec->description = $description;
186 $rrec->category_id = $category_id;
187 $rrec->starttime = toDatetime( $rec_start );
188 $rrec->endtime = toDatetime( $end_time );
189 $rrec->path = $filename;
190 $rrec->autorec = $autorec;
192 $rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) );
195 $cmdline = AT." ".date("H:i m/d/Y", $at_start);
196 $descriptor = array( 0 => array( "pipe", "r" ),
197 1 => array( "pipe", "w" ),
198 2 => array( "pipe", "w" ),
200 $env = array( "CHANNEL" => $crec->channel,
201 "DURATION" => $duration,
202 "OUTPUT" => INSTALL_PATH.SPOOL."/".$filename,
203 "TYPE" => $crec->type,
209 $process = proc_open( $cmdline , $descriptor, $pipes, SPOOL, $env );
210 if( is_resource( $process ) ) {
211 fwrite($pipes[0], SLEEP." ".$sleep_time."\n" );
212 fwrite($pipes[0], DO_RECORD . "\n" );
213 fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" );
216 $ffmpeg_cmd = FFMPEG." -i \${OUTPUT} -r 1 -s 160x90 -ss ".(FORMER_TIME+2)." -vframes 1 -f image2 ".INSTALL_PATH.THUMBS."/".$filename.".jpg\n";
217 fwrite($pipes[0], $ffmpeg_cmd );
221 $rstring = stream_get_contents( $pipes[2]);
224 proc_close( $process );
228 throw new Exception("AT実行エラー");
232 $tok = strtok( $rstring, " \n" );
233 while( $tok !== false ) {
234 array_push( $rarr, $tok );
235 $tok = strtok( " \n" );
237 $key = array_search("job", $rarr);
238 if( $key !== false ) {
239 if( is_numeric( $rarr[$key+1]) ) {
240 $rrec->job = $rarr[$key+1];
241 return $rrec->job; // 成功
246 throw new Exception( "job番号の取得に失敗" );
248 catch( Exception $e ) {
249 if( $rrec != null ) {
261 public static function cancel( $reserve_id = 0, $program_id = 0 ) {
266 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "id" , $reserve_id );
268 else if( $program_id ) {
269 $rec = new DBRecord( TBL_PREFIX.RESERVE_TBL, "program_id" , $program_id );
272 throw new Exception("IDの指定が無効です");
274 if( ! $rec->complete ) {
276 if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + FORMER_TIME) )
277 throw new Exception("過去の録画予約です");
278 exec( ATRM . " " . $rec->job );
282 catch( Exception $e ) {