FFmpegInteropX
FFmpegInteropX copied to clipboard
[Help wanted] Is it possible to throw specified exception?
FFmpegMediaSource.CreateFromUriAsync
always throws unspecified error if there is something wrong:
System.Exception: Unspecified error (Exception from HRESULT: 0x80004005)
It might be HTTP 404/403 or something else and it is hard to handle these exceptions.
FFplay reports these detail errors, and I am wondering is it possible for FFmpegInteropX to throw specified errors like FFplay.
Unfortunately, the only kind of exceptions we can throw from C++/WinRT is HRESULT exceptions. We also cannot set any error messages or details. This leaves us with a very limited set of exception options. A few very common things like invalid argument are possible, but not nearly detailed enough to report the actual error situation.
We could declare our own HRESULT values. But I think it won't be very helpful, because you'd have to manually lookup which code means which error.
Also from ffmpeg side, it is often not easy to find out programmatically what has really gone wrong. Detailed error messages are often only output as log messages. You will find them in debug output of the app, if you are running in debugger. You could also register a log handler, to get the ffmpeg log output into an app's log file, when debugger is not attached (see FFmpegInteropLogging class).
Actually it looks like we can provide custom exception messages! I only never saw it, because the constructor overloads do not show up on intellisense (WTF)?!
This will show an exception with message "Test" in C# Sample:
throw winrt::hresult_error(E_FAIL, L"Test");
C++/CX also allowed to set custom messages, but they would never show on C# side, so I did not bother using them. Now that this seems to work in C++/WinRT, we could indeed be moving from HRESULT based model to throwing detailed exceptions, especially in the constructor methods. We can also use some of the HRESULT values from mferror.h (MF_E_*).
Actually it looks like we can provide custom exception messages! I only never saw it, because the constructor overloads do not show up on intellisense (WTF)?!
This will show an exception with message "Test" in C# Sample:
throw winrt::hresult_error(E_FAIL, L"Test");
C++/CX also allowed to set custom messages, but they would never show on C# side, so I did not bother using them. Now that this seems to work in C++/WinRT, we could indeed be moving from HRESULT based model to throwing detailed exceptions, especially in the constructor methods. We can also use some of the HRESULT values from mferror.h (MF_E_*).
It sounds great! Thank you for the detailed explanation.