public HttpResponseMessage GetLiveStream(Int32 callId, Int32 lineId, Int32 unitId, String ani) { HttpResponseMessage response = Request.CreateResponse(); try { Boolean startStreaming = false; String argsStr = $"args: lineId: {lineId}, unitId: {unitId}, ani: {ani}, callId: {callId}"; UInt32 responseCode = 0; // Only send monitor start request if the stream does not already exist. if (AudioHandler.IsExistingStream(callId)) { _logger.LogInfo($"No monitor start request sent. Stream already exists.\r\n\t{argsStr}"); startStreaming = true; } else { MailmanResponse mailmanResponse = CircuitsInterop.MonitorRequestStart(callId, lineId, unitId, ani); responseCode = CircuitsInterop.GetResponseCode(mailmanResponse); //CircuitsInterop.MonitorRequestStart(callId, lineId, unitId, ani, false); //responseCode = ResponseCodes.LM_OK; _logger.LogInfo($"Monitor start request sent.\r\n\t{argsStr}\r\n\tResponseCode: {responseCode}"); switch (responseCode) { case ResponseCodes.LM_OK: startStreaming = true; AudioHandler.CreateNewStream(callId); break; case ResponseCodes.LM_INVALID_INFO: case ResponseCodes.LM_MONITOR_REQ_INVALID: response.StatusCode = HttpStatusCode.BadRequest; break; case ResponseCodes.LM_NO_MONITORING_SESSION: case ResponseCodes.LM_INTERNAL_ERROR: case ResponseCodes.LM_COMM_FAILURE: case ResponseCodes.LM_NOT_REGISTERED: response.StatusCode = HttpStatusCode.InternalServerError; break; default: response.StatusCode = HttpStatusCode.InternalServerError; break; } } if (startStreaming) { CurrentSessionHandler session = new CurrentSessionHandler(HttpContext.Current.Session); response.Content = new PushStreamContent( async(outputStream, httpContent, context) => { await AudioHandler.WriteToAsync(outputStream, httpContent, context, callId, lineId, unitId, ani, session.Username); }, new MediaTypeHeaderValue("audio/wav") ); _logger.LogInfo($"User '{session.Username}' started streaming call ID: {callId}"); response.StatusCode = HttpStatusCode.OK; } else { _logger.LogWarning($"Request Monitor Start returned response code: {responseCode}\r\n\t{argsStr}"); } return(response); } catch (Exception ex) { _logger.LogError(ex, "Exception in GetLiveStream"); response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent("Error retrieving live stream audio data"); return(response); } }