[英]Live Video Streaming using Raspberry Pi and C#
我在一個直播項目中,通過視頻將視頻(從網絡攝像頭拍攝)流式傳輸並使用C#(UWP,Windows 10 IoT核心版)將其流式傳輸到桌面。 即使我發現有些項目使用UWP在Java(對於Rasp)和客戶端方面進行服務器端實現,我也找不到關於C#中服務器端編程的任何項目。
另外,是否真的有可能使用C#進行此類服務器端編程以進行實時流傳輸,因為此Microsoft鏈接說並非如此。 查看Microsoft鏈接
任何幫助將不勝感激。
問候,TS
即使我發現有些項目使用UWP在Java(對於Rasp)和客戶端方面進行服務器端實現,我也找不到關於C#中服務器端編程的任何項目。
我已經編碼並成功測試了另一個項目。 如果可以幫助您,您可以參考。
在MyVideoServer應用程序中,重要的是獲取視頻的攝像機ID和PreviewFrame。 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 App。 然后,您可以在PC上部署MyVideoClient App。 然后輸入Raspberry Pi 3 IP地址並單擊“連接”按鈕。 視頻流將顯示在應用程序上。
這是示例代碼,您可以參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.