I write down the picture from the camera to the stream:

var stream = new InMemoryRandomAccessStream(); await _mediaCapture.StartRecordToStreamAsync(encodingProfile,stream); 

Task: somehow break this into bytes and transfer it to the socket, and create a stream again at the reception? It turns out a kind of broadcast.

The stream must be encrypted and decrypted using aes. The encryption method is implemented, takes an array of bytes. How to transfer there a stream?

  • one
    It is not necessary to break into bytes. Just write in the socket. TCP itself will take care of the fragmentation of the data sent. And surely it will not be as you intend. Your task will be to collect these pieces on the other side. - Max ZS
  • I will have UDP, not TCP. And how to write a stream to a socket? I only know about SocketAsyncEventArgs, where there is a buffer parameter, and then this object is passed to the socket. What is the problem of collecting on that side? - Sanych Goilo
  • UDP does not work with the stream, it works with datagrams. Since the arrival order of datagrams is not guaranteed, you cannot send data so easily, you need some markers of what the data is. - VladD
  • Ok, how to make a datagram? - Sanych Goilo
  • @SanychGoilo, why complicate things? Why UDP? Use TCP client or socket! I also advise the NoDelay property of the client or socket to be set to true. - Alexis

1 answer 1

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:

  1. Created a Udp socket (packets will be lost, beaten, no integrity in it)
  2. We created a special class wrapper to push the IOutputStream inside MediaCapture. There are options, you can return an empty IInputStream instead of exceptions.
  3. 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.

  • About udp often flashes the phrase "Packets will beat" - what does this mean? For example, I transfer the video, does this mean that the client can see a bad picture? - Sanych Goilo pm
  • one
    It all depends on the codec. The playback may simply stop, the frame may be lost, digital interference may come (when the picture as if "crumbles"). For example, there are different types of frames (see Wikipedia). There is a so-called. "reference frames" that are encoded as a picture. If there are others that are set in the style "in-he is the reference one, but you need to change these pixels, and move these fragments up by 35 pixels, etc.". It is immediately clear that the broken reference frame will make a problem for the entire sequence. When transmitting via TCP, there will be a pause until the data is transmitted over the channel. - Manushin Igor
  • And how often does this packet loss happen? - Sanych Goilo
  • one
    This should already be studied in more detail. If you work on a local network on a free channel (i.e., without torrents, etc.), then almost never. The provider’s speed limit is dropping packets (see the current bucket's algorithm - ru.wikipedia.org/wiki/… ). If there is work on the mobile network, then the percentage can be large, especially if there is a reconnection between the protocols. - Manushin Igor 4:41 pm
  • 2
    And it is almost not used for simple programs. The video can be lost (this is normal, clients can handle it correctly), in games for a couple of seconds, it can also be a blunt (and then, all this applies only to shooters) - Manushin Igor