From: Duncan C. <dun...@wo...> - 2007-10-30 00:35:45
|
Sun Oct 21 12:28:36 PDT 2007 Peter Gavin <pg...@gm...> * gstreamer: add vorbis-play demo adddir ./demo/gstreamer addfile ./demo/gstreamer/Makefile hunk ./demo/gstreamer/Makefile 1 + +PROGS = vorbis-play +SOURCES = VorbisPlay.hs + +all: $(PROGS) + +vorbis-play : VorbisPlay.hs + $(HC_RULE) + +HC_RULE = $(HC) --make $< -o $@ $(HCFLAGS) + +clean: + rm -f $(SOURCES:.hs=.hi) $(SOURCES:.hs=.o) $(PROGS) + +HC=ghc addfile ./demo/gstreamer/VorbisPlay.hs hunk ./demo/gstreamer/VorbisPlay.hs 1 +import System.Exit +import Data.Maybe +import qualified Media.Streaming.GStreamer as Gst +import qualified System.Glib as G +import qualified System.Glib.MainLoop as G +import qualified System.Glib.Properties as G +import qualified System.Glib.GError as G +import Control.Monad +import System.IO +import System + +mkElement action = + do element <- action + case element of + Just element' -> + return element' + Nothing -> [_$_] + do hPutStrLn stderr "could not create all GStreamer elements\n" + exitFailure + +main = + do args <- getArgs + when (length args /= 1) $ + do hPutStrLn stderr "Usage: vorbis-play <Ogg/Vorbis filename>\n" + exitFailure + [_$_] + Gst.init + [_$_] + mainLoop <- G.mainLoopNew Nothing True + [_$_] + pipeline <- Gst.pipelineNew "audio-player" + source <- mkElement $ Gst.elementFactoryMake "filesrc" "file-source" + parser <- mkElement $ Gst.elementFactoryMake "oggdemux" "ogg-parser" + decoder <- mkElement $ Gst.elementFactoryMake "vorbisdec" "vorbis-decoder" + conv <- mkElement $ Gst.elementFactoryMake "audioconvert" "convert" + sink <- mkElement $ Gst.elementFactoryMake "alsasink" "alsa-output" + [_$_] + let elements = [source, parser, decoder, conv, sink] + [_$_] + G.objectSetPropertyString "location" source (head args) + [_$_] + bus <- Gst.pipelineGetBus (Gst.castToPipeline pipeline) + Gst.busAddWatch bus G.priorityDefault $ \bus message -> + do case Gst.messageType message of + Gst.MessageEOS -> + do putStrLn "end of stream" + G.mainLoopQuit mainLoop + Gst.MessageError -> + let G.GError _ _ msg = fst $ fromJust $ Gst.messageParseError message + messageStr = "Error: " ++ msg + in do hPutStrLn stderr messageStr + G.mainLoopQuit mainLoop + _ -> return () + return True + [_$_] + mapM_ (Gst.binAdd $ Gst.castToBin pipeline) elements + [_$_] + Gst.elementLink source parser + Gst.elementLink decoder conv + Gst.elementLink conv sink + [_$_] + Gst.onElementPadAdded parser $ \pad -> + do sinkPad <- Gst.elementGetPad decoder "sink" + Gst.padLink pad $ fromJust sinkPad + return () + [_$_] + Gst.elementSetState pipeline Gst.StatePlaying + [_$_] + G.mainLoopRun mainLoop + [_$_] + Gst.elementSetState pipeline Gst.StateNull + [_$_] + return () |