3
votes

I am trying write a real-time flv stream demuxer on windows-runtime, for win8.1 and wp8.1's MediaElement.

I've already finish the demux code, flv files can be correctly demuxed into h264 and aac tag-datas.

When I was trying to play network files and streams, I got a very strange network problem:

The same code,

  1. run under win8.1, all good, whatever files or network streams (proves demux code is ok);
  2. run under wp8.1 (real phone or emulator), files are good, network streams are bad - no matter how I read bytes from HttpClient Stream, the target server only gives me 65536 bytes data, and then the connection is choked, no any response and error, and even no timeout, it's just choking the thread.

Code for opening the stream:

var uri = new Uri("http://hdl.xxx.com/live/yyyy")
//uri is dymatic
var client = new HttpClient();
var stream = await client.GetStreamAsync(uri);
openStream(stream)

Code for reading the data:

public static byte[] ReadBlocks(this Stream stream, int count)
{
    byte[] buffer = new byte[count];
    int offset = 0;
    int length;

    while (offset < count)
    {
        //a loop statement to guarantee I can get *count* bytes
        Debug.WriteLine("read " + (count - offset));
        //a debug message show how many bytes do I need
        length = stream.Read(buffer, offset, count - offset);
        if (length == 0)
        {
            throw new EndOfStreamException();
        }
        Debug.WriteLine("got " + length);
        //a debug message show how many bytes I got
        offset += length;
    }
    return buffer;
}

For example, when I need to rea 1024 bytes from the flv stream, I run stream.ReadBlocks(1024) under wp8.1, the debug tells me like:

read 1024
got 768
read 256

and then nothing happend any more. I wrote an extra counter, the counter shows once server send a total of 65536 bytes, next Read method of stream will always be choked.

I'm sure the uri is available. I can download some stream data as a flv file by using pc web browser, and this downloaded flv file can be played under wp8.1 as well.

It looks like this problem only happens under wp8.1, android and ios are not affected.

So is it my code's problem or actually the server is not set up properly?

From last three weeks, I've tried every http method that can open a stream, but still got choked at 65536 bytes.

Could someone help me, please?

1
Can you share the server URL? Is the response chunked-encoded? - kiewic
Due to my further test, I found out the reason - wp8.1 sdk. It's not server's falut, just wp8.1 sdk's bug. I tried to use StreamSocket to emulate Http request, and then I got the full data stream. What a day! - Harreke
The too-late clue: in Flash we use URLStream or Sockets for this kind of thing. StreamSocket sounds like a hybrid version of those two. Side note: Their max packet size is 65536 bytes so a larger file just streams bytes in max size chunks until the last chunk which might be a lot less bytes - VC.One
Well, there is no URLStream in WinRT, the official SDK method for streams is corrupted, so I have to write one by manipulating sockets. The unacceptable fact is, in the same framework (WinRT for Win8.1 and WP8.1), run the same code, Win8.1 is ok but WP8.1 is wrong. - Harreke
Can you post you solution code? I face the same problem.. - Tomáš Bezouška

1 Answers

2
votes

I just solved the same problem - do NOT use System.Net.HttpClient, but Windows.Web.Http.HttpClient

The one in System.Net uses header Connection: Close by default, which causes the stream to close, reading only 65 kB. It also contains a bug which prevents you to override the header to Keep-Alive (it throws some nonesense exception)