/// <summary> /// 获取所有发送器 /// </summary> /// <returns></returns> public unsafe RtpSender[] GetSenders() { var ptrList = PeerConnection_GetSenders(this.Handler); if (ptrList == IntPtr.Zero) { return(new RtpSender[0]); } var list = new PointerArray(ptrList); var items = list.GetBuffer(); var ret = new List <RtpSender>(); while (*items != null) { IntPtr senderPtr = new IntPtr(*items); var sender = UniqueNative <RtpSender>(senderPtr, () => new RtpSender(senderPtr)); if (sender != null) { ret.Add(sender); } ++items; } return(ret.ToArray()); }
/// <summary> /// 持有一个数据接收器 /// </summary> /// <param name="handler">数据接收器指针</param> internal protected RtpReceiver(IntPtr handler, IDispatcher dispatcher) : base(handler) { IntPtr track = default; dispatcher.Invoke(() => { WebrtcObject_AddRef(handler); track = RtpReceiver_GetMediaStreamTrack(handler); }); Dispatcher = dispatcher; if (track == IntPtr.Zero) { throw new Exception("无法获取媒体轨道"); } IntPtr proxyPtr = default; dispatcher.Invoke(() => proxyPtr = MediaStreamTrack.MediaStreamTrack_GetKind(track)); if (proxyPtr == IntPtr.Zero) { throw new Exception("无法获取媒体类型"); } var buffer = new PointerArray(proxyPtr); unsafe { byte **pointer = (byte **)buffer.GetBuffer(); var kind = new string((sbyte *)*pointer); if (kind == "video") { Track = new VideoTrack(track, dispatcher); } else if (kind == "audio") { Track = new AudioTrack(track, dispatcher); } else { var unkown = new UnkownMediaStreamTrack(track, dispatcher); unkown.SetKind(kind); Track = unkown; } } NativeFirstPacketReceived = state => FirstPacketReceived?.Invoke((MediaType)state); dispatcher.Invoke(() => RtpReceiver_SetFirstPacketReceivedObserve(handler, NativeFirstPacketReceived)); }
/// <summary> /// 获取单个视频设备支持的所有分辨率 /// </summary> /// <param name="index">视频设备顺序</param> /// <returns>视频设备支持的所有分辨率</returns> public unsafe static VideoDeviceCapabilities[] GetDeviceCapabilities(int index) { try { var ptrs = PeerConnectionFactory_GetDeviceCapabilities(index); if (ptrs == IntPtr.Zero) { return(new VideoDeviceCapabilities[0]); } var buffer = new PointerArray(ptrs); byte **pointer = (byte **)buffer.GetBuffer(); byte **it = pointer; var list = new List <VideoDeviceCapabilities>(); while (*it != null) { int *info = (int *)*it; ++it; int width = *info; ++info; int height = *info; ++info; int fps = *info; ++info; int type = *info; ++info; int interlaced = *info; ++info; list.Add(new VideoDeviceCapabilities { Width = width, Height = height, Fps = fps, VideoType = (VideoType)type, Interlaced = interlaced > 0 }); } return(list.ToArray()); } catch (Exception exp) { return(new VideoDeviceCapabilities[0]); } }
/// <summary> /// 获取所有视频设备 /// </summary> /// <returns>所有视频设备</returns> public unsafe static VideoDeviceInfo[] GetDeviceInfo() { try { var ptrs = PeerConnectionFactory_GetDeviceInfo(); if (ptrs == null) { return(new VideoDeviceInfo[0]); } var buffer = new PointerArray(ptrs); byte **pointer = (byte **)buffer.GetBuffer(); byte **it = pointer; var list = new List <VideoDeviceInfo>(); var idx = 0; while (*it != null) { var name = new string((sbyte *)(*it)); ++it; var id = new string((sbyte *)(*it)); ++it; var pid = new string((sbyte *)(*it)); ++it; var en = *(int *)*it; ++it; list.Add(new VideoDeviceInfo { DeviceName = name, DeviceId = id, ProductId = pid, Enable = en, Index = idx }); ++idx; } return(list.ToArray()); } catch (Exception exp) { return(new VideoDeviceInfo[0]); } }
/// <summary> /// 搜到消息 /// </summary> /// <param name="buffer">消息数据</param> private unsafe void OnMessage(IntPtr buffer) { if (buffer == IntPtr.Zero) { return; } var arr = new PointerArray(buffer); var ptr = arr.GetBuffer(); int length = *(int *)*ptr; ++ptr; byte *data = (byte *)*ptr; ++ptr; int binnary = *(int *)*ptr; ++ptr; if (binnary > 0) { if (Data != null) { byte[] dataArray = new byte[length]; Marshal.Copy(new IntPtr(data), dataArray, 0, length); Data?.Invoke(dataArray); } } else { if (Message != null) { byte[] dataArray = new byte[length]; Marshal.Copy(new IntPtr(data), dataArray, 0, length); var msg = System.Text.Encoding.UTF8.GetString(dataArray); Message?.Invoke(msg); } } }