我正在参与一个实时流式传输视频(从网络摄像头拍摄)的大学项目,并使用 C#(UWP、Windows 10 IoT Core)将其流式传输到桌面。尽管我发现一些项目使用 Java(针对 Rasp)执行服务器端实现,并使用 UWP 执行客户端,但我找不到任何有关 C# 中服务器端编程的项目。
另外,是否真的可以使用 C# 进行服务器端编程以进行直播,正如 Microsoft 链接所说的那样。 View the Microsoft Link
任何帮助将不胜感激。
问候, TS
最佳答案
block 引用>Even though I found some projects doing the server side implementation in Java (For Rasp) and Client side using UWP I couldn't find any Projects regarding Server side programming in C#.
还有另一个项目我已经编码并测试成功。如果对您有帮助的话可以引用一下。
在 MyVideoServer 应用程序中,重要的是获取视频的摄像机 ID 和预览帧。
previewFrame = await MyMediaCapture.GetPreviewFrameAsync(videoFrame);
然后通过streamSocketClient将视频流发送到客户端。await streamSocketClient.sendBuffer(buffer);
public MainPage() { this.InitializeComponent(); InitializeCameraAsync(); InitSocket(); } MediaCapture MyMediaCapture; VideoFrame videoFrame; VideoFrame previewFrame; IBuffer buffer; DispatcherTimer timer; StreamSocketListenerServer streamSocketSrv; StreamSocketClient streamSocketClient; private async void InitializeCameraAsync() { var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation cameraDevice = allVideoDevices.FirstOrDefault(); var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; MyMediaCapture = new MediaCapture(); try { await MyMediaCapture.InitializeAsync(mediaInitSettings); } catch (UnauthorizedAccessException) { } PreviewControl.Height = 180; PreviewControl.Width = 240; PreviewControl.Source = MyMediaCapture; await MyMediaCapture.StartPreviewAsync(); videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, 240, 180, 0); buffer = new Windows.Storage.Streams.Buffer((uint)(240 * 180 * 8)); }
然后关键的服务器代码是在InitSocket函数中尝试创建服务器并通过套接字通信连接客户端。 StreamSocketListenerServer 应创建为对象并启动。同时设置服务器ip端口。
streamSocketSrv = new StreamSocketListenerServer(); await streamSocketSrv.start("22333");
最后但并非最不重要的一点是,Timer_Tick 将每 100 毫秒向客户端发送视频流。private async void InitSocket() { streamSocketSrv = new StreamSocketListenerServer(); await streamSocketSrv.start("22333"); streamSocketClient = new StreamSocketClient(); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(100); timer.Tick += Timer_Tick; timer.Start(); }
接下来您可以在 Raspberry Pi 3 上部署 MyVideoServer 应用程序。 然后您可以在PC上部署MyVideoClient应用程序。然后输入 Raspberry Pi 3 IP 地址并单击“连接”按钮。视频流将显示在应用程序上。
这是sample代码,你可以引用一下。
关于c# - 使用 Raspberry Pi 和 C# 进行实时视频流传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849919/