Here is the code:
private async Task<IDisposable> SendStreamAsync(MediaEncodingProfile encodingProfile, MediaCapture mediaCapture) { var socket = new DatagramSocket(); var outputStream = await socket.GetOutputStreamAsync(new HostName("example.com"), "12345"); var writeOnlyStream = new WriteOnlyStreamStream(outputStream); await mediaCapture.StartRecordToStreamAsync(encodingProfile, writeOnlyStream); return new CompositeDisposable {writeOnlyStream, outputStream, socket}; } private sealed class WriteOnlyStreamStream : IRandomAccessStream { readonly IOutputStream _outputStream; public WriteOnlyStreamStream(IOutputStream outputStream) { _outputStream = outputStream; } public IInputStream GetInputStreamAt(ulong position) { throw new NotSupportedException(); //or we can return empty stream } public IOutputStream GetOutputStreamAt(ulong position) { return _outputStream; } public ulong Size { get { return 0; } set { } } public bool CanRead => false; public bool CanWrite => true; public IRandomAccessStream CloneStream() { throw new NotSupportedException(); } public ulong Position => 0; public void Seek(ulong position) { } public void Dispose() { this._outputStream.Dispose(); } public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { throw new NotSupportedException(); } public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) { return _outputStream.WriteAsync(buffer); } public IAsyncOperation<bool> FlushAsync() { return _outputStream.FlushAsync(); } }
What's going on here:
- Created a Udp socket (packets will be lost, beaten, no integrity in it)
- We created a special class wrapper to push the IOutputStream inside MediaCapture. There are options, you can return an empty IInputStream instead of exceptions.
- We created IDisposable, which will need to be closed after all operations (we use Reactive Extensions).
As correctly advised in questions, here it is better to use TCP sockets. For the client side, everything will be 1-in-1, only classes will change a bit. For the server part, you need to use the class StreamSocketListener (it is in uwp), from which you already have to listen to some port.