soundbeam – Blame information for rev 2

Subversion Repositories:
Rev:
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  
2 office 62 private static readonly BindingList<DiscoveredServer> DiscoveredServersBindingList = new BindingList<DiscoveredServer>();
63  
64 private static readonly BindingSource DiscoveredServersBindingSource =
65 new BindingSource(DiscoveredServersBindingList, null);
66  
1 office 67 public Form1()
68 {
69 InitializeComponent();
70  
2 office 71 // Set the tool strip combo box binding.
72 sendToolStripComboBox.ComboBox.DisplayMember = "Hostname";
73 sendToolStripComboBox.ComboBox.ValueMember = "Hostname";
74 sendToolStripComboBox.ComboBox.DataSource = DiscoveredServersBindingSource;
75 sendToolStripComboBox.ComboBox.BindingContext = this.BindingContext;
76  
1 office 77 // Register local service.
78 SoundBeamRegisterService.Name = "SoundBeam Audio Sink";
79 SoundBeamRegisterService.RegType = "_soundbeam._tcp";
80 SoundBeamRegisterService.Port = 9000;
2 office 81 SoundBeamRegisterService.UPort = 9000;
82 SoundBeamRegisterService.ReplyDomain = "local.";
1 office 83  
84 SoundBeamRegisterService.Response += OnRegisterServiceResponse;
85 SoundBeamRegisterService.Register();
86  
87 // Browse for services.
88 SoundBeamServiceBrowser.ServiceAdded += OnSoundBeamServiceBrowserServiceAdded;
2 office 89 SoundBeamServiceBrowser.Browse(0, AddressProtocol.Any, "_soundbeam._tcp", "local.");
1 office 90 }
91  
92 /// <summary>
93 /// Clean up any resources being used.
94 /// </summary>
95 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
96 protected override void Dispose(bool disposing)
97 {
98 if (disposing && (components != null))
99 {
100  
101 TcpServer.Dispose();
102 WasapiCapture.Dispose();
103 SoundBeamRegisterService.Dispose();
104 SoundBeamServiceBrowser.Dispose();
105 components.Dispose();
106 }
107 base.Dispose(disposing);
108 }
109  
110 private void OnTcpServerClientConnected(object sender, TcpClientConnectedEventArgs args)
111 {
112 WasapiOut.Add(args.TcpClient, new WasapiOut());
113 using (var waveReader = new WaveFileReader(args.TcpClient.GetStream()))
114 {
115 WasapiOut[args.TcpClient].Initialize(waveReader);
116 WasapiOut[args.TcpClient].Play();
117 }
118 }
119  
120 private void OnSoundBeamServiceBrowserServiceAdded(object o, ServiceBrowseEventArgs args)
121 {
2 office 122 if (args?.Service == null)
123 return;
124  
125 args.Service.Resolved += OnSoundBeamServiceResolved;
126 args.Service.Resolve();
1 office 127 }
128  
2 office 129 private void OnSoundBeamServiceResolved(object o, ServiceResolvedEventArgs args)
130 {
131 if (args?.Service == null)
132 return;
133  
134 args.Service.Resolved -= OnSoundBeamServiceResolved;
135  
136 // Do not add already added servers.
137 if (DiscoveredServersBindingList.Any(server =>
138 string.Equals(server.Hostname, args.Service.HostTarget, StringComparison.OrdinalIgnoreCase) &&
139 server.Port == args.Service.Port))
140 return;
141  
142 Invoke((Action) (() =>
143 {
144 DiscoveredServersBindingList.Add(new DiscoveredServer(args.Service.HostTarget, args.Service.Port));
145 }));
146 }
147  
1 office 148 private void OnRegisterServiceResponse(object o, RegisterServiceEventArgs args)
149 {
150 if (args.IsRegistered)
151 {
152 notifyIcon1.ShowBalloonTip(250, "SoundBeam Audio Sink Registered", "Bonjour service registered successfully.", ToolTipIcon.Info);
153 return;
154 }
155  
156 notifyIcon1.ShowBalloonTip(250, "SoundBeam Sink Failed to Register", "Failed to register SoundBeam sink with bonjour.", ToolTipIcon.Error);
157 }
158  
159 private void OnCaptureDataAvailable(object sender, DataAvailableEventArgs args)
160 {
161 // Send data to destination.
162 }
163  
164 private void OnReceiveToolStripMenuItemCheckedChanged(object sender, EventArgs e)
165 {
166 switch (((ToolStripMenuItem) sender).Checked)
167 {
168 case true:
169 // Listen for TCP connections.
170 TcpServer.ClientConnected += OnTcpServerClientConnected;
171 TcpServer.Start();
172 break;
173 default:
174 TcpServer.ClientConnected -= OnTcpServerClientConnected;
175 TcpServer.Stop();
176 break;
177 }
178 }
179  
180 private void OnSendToolStripMenuItemCheckedChanged(object sender, EventArgs e)
181 {
182  
183 }
184  
185 private void OnQuitToolStripMenuItemClick(object sender, EventArgs e)
186 {
2 office 187 Dispose(true);
188  
1 office 189 Application.Exit();
190 }
2 office 191  
192 private void OnFormLoad(object sender, EventArgs e)
193 {
194  
195 }
1 office 196 }
197 }