-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stream disconnect when scene changes from total darkness abruptly #4
Comments
Weird. Before I try to replicate, what if you add in a try, catch around
the part that's failing? Maybe it's just one frame that's messed up, and if
we just skip it and wait for the frame after it'll be fine?
…On Sun, Nov 15, 2020, 8:25 AM dustinkerstein ***@***.***> wrote:
First of all, thanks for this awesome demo. It's working perfectly 99% of
the time.
A minor issue, though definitely a snag for my usage, is when the scene
changes from total darkness (ie. taking lens cap off) the stream will
disconnect with the error:
Error received while reading the MJPEG.
Any ideas on why this could be happening? Are you able to replicate?
Thanks!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXDT7T2GCDS4V2UEQQZK73SP7JF3ANCNFSM4TWGGQAA>
.
|
Yeah, that sound good. It's happening somewhere in |
Here's that chunk of code for reference:
|
IDK I wrote it years ago. You know more about it than I do at this point.
Keep throwing debug and print statements until you find the error. Also it
might have try catches where the bad image is coming in, but still trying
to use the empty image. If you can tell me where the actual crash is
happening I might be able to help more.
…On Sun, Nov 15, 2020, 8:52 AM dustinkerstein ***@***.***> wrote:
Here's that chunk of code for reference:
private void OnGetResponse(IAsyncResult asyncResult)
{
responseReceived = true;
//Debug.Log("OnGetResponse");
byte[] imageBuffer = new byte[1024 * 1024];
//Debug.Log("Starting request");
// get the response
HttpWebRequest req = (HttpWebRequest)asyncResult.AsyncState;
try
{
//Debug.Log("OnGetResponse try entered.");
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(asyncResult);
//Debug.Log("response received");
// find our magic boundary value
string contentType = resp.Headers["Content-Type"];
if (!string.IsNullOrEmpty(contentType) && !contentType.Contains("="))
{
Debug.Log("MJPEG Exception thrown");
throw new Exception("Invalid content-type header. The camera is likely not returning a proper MJPEG stream.");
}
string boundary = resp.Headers["Content-Type"].Split('=')[1].Replace("\"", "");
byte[] boundaryBytes = Encoding.UTF8.GetBytes(boundary.StartsWith("--") ? boundary : "--" + boundary);
Stream s = resp.GetResponseStream();
BinaryReader br = new BinaryReader(s);
_streamActive = true;
byte[] buff = br.ReadBytes(_chunkSize);
while (_streamActive)
{
// find the JPEG header
int imageStart = FindBytes(buff, JpegHeader);// buff.Find(JpegHeader);
if (imageStart != -1)
{
// copy the start of the JPEG image to the imageBuffer
int size = buff.Length - imageStart;
Array.Copy(buff, imageStart, imageBuffer, 0, size);
while (true)
{
buff = br.ReadBytes(_chunkSize);
// Find the end of the jpeg
int imageEnd = FindBytes(buff, boundaryBytes);
if (imageEnd != -1)
{
// copy the remainder of the JPEG to the imageBuffer
Array.Copy(buff, 0, imageBuffer, size, imageEnd);
size += imageEnd;
// Copy the latest frame into `CurrentFrame`
byte[] frame = new byte[size];
Array.Copy(imageBuffer, 0, frame, 0, size);
CurrentFrame = frame;
// tell whoever's listening that we have a frame to draw
if (FrameReady != null)
FrameReady(this, new FrameReadyEventArgs());
// copy the leftover data to the start
Array.Copy(buff, imageEnd, buff, 0, buff.Length - imageEnd);
// fill the remainder of the buffer with new data and start over
byte[] temp = br.ReadBytes(imageEnd);
Array.Copy(temp, 0, buff, buff.Length - imageEnd, temp.Length);
break;
}
// copy all of the data to the imageBuffer
Array.Copy(buff, 0, imageBuffer, size, buff.Length);
size += buff.Length;
if (!_streamActive)
{
//Debug.Log("CLOSING");
resp.Close();
break;
}
}
}
}
resp.Close();
}
catch (Exception ex)
{
if (Error != null)
_context.Post(delegate { Error(this, new ErrorEventArgs() { Message = ex.Message }); }, null);
return;
}
}
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXDT7R7HBD22WKEPBUK2LDSP7MJJANCNFSM4TWGGQAA>
.
|
It looks like it's happening here:
But none of the variables are zero/null as when I
I'll try a little more debug now. |
Ah, the buffer size was too small based on the 4096 offset. I increased the buffer to |
First of all, thanks for this awesome demo. It's working perfectly 99% of the time.
A minor issue, though definitely a snag for my usage, is when the scene changes from total darkness (ie. taking lens cap off) the stream will disconnect with the error:
Error received while reading the MJPEG.
Any ideas on why this could be happening? Are you able to replicate? Thanks!
The text was updated successfully, but these errors were encountered: