Esempio n. 1
0
        public void sourceRequest(ISourceRequestResult aISourceRequestResult)
        {
            System.Threading.Thread.Sleep(100);

            if (aISourceRequestResult == null)
            {
                return;
            }

            uint lStreamIndex = 0;

            aISourceRequestResult.getStreamIndex(out lStreamIndex);

            if (lStreamIndex == 0)
            {
                aISourceRequestResult.setData(
                    mRawDataFrames[mCurrentFrameIndex].NativeDataFrame,
                    (uint)mRawDataFrames[mCurrentFrameIndex].NativeDataFrameLength, 1);

                ++mCurrentFrameIndex;

                if (mCurrentFrameIndex >= mRawDataFrames.Count)
                {
                    mCurrentFrameIndex = 0;
                }
            }
        }
Esempio n. 2
0
        public void sourceRequest(ISourceRequestResult aISourceRequestResult)
        {
            System.Threading.Thread.Sleep(100);

            if (aISourceRequestResult == null)
            {
                return;
            }

            uint lStreamIndex = 0;

            aISourceRequestResult.getStreamIndex(out lStreamIndex);

            if (lStreamIndex == 0)
            {
                aISourceRequestResult.setData(mRawData, (uint)mPixels.Length, 1);
            }
        }
Esempio n. 3
0
        public void sourceRequest(ISourceRequestResult aISourceRequestResult)
        {
            if (aISourceRequestResult == null)
            {
                return;
            }

            uint lStreamIndex = 0;

            aISourceRequestResult.getStreamIndex(out lStreamIndex);

            if (lStreamIndex == 0)
            {
                lock (this)
                {
                    aISourceRequestResult.setData(mRawData, (uint)mLength, 1);
                }
            }
        }
        private void write(byte[] nal_unit)
        {
            if (mISourceRequestResult != null)
            {
                MemoryStream l_proxyMemory = new MemoryStream();
                l_proxyMemory.Position = 0;

                l_proxyMemory.Write(new byte[] { 0x00, 0x00, 0x00, 0x01 }, 0, 4);  // Write Start Code
                l_proxyMemory.Write(nal_unit, 0, nal_unit.Length);                 // Write NAL

                var ldata = l_proxyMemory.ToArray();

                IntPtr lptrData = Marshal.AllocHGlobal(ldata.Length);

                Marshal.Copy(ldata, 0, lptrData, ldata.Length);

                mISourceRequestResult.setData(lptrData, (uint)ldata.Length, 1);

                Marshal.FreeHGlobal(lptrData);
            }
        }
