module Data.ZoomCache.Types (
Codec(..)
, TrackMap
, TrackSpec(..)
, IdentifyCodec
, Timestampable(..)
, before
, ZoomReadable(..)
, ZoomWritable(..)
, ZoomRaw(..)
, ZoomSummary(..)
, ZoomSummarySO(..)
, ZoomWork(..)
, Packet(..)
, packetFromPacketSO
, Summary(..)
, summaryFromSummarySO
, PacketSO(..)
, SummarySO(..)
, SummaryData()
, summarySODuration
, CacheFile(..)
, mkCacheFile
, fiFull
) where
import Blaze.ByteString.Builder
import Data.ByteString (ByteString)
import Data.Dynamic
import Data.Int
import Data.IntMap (IntMap)
import qualified Data.IntMap as IM
import Data.Iteratee (Iteratee)
import Data.Maybe (fromJust)
import Data.ZoomCache.Common
type TrackMap = IntMap TrackSpec
data TrackSpec = TrackSpec
{ specType :: !Codec
, specDeltaEncode :: !Bool
, specZlibCompress :: !Bool
, specSRType :: !SampleRateType
, specRate :: !Rational
, specName :: !ByteString
}
deriving (Show)
data Codec = forall a . ZoomReadable a => Codec a
instance Show Codec where
show = const "<<Codec>>"
type IdentifyCodec = ByteString -> Maybe Codec
data CacheFile = CacheFile
{ cfGlobal :: Global
, cfSpecs :: IntMap TrackSpec
}
mkCacheFile :: Global -> CacheFile
mkCacheFile g = CacheFile g IM.empty
fiFull :: CacheFile -> Bool
fiFull (CacheFile g specs) = IM.size specs == noTracks g
class Timestampable a where
timestamp :: a -> Maybe TimeStamp
before :: (Timestampable a) => Maybe TimeStamp -> a -> Bool
before Nothing _ = True
before (Just b) x = t == Nothing || (fromJust t) < b
where
t = timestamp x
instance Timestampable (TimeStamp, a) where
timestamp = Just . fst
instance Timestampable a => Timestampable [a] where
timestamp [] = Nothing
timestamp (x:_) = timestamp x
data PacketSO = PacketSO
{ packetSOTrack :: !TrackNo
, packetSOEntry :: !SampleOffset
, packetSOExit :: !SampleOffset
, packetSOCount :: !Int
, packetSOData :: !ZoomRaw
, packetSOSampleOffsets :: ![SampleOffset]
}
data Packet = Packet
{ packetTrack :: !TrackNo
, packetEntry :: !TimeStamp
, packetExit :: !TimeStamp
, packetCount :: !Int
, packetData :: !ZoomRaw
, packetTimeStamps :: ![TimeStamp]
}
packetFromPacketSO :: Rational -> PacketSO -> Packet
packetFromPacketSO r PacketSO{..} = Packet {
packetTrack = packetSOTrack
, packetEntry = timeStampFromSO r packetSOEntry
, packetExit = timeStampFromSO r packetSOExit
, packetCount = packetSOCount
, packetData = packetSOData
, packetTimeStamps = map (timeStampFromSO r) packetSOSampleOffsets
}
instance Timestampable Packet where
timestamp = Just . packetEntry
data SummarySO a = SummarySO
{ summarySOTrack :: !TrackNo
, summarySOLevel :: !Int
, summarySOEntry :: !SampleOffset
, summarySOExit :: !SampleOffset
, summarySOData :: !(SummaryData a)
}
deriving (Typeable)
summarySODuration :: SummarySO a -> SampleOffsetDiff
summarySODuration s = SODiff $ (unSO $ summarySOExit s) (unSO $ summarySOEntry s)
data Summary a = Summary
{ summaryTrack :: !TrackNo
, summaryLevel :: !Int
, summaryEntry :: !TimeStamp
, summaryExit :: !TimeStamp
, summaryData :: !(SummaryData a)
}
deriving (Typeable)
summaryFromSummarySO :: Rational -> SummarySO a -> Summary a
summaryFromSummarySO r SummarySO{..} = Summary {
summaryTrack = summarySOTrack
, summaryLevel = summarySOLevel
, summaryEntry = timeStampFromSO r summarySOEntry
, summaryExit = timeStampFromSO r summarySOExit
, summaryData = summarySOData
}
instance Timestampable (Summary a) where
timestamp = Just . summaryEntry
class Typeable a => ZoomReadable a where
data SummaryData a :: *
trackIdentifier :: a -> ByteString
readRaw :: (Functor m, Monad m)
=> Iteratee ByteString m a
readSummary :: (Functor m, Monad m)
=> Iteratee ByteString m (SummaryData a)
prettyRaw :: a -> String
prettySummaryData :: SummaryData a -> String
deltaDecodeRaw :: [a] -> [a]
deltaDecodeRaw = id
data ZoomRaw = forall a . ZoomReadable a => ZoomRaw [a]
data ZoomSummarySO = forall a . ZoomReadable a => ZoomSummarySO (SummarySO a)
data ZoomSummary = forall a . ZoomReadable a => ZoomSummary (Summary a)
instance Timestampable ZoomSummary where
timestamp (ZoomSummary s) = timestamp s
class ZoomReadable a => ZoomWritable a where
data SummaryWork a :: *
fromRaw :: a -> Builder
fromSummaryData :: SummaryData a -> Builder
initSummaryWork :: SampleOffset -> SummaryWork a
updateSummaryData :: SampleOffset -> a
-> SummaryWork a
-> SummaryWork a
toSummaryData :: SampleOffsetDiff -> SummaryWork a -> SummaryData a
appendSummaryData :: SampleOffsetDiff -> SummaryData a
-> SampleOffsetDiff -> SummaryData a
-> SummaryData a
deltaEncodeRaw :: SummaryWork a -> a -> a
deltaEncodeRaw _ = id
data ZoomWork = forall a . (Typeable a, ZoomWritable a) => ZoomWork
{ levels :: IntMap (SummarySO a)
, currWork :: Maybe (SummaryWork a)
}