Моя IP-камера поддерживает аудиовыход (обратный аудиоканал). Я хочу транслировать звук с микрофона ПК в прямом эфире через RTSP, чтобы он был слышен через динамик камеры. Спецификация потоковой передачи ONVIF сообщает мне, что мне нужно отправлять аудиоданные по предоставленному URL-адресу RTSP. Моя камера поддерживает профиль ONVIF T.
Я пробовал:
using Rtsp;
using Rtsp.Messages;
using Rtsp.Sdp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace Rtsp
{
public class RtspClient
{
private RtspListener rtsp_client;
private RtspTcpTransport tcp_socket;
public string url;
public bool canPlay = false;
public string username;
public string password;
public ushort seqNo = 0;
public event EventHandler RtspError;
public event EventHandler RtpDataReceived;
public Stopwatch stopwatch { get; private set; }
public RtspClient(string _url, string _username, string _password)
{
url = _url;
username = _username;
password = _password;
var uri = new Uri(_url);
tcp_socket = new RtspTcpTransport(uri.Host, 554); // 554);
if (tcp_socket.Connected == false)
{
Console.WriteLine("Error - did not connect");
return;
}
// Connect a RTSP Listener to the TCP Socket to send messages and listen for replies
rtsp_client = new RtspListener(tcp_socket);
rtsp_client.MessageReceived += Rtsp_client_MessageReceived;
rtsp_client.DataReceived += DataReceived;
rtsp_client.Start(); // start reading messages from the server
rtsp_client.AutoReconnect = true;
RtspRequest describe_message = new RtspRequestDescribe();
describe_message.RtspUri = uri;
describe_message.AddHeader("Accept: application/sdp");
describe_message.AddHeader("Require: www.onvif.org/ver20/backchannel");
rtsp_client.SendMessage(describe_message);
stopwatch = new Stopwatch();
stopwatch.Start();
}
private void DataReceived(object sender, RtspChunkEventArgs e)
{
int rtp_version = (e.Message.Data[0] >> 6);
int rtp_padding = (e.Message.Data[0] >> 5) & 0x01;
int rtp_extension = (e.Message.Data[0] >> 4) & 0x01;
int rtp_csrc_count = (e.Message.Data[0] >> 0) & 0x0F;
int rtp_marker = (e.Message.Data[1] >> 7) & 0x01;
int rtp_payload_type = (e.Message.Data[1] >> 0) & 0x7F;
uint rtp_sequence_number = ((uint)e.Message.Data[2]
Подробнее здесь: [url]https://stackoverflow.com/questions/59465932/how-to-send-audio-on-rtsp-backchannel[/url]
Моя IP-камера поддерживает аудиовыход (обратный аудиоканал). Я хочу транслировать звук с микрофона ПК в прямом эфире через RTSP, чтобы он был слышен через динамик камеры. Спецификация потоковой передачи ONVIF сообщает мне, что мне нужно отправлять аудиоданные по предоставленному URL-адресу RTSP. Моя камера поддерживает профиль ONVIF T. Я пробовал: [code] public static RtspClient rtspClient; public static IWaveIn sourceStream;
//Call Device Url and get Services. string DeviceServiceUrl = "http://" + CameraIp + "/onvif/device_service"; var deviceClient = new DeviceClient("DeviceBinding", new EndpointAddress(DeviceServiceUrl)); deviceClient.Endpoint.Behaviors.Add(ClientMessageInspector); var getServices = deviceClient.GetServices(false);
//Call media2 getStreamingUri. string url = "http://" + CameraIp + "/onvif/media2_service"; var Media2Client = new Media2Client("Media2Binding", new EndpointAddress(url)); Media2Client.Endpoint.Behaviors.Add(ClientMessageInspector); var media2GetProfiles = Media2Client.GetProfiles(null, null); var resp = Media2Client.GetAudioDecoderConfigurationOptions(null, null); var responseGetAudioStreamUri = Media2Client.GetStreamUri("tcp", profiles[0].token); //This gets rtsp url of media from camera.
rtspClient = new RtspClient(responseGetAudioStreamUri, UserName, Password); sourceStream = new WaveInEvent(); sourceStream.WaveFormat = new WaveFormat(64, 8, 1); //8000 16 sourceStream.DataAvailable += new EventHandler(SourceStream_DataAvailable); sourceStream.StartRecording();
Console.ReadKey(); }
//This method gets data from PC microphone and enocodes it into Mu-Law G711 and send to rtsp url. private static void SourceStream_DataAvailable(object sender, WaveInEventArgs e) { byte[] encoded = TwoWayAudio_Encode_MuLaw(e.Buffer, 0, e.BytesRecorded); rtspClient.SendData(encoded, encoded.Length, 3); }
private static byte[] TwoWayAudio_Encode_MuLaw(byte[] data, int offset, int length) { byte[] encoded = new byte[length / 2]; int outIndex = 0; for (int n = 0; n < length; n += 2) { encoded[outIndex++] = MuLawEncoder.LinearToMuLawSample(BitConverter.ToInt16(data, offset + n)); } return encoded; } [/code] Клиент RTSP в моем проекте взят отсюда. [b]Rtspclient.cs[/b] [code]using Rtsp; using Rtsp.Messages; using Rtsp.Sdp; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions;
namespace Rtsp { public class RtspClient { private RtspListener rtsp_client; private RtspTcpTransport tcp_socket; public string url; public bool canPlay = false; public string username; public string password; public ushort seqNo = 0; public event EventHandler RtspError; public event EventHandler RtpDataReceived; public Stopwatch stopwatch { get; private set; }