Esempio n. 5
0
        // Thread entry point
        public void WorkerThread()
        {
            while (true)
            {
                // reset reload event
                //reloadEvent.Reset();

                HttpWebRequest req    = null;
                WebResponse    resp   = null;
                Stream         stream = null;
                //byte[] delimiter = null;
                //byte[] delimiter2 = null;
                //byte[] boundary = null;
                //int boundaryLen, delimiterLen = 0, delimiter2Len = 0;
                //int read, todo = 0, total = 0, pos = 0, align = 1;
                //int start = 0, stop = 0;

                // align
                //  1 = searching for image start
                //  2 = searching for image end
                try
                {
                    //Create an HTTP request, as long as the request does not end, MJPEG server will always send real-time image content to the response body of the request
                    HttpWebRequest hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(mURL);
                    hwRequest.Method = "GET";
                    HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                    //Read the separator of each image specified by boundary, DroidCam is: - dcmjpeg
                    string contentType = hwResponse.Headers["Content-Type"];
                    string boundryKey  = "boundary=";
                    string boundary    = contentType.Substring(contentType.IndexOf(boundryKey) + boundryKey.Length);

                    //Get response volume flow
                    stream = hwResponse.GetResponseStream();
                    string headerName = "Content-Length:";
                    //Temporary storage of string data
                    StringBuilder sb  = new StringBuilder();
                    int           len = 1024;
                    while (true)
                    {
                        //Read a line of data
                        while (true)
                        {
                            char c = (char)stream.ReadByte();
                            //Console.Write(c);
                            if (c == '\n')
                            {
                                break;
                            }
                            sb.Append(c);
                        }
                        string line = sb.ToString();
                        sb.Remove(0, sb.Length);
                        //Whether the current line contains content length:
                        int i = line.IndexOf(headerName);
                        if (i != -1)
                        {
                            //Before each picture, there is a brief introduction to the picture (picture type and length). Here, we only care about the value after the length (content length:), which is used for subsequent reading of the picture
                            int imageFileLength = Convert.ToInt32(line.Substring(i + headerName.Length).Trim());
                            //Content-Length:xxx  After that, there will be a / r/n newline character, which will be the real image data
                            //Skip / r/n here
                            stream.Read(new byte[2], 0, 2);
                            //Start to read the image data. imageFileLength is the length after the content length: read
                            byte[] imageFileBytes = new byte[imageFileLength];
                            var    lreadBytes     = stream.Read(imageFileBytes, 0, imageFileBytes.Length);

                            var lrestLength = imageFileLength - lreadBytes;

                            while (lrestLength > 0)
                            {
                                lreadBytes += stream.Read(imageFileBytes, lreadBytes, lrestLength);

                                lrestLength = imageFileLength - lreadBytes;
                            }

                            //JPEG The header of the file is: FF D8 FF ,The end of the file is: FF D9,very important,It's better to print when debugging, so as to distinguish whether the read-in data is exactly the same as all the contents of the picture
                            //Console.WriteLine("file header): + imagefilebytes [0]. ToString (" X ") +" + imagefilebytes [1]. ToString ("X") + "+ imagefilebytes [2]. ToString (" X ") +" + imagefilebytes [3]. ToString ("X") + "+ imagefilebytes [4]. ToString (" X "))));
                            //Console.WriteLine (end of file: + imagefilebytes [imagefilelength - 2]. ToString ("X") + "+ imagefilebytes [imagefilelength - 1]. ToString (" X ")));
                            //If the file read in is incomplete, the bigger the picture is, the faster the program cycle read speed is, and the more likely it is to lead to incomplete file read. If there is a good solution, I hope you can give me some advice. Thank you very much!
                            //Is the end of the file FF D9
                            //if (imageFileBytes[imageFileLength - 2].ToString("X") != "FF" && imageFileBytes[imageFileLength - 1].ToString("X") != "D9")
                            //{
                            //    //If the content of the read file is incomplete, skip the second file and let the stream position jump to the beginning of the next picture
                            //    //Console.WriteLine (start correction...);
                            //    char l = '0';
                            //    while (true)
                            //    {
                            //        char c = (char)stream.ReadByte();
                            //        //Here, only the first two characters in dcmjpeg are judged. When two consecutive characters in the read stream are, it means that the stream has read to the beginning of the next picture
                            //        if (l == boundary[0] && c == boundary[1])
                            //        {
                            //            break;
                            //        }
                            //        l = c;
                            //    }
                            //}
                            //else
                            {
                                MemoryStream lbuff = new MemoryStream(imageFileBytes);

                                lbuff.Position = 0;

                                var lBitmap = JpegBitmapDecoder.Create(lbuff, BitmapCreateOptions.None, BitmapCacheOption.None);

                                if (lBitmap.Frames != null && lBitmap.Frames.Count > 0)
                                {
                                    BitmapSource bitmapSource = lBitmap.Frames[0];

                                    //Read the picture successfully!
                                    //m_callbackAction(bitmapSource);

                                    if (mISourceRequestResult != null)
                                    {
                                        IntPtr lptrData = Marshal.AllocHGlobal(imageFileBytes.Length);

                                        Marshal.Copy(imageFileBytes, 0, lptrData, imageFileBytes.Length);

                                        mISourceRequestResult.setData(lptrData, (uint)imageFileBytes.Length, 1);

                                        Marshal.FreeHGlobal(lptrData);
                                    }
                                }
                            }
                            //If you sleep several tens of milliseconds properly here, it will reduce the situation of incomplete picture reading. The reason of incomplete picture random reading has not been found yet
                            //Thread.Sleep(250);
                        }
                    }
                    stream.Close();
                    hwResponse.Close();
                }
                catch (WebException ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                catch (ApplicationException ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                }
                finally
                {
                    // abort request
                    if (req != null)
                    {
                        req.Abort();
                        req = null;
                    }
                    // close response stream
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    // close response
                    if (resp != null)
                    {
                        resp.Close();
                        resp = null;
                    }
                }

                //// need to stop ?
                //if (stopEvent.WaitOne(0, true))
                //    break;
            }
        }