python-vlc icon indicating copy to clipboard operation
python-vlc copied to clipboard

Call libvlc_log_get_context invoke exception.

Open fingul opened this issue 7 years ago • 4 comments

Thanks for great app.

When call libvlc_log_get_context invoke exception.

I think, in below line, ListPOINTER must be replaced with ctypes.POINTER.

https://github.com/oaubert/python-vlc/blob/master/generated/vlc.py#L4363

  • traceback
Traceback (most recent call last):
    File "_ctypes/callbacks.c", line 234, in 'calling callback function'
    File "/Users/m/stream_monitor_client/__vlc_no_audio/test_vlc.py", line 78, in log_handler
        print(vlc.libvlc_log_get_context(ctx))
    File "/Users/m/anaconda3/envs/sm/lib/python3.6/site-packages/vlc.py", line 4344, in libvlc_log_get_context
        None, Log_ptr, ListPOINTER(ctypes.c_char_p), ListPOINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_uint))
    File "/Users/m/anaconda3/envs/sm/lib/python3.6/site-packages/vlc.py", line 243, in _Cfunction
        f = p((name, dll), flags)
    TypeError: 'out' parameter 2 must be a pointer type, not ListPOINTER

Have a nice day!

fingul avatar Feb 05 '17 16:02 fingul

ListPOINTER is used to simplify passing a list of elements as parameters. I have some fixes in mind, but do you have a small test case that I could run against? It would help ensuring it works.

oaubert avatar Feb 24 '17 22:02 oaubert

Here is some test case. Thanks!

on log callback handler, it need to use libc. I`ve test @ linux and macOS / python 3.6

  • call test_callback(log_handler) -> print result
  • call test_callback(log_handler_need_patch) -> will be raise exception
import logging
import platform
import ctypes
import time
import vlc
from vlc import _Cfunction, _Cfunctions, Log_ptr

logging.basicConfig(level=logging.DEBUG)
libc = ctypes.cdll.LoadLibrary("libc.{}".format("so.6" if platform.uname()[0] != "Darwin" else "dylib"))


# https://github.com/oaubert/python-vlc/issues/25
def libvlc_log_get_context_patch(ctx):
    # patch of vlc.libvlc_log_get_context(ctx)
    f = _Cfunctions.get('libvlc_log_get_context', None) or \
        _Cfunction('libvlc_log_get_context', ((1,), (2,), (2,), (2,),), None,
                   None, Log_ptr, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_char_p),
                   ctypes.POINTER(ctypes.c_uint))
    return f(ctx)


@vlc.CallbackDecorators.LogCb
def log_handler(instance, log_level, ctx, fmt, va_list):
    bufferString = ctypes.create_string_buffer(4096)
    libc.vsprintf(bufferString, fmt, ctypes.cast(va_list, ctypes.c_void_p))
    msg = bufferString.value.decode('utf8')
    module, _file, _line = libvlc_log_get_context_patch(ctx)
    module = module.decode('utf8')
    logging.info(
        "log_level={log_level}, module={module}, msg={msg}".format(log_level=log_level, module=module, msg=msg))


@vlc.CallbackDecorators.LogCb
def log_handler_need_patch(instance, log_level, ctx, fmt, va_list):
    try:
        module, _file, _line = vlc.libvlc_log_get_context(ctx)
    except TypeError:
        logging.exception("vlc.libvlc_log_get_context(ctx)")


def test_callback(cb):
    url = 'rtmp://184.72.239.149/vod/BigBuckBunny_115k.mov'

    instance = vlc.Instance('--vout dummy --aout dummy')
    # instance.log_set(log_handler, None)
    instance.log_set(cb, None)
    player = instance.media_player_new()
    media = instance.media_new(url)
    player.set_media(media)
    player.play()
    time.sleep(3)


if __name__ == '__main__':
    test_callback(log_handler)
    # test_callback(log_handler_need_patch)

fingul avatar Feb 28 '17 21:02 fingul

Got the same issue with the latest version by now. Are there any changes it will be fixed?

For now, ended up with this:

_libvlc_log_get_context_type = ctypes.CFUNCTYPE(
    None, vlc.Log_ptr, ctypes.POINTER(ctypes.c_char_p),
    ctypes.POINTER(ctypes.c_char_p),
    ctypes.POINTER(ctypes.c_uint),
    )
_libvlc_log_get_context = _libvlc_log_get_context_type(
    ('libvlc_log_get_context', vlc.dll),
    ((1,), (2,), (2,), (2,),),
    )

Thank you, @fingul.

sovetov avatar Mar 21 '19 16:03 sovetov

It is half-fixed now: the correct type mapping is generated for the function. But the test function (disabled for now in the tests/test.py file) crashes after displaying some messages. If you manage to find the error, I will gladly merge the outcome.

oaubert avatar Apr 04 '19 12:04 oaubert