HaskellからGStreamerを叩く

-- Main.hs

module Main where

import qualified System.Glib.MainLoop as G
import qualified Media.Streaming.GStreamer.Core as Gst

maybeFail :: String -> Maybe a -> IO a
maybeFail message = maybe (fail message) return

busCall loop bus message = do
    case (Gst.messageType message) of
        Gst.MessageEOS -> G.mainLoopQuit loop
        Gst.MessageError -> do
            err <- maybeFail "no error message" $ Gst.messageParseError message
            putStrLn $ show err
            G.mainLoopQuit loop
        _ -> return ()
    return True

main = do
    Gst.init
    loop <- G.mainLoopNew Nothing False
    -- Gst.pipelineNew は何故か Gst.Element を返すのでキャストが要る
    pipeline <- Gst.pipelineNew "pipeline0" >>= return . Gst.castToPipeline
    source  <- Gst.elementFactoryMake "videotestsrc"  (Just "source") >>= maybeFail "cannot open videotestsrc"
    sink    <- Gst.elementFactoryMake "autovideosink" (Just "output") >>= maybeFail "cannot open autovideosink"
    bus <- Gst.pipelineGetBus pipeline
    Gst.busAddWatch bus G.priorityDefault (busCall loop)
    sequence_ $ map (Gst.binAdd pipeline) [source, sink]
    Gst.elementLink source sink
    Gst.elementSetState pipeline Gst.StatePlaying
    G.mainLoopRun loop
    Gst.elementSetState pipeline Gst.StateNull
    return ()

誰か使いやすいラッパーライブラリを書くべきだ