{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.GstBase.Functions
    (

 -- * Methods
-- ** typeFindHelper #method:typeFindHelper#

    typeFindHelper                          ,


-- ** typeFindHelperForBuffer #method:typeFindHelperForBuffer#

    typeFindHelperForBuffer                 ,


-- ** typeFindHelperForBufferWithExtension #method:typeFindHelperForBufferWithExtension#

    typeFindHelperForBufferWithExtension    ,


-- ** typeFindHelperForData #method:typeFindHelperForData#

    typeFindHelperForData                   ,


-- ** typeFindHelperForDataWithExtension #method:typeFindHelperForDataWithExtension#

    typeFindHelperForDataWithExtension      ,


-- ** typeFindHelperForExtension #method:typeFindHelperForExtension#

    typeFindHelperForExtension              ,


-- ** typeFindHelperGetRange #method:typeFindHelperGetRange#

    typeFindHelperGetRange                  ,


-- ** typeFindHelperGetRangeFull #method:typeFindHelperGetRangeFull#

    typeFindHelperGetRangeFull              ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL

import qualified GI.Gst.Enums as Gst.Enums
import qualified GI.Gst.Objects.Object as Gst.Object
import qualified GI.Gst.Objects.Pad as Gst.Pad
import qualified GI.Gst.Structs.Buffer as Gst.Buffer
import qualified GI.Gst.Structs.Caps as Gst.Caps
import qualified GI.GstBase.Callbacks as GstBase.Callbacks

-- function gst_type_find_helper_get_range_full
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #GstObject that will be passed as first argument to @func", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "parent", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "the parent of @obj or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "func", argType = TInterface (Name {namespace = "GstBase", name = "TypeFindHelperGetRangeFunction"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A generic #GstTypeFindHelperGetRangeFunction that will\n       be used to access data at random offsets when doing the typefinding", sinceVersion = Nothing}, argScope = ScopeTypeCall, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The length in bytes", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "extension", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "extension of the media, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "caps", argType = TInterface (Name {namespace = "Gst", name = "Caps"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "returned caps", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "FlowReturn"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_get_range_full" gst_type_find_helper_get_range_full ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Gst.Object.Object ->                -- parent : TInterface (Name {namespace = "Gst", name = "Object"})
    FunPtr GstBase.Callbacks.C_TypeFindHelperGetRangeFunction -> -- func : TInterface (Name {namespace = "GstBase", name = "TypeFindHelperGetRangeFunction"})
    Word64 ->                               -- size : TBasicType TUInt64
    CString ->                              -- extension : TBasicType TUTF8
    Ptr (Ptr Gst.Caps.Caps) ->              -- caps : TInterface (Name {namespace = "Gst", name = "Caps"})
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO CInt

{- |
Utility function to do pull-based typefinding. Unlike 'GI.GstBase.Functions.typeFindHelper'
however, this function will use the specified function /@func@/ to obtain the
data needed by the typefind functions, rather than operating on a given
source pad. This is useful mostly for elements like tag demuxers which
strip off data at the beginning and\/or end of a file and want to typefind
the stripped data stream before adding their own source pad (the specified
callback can then call the upstream peer pad with offsets adjusted for the
tag size, for example).

When /@extension@/ is not 'Nothing', this function will first try the typefind
functions for the given extension, which might speed up the typefinding
in many cases.

/Since: 1.14.3/
-}
typeFindHelperGetRangeFull ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a, Gst.Object.IsObject b) =>
    a
    {- ^ /@obj@/: A 'GI.Gst.Objects.Object.Object' that will be passed as first argument to /@func@/ -}
    -> Maybe (b)
    {- ^ /@parent@/: the parent of /@obj@/ or 'Nothing' -}
    -> GstBase.Callbacks.TypeFindHelperGetRangeFunction
    {- ^ /@func@/: A generic 'GI.GstBase.Callbacks.TypeFindHelperGetRangeFunction' that will
       be used to access data at random offsets when doing the typefinding -}
    -> Word64
    {- ^ /@size@/: The length in bytes -}
    -> Maybe (T.Text)
    {- ^ /@extension@/: extension of the media, or 'Nothing' -}
    -> m ((Gst.Enums.FlowReturn, Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the last @/GstFlowReturn/@ from pulling a buffer or 'GI.Gst.Enums.FlowReturnOk' if
         typefinding was successful. -}
typeFindHelperGetRangeFull obj parent func size extension = liftIO $ do
    obj' <- unsafeManagedPtrCastPtr obj
    maybeParent <- case parent of
        Nothing -> return nullPtr
        Just jParent -> do
            jParent' <- unsafeManagedPtrCastPtr jParent
            return jParent'
    func' <- GstBase.Callbacks.mk_TypeFindHelperGetRangeFunction (GstBase.Callbacks.wrap_TypeFindHelperGetRangeFunction Nothing func)
    maybeExtension <- case extension of
        Nothing -> return nullPtr
        Just jExtension -> do
            jExtension' <- textToCString jExtension
            return jExtension'
    caps <- allocMem :: IO (Ptr (Ptr Gst.Caps.Caps))
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_get_range_full obj' maybeParent func' size maybeExtension caps prob
    let result' = (toEnum . fromIntegral) result
    caps' <- peek caps
    caps'' <- (wrapBoxed Gst.Caps.Caps) caps'
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    safeFreeFunPtr $ castFunPtrToPtr func'
    touchManagedPtr obj
    whenJust parent touchManagedPtr
    freeMem maybeExtension
    freeMem caps
    freeMem prob
    return (result', caps'', prob'')


-- function gst_type_find_helper_get_range
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #GstObject that will be passed as first argument to @func", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "parent", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "the parent of @obj or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "func", argType = TInterface (Name {namespace = "GstBase", name = "TypeFindHelperGetRangeFunction"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A generic #GstTypeFindHelperGetRangeFunction that will\n       be used to access data at random offsets when doing the typefinding", sinceVersion = Nothing}, argScope = ScopeTypeCall, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The length in bytes", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "extension", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "extension of the media, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_get_range" gst_type_find_helper_get_range ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Gst.Object.Object ->                -- parent : TInterface (Name {namespace = "Gst", name = "Object"})
    FunPtr GstBase.Callbacks.C_TypeFindHelperGetRangeFunction -> -- func : TInterface (Name {namespace = "GstBase", name = "TypeFindHelperGetRangeFunction"})
    Word64 ->                               -- size : TBasicType TUInt64
    CString ->                              -- extension : TBasicType TUTF8
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO (Ptr Gst.Caps.Caps)

{- |
Utility function to do pull-based typefinding. Unlike 'GI.GstBase.Functions.typeFindHelper'
however, this function will use the specified function /@func@/ to obtain the
data needed by the typefind functions, rather than operating on a given
source pad. This is useful mostly for elements like tag demuxers which
strip off data at the beginning and\/or end of a file and want to typefind
the stripped data stream before adding their own source pad (the specified
callback can then call the upstream peer pad with offsets adjusted for the
tag size, for example).

When /@extension@/ is not 'Nothing', this function will first try the typefind
functions for the given extension, which might speed up the typefinding
in many cases.

Free-function: gst_caps_unref
-}
typeFindHelperGetRange ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a, Gst.Object.IsObject b) =>
    a
    {- ^ /@obj@/: A 'GI.Gst.Objects.Object.Object' that will be passed as first argument to /@func@/ -}
    -> Maybe (b)
    {- ^ /@parent@/: the parent of /@obj@/ or 'Nothing' -}
    -> GstBase.Callbacks.TypeFindHelperGetRangeFunction
    {- ^ /@func@/: A generic 'GI.GstBase.Callbacks.TypeFindHelperGetRangeFunction' that will
       be used to access data at random offsets when doing the typefinding -}
    -> Word64
    {- ^ /@size@/: The length in bytes -}
    -> Maybe (T.Text)
    {- ^ /@extension@/: extension of the media, or 'Nothing' -}
    -> m ((Maybe Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data
    stream.  Returns 'Nothing' if no 'GI.Gst.Structs.Caps.Caps' matches the data stream. -}
typeFindHelperGetRange obj parent func size extension = liftIO $ do
    obj' <- unsafeManagedPtrCastPtr obj
    maybeParent <- case parent of
        Nothing -> return nullPtr
        Just jParent -> do
            jParent' <- unsafeManagedPtrCastPtr jParent
            return jParent'
    func' <- GstBase.Callbacks.mk_TypeFindHelperGetRangeFunction (GstBase.Callbacks.wrap_TypeFindHelperGetRangeFunction Nothing func)
    maybeExtension <- case extension of
        Nothing -> return nullPtr
        Just jExtension -> do
            jExtension' <- textToCString jExtension
            return jExtension'
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_get_range obj' maybeParent func' size maybeExtension prob
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    safeFreeFunPtr $ castFunPtrToPtr func'
    touchManagedPtr obj
    whenJust parent touchManagedPtr
    freeMem maybeExtension
    freeMem prob
    return (maybeResult, prob'')


-- function gst_type_find_helper_for_extension
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "object doing the typefinding, or %NULL (used for logging)", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "extension", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an extension", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_for_extension" gst_type_find_helper_for_extension ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    CString ->                              -- extension : TBasicType TUTF8
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find the best 'GI.Gst.Structs.Caps.Caps' associated with /@extension@/.

All available typefinders will be checked against the extension in order
of rank. The caps of the first typefinder that can handle /@extension@/ will be
returned.

Free-function: gst_caps_unref
-}
typeFindHelperForExtension ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a) =>
    Maybe (a)
    {- ^ /@obj@/: object doing the typefinding, or 'Nothing' (used for logging) -}
    -> T.Text
    {- ^ /@extension@/: an extension -}
    -> m (Maybe Gst.Caps.Caps)
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to
    /@extension@/, or 'Nothing' if no type could be found. The caller should free
    the caps returned with @/gst_caps_unref()/@. -}
