Commit 97b6857b authored by Manu's avatar Manu 🏅
Browse files

Implement audio sender app

parent 99af9fa7

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectoTalk", "DirectoTalk\DirectoTalk.csproj", "{F1071CCA-C3E3-4B0A-8F45-C230E0A4955B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F1071CCA-C3E3-4B0A-8F45-C230E0A4955B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1071CCA-C3E3-4B0A-8F45-C230E0A4955B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1071CCA-C3E3-4B0A-8F45-C230E0A4955B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1071CCA-C3E3-4B0A-8F45-C230E0A4955B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FD0E7140-AF9F-4576-AD7C-55BEB5E678DD}
EndGlobalSection
EndGlobal
<Application x:Class="DirectoTalk.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DirectoTalk"
StartupUri="TalkWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace DirectoTalk
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
using DirectoTalk.ViewModels;
using NAudio.Wave;
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
namespace DirectoTalk
{
public class CallingMachine : IDisposable
{
public NetworkHandler NetworkHandler { get; set; }
private WaveIn waveSource = null;
public CallingMachine(string ipToCall)
{
NetworkHandler = new NetworkHandler(ipToCall);
}
public void StartCall()
{
if (waveSource == null)
{
waveSource = new WaveIn
{
WaveFormat = new WaveFormat(44100, 1)
};
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(WaveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(WaveSource_RecordingStopped);
waveSource.StartRecording();
}
}
public void StopCall()
{
if (waveSource != null)
{
waveSource.StopRecording();
waveSource.Dispose();
}
}
private async void WaveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (Application.Current.MainWindow != null)
{
TalkViewModel talkVM = (TalkViewModel)Application.Current.MainWindow.DataContext;
talkVM.AmountOfBytes = e.BytesRecorded.ToString(CultureInfo.InvariantCulture);
talkVM.AudioData = string.Join(' ', e.Buffer.Take(50));
SendResult result = await NetworkHandler.SendAudio(e.Buffer).ConfigureAwait(false);
talkVM.CurrentStatusCode = result.StatusCode;
talkVM.CurrentResponse = result.Message;
}
}
private void WaveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
}
public void Dispose() => StopCall();
}
}
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<StartupObject>DirectoTalk.App</StartupObject>
<ApplicationIcon>call.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="call.ico" />
<None Remove="call.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NAudio" Version="1.10.0" />
</ItemGroup>
<ItemGroup>
<Content Include="call.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="call.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
\ No newline at end of file
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace DirectoTalk
{
public class NetworkHandler : IDisposable
{
private readonly HttpClient httpClient = new HttpClient();
public NetworkHandler(string destinationAddress)
{
httpClient.BaseAddress = new Uri(destinationAddress);
}
public async Task<SendResult> SendAudio(byte[] buffer)
{
ByteArrayContent binaryContent = new ByteArrayContent(buffer);
binaryContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
using (var httpRequest = new HttpRequestMessage())
{
httpRequest.Content = binaryContent;
try
{
HttpResponseMessage response = await httpClient.SendAsync(httpRequest).ConfigureAwait(false);
return new SendResult(response.StatusCode.ToString(), await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
catch (Exception ex)
{
return new SendResult("XXX", $"{ex.Message} {ex.InnerException?.Message}");
}
}
}
public void Dispose()
{
httpClient.Dispose();
}
}
}
\ No newline at end of file
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DirectoTalk.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DirectoTalk.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] call {
get {
object obj = ResourceManager.GetObject("call", resourceCulture);
return ((byte[])(obj));
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="call" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\call.ico;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
\ No newline at end of file
namespace DirectoTalk
{
public class SendResult
{
public SendResult(string statusCode, string message)
{
StatusCode = statusCode;
Message = message;
}
public string StatusCode { get; set; }
public string Message { get; set; }
}
}
\ No newline at end of file
<Window x:Class="DirectoTalk.TalkWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DirectoTalk"
mc:Ignorable="d"
Title="DirectoTalk" MinHeight="240" Height="540" MinWidth="400" Width="400" Icon="call.ico">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<DockPanel>
<Label Margin="10" Padding="5">Partner IP:</Label>
<TextBox Margin="10" Padding="5" Text="127.0.0.1"></TextBox>
</DockPanel>
<DockPanel Grid.Row="0" Grid.Column="1" >
<Label Margin="10" Padding="5">Unused:</Label>
<TextBox Margin="10" Padding="5"></TextBox>
</DockPanel>
<Button Grid.Row="1" Grid.Column="0" Margin="10" Padding="5" Click="CallButton_Click" IsEnabled="{Binding IsCallEnabled}">CALL</Button>
<Button Grid.Row="1" Grid.Column="1" Margin="10" Padding="5" Click="HangUpButton_Click" IsEnabled="{Binding IsHangUpEnabled}">HANG UP</Button>
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="10" Padding="5,1,5,5" TextWrapping="Wrap" Background="Black">
<TextBlock.Inlines>
<Run Foreground="#ffffff" FontWeight="Bold" Text="{Binding CurrentStatusCode}" />
<LineBreak></LineBreak>
<Run Foreground="#ffffff" FontWeight="Bold" Text="{Binding CurrentResponse}" />
<LineBreak></LineBreak>
<Run Foreground="#0000ff" FontWeight="Bold" Text="{Binding AmountOfBytes}" />
<LineBreak></LineBreak>
<Run Foreground="#00ff00" Text="{Binding AudioData}" />
</TextBlock.Inlines>
</TextBlock>
</Grid>
</Window>
using DirectoTalk.ViewModels;
using System;
using System.Windows;
namespace DirectoTalk
{
/// <summary>
/// Interaction logic for TalkWindow.xaml
/// </summary>
public partial class TalkWindow : Window, IDisposable
{
private readonly TalkViewModel TalkViewModel;
public TalkWindow()
{
InitializeComponent();
TalkViewModel = new TalkViewModel();
DataContext = TalkViewModel;
}
private void CallButton_Click(object sender, RoutedEventArgs e)
{
TalkViewModel.Call();
}
private void HangUpButton_Click(object sender, RoutedEventArgs e)
{
TalkViewModel.HangUp();
}
public void Dispose()
{
TalkViewModel.Dispose();
}
}
}
using System;
namespace DirectoTalk.ViewModels
{
public class TalkViewModel : ViewModel, IDisposable
{
public CallingMachine CallingMachine { get; set; }
public TalkViewModel()
{
CallingMachine = new CallingMachine(IpToCall);
}
private string ipToCall = "https://127.0.0.1";
public string IpToCall
{
get
{
return ipToCall;
}
set
{
ipToCall = value;
OnPropertyChanged(nameof(IpToCall));
}
}
private string amountOfBytes = "Amount of Bytes will come here";
public string AmountOfBytes
{
get
{
return amountOfBytes;
}
set
{
amountOfBytes = value;
OnPropertyChanged(nameof(AmountOfBytes));
}
}
private string audioData = "No data yet";
public string AudioData
{
get
{
return audioData;
}
set
{
audioData = value;
OnPropertyChanged(nameof(AudioData));
}
}
private bool isCallEnabled = true;
public bool IsCallEnabled
{
get
{
return isCallEnabled;
}
set
{
isCallEnabled = value;
OnPropertyChanged(nameof(IsCallEnabled));
}
}
private bool isHangUpEnabled = false;
public bool IsHangUpEnabled
{
get
{
return isHangUpEnabled;
}
set
{
isHangUpEnabled = value;
OnPropertyChanged(nameof(IsHangUpEnabled));
}
}
private string currentStatusCode;
public string CurrentStatusCode
{
get
{
return currentStatusCode;
}
set
{