/// <summary> /// Each client connection will be served by this thread. /// </summary> /// <param name="client"></param> private void ClientTask(object client) { StreamSocket socket = (StreamSocket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.Information.RemoteAddress.ToString())); lock (_Clients) //TODO: see if needed, Add may be thread-safe already _Clients.Add(socket); try { using (MjpegWriter wr = new MjpegWriter(socket.OutputStream.AsStreamForWrite())) { // Writes the response header to the client. wr.WriteHeader(); // Streams the images from the source to the client. foreach (var imgStream in this.ImagesSource.JpegStreams()) { if (this.Interval > 0) { Task.Delay(this.Interval).Wait(); //see https://stackoverflow.com/questions/12641223/thread-sleep-replacement-in-net-for-windows-store } wr.Write(imgStream); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } finally { socket.Dispose(); lock (_Clients) //TODO: see if needed, Remove may be thread-safe already _Clients.Remove(socket); } }
/// <summary> /// Each client connection will be served by this thread. /// </summary> /// <param name="client"></param> private void ClientThread(object client) { Socket socket = (Socket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString())); lock (_Clients) _Clients.Add(socket); try { using (MjpegWriter wr = new MjpegWriter(new NetworkStream(socket, true))) { // Writes the response header to the client. wr.WriteHeader(); // Streams the images from the source to the client. foreach (var imgStream in this.ImagesSource.JpegStreams()) { if (this.Interval > 0) { Thread.Sleep(this.Interval); } wr.Write(imgStream); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } finally { lock (_Clients) _Clients.Remove(socket); } }
public static void WriteJpeg(this SoftwareBitmap img, MemoryStream ms) { var data = MjpegWriter.EncodedBytes(img, BitmapEncoder.JpegEncoderId).GetAwaiter().GetResult(); ms.Write(data, 0, data.Length); //TODO: do we need to also call Flush()? }