typeFindHelperForExtension obj extension = liftIO $ do
    maybeObj <- case obj of
        Nothing -> return nullPtr
        Just jObj -> do
            jObj' <- unsafeManagedPtrCastPtr jObj
            return jObj'
    extension' <- textToCString extension
    result <- gst_type_find_helper_for_extension maybeObj extension'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    whenJust obj touchManagedPtr
    freeMem extension'
    return maybeResult


-- function gst_type_find_helper_for_data_with_extension
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "object doing the typefinding, or %NULL (used for logging)", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "* a pointer with data to typefind", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "extension", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "extension of the media, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_for_data_with_extension" gst_type_find_helper_for_data_with_extension ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Word8 ->                            -- data : TCArray False (-1) 2 (TBasicType TUInt8)
    Word64 ->                               -- size : TBasicType TUInt64
    CString ->                              -- extension : TBasicType TUTF8
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find what type of data is contained in the given /@data@/, the
assumption being that the data represents the beginning of the stream or
file.

All available typefinders will be called on the data in order of rank. If
a typefinding function returns a probability of 'GI.Gst.Enums.TypeFindProbabilityMaximum',
typefinding is stopped immediately and the found caps will be returned
right away. Otherwise, all available typefind functions will the tried,
and the caps with the highest probability will be returned, or 'Nothing' if
the content of /@data@/ could not be identified.

