packages feed

sandwich-webdriver-0.2.2.0: src/Test/Sandwich/WebDriver/Internal/Video.hs

{-# LANGUAGE CPP #-}

module Test.Sandwich.WebDriver.Internal.Video where

import Control.Monad.IO.Class
import Data.Maybe
import Data.String.Interpolate
import System.Environment
import System.Process
import Test.Sandwich.WebDriver.Internal.Types

#ifdef darwin_HOST_OS
import Safe

getMacScreenNumber :: IO (Maybe Int)
getMacScreenNumber = undefined
#endif


getVideoArgs path (width, height, x, y) (VideoSettings {..}) maybeXvfbSession = do
#ifdef linux_HOST_OS
  displayNum <- case maybeXvfbSession of
    Nothing -> fromMaybe "" <$> (liftIO $ lookupEnv "DISPLAY")
    Just (XvfbSession {xvfbDisplayNum}) -> return $ ":" <> show xvfbDisplayNum

  let videoPath = [i|#{path}.avi|]
  let env' = [("DISPLAY", displayNum)]
  let env = case maybeXvfbSession of
       Nothing -> Just env'
       Just (XvfbSession {xvfbXauthority}) -> Just (("XAUTHORITY", xvfbXauthority) : env')
  let cmd = ["-draw_mouse", (if hideMouseWhenRecording then "0" else "1")
            , "-y"
            , "-nostdin"
            , "-f", "x11grab"
            , "-s", [i|#{width}x#{height}|]
            , "-i", [i|#{displayNum}.0+#{x},#{y}|]]
            ++ x11grabOptions
            ++ [videoPath]
  return ((proc "ffmpeg" cmd) { env = env })
#endif

#ifdef darwin_HOST_OS
  maybeScreenNumber <- liftIO getMacScreenNumber
  let videoPath = [i|#{path}.avi|]
  let cmd = case maybeScreenNumber of
        Just screenNumber -> ["-y"
                             , "-nostdin"
                             , "-f", "avfoundation"
                             , "-i", [i|#{screenNumber}|]]
                             ++ avfoundationOptions
                             ++ [videoPath]
        Nothing -> error [i|Not launching ffmpeg since OS X screen number couldn't be determined.|]
  return ((proc "ffmpeg" cmd) { env = Nothing })
#endif

#ifdef mingw32_HOST_OS
  let videoPath = [i|#{path}.mkv|]
  let cmd = ["-f", "gdigrab"
            , "-nostdin"
            , "-draw_mouse", (if hideMouseWhenRecording then "0" else "1")
            , "-i", "desktop"]
            ++ gdigrabOptions
            ++ [videoPath]
  return ((proc "ffmpeg.exe" cmd) { env = Nothing })
#endif