private void UpdateState(Uri uri, string method, string content, HttpListenerContext context) { HttpListenerResponse response = context.Response; try { DVRBResult result; // parse xml body XmlSchemas.server_state body = new XmlSchemas.server_state(); body.ReadXml(new System.IO.StringReader(content)); log.Debug("updateState:: Recording --> " + body.recording[0].state); log.Debug("updateState:: Streaming --> " + body.streaming[0].state); log.Debug("updateState:: vod --> " + body.vod[0].state); if (this.dvrbConfig == null && !(body.recording[0].state == "stop" && body.streaming[0].state == "stop" && body.vod[0].state == "stop")) { throw new System.Exception("System is not configured"); } // recording management switch (body.recording[0].state) { case "stop": result = this.encoder.EnableRecording(false); if (!result.Succeeded) { throw new System.Exception("encoder.EnableRecording returns ERROR [" + result.Code + "]:" + result.Description); } break; case "play": result = this.encoder.EnableRecording(true); if (!result.Succeeded) { throw new System.Exception("encoder.EnableRecording returns ERROR [" + result.Code + "]:" + result.Description); } break; default: throw new System.Exception("Unknown record state received: " + body.recording[0].state); } // streaming and OnDemand management Streaming.Modes state = Streaming.Modes.None; if (body.streaming[0].state == "play" && body.vod[0].state == "stop") { state = Streaming.Modes.Live; } else if (body.streaming[0].state == "stop" && body.vod[0].state == "play") { state = Streaming.Modes.OnDemand; } else if (body.streaming[0].state == "play" && body.vod[0].state == "play") { state = Streaming.Modes.All; } else if (body.streaming[0].state == "stop" && body.vod[0].state == "stop") { state = Streaming.Modes.None; } else { throw new System.Exception("Unknown state received: VOD = " + body.vod[0].state + ", Live = " + body.streaming[0].state); } // Change state this.streaming.Active = state; // If Live is active we must start encoder. if ((state & Streaming.Modes.Live) != 0) { result = this.encoder.Start(); if (!result.Succeeded) { throw new System.Exception("wmEncoder.Start returns ERROR [" + result.Code + "]:" + result.Description); } } else { if (body.recording[0].state == "stop") { result = this.encoder.Stop(); if (!result.Succeeded) { throw new System.Exception("wmEncoder.Stop returns ERROR [" + result.Code + "]:" + result.Description); } } } // If OnDemand or Live is active we must start the streaming server. if (state != Streaming.Modes.None) { bool allow_unicast = false; if (System.Int32.Parse(this.dvrbConfig.output[0].GetliveRows()[0].allow_unicast) > 0) { allow_unicast = true; } if ((state & Streaming.Modes.Live) != 0) { result = this.streaming.SetLiveStreamingInfo( internalAddr, internalPort, allow_unicast, this.dvrbConfig.output[0].GetliveRows()[0].mgroup, System.Int32.Parse(this.dvrbConfig.output[0].GetliveRows()[0].port)); if (!result.Succeeded) { throw new System.Exception("m_Streaming.SetLiveStreamingInfo ERROR: " + result.Description); } } result = this.streaming.Start(); if (!result.Succeeded) { throw new System.Exception("m_Streaming.Start returns ERROR [" + result.Code + "]:" + result.Description); } } else { result = this.streaming.Stop(); if (!result.Succeeded) { throw new System.Exception("m_Streaming.Stop returns ERROR [" + result.Code + "]:" + result.Description); } } this.SendResponse(200, "OK", response); } catch (System.Exception e) { log.Error("updateState:: Bad request from " + context.Request.UserHostAddress + ", exception :" + e.Message); this.SendResponse(400, "Bad Request: " + e.Message, response); } }
private void GetState(Uri uri, string method, string content, HttpListenerContext context) { HttpListenerResponse response = context.Response; XmlSchemas.server_state body = new XmlSchemas.server_state(); // Get recording state WMEncoderLib.WMENC_ARCHIVE_STATE record_state = this.encoder.RecordingState; switch (record_state) { case WMEncoderLib.WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_RUNNING: body.recording.AddrecordingRow("play"); break; case WMEncoderLib.WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_PAUSED: body.recording.AddrecordingRow("pause"); break; case WMEncoderLib.WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_STOPPED: body.recording.AddrecordingRow("stop"); break; } // get Live state if (this.streaming.LiveStreamingState == Streaming.State.Running) { body.streaming.AddstreamingRow("play"); } else { body.streaming.AddstreamingRow("stop"); } // get OnDemand state if (this.streaming.OnDemandState == Streaming.State.Running) { body.vod.AddvodRow("play"); } else { body.vod.AddvodRow("stop"); } log.Debug("getState:: Recording --> " + body.recording[0].state); log.Debug("getState:: Streaming --> " + body.streaming[0].state); log.Debug("getState:: vod --> " + body.vod[0].state); StringWriter sw = new StringWriter(); body.WriteXml(sw); string xml = sw.ToString(); this.SendResponse(xml, "application/xml", 200, "OK", response); }