Browse Source

temp commit for demon

arthur.wu 2 years ago
parent
commit
0708bb29bc

+ 42 - 26
VRTC/RtcUtil.x64/MediaDevices.cpp

@@ -2,36 +2,52 @@
 #include "MediaDevices.h"
 #include "webrtc/modules/video_capture/video_capture_factory.h"
 
-MediaDevices::MediaDevices()
-{
-}
 
-MediaDevices::~MediaDevices()
+int __stdcall CreateVideoDeviceInfo(void** hDeviceInfo)
 {
+	try
+	{
+		webrtc::VideoCaptureModule::DeviceInfo* deviceInfo = webrtc::VideoCaptureFactory::CreateDeviceInfo();		
+		*hDeviceInfo = deviceInfo;
+
+		int num = deviceInfo->NumberOfDevices();
+		return num;
+	}
+	catch (int e)
+	{
+		*hDeviceInfo = nullptr;
+		return 0;
+	}	
 }
 
-std::vector<MediaDeviceInfo> MediaDevices::EnumerateDevices()
+int __stdcall GetVidoeDeviceInfo(void* hDeviceInfo, uint32_t deviceNumber, void** mediaDeviceInfo)
 {
-	std::vector<MediaDeviceInfo> devices;
-	std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(webrtc::VideoCaptureFactory::CreateDeviceInfo());
-	if (info)
-	{
-		int num_devices = info->NumberOfDevices();
-		for (int i = 0; i < num_devices; ++i)
-		{
-			const uint32_t kSize = 256;
-			char name[kSize] = { 0 };
-			char id[kSize] = { 0 };
-			char pId[kSize] = { 0 };
-			if (info->GetDeviceName(i, name, kSize, id, kSize, pId, kSize) != -1)
-			{
-				MediaDeviceInfo deviceInfo;
-				deviceInfo.label = name;
-				deviceInfo.deviceId = id;				
-				devices.push_back(deviceInfo);
-			}
-		}
-	}
+	try
+	{		
+		webrtc::VideoCaptureModule::DeviceInfo* deviceInfo = static_cast<webrtc::VideoCaptureModule::DeviceInfo*>(hDeviceInfo);
+		std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(deviceInfo);
+		const uint32_t kSize = 256;
+		char deviceNameUTF8[kSize] = {0};		
+		char deviceUniqueIdUTF8[kSize] = { 0 };		
+		char productUniqueIdUTF8[kSize] = { 0 };
+		
+		VideoDeviceInfo* df = (VideoDeviceInfo*)malloc(sizeof(struct VideoDeviceInfo)); //TODO who will free it ?
+		int32_t iRet = info->GetDeviceName(deviceNumber, deviceNameUTF8, kSize, deviceUniqueIdUTF8, kSize, productUniqueIdUTF8, kSize);
 
-	return devices;
+		/*MyDeviceInfo df;*/
+		df->deviceNumber = deviceNumber;
+		strcpy(df->deviceNameUTF8, deviceNameUTF8);
+		strcpy(df->deviceUniqueIdUTF8, deviceUniqueIdUTF8);
+		strcpy(df->productUniqueIdUTF8, productUniqueIdUTF8);
+				
+		void* result = static_cast<void*>(df);
+		*mediaDeviceInfo = result;
+		
+		return (int)iRet;
+		//return 0;
+	}
+	catch (int e)
+	{		
+		return -1;
+	}
 }

+ 21 - 9
VRTC/RtcUtil.x64/MediaDevices.h

@@ -2,6 +2,11 @@
 
 #include <vector>
 
+EXTERN_C{
+_declspec(dllexport) int __stdcall CreateVideoDeviceInfo(void** deviceInfo);
+_declspec(dllexport) int __stdcall GetVidoeDeviceInfo(void* hDeviceInfo, uint32_t deviceNumber, void** mediaDeviceInfo);
+}
+
 enum MediaDeviceKind {
 	AUDIOINPUT,
 	AUDIOOUTPUT,
@@ -18,6 +23,22 @@ public:
 	std::string label;
 };
 
