Closed jbaudanza closed 4 years ago
Hi, thanks for raising the issue. Let's see...
When you use one of StreamEncoder
, FileEncoder
, StreamDecoder
or FileDecoder
, the encoder/decoder is initialised when the first write/read is requested from the stream (they are all node.js streams). Talking about StreamEncoder
, the encoder is initialised inside the _transform
method:
It is done before doing any encoder call, and according to node.js documentation, the stream is paused until I call callback
(which is done when something fails, or after sending the data to be processed by flac). So, theorically only one _transform
method is running at the same time.
Well there is also the _flush
method, that is called by node when you (for example) call flacEncoder.end()
. This method, according to the docs, is called when there is no more data to read (so after _transform
has been called and all their callback
s called back). In the _flush
method is where the encoder is de-initialised, but because the stream is being closed, it should not be a problem.
With that facts, you got the Encoder has not been initialised yet
error for some reason I cannot imagine currently. So if you are able to provide some stack traces or logs, would be helpful.
Something I never had is some debug logs for the library, so maybe today is a good day to add them to track this kind of weird issues (I will provide more information when I have this done).
Once again, thanks, and glad to hear you like the project :)
Ah ok. I think I see what's happening. My client is closing the connection before sending any PCM data, so _write is never being called. It looks like this:
StreamEncoder()
is created
stream is passed to S3 Uploader to start streaming to S3. (not sure if this is relevant)
flacEncoder.end()
raises 'Encoder has not been initialized yet' error.Thank you for your help!
Mmmm that makes a lot of sense in fact! That's a good point.
I will create a fix for this, so if for some reason, the user does not provide any data (like in your case), send an empty flac file or something like this.
Yeah, I actually think you could argue this is not a bug. I can see two possible "fixes" in this case:
end
, but it says something more descriptive like, "Encoder never received any data to encode".I think both behaviors would be reasonable.
Finally implemented the "no data emitted" fix for this ce03102b66821cfa912fb186166f519bc4e8c0ec. Also added tests to cover this scenarios for StreamEncoder
, FileEncoder
and StreamDecoder
(did not see any way to replicate this in FileDecoder
).
Should not error anymore when calling .end()
without writing anything to the stream :)
Thanks for everything, really helped to improve the library. Closing the issue for now. If you see the same error after the release, feel free to reopen it. I will publish a new version later this weekend.
Hi again, I published a new version of the package 2.5.0
with this fix. Just to let you know :)
Thank you so much. I will deploy it and let you know if I encounter and more issues
I am using the StreamEncoder to encode PCM data from a WebSocket and send the result to an S3 Bucket. I am initializing the encoder like this:
After initialization, I pass the encoder object to the S3 uploader and begin calling
flacEncoder.write
when I receive PCM data on the WebSocket.Usually, this works fine. But on one occasion, I saw in my logs that this error was raised: https://github.com/melchor629/node-flac-bindings/blob/67f5d45c60427b868ba1aa27b067008bab7f6e26/src/encoder/encoder.cpp#L562
Is there something I should be doing to wait for the encoder to initialize before streaming data to it? I didn't see anything in the docs or the interface.
Thanks for the great module! It's been very helpful.