soundbeam – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Data; |
||
5 | using System.Drawing; |
||
6 | using System.IO; |
||
7 | using System.Linq; |
||
8 | using System.Net.Sockets; |
||
9 | using System.Text; |
||
10 | using System.Threading; |
||
11 | using System.Threading.Tasks; |
||
12 | using System.Windows.Forms; |
||
13 | using CSCore; |
||
14 | using CSCore.Codecs.WAV; |
||
15 | using CSCore.SoundIn; |
||
16 | using CSCore.SoundOut; |
||
17 | using Mono.Zeroconf; |
||
18 | |||
19 | namespace SoundBeam |
||
20 | { |
||
21 | /*using (WasapiCapture capture = new WasapiLoopbackCapture()) |
||
22 | { |
||
23 | //if nessesary, you can choose a device here |
||
24 | //to do so, simply set the device property of the capture to any MMDevice |
||
25 | //to choose a device, take a look at the sample here: http://cscore.codeplex.com/ |
||
26 | |||
27 | //initialize the selected device for recording |
||
28 | capture.Initialize(); |
||
29 | capture.DataAvailable += OnCaptureDataAvailable; |
||
30 | |||
31 | //create a wavewriter to write the data to |
||
32 | using (WaveWriter w = new WaveWriter("dump.wav", capture.WaveFormat)) |
||
33 | { |
||
34 | //setup an eventhandler to receive the recorded data |
||
35 | capture.DataAvailable += (s, a) => |
||
36 | { |
||
37 | //save the recorded audio |
||
38 | w.Write(a.Data, a.Offset, a.ByteCount); |
||
39 | }; |
||
40 | |||
41 | //start recording |
||
42 | capture.Start(); |
||
43 | |||
44 | Console.ReadKey(); |
||
45 | |||
46 | //stop recording |
||
47 | capture.Stop(); |
||
48 | } |
||
49 | }*/ |
||
50 | public partial class Form1 : Form |
||
51 | { |
||
52 | private static readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource(); |
||
53 | |||
54 | private static readonly TcpServer TcpServer = new TcpServer("0.0.0.0", 9000); |
||
55 | |||
56 | private static readonly WasapiCapture WasapiCapture = new WasapiCapture(); |
||
57 | private static readonly RegisterService SoundBeamRegisterService = new RegisterService(); |
||
58 | private static readonly ServiceBrowser SoundBeamServiceBrowser = new ServiceBrowser(); |
||
59 | |||
60 | private static readonly Dictionary<TcpClient, WasapiOut> WasapiOut = new Dictionary<TcpClient, WasapiOut>(); |
||
61 | |||
62 | public Form1() |
||
63 | { |
||
64 | InitializeComponent(); |
||
65 | |||
66 | // Register local service. |
||
67 | SoundBeamRegisterService.Name = "SoundBeam Audio Sink"; |
||
68 | SoundBeamRegisterService.RegType = "_soundbeam._tcp"; |
||
69 | SoundBeamRegisterService.Port = 9000; |
||
70 | SoundBeamRegisterService.ReplyDomain = "local"; |
||
71 | |||
72 | SoundBeamRegisterService.Response += OnRegisterServiceResponse; |
||
73 | SoundBeamRegisterService.Register(); |
||
74 | |||
75 | // Browse for services. |
||
76 | SoundBeamServiceBrowser.ServiceAdded += OnSoundBeamServiceBrowserServiceAdded; |
||
77 | SoundBeamServiceBrowser.Browse(0, AddressProtocol.Any, "_soundbeam._tcp", "local"); |
||
78 | } |
||
79 | |||
80 | /// <summary> |
||
81 | /// Clean up any resources being used. |
||
82 | /// </summary> |
||
83 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
84 | protected override void Dispose(bool disposing) |
||
85 | { |
||
86 | if (disposing && (components != null)) |
||
87 | { |
||
88 | |||
89 | TcpServer.Dispose(); |
||
90 | WasapiCapture.Dispose(); |
||
91 | SoundBeamRegisterService.Dispose(); |
||
92 | SoundBeamServiceBrowser.Dispose(); |
||
93 | components.Dispose(); |
||
94 | } |
||
95 | base.Dispose(disposing); |
||
96 | } |
||
97 | |||
98 | private void OnTcpServerClientConnected(object sender, TcpClientConnectedEventArgs args) |
||
99 | { |
||
100 | WasapiOut.Add(args.TcpClient, new WasapiOut()); |
||
101 | using (var waveReader = new WaveFileReader(args.TcpClient.GetStream())) |
||
102 | { |
||
103 | WasapiOut[args.TcpClient].Initialize(waveReader); |
||
104 | WasapiOut[args.TcpClient].Play(); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | private void OnSoundBeamServiceBrowserServiceAdded(object o, ServiceBrowseEventArgs args) |
||
109 | { |
||
110 | var a = args.Service.HostTarget; |
||
111 | } |
||
112 | |||
113 | private void OnRegisterServiceResponse(object o, RegisterServiceEventArgs args) |
||
114 | { |
||
115 | if (args.IsRegistered) |
||
116 | { |
||
117 | notifyIcon1.ShowBalloonTip(250, "SoundBeam Audio Sink Registered", "Bonjour service registered successfully.", ToolTipIcon.Info); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | notifyIcon1.ShowBalloonTip(250, "SoundBeam Sink Failed to Register", "Failed to register SoundBeam sink with bonjour.", ToolTipIcon.Error); |
||
122 | } |
||
123 | |||
124 | private void OnCaptureDataAvailable(object sender, DataAvailableEventArgs args) |
||
125 | { |
||
126 | // Send data to destination. |
||
127 | } |
||
128 | |||
129 | private void OnReceiveToolStripMenuItemCheckedChanged(object sender, EventArgs e) |
||
130 | { |
||
131 | switch (((ToolStripMenuItem) sender).Checked) |
||
132 | { |
||
133 | case true: |
||
134 | // Listen for TCP connections. |
||
135 | TcpServer.ClientConnected += OnTcpServerClientConnected; |
||
136 | TcpServer.Start(); |
||
137 | break; |
||
138 | default: |
||
139 | TcpServer.ClientConnected -= OnTcpServerClientConnected; |
||
140 | TcpServer.Stop(); |
||
141 | break; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | private void OnSendToolStripMenuItemCheckedChanged(object sender, EventArgs e) |
||
146 | { |
||
147 | |||
148 | } |
||
149 | |||
150 | private void OnQuitToolStripMenuItemClick(object sender, EventArgs e) |
||
151 | { |
||
152 | Application.Exit(); |
||
153 | } |
||
154 | } |
||
155 | } |