When /@extension@/ is not 'Nothing', this function will first try the typefind
functions for the given extension, which might speed up the typefinding
in many cases.

Free-function: gst_caps_unref

/Since: 1.16/
-}
typeFindHelperForDataWithExtension ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a) =>
    Maybe (a)
    {- ^ /@obj@/: object doing the typefinding, or 'Nothing' (used for logging) -}
    -> ByteString
    {- ^ /@data@/: * a pointer with data to typefind -}
    -> Maybe (T.Text)
    {- ^ /@extension@/: extension of the media, or 'Nothing' -}
    -> m ((Maybe Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data,
    or 'Nothing' if no type could be found. The caller should free the caps
    returned with @/gst_caps_unref()/@. -}
typeFindHelperForDataWithExtension obj data_ extension = liftIO $ do
    let size = fromIntegral $ B.length data_
    maybeObj <- case obj of
        Nothing -> return nullPtr
        Just jObj -> do
            jObj' <- unsafeManagedPtrCastPtr jObj
            return jObj'
    data_' <- packByteString data_
    maybeExtension <- case extension of
        Nothing -> return nullPtr
        Just jExtension -> do
            jExtension' <- textToCString jExtension
            return jExtension'
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_for_data_with_extension maybeObj data_' size maybeExtension prob
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    whenJust obj touchManagedPtr
    freeMem data_'
    freeMem maybeExtension
    freeMem prob
    return (maybeResult, prob'')


-- function gst_type_find_helper_for_data
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "object doing the typefinding, or %NULL (used for logging)", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "* a pointer with data to typefind", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_for_data" gst_type_find_helper_for_data ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Word8 ->                            -- data : TCArray False (-1) 2 (TBasicType TUInt8)
    Word64 ->                               -- size : TBasicType TUInt64
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find what type of data is contained in the given /@data@/, the
assumption being that the data represents the beginning of the stream or
file.

All available typefinders will be called on the data in order of rank. If
a typefinding function returns a probability of 'GI.Gst.Enums.TypeFindProbabilityMaximum',
typefinding is stopped immediately and the found caps will be returned
right away. Otherwise, all available typefind functions will the tried,
and the caps with the highest probability will be returned, or 'Nothing' if
the content of /@data@/ could not be identified.

Free-function: gst_caps_unref
-}
typeFindHelperForData ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a) =>
    Maybe (a)
    {- ^ /@obj@/: object doing the typefinding, or 'Nothing' (used for logging) -}
    -> ByteString
    {- ^ /@data@/: * a pointer with data to typefind -}
    -> m ((Maybe Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data,
    or 'Nothing' if no type could be found. The caller should free the caps
    returned with @/gst_caps_unref()/@. -}
typeFindHelperForData obj data_ = liftIO $ do
    let size = fromIntegral $ B.length data_
    maybeObj <- case obj of
        Nothing -> return nullPtr
        Just jObj -> do
            jObj' <- unsafeManagedPtrCastPtr jObj
            return jObj'
    data_' <- packByteString data_
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_for_data maybeObj data_' size prob
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    whenJust obj touchManagedPtr
    freeMem data_'
    freeMem prob
    return (maybeResult, prob'')


-- function gst_type_find_helper_for_buffer_with_extension
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "object doing the typefinding, or %NULL (used for logging)", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "buf", argType = TInterface (Name {namespace = "Gst", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GstBuffer with data to typefind", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "extension", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "extension of the media, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_for_buffer_with_extension" gst_type_find_helper_for_buffer_with_extension ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Gst.Buffer.Buffer ->                -- buf : TInterface (Name {namespace = "Gst", name = "Buffer"})
    CString ->                              -- extension : TBasicType TUTF8
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find what type of data is contained in the given 'GI.Gst.Structs.Buffer.Buffer', the
assumption being that the buffer represents the beginning of the stream or
file.

All available typefinders will be called on the data in order of rank. If
a typefinding function returns a probability of 'GI.Gst.Enums.TypeFindProbabilityMaximum',
typefinding is stopped immediately and the found caps will be returned
right away. Otherwise, all available typefind functions will the tried,
and the caps with the highest probability will be returned, or 'Nothing' if
the content of the buffer could not be identified.

When /@extension@/ is not 'Nothing', this function will first try the typefind
functions for the given extension, which might speed up the typefinding
in many cases.

Free-function: gst_caps_unref

/Since: 1.16/
-}
typeFindHelperForBufferWithExtension ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a) =>
    Maybe (a)
    {- ^ /@obj@/: object doing the typefinding, or 'Nothing' (used for logging) -}
    -> Gst.Buffer.Buffer
    {- ^ /@buf@/: a 'GI.Gst.Structs.Buffer.Buffer' with data to typefind -}
    -> Maybe (T.Text)
    {- ^ /@extension@/: extension of the media, or 'Nothing' -}
    -> m ((Maybe Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data,
    or 'Nothing' if no type could be found. The caller should free the caps
    returned with @/gst_caps_unref()/@. -}
typeFindHelperForBufferWithExtension obj buf extension = liftIO $ do
    maybeObj <- case obj of
        Nothing -> return nullPtr
        Just jObj -> do
            jObj' <- unsafeManagedPtrCastPtr jObj
            return jObj'
    buf' <- unsafeManagedPtrGetPtr buf
    maybeExtension <- case extension of
        Nothing -> return nullPtr
        Just jExtension -> do
            jExtension' <- textToCString jExtension
            return jExtension'
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_for_buffer_with_extension maybeObj buf' maybeExtension prob
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    whenJust obj touchManagedPtr
    touchManagedPtr buf
    freeMem maybeExtension
    freeMem prob
    return (maybeResult, prob'')


-- function gst_type_find_helper_for_buffer
-- Args : [Arg {argCName = "obj", argType = TInterface (Name {namespace = "Gst", name = "Object"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "object doing the typefinding, or %NULL (used for logging)", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "buf", argType = TInterface (Name {namespace = "Gst", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GstBuffer with data to typefind", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prob", argType = TInterface (Name {namespace = "Gst", name = "TypeFindProbability"}), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to store the probability of the found\n    caps, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper_for_buffer" gst_type_find_helper_for_buffer ::
    Ptr Gst.Object.Object ->                -- obj : TInterface (Name {namespace = "Gst", name = "Object"})
    Ptr Gst.Buffer.Buffer ->                -- buf : TInterface (Name {namespace = "Gst", name = "Buffer"})
    Ptr CUInt ->                            -- prob : TInterface (Name {namespace = "Gst", name = "TypeFindProbability"})
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find what type of data is contained in the given 'GI.Gst.Structs.Buffer.Buffer', the
assumption being that the buffer represents the beginning of the stream or
file.

All available typefinders will be called on the data in order of rank. If
a typefinding function returns a probability of 'GI.Gst.Enums.TypeFindProbabilityMaximum',
typefinding is stopped immediately and the found caps will be returned
right away. Otherwise, all available typefind functions will the tried,
and the caps with the highest probability will be returned, or 'Nothing' if
the content of the buffer could not be identified.

Free-function: gst_caps_unref
-}
typeFindHelperForBuffer ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Object.IsObject a) =>
    Maybe (a)
    {- ^ /@obj@/: object doing the typefinding, or 'Nothing' (used for logging) -}
    -> Gst.Buffer.Buffer
    {- ^ /@buf@/: a 'GI.Gst.Structs.Buffer.Buffer' with data to typefind -}
    -> m ((Maybe Gst.Caps.Caps, Gst.Enums.TypeFindProbability))
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data,
    or 'Nothing' if no type could be found. The caller should free the caps
    returned with @/gst_caps_unref()/@. -}
typeFindHelperForBuffer obj buf = liftIO $ do
    maybeObj <- case obj of
        Nothing -> return nullPtr
        Just jObj -> do
            jObj' <- unsafeManagedPtrCastPtr jObj
            return jObj'
    buf' <- unsafeManagedPtrGetPtr buf
    prob <- allocMem :: IO (Ptr CUInt)
    result <- gst_type_find_helper_for_buffer maybeObj buf' prob
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    prob' <- peek prob
    let prob'' = (toEnum . fromIntegral) prob'
    whenJust obj touchManagedPtr
    touchManagedPtr buf
    freeMem prob
    return (maybeResult, prob'')


-- function gst_type_find_helper
-- Args : [Arg {argCName = "src", argType = TInterface (Name {namespace = "Gst", name = "Pad"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A source #GstPad", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The length in bytes", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_type_find_helper" gst_type_find_helper ::
    Ptr Gst.Pad.Pad ->                      -- src : TInterface (Name {namespace = "Gst", name = "Pad"})
    Word64 ->                               -- size : TBasicType TUInt64
    IO (Ptr Gst.Caps.Caps)

{- |
Tries to find what type of data is flowing from the given source 'GI.Gst.Objects.Pad.Pad'.

Free-function: gst_caps_unref
-}
typeFindHelper ::
    (B.CallStack.HasCallStack, MonadIO m, Gst.Pad.IsPad a) =>
    a
    {- ^ /@src@/: A source 'GI.Gst.Objects.Pad.Pad' -}
    -> Word64
    {- ^ /@size@/: The length in bytes -}
    -> m (Maybe Gst.Caps.Caps)
    {- ^ __Returns:__ the 'GI.Gst.Structs.Caps.Caps' corresponding to the data
    stream.  Returns 'Nothing' if no 'GI.Gst.Structs.Caps.Caps' matches the data stream. -}
typeFindHelper src size = liftIO $ do
    src' <- unsafeManagedPtrCastPtr src
    result <- gst_type_find_helper src' size
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    touchManagedPtr src
    return maybeResult