+struct VideoDeviceInfo
+{
+	uint32_t deviceNumber;
+	char deviceNameUTF8[256];	
+	char deviceUniqueIdUTF8[256];
+	char productUniqueIdUTF8[256];
+
+	VideoDeviceInfo()
+	{
+		deviceNumber = 0;
+		memset(deviceNameUTF8, 0, 256);
+		memset(deviceUniqueIdUTF8, 0, 256);
+		memset(productUniqueIdUTF8, 0, 256);
+	}
+};
+
 MediaDeviceInfo::MediaDeviceInfo()
 {
 }
@@ -26,14 +47,5 @@ MediaDeviceInfo::~MediaDeviceInfo()
 {
 }
 
-class MediaDevices
-{
-public:
-	MediaDevices();
-	~MediaDevices();
-    static std::vector<MediaDeviceInfo> EnumerateDevices();
 
-private:
-
-};
 

+ 2 - 0
VRTC/RtcUtil.x64/RtcUtil.x64.vcxproj

@@ -24,6 +24,7 @@
     <ProjectGuid>{7517cd46-4a5e-4a97-894e-6c6991517b96}</ProjectGuid>
     <RootNamespace>RtcUtilx64</RootNamespace>
     <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
+    <ProjectName>RtcUtil.Native.x64</ProjectName>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -78,6 +79,7 @@
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     <LinkIncremental>true</LinkIncremental>
+    <OutDir>$(SolutionDir)\bin\net6.0-windows\x64</OutDir>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     <LinkIncremental>false</LinkIncremental>

