/** * Constructor for WebService * @param prefix string where the server should listen. Example "http://localhost:8080/" */ public WebService(string prefix, Encoder encoder, Streaming streaming) { log.Debug("Creating Web Service"); this.dvrbConfig = null; this.uriPrefixes = new string[] { prefix }; this.encoder = encoder; this.streaming = streaming; // Create a listener. this.listener = new HttpListener(); }
private void UpdateDVRB(Uri uri, string method, string content, HttpListenerContext context) { DVRBResult result; HttpListenerResponse response = context.Response; try { // parse xml body XmlSchemas.dvrb body = new XmlSchemas.dvrb(); body.ReadXml(new System.IO.StringReader(content)); log.Debug(content); Encoder.CodecType enc_type = Encoder.CodecType.CBR; switch (body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].type) { case "CBR": enc_type = Encoder.CodecType.CBR; break; case "VBR_QUALITY": enc_type = Encoder.CodecType.VBR_QUALITY; break; case "VBR_BITRATE": enc_type = Encoder.CodecType.VBR_BITRATE; break; case "VBR_PEAK": enc_type = Encoder.CodecType.VBR_PEAK; break; } if (body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].bitrate.Length == 0) { body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].bitrate = "0"; } if (body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].quality.Length == 0) { body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].quality = "0"; } if (body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].peak.Length == 0) { body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].peak = "0"; } // Set video props. result = this.encoder.SetVideoParams( System.Int32.Parse(body.encoding[0].GetvideoRows()[0].width), System.Int32.Parse(body.encoding[0].GetvideoRows()[0].height), System.Int32.Parse(body.encoding[0].GetvideoRows()[0].fps), enc_type, System.Int32.Parse(body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].bitrate), System.Int32.Parse(body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].quality), System.Int32.Parse(body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].peak), System.UInt64.Parse(body.encoding[0].GetvideoRows()[0].GetvcodecRows()[0].keyframe)); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetVideoParams ERROR: " + result.Description); } // Set audio props. result = this.encoder.SetAudioParams( System.Int16.Parse(body.encoding[0].GetaudioRows()[0].channels), System.Int32.Parse(body.encoding[0].GetaudioRows()[0].freq), System.Int32.Parse(body.encoding[0].GetaudioRows()[0].GetacodecRows()[0].bitrate), System.Int16.Parse(body.encoding[0].GetaudioRows()[0].bits)); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetAudioParams ERROR: " + result.Description); } // Set metadata result = this.encoder.SetMetadata( body.metadata[0].title, body.metadata[0].subject, body.metadata[0].description, body.metadata[0].source, body.metadata[0].language, body.metadata[0].relation, body.metadata[0].coverage, body.metadata[0].creator, body.metadata[0].publisher, body.metadata[0].contributor, body.metadata[0].rights, body.metadata[0].date, body.metadata[0].type, body.metadata[0].format, body.metadata[0].id); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetMetadata ERROR: " + result.Description); } // Set recording params if (body.file[0].name.Length > 0 && body.file[0].path.Length > 0 && body.file[0].interval.Length > 0) { result = this.encoder.SetRecordParams(body.file[0].name, body.file[0].path, System.Int32.Parse(body.file[0].interval)); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetRecordParams ERROR: " + result.Description); } } // Set source params foreach (XmlSchemas.dvrb.inputRow input in body.input) { Encoder.ESourceType source_type; switch (input.type) { case "video": source_type = Encoder.ESourceType.DeviceVideo; break; case "audio": source_type = Encoder.ESourceType.DeviceAudio; break; case "desktop": source_type = Encoder.ESourceType.DesktopVideo; break; default: throw new System.Exception("Unknown source type " + input.type); } result = this.encoder.SetSource(source_type, input.name); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetSource ERROR: " + result.Description); } } // Set live streaming params result = this.encoder.SetOutParams(internalPort); if (!result.Succeeded) { throw new System.Exception("wmEncoder.SetOutParams ERROR: " + result.Description); } // Set OnDemand params if (body.output[0].GetvodRows()[0].path.Length > 0) { result = this.streaming.SetVoDInfo(body.output[0].GetvodRows()[0].path); if (!result.Succeeded) { throw new System.Exception("m_Streaming.SetVoDInfo ERROR: " + result.Description); } } this.dvrbConfig = body; this.SendResponse(200, "OK", response); } catch (System.Exception e) { log.Error("updateDVRB:: Bad request from " + context.Request.UserHostAddress + ", exception :" + e.Message); this.SendResponse(400, "Bad Request: " + e.Message, response); } }