Create live audio stream from line in device for HTML5 - c#

I am using NAudio to capture an audio signal from my line in device into a byte array. I can successfully send that byte array across my WLAN via UDP broadcast and receive it on another computer. Once the byte array has been received, I am able to play the audio stream.
My goal is to stream an audio signal from a line in device so it can be consumed by an HTML5 audio tag or jPlayer. Do you have an example or reading material on how to convert the input byte array to stream as compatible HTML5 format?
I would to to create a .Net solution without using any third party applications.
Here is a sample of how I am capturing and broadcasting the audio signal via UDP.
var waveIn = new WaveInEvent();
waveIn.DeviceNumber = deviceID;
waveIn.WaveFormat = Program.WAVEFORMAT;
waveIn.BufferMilliseconds = 50;
waveIn.DataAvailable += OnDataAvailable;
var udpSender = new UdpClient();
udpSender.JoinMulticastGroup(Program.MulticastIP);
waveIn.StartRecording();
private void OnDataAvailable(object sender, WaveInEventArgs e)
{
udpSender.Send(e.Buffer, e.BytesRecorded, Program.EndPoint);
}

The answer to your question is really dependent on whether you want to stream the audio to a HTML5 compatible browser, or whether you simply want to playback recorded audio.
According to W3Schools, all major browsers support the major audio formats (wav/ogg/mp3):
http://www.w3schools.com/html/html5_audio.asp
Playing a pre-recorded audio file using the HTML5 audio tag is pretty easy. The link above describes how to accomplish that.
Live-streaming the audio to a HTML5 browser is a whole different story. You need some kind of streaming server. I would advise against trying to implement this yourself. It's much easier to just use an external library for this.
The only reliable solution I can think of right now is using one of the freely available .Net P/Invoke libraries for libvlc, which provides access to the streaming capabilities of the VLC Media Player.
The following StackOverflow question describes how to setup an audio streaming server using libvlc:
Use libvlc to stream mp3 to network
You can try to accomplish the same thing in C#. It might require some P/Invoke work, but it's the most realiable solution I can think of.

Related

convert audio to bytes without recording c#

I am currently developing a c# Windows Form. My intention is to create a form that can make calls to other PCs in the same network.
So far I have found solutions that record the audio from my microphone and then convert it to bytes and send it using Tcp sockets. The thing is, is there a way to directly convert the audio to bytes and send it through a socket without recording the audio in a file and then send it.
Thanks in advance.
would converting the record to a memorystream be what your looking for?
if so you want this How to record audio using naudio onto byte[] rather than file
You can then write the stream to a tcpsocket. (you could write the thing direct to networkstream but i would consider it bad practice)
It would be wise to write samplerate*3 just in case of lantcy issues.