+ 62 - 0
VRTC/RtcUtil/MediaDevices.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace RtcUtil
+{
+    public enum MediaDeviceKind
+    {
+        AUDIOINPUT,
+        AUDIOOUTPUT,
+        VIDEOINPUT
+    };
+
+    public class MediaDeviceInfo
+    {
+        public string DeviceId { get; set; }
+        public string Label { get; set; }
+        public MediaDeviceKind Kind { get; set; }
+        public string GroupId { get; set; }
+    }
+
+    [StructLayout(LayoutKind.Sequential)]
+    public struct NativeMediaDeviceInfo
+    {
+        public UInt32 DeviceNumber;
+        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
+        public string DeviceNameUTF8;        
+        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
+        public string DeviceUniqueIdUTF8;        
+        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
+        public string ProductUniqueIdUTF8;        
+    }
+
+    public class MediaDevices
+    {
+        [DllImport("x64\\RtcUtil.Native.x64.dll", EntryPoint = "CreateVideoDeviceInfo", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        internal static extern int CreateVideoDeviceInfo(out IntPtr deviceInfo);
+        [DllImport("x64\\RtcUtil.Native.x64.dll", EntryPoint = "GetVidoeDeviceInfo", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        internal static extern int GetVidoeDeviceInfo(IntPtr hDeviceInfo, UInt32 deviceNumber, out IntPtr meadiaDeviceInfo);
+        public static IList<string> EnumerateVideoDevices()
+        {
+            var list = new List<string>();  
+            IntPtr ptrInfo;
+            var num = CreateVideoDeviceInfo(out ptrInfo);
+            if (ptrInfo != IntPtr.Zero)
+            {
+                for(UInt32 i =0;i< num; i++)
+                {
+                    IntPtr ptrMdDeviceInfo;
+                    var retValue = GetVidoeDeviceInfo(ptrInfo, i, out ptrMdDeviceInfo);
+                    if (retValue != -1)
+                    {
+                        var info = (NativeMediaDeviceInfo)Marshal.PtrToStructure(ptrMdDeviceInfo, typeof(NativeMediaDeviceInfo));
+                        list.Add(info.DeviceNameUTF8);//TODO need other properties , such as ID ?
+                    }
+                }
+                
+            }
+
+            return list;
+        }
+    }
+}

+ 14 - 0
VRTC/RtcUtil/RtcUtil.csproj

@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+    <BaseOutputPath></BaseOutputPath>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\RtcUtil.x64\RtcUtil.x64.vcxproj" />
+  </ItemGroup>
+
+</Project>

+ 8 - 1
VRTC/VRTC.WpfClient/MainWindow.xaml

@@ -32,8 +32,9 @@
                 </ListBox>
             </Grid>
         </GroupBox>
-        <Grid Grid.Row="1">
+        <Grid Grid.Row="1" Grid.RowSpan="2">
             <Grid.RowDefinitions>
+                <RowDefinition Height="Auto"></RowDefinition>
                 <RowDefinition Height="Auto"></RowDefinition>
                 <RowDefinition Height="*"></RowDefinition>
             </Grid.RowDefinitions>
@@ -44,9 +45,15 @@
                 <TextBox Text="{Binding RoomId}"></TextBox>
                 <Label VerticalAlignment="Center">Client ID:</Label>
                 <TextBox Text="{Binding UserId}"></TextBox>
+                <Label VerticalAlignment="Center">Device:</Label>
+                <ComboBox Width="200" VerticalAlignment="Center" ItemsSource="{Binding VideoDevices}" SelectedItem="{Binding DefaultVideoDevice}"></ComboBox>
+                <CheckBox Margin="7,0" VerticalAlignment="Center" IsChecked="{Binding AudioEnabled}">Audio Enabled</CheckBox>
                 <Button Command="{Binding ConnectCommand}">Connect</Button>
                 <Button Command="{Binding ExitCommand}">Close</Button>
             </StackPanel>
+            <StackPanel Grid.Row="1" Orientation="Horizontal">
+                
+            </StackPanel>
             
            
 

+ 3 - 0
VRTC/VRTC.WpfClient/VRTC.WpfClient.csproj

@@ -7,6 +7,8 @@
     <UseWPF>true</UseWPF>
     <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
     <Platforms>AnyCPU;x64</Platforms>
+    <BaseOutputPath>..\bin\</BaseOutputPath>
+	<OutputPath>..\bin\</OutputPath>
   </PropertyGroup>
 
 	<ItemGroup>
@@ -16,6 +18,7 @@
 	</ItemGroup>
 
 	<ItemGroup>
+	  <ProjectReference Include="..\RtcUtil\RtcUtil.csproj" />
 	  <ProjectReference Include="..\WebRtc.NET\WebRtc.NET.vcxproj" />
 	</ItemGroup>
 

+ 45 - 11
VRTC/VRTC.WpfClient/ViewModels/MainWindowViewModel.cs

@@ -1,4 +1,5 @@
-using System;
+using RtcUtil;
+using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
@@ -11,12 +12,14 @@ using WebRtc.NET;
 namespace VRTC.WpfClient
 {
     class MainWindowViewModel : ViewModel
-    {        
+    {
         private string _hostUrl;
         private uint _roomId;
         private string _userId;
         private VRTCClient _client;
         private readonly Action<Action> _runOnMainDispatcher;
+        private bool _audioEnabled;
+        private string _defaultVideoDevice;
 
         public ButtonCommand ConnectCommand { get; set; }
 
@@ -60,7 +63,7 @@ namespace VRTC.WpfClient
             }
         }
 
-       
+
         //private List<SequenceExecutor<int>> _sequenceExecutors = new List<SequenceExecutor<int>>();
         /// <summary>
         /// Request window close
@@ -93,6 +96,34 @@ namespace VRTC.WpfClient
 
         public ObservableCollection<VideoViewModel> RemoteVideos { get; }
 
+        public IList<string> VideoDevices { get; }
+
+        public string DefaultVideoDevice
+        {
+            get => _defaultVideoDevice;
+            set
+            {
+                if (_defaultVideoDevice != value)
+                {
+                    _defaultVideoDevice = value;
+                    OnPropertyChanged(()=>DefaultVideoDevice);
+                }
+            }
+        }
+
+        public bool AudioEnabled
+        {
+            get { return _audioEnabled; }
+            set
+            {
+                if (_audioEnabled != value)
+                {
+                    _audioEnabled = value;
+                    OnPropertyChanged(() => AudioEnabled);
+                }
+            }
+        }
+
         public MainWindowViewModel(Action<Action> runOnMainDispatcher)
         {
             LogItems = new ObservableCollection<LogItem>();
@@ -111,40 +142,43 @@ namespace VRTC.WpfClient
             LocalVideo = new VideoViewModel();
             RemoteVideos = new ObservableCollection<VideoViewModel>();
             this._runOnMainDispatcher = runOnMainDispatcher;
+            VideoDevices = MediaDevices.EnumerateVideoDevices();
+            DefaultVideoDevice = VideoDevices.FirstOrDefault();
         }
 
         private void OnExit(object obj)
         {
-            if(_client!= null)
+            if (_client != null)
             {
                 _client.LocalVideoFrameReceived -= OnRenderLocal;
                 _client.RemoteVieoFrameReceived -= OnRendRemote;
             }
-            
+
             _client?.Dispose();
             _client = null;
         }
 
         private async void OnConnect(object obj)
         {
-            try {
+            try
+            {
                 var device = ManagedConductor.GetVideoDevices().First();
                 _client = new VRTCClient(device);
-               
+
                 await _client.Connect(HostUrl, RoomId, UserId, false);
                 _client.LocalVideoFrameReceived += OnRenderLocal;
                 _client.RemoteVieoFrameReceived += OnRendRemote;
             }
             catch (Exception ex) { Logger.WriteLineError($"OnConnect ex:{ex}"); }
-           
+
         }
 
         private void OnRendRemote(object? sender, VideoFrameData e)
         {
-            var videoVm = RemoteVideos.FirstOrDefault(x=>x.Id == e.ClientId);
+            var videoVm = RemoteVideos.FirstOrDefault(x => x.Id == e.ClientId);
             if (videoVm == null)
             {
-                videoVm = new VideoViewModel() { Id = e.ClientId};
+                videoVm = new VideoViewModel() { Id = e.ClientId };
                 _runOnMainDispatcher?.Invoke(() => { RemoteVideos.Add(videoVm); });
             }
             videoVm.OnFrameReceived(this, e);
@@ -170,7 +204,7 @@ namespace VRTC.WpfClient
 
 
         private void OnCloseCommand(object obj)
-        {           
+        {
             OnRequestClosed();
         }
 

+ 14 - 0
VRTC/VRTC.sln

@@ -20,6 +20,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebRtc.NET", "WebRtc.NET\We
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RtcUtil.x64", "RtcUtil.x64\RtcUtil.x64.vcxproj", "{7517CD46-4A5E-4A97-894E-6C6991517B96}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RtcUtil", "RtcUtil\RtcUtil.csproj", "{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -78,6 +80,18 @@ Global
 		{7517CD46-4A5E-4A97-894E-6C6991517B96}.Release|x64.Build.0 = Release|x64
 		{7517CD46-4A5E-4A97-894E-6C6991517B96}.Release|x86.ActiveCfg = Release|Win32
 		{7517CD46-4A5E-4A97-894E-6C6991517B96}.Release|x86.Build.0 = Release|Win32
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|x64.ActiveCfg = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|x64.Build.0 = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Debug|x86.Build.0 = Debug|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|Any CPU.Build.0 = Release|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|x64.ActiveCfg = Release|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|x64.Build.0 = Release|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|x86.ActiveCfg = Release|Any CPU
+		{A8BF94B2-26F0-4987-8DDC-F94A14FC4742}.Release|x86.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 17 - 17
VRTC/VRTC/Program.cs

@@ -15,24 +15,24 @@ try
         {
             var text = "192.168.6.175";//TODO should from settings
             //var ok = mc.RunStunServer("192.168.6.175:3478");
-            var ok = mc.RunTurnServer("192.168.6.175:3478", text, "test", "auth.txt");
-            if (!ok)
-            {
-                Console.WriteLine("TURN server start failed ;/");
-            }           
-            else
-            {
-                using (turnCancel = new CancellationTokenSource())
-                {
-                    var stop = turnCancel.Token;
-                    while (!stop.IsCancellationRequested && mc.ProcessMessages(1000))
-                    {
-                        Debug.Write(".");
-                    }
+            //var ok = mc.RunTurnServer("192.168.6.175:3478", text, "test", "auth.txt");
+            //if (!ok)
+            //{
+            //    Console.WriteLine("TURN server start failed ;/");
+            //}           
+            //else
+            //{
+            //    using (turnCancel = new CancellationTokenSource())
+            //    {
+            //        var stop = turnCancel.Token;
+            //        while (!stop.IsCancellationRequested && mc.ProcessMessages(1000))
+            //        {
+            //            Debug.Write(".");
+            //        }
 
-                    Console.WriteLine("TURN server stoped.");
-                }
-            }
+            //        Console.WriteLine("TURN server stoped.");
+            //    }
+            //}
         }
     }, TaskCreationOptions.LongRunning);
     while (true)