public async Task<HttpResponseMessage> Get(string hubName, string node) { string hubId = await _lookupToken.GetHomeHubId(User); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new PushStreamContent(async (outputStream, httpContent, transportContext) => { Services.Log.Info(string.Format("Requesting camera stream from {0} - {1}", node, hubId)); using (var chatHub = ChatHub.Get(hubId)) { chatHub.MessageToHome(new HubCameraCommand { Node = node, Active = true, Type = "h264" }); try { var totalData = 0; using (var videoHub = VideoHub.Get(hubId, node)) { using (var videoWaitable = new VideoHubWaitable(videoHub)) { var moreData = true; while (moreData) { var videoData = await videoWaitable.WaitData(); if (videoData.Length != 0) { totalData += videoData.Length; Debug.WriteLine(string.Format("uploaded: {0}", totalData)); await outputStream.WriteAsync(videoData.Bytes, 0, videoData.Length); } else { moreData = false; } } } } } catch (HttpException ex) { if (ex.ErrorCode == -2147023667) // The remote host closed the connection. { return; } } finally { // Close output stream as we are done outputStream.Close(); chatHub.MessageToHome(new HubCameraCommand { Node = node, Active = false, Type = "h264" }); } } }, new MediaTypeHeaderValue("video/webm")) // "text/plain" }; return response; }
private async Task PipeSnapshotImage(string hubId, string node, Stream outputStream, bool thumbnail, bool singleImage) { int duration = thumbnail || singleImage ? ShortTimelapse : LongTimelapse; CameraActivator.Trigger(hubId, node, duration); using (var videoHub = VideoHub.Get(hubId, node)) { using (var videoWaitable = new VideoHubWaitable(videoHub, true)) { var imageSize = 0; var videoData = await videoWaitable.WaitData(); if (videoData.Length != 0 && !(videoData.Length == 1 && videoData.Bytes[0] == 0)) { var videoBytes = videoData.Bytes; var videoLength = videoData.Length; if (thumbnail) { videoBytes = CreateThumbnail(videoBytes, videoLength); videoLength = videoBytes.Length; } await outputStream.WriteAsync(videoBytes, 0, videoLength); imageSize += videoData.Length; CameraActivator.Received(hubId, node); } // Services.Log.Info(string.Format("Sent camera snapshot with {0} bytes", imageSize)); } } }