<?php

namespace App\Media\QuickTime\Atoms\moov\trak\mdia\minf\stbl;

use App\Media\QuickTime\Atoms\SubAtom;

class stts extends SubAtom
{
	public function __construct(int $offset,int $size,string $filename,?string $data)
	{
		parent::__construct($offset,$size,$filename);

		$read = unpack($this->unpack(self::atom_record),substr($data,0,$ptr=$this->unpack_size(self::atom_record)));

		for ($i=0; $i<$read['count']; $i++) {
			$this->cache->push(unpack('Ncount/Nduration',substr($data,$ptr,8)));
			$ptr += 8;
		}

		// For debugging
		if (FALSE)
			$this->debug = hex_dump($data ?: $this->data());
	}

	public function frame_rate(float $time_scale): float
	{
		return $this->cache
			->pluck('duration')
			->map(fn($item)=>$time_scale/$item)
			->max();
	}
}