JSON representation problem with liberator - clojure

I'm trying to define a really simple resource with Liberator. To start off a minimal example, I wrote an handler like this:
(resource
:handle-ok {:hello "world"})
But despite clearly passing a value (that isn't nil) I got the following exception:
java.lang.IllegalArgumentException: No method in multimethod 'render-map-generic' for dispatch value: null.
And that issue has been present in my actual resource too, so I tried to be explicit about how it should represent the map, and I included: :available-media-types ["application/json"]:
(resource
:available-media-types ["application/json"]
:handle-ok {:hello "world"})
Now it works (surprised me I had to do that), but now whenever I indicate the resource doesn't exist, I get a really weird error response (not an exception). And to demonstrate that:
(resource
:available-media-types ["application/json"]
:exists? false
:handle-ok {:hello "world"})
Now I'm getting a 404 (which I do appreciate), with the following response body:
There was an error parsing JSON data
Unexpected token R in JSON at position 0
And here's the x-liberator-trace:
:decision (:initialize-context {}); :decision (:service-available? true); :decision (:known-method? :get); :decision (:uri-too-long? false); :decision (:method-allowed? :get); :decision (:malformed? false); :decision (:authorized? true); :decision (:allowed? true); :decision (:valid-content-header? true); :decision (:known-content-type? true); :decision (:valid-entity-length? true); :decision (:is-options? false); :decision (:accept-exists? [false {:representation {:media-type "application/json"}}]); :decision (:accept-language-exists? nil); :decision (:accept-charset-exists? nil); :decision (:accept-encoding-exists? nil); :decision (:processable? true); :decision (:exists? false); :decision (:if-match-star-exists-for-missing? false); :decision (:method-put? false); :decision (:existed? false); :decision (:post-to-missing? false); :handler (:handle-not-found)
And you might suggest from it that I could just hack my way out and define a :handle-not-found handler, and I have, but it just gets completely overridden by this response.
I'm not sure if it's a bug or if I'm just not using Liberator right. With the example being so simple, I assume it's the latter. But on the other hand, when I use "application/edn" instead, it works flawlessly for it. I also wondered if it's a regression so I checked a couple of versions back but same results.

Related

MediaFoundation HEVC H265 encoding

I'm able to successfully encode H264 video using MediaFoundation.
Now I want to export HEVC which is, according to official documentation, is supported.
I am doing everything as described there but whenever I want to set input media type to the writer it ends up with an error:
No suitable transform was found to encode or decode the content.
Here is a short code of what i do:
/* Set output media type */
IMFMediaType* pMediaTypeOut = nullptr;
MFCreateMediaType(&pMediaTypeOut);
pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_HEVC);
MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, 640, 480);
MFSetAttributeRatio(pMediaTypeOut, MF_MT_FRAME_RATE, 30, 1);
pMediaTypeOut->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH265VProfile_Main_420_8);
pMediaTypeOut->SetUINT32(MF_MT_MPEG2_LEVEL, eAVEncH265VLevel1);
// Add it to the sink writer
m_pWriter->AddStream(pMediaTypeOut, &m_streamIndex);
/* Set input media type */
IMFMediaType* pMediaTypeIn = nullptr;
MFCreateMediaType(&pMediaTypeIn);
pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12);
MFSetAttributeSize(pMediaTypeIn, MF_MT_FRAME_SIZE, 640, 480);
MFSetAttributeRatio(pMediaTypeIn, MF_MT_FRAME_RATE, 30, 1),
MFSetAttributeRatio(pMediaTypeIn, MF_MT_PIXEL_ASPECT_RATIO, 1, 1),
// Here it ends up with an error
m_pWriter->SetInputMediaType(m_streamIndex, pMediaTypeIn, nullptr);
Does anyone know if the HEVC encoding is really supported or it is just a documentation.
And if it is really supported by Microsoft, then what am I doing wrong?
BTW: I am using latest version of Windows 10.
Error checking is omitted for code simplicity
Your output media type is incomplete:
MF_MT_AVG_BITRATE
Average encoded bit rate, in bits per second. Must be greater than zero.
Similar problem with MF_MT_INTERLACE_MODE.
Code snippet that works:
Check(MFStartup(MF_VERSION));
ComPtr<IMFSinkWriter> SinkWriter;
Check(MFCreateSinkWriterFromURL(L"D:\\temp.mp4", nullptr, nullptr, SinkWriter.GetAddressOf()));
ComPtr<IMFMediaType> OutputMediaType;
Check(MFCreateMediaType(OutputMediaType.GetAddressOf()));
Check(OutputMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Check(OutputMediaType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_HEVC));
Check(MFSetAttributeSize(OutputMediaType.Get(), MF_MT_FRAME_SIZE, 640, 480));
Check(OutputMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
Check(MFSetAttributeRatio(OutputMediaType.Get(), MF_MT_FRAME_RATE, 30, 1));
//Check(OutputMediaType->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH265VProfile_Main_420_8));
//Check(OutputMediaType->SetUINT32(MF_MT_MPEG2_LEVEL, eAVEncH265VLevel1));
Check(OutputMediaType->SetUINT32(MF_MT_AVG_BITRATE, 1000000));
DWORD StreamIndex;
Check(SinkWriter->AddStream(OutputMediaType.Get(), &StreamIndex));
ComPtr<IMFMediaType> InputMediaType;
Check(MFCreateMediaType(InputMediaType.GetAddressOf()));
Check(InputMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Check(InputMediaType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12));
Check(MFSetAttributeSize(InputMediaType.Get(), MF_MT_FRAME_SIZE, 640, 480));
Check(InputMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
Check(MFSetAttributeRatio(InputMediaType.Get(), MF_MT_FRAME_RATE, 30, 1)),
//Check(MFSetAttributeRatio(InputMediaType.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1)),
Check(SinkWriter->SetInputMediaType(StreamIndex, InputMediaType.Get(), nullptr));
Maybe you can check the existence of the h265 encoder first by DXVA Checker tool or programmatically use MFEnumEx(MFT_CATEGORY_VIDEO_ENCODER, ...) to lookup.
If not found, you can try installing the HEVC Video Extension on MS App Store. It seems MS has separated the MF HEVC codec into app package after 2018 Fall Creator update. Also, the HW HEVC Encoder needed be installed via vendor's VGA driver.

SDL_Delay() while getting events (kinda nooby)

C++, SDL 2
Im making a disco simulator thats just loops through pictures and plays music.
(Epilepsy warning) http://imgur.com/9ePOIAw
Basically I want to run this code
while (isPartying){
currentImage = image1;
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_Delay(25);
SDL_UpdateWindowSurface(window);
currentImage = image2;
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(25);
currentImage = image3;
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(25);
//image 3, 4, 5 and so on
}
while getting events all the time.
while (SDL_PollEvent(&ev) != 0){
if (ev.type == SDL_QUIT){
isPartying = false;
}
}
I want it to get events while Im in the middle of the isPartying loop. Now it only checks for events at the beginning (or the end, depends on where I put the event loop of course). Anyone know a better way to wait for the next picture then SDL_Delay()? Or maybe even have another solution
Ano
Basically, what you want to do is to achieve two things at a time.
You have two options:
Using SDL_Thread, but I prefere to not use threads when another solution is possible (because you may have to deal with semaphores or mutexes)
Using SDL_TimerCallback (some would say that too much timers kill the flow of the code, but it's what we gonna use)
Here is a code sample (NB: you must pass SDL_INIT_TIMER to SDL_Init()).
void IsPartying() {
SDL_Event ev;
while (SDL_PollEvent(&ev)){
if (ev.type == SDL_QUIT){
return false;
}
}
return true
}
uint32_t ChangeImage(uint32_t interval, void* param) {
int *imageNb = param;
(*imageNb)++
return interval
}
void Processing() {
// Store your images in SDL_Surface **imageArray
int imageNb = 0;
SDL_TimerID t = SDL_AddTimer(25, ChangeImage, &imageNb);
while (IsPartying()) {
SDL_BlitSurface(imageArray[imageNb], NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(1); // Release proc charge
}
SDL_RemoveTimer(SDL_TimerID);
}
Of course you still need to check if you iterated over all the images, else you'll got a nice segfault while trying to access an unallocated cell of the array.

Win32 application. HBITMAP LoadImage fails to load anything

I'm writing a function that quickly draws an image of a menu for a game I'm making. I'm able to draw the background and text blocks just fine but I'm having trouble creating a bitmap image on the screen
bool menu::drawMenu(PAINTSTRUCT ps)
{
HWND hWnd = GetActiveWindow();
HDC hdc = GetDC(hWnd), hdcMem;
//Draw a new background
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
SelectObject(hdc, blackPen);
SelectObject(hdc, blackBrush);
Rectangle(hdc, 0, 0, 1080, 720);
//insert selection text
TextOut(hdc, 30, 0, L"New Game", 8);
TextOut(hdc, 30, 30, L"Exit Game", 9);
//draw arrow sprite
HBITMAP arrow = (HBITMAP)LoadImage(NULL, L"C:\\Users\\Tim\documents\\visual studio 2013\\Projects\\BoulderBisque\\BoulderBisque\\arrow.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
DWORD lastError = GetLastError();
if (arrow == NULL)
{
TextOut(hdc, 0, 60, L"Image Load Failed", 17);
return false;
}
hdcMem = CreateCompatibleDC(hdc);
if (hdcMem == NULL)
{
TextOut(hdc, 0, 90, L"Memory Creation Failed", 22);
return false;
}
SelectObject(hdcMem, arrow);
BitBlt(hdc, 0, choice * 30, 16, 16, hdcMem, 0, 0, SRCCOPY);
//cleanup
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
return true;
}
As of now arrow is NULL and I get the Load Image Failed textbox.
I am using the relative path of arrow.bmp I also tried using a full path, that didn't work either.
You may have noticed that this function is outside the WndProc. Everything else draws fine.
I tried running it in there too everything but the arrow.bmp loads.
What am I doing wrong to cause arrow.bmp to be NULL? I have other methods I plan to run in similar ways so getting this function to run would really be a big help.
EDIT* Whenever I give the full path name it still fails to load.
Also is this not the appropriate code for SO? This is my first question...
EDIT** The aditional '/'s haven't fixed the issue.
EDIT*** Using GetLastError, I found the error code to be 2, ERROR_FILE_NOT_FOUND
Your early versions of the question checked the return value of LoadImage but then did nothing more. The documentation says this:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
So, if the function fails, call GetLastError to find out why. When you do so, you obtain the error code ERROR_FILE_NOT_FOUND. Which is pretty conclusive. The filename that you specified does not exist.
Do note that the code in the latest update to the question calls GetLastError unconditionally. That is a mistake, one that is seen all too frequently here on Stack Overflow. The documentation only tells you to call GetLastError when the call to LoadImage fails. If the call to LoadImage succeeds, then the value return by GetLastError is meaningless. Error handling in Win32 is largely handled in the same way as LoadImage does it, but not always. So you have to read the documentation very carefully.
Perhaps instead of
C:\\Users\\Tim\documents\\...
you meant
C:\\Users\\Tim\\documents\\...
OK, now you've got the path right. The call to LoadImage returns NULL, but GetLastError is no longer helpful and returns ERROR_SUCCESS. Which is weird in itself.
I believe that the problem is that your image uses a format that LoadImage does not understand. I took your .bmp file, loaded it in Paint.net and then re-saved it. Once I did that, the re-saved image was loaded successfully.
Rather than trying to load this from a file it would make far more sense to link the image as a resource and load it that way.
I found through experimenting with Gimp, that your .bmp file must have the following attributes in order for it to work:
depth:24bit
"Do not show write color space information" box checked: Gimp export options
If that still doesn't work then try to "unblock" it: File options.

GetTopLevelParent only return one instance

When my application have multiple Window.
and GetTopLevelParent only returned one result.
Did the following code are act on all window?
HWND tParent = CWnd::GetTopLevelParent(); // Get a valid handle now.
if (tParent != NULL)
{
cwnd->SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);
}

Win32 LoadImage() from File Error

Sorry if this is duplicate but I can't find answer elsewhere. I am simply trying to load an image during runtime using the LoadImage() function of WINAPI. I receive the error code(8) which indicates that there is not enough storage space(error codes found here).
the file is relatively small(2.5kb) so I wonder if there is problem with my code:
void OnCreate()
{
...
HBITMAP hbmDeck = (HBITMAP)LoadImage(hInstance, L"standard.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
DWORD err = GetLastError();
HBITMAP hbmT = SelectBitmap(hdc, hbmDeck);
if(!hbmT)
{
MessageBox(NULL, L"Failed to LoadImage - 'hbmDeck'", L"OnCreate()", MB_OK);
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
...
}
Yeah, some of the API return error codes are a bit cryptic and don't fit the error.
The file you are trying to load "standard.bmp" is a file on the disk NOT in the resource section right? Well to load a file from the disk, the fist parameter of LoadImage (hInst) has to be NULL and the fuLoad flag needs to include LR_LOADFROMFILE which you correctly have.