Record audio stream via rtsp from IP camera [C#]

Im new to this forum (as registered user) and trying to figure out how to properly get an audio stream from an IP camera via rtsp protocol and transfer it e.g directly to byte array.
It should be real time so I want to avoid creating any audio files or so.
I've found Ozeki SDK but it's shareware.
My project is only for academical purposes.
Of course Im not looking for exact solution, just for some proper library which could contain features to handle this issue.
Thank you very much for any answer in advance.

C# Video Streaming

I'm developing a C# video streaming application using NReco library. At this moment I was able to encode audio and video separately and save data in to queue. I can stream video using UDP protocol without audio and it played nicely in ffplay equally I can stream audio using UDP protocol without video and it also played nicely.Now I want to merge these two streams in to one and stream through UDP protocol and player should play both audio and video. But I have no idea how to do it. I would appreciate if some one can give me any points to do this or any other different method achieve this.
Thank You.
The answer highly depends on the source of video and audio streams. NReco.VideoConverter is a wrapper to FFMpeg tool and it actually can combine video and audio streams (see filters configuration in FFMpeg documentation) if either video or audio input can be specified as ffmpeg input source (UDP stream or direct show input device).
If both video and audio data represented by byte stream in your C# code you cannot pass them together using NReco.VideoConverter (ConvertLiveMedia method) because it uses stdin for communicating with ffmpeg and only one stream can be passed from C# code.

Audio Conversion C#

What is the best way to convert various audio formats to PCM?
For example: mp3, evrc, ogg vox.
Is there a library out there that will allow me to implement this relatively easily?
EDIT:
I guess my initial question wasn't really what I needed. Most of the libs I have found are file converters. What I need is a block converter, where I pass in a 1Kb block of vox data and it returns its converted PCM block. Of course I’ll have to tell the converter what type of data it is and various pieces of codec information.
The solution I am going for is to save and VOIP formats into a common wav format and to play that conformed file in real time. I thought there should be an easy way to do this because all audio is eventually turned into PCM before it is outputted anyways.
You can use NAudio to pass blocks of compressed audio into any ACM codecs you have installed on your machine. You do need to know how to create the appropriate WAVEFORMAT structure to describe the compressed audio type correctly though.
Check out AVBlocks SDK for .NET. It supports several audio formats, and audio transforms like Multi-channel audio to Stereo audio, resampling and bitrate conversion.
Try modified code from http://alvas.net/alvas.audio,tips.aspx#tip91
static void AnyToWav(string fileName)
{
DsReader dr = new DsReader(fileName);
IntPtr formatPcm = dr.ReadFormat();
byte[] dataPcm = dr.ReadData();
dr.Close();
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"), AudioCompressionManager.FormatBytes(formatPcm));
ww.WriteData(formatPcm);
ww.Close();
}
Don't know any lib that does it all but we do mp3->wav using madxlib.
It's free but I suggest paying the $10 for the sdk as it comes with documentation and examples.
There is a c# tutorial on youtube that might be helpful to you. It shows how to use a specific audio library called alvas.audio that really does some neat things with audio. I found the video to be very educational. The audio library is completely written in c#. Watch the video for more details: http://www.youtube.com/watch?v=2DIQECXFPeU

HowTo Multicast a Stream Captured with DirectShow?

I have a requirement to build a very simple streaming server. It needs to be able to capture video from a device and then stream that video via multicast to several clients on a LAN.
The capture part of this is pretty easy (in C#) thanks to a library someone wrote with DirectShow.Net (http://www.codeproject.com/KB/directx/directxcapture.aspx).
The question I have now is how to multicast this? This is the part I'm stuck on. I'm not sure what to do next, or what steps to take.
There are no filters available that you can plug and use.
You need to do three things here:
Compress the video into MPEG2 or MPEG4
Mux it into MPEG Transport Stream
Broadcast it
There are lots of codecs available for part 1, and some devices can even output compressed video.
The part 3 is quite simple too.
Main problem goes with part 2, as MPEG Transport Stream is patented. It is licensed so that you cannot develop free software based on it (VLC and FFMPEG violate that license), and you have to pay several hundred dollars just to obtain a copy of specification.
If you have to develop it, you need to:
Obtain a copy of ISO/IEC 13818-1-2000 (you may download it as PDF from their site), it describes MPEG Transport Stream
Develop a renderer filter that takes MPEG Elementary Streams and muxes them into Transport Stream
It has to be a renderer as Transport Stream is not a transform filter. There are some kind of outband data (program allocation tables and reference clocks) that need to be sent on a regular basis, and you need to keep a worker thread to do that.
To achieve that you need to setup/write some kind of video streaming server.
I've used VideoCapX for the same purpose on my project. The documentation and support is not top notch, but it's good enough. It's using WMV streaming technology. The stream is called MMS stream. You can view it with any most media player. I've tested with Windows Media Player, Media Player Classics and VLC. If you would like to see it's capability without writing any code just yet, take a look at U-Broadcast, it uses VideoCapX to do the job behind the scene.
I've been using DirectShow.Net for almost 2 years, and I still find it hard to write a streaming server myself, due to the complexity of DirectShow technology.
Other than WMV, you can take a look at Helix Server or Apple Streaming Server. The latter one is not free, so is WMV Streaming Server from Microsoft.
You can also take a look at VLC or Windows Media Encoder to do streaming straight from the application. But so far I find U-Broadcast out do both of the above. VLC has some compatibility issue with codec and playback from non VLC player, WME has problem with starting up capturing device.
Good Luck
NOTE: I'm not associated with VideoCapX or it's company, I'm just a happy user of it.
http://www.codeproject.com/KB/directx/DShowStreamingServer.aspx might help, and http://en.wikipedia.org/wiki/VLC_media_player#cite_note-14
VLC also "should" be able to stream from any device natively.

Categories

Resources