This project has moved. For the latest updates, please go here.

Universal Windows Phone 8.1 Background Streaming

Sep 19, 2014 at 12:05 PM
Hello!

I use the Windows Phone Streaming Media Framework with Windows Phone 8.1 (Universal App).
I stream for example a m3u8 stream and it plays smoothly.
The next step for me is to play it with the BackgroundAudioTask.
The Background Task has a memoryLimit ( in my case 20 MIB).
My problem is that the stream consume too much memory.

If i start the Background Audio Task the current memory size is:
<4,00MiB/20,00MiB>

if i start to stream, it increase to (every 3 seconds call)
<7,00MiB/20,00MiB>
<9,00MiB/20,00MiB>
<17,00MiB/20,00MiB>

and then, the App crash, because i consume too much memory for the Background Audio Task.
Question is: how can i handle it to stream and cleanup the stream from memory?

I tested some values with the DefaultBufferingPolicy
_bufferingPolicy = new DefaultBufferingPolicy
            {
                 BytesMinimumStarting = 24 * 1024,
                BytesMinimum = 64 * 1024
            };
but this change nothing.
Is there any cleanup function or something else that i can use?
My call for the stream is:
 private async void StartStream(string source)
        {
            InitializeMediaStream();

            var mss = await _mediaStreamFacade.CreateMediaStreamSourceAsync(new Uri(source), CancellationToken.None).ConfigureAwait(false);
            mediaPlayer.SetMediaSource(mss);
            mediaPlayer.Play();
        }

Thanks for help and hints!
Coordinator
Sep 20, 2014 at 6:37 AM
I suspect this is a manifestation of what was discussed here and here. IIRC, HttpClient and HttpWebRequest both use IXMLHTTPRequest2, and since there is no way to turn off caching, it will read from the web as fast as it can, eating up all available memory. The only work around I'm aware of is to use a socket instead of something IXMLHTTPRequest2 based (if you use IXMLHTTPRequest3, then the XHR_PROP_NO_CACHE property might fix things).

You might be able to use SM.Mono.Net. I haven't gotten around to cleaning up that code or trying it myself for this purpose, but I have heard from others that it does solve these kinds of out-of-memory problems.
Sep 22, 2014 at 9:46 AM
Thanks for the answer.

I try it with SM.Mono.Net.
Do you maybe have a hint how to compile the WinRT part?
There are some missing file links.
Coordinator
Sep 22, 2014 at 3:44 PM
I think the WinRT project is out-of-date. If you are targeting WinRT, then you are probably better off with the stuff I just checked in. Get the latest source and tell it to use the native HttpClient (put "SM.Media.TsMediaModule.UseNativeHttpClient = true;" in, say, your app's constructor). That code is only a few hours old and I still need to test things like the user-agent and range headers.

If you do need to use SM.Mono.Net, then I think the SM.Mono.WinRT PCL probably really needs to be SM.Mono.Win8 and/or SM.Mono.WP81. Create the project or projects you need and alt-drag/share the files from the WP8 project.
Sep 22, 2014 at 6:28 PM
Hello henric

Thanks for the changing.

I try the latest source, but it is not working for me in the Background.
It is still the same memory consumption.

<9,00MiB/20,00MiB>
<17,00MiB/20,00MiB>
...
and then the exception.

I set SM.Media.TsMediaModule.UseNativeHttpClient = true, is there maybe something else that i must keep in mind?
Coordinator
Sep 23, 2014 at 12:56 AM
How are you doing background audio? Are you using the BackgroundAudioPlayer (like BackgroundAudio.Sample.WP8) or BackgroundMediaPlayer (WP8.1)? For the former, the "SM.Media.TsMediaModule.UseNativeHttpClient = true" should probably be in AudioTrackStreamer's constructor; for the latter, it should probably be in your IBackgroundTask's "void Run(IBackgroundTaskInstance taskInstance)".

If you want to be sure, then put a breakpoint, Debug.WriteLine(), or some-such in WinRtHttpClients' "CreateClient(...)".
Coordinator
Sep 23, 2014 at 12:59 AM
Come to think of it, can one mix WinRT libraries and BackgroundAudioPlayer...?
Sep 23, 2014 at 10:04 AM
I use the BackgroundMediaPlayer (WP8.1)
I created today a new example Project and it looks that the Memory consumption is better.

It stuck on around 11 Mib, so it looks good. :-)

BufferingManager.UpdateState read blocking -> True duration 00:00:25.4722889 size 320242 memory 1,65 MiB
Audio count 56 size 320242 newest 00:00:29.6054334 oldest 00:00:04.1331445
<11,00MiB/20,00MiB>
<11,00MiB/20,00MiB>
<11,00MiB/20,00MiB>

It is the half of the possible Memory size, but the Problem is now that the stream only plays around 10 seconds and shutdown with:

Failed with error code System.Exception: The request is invalid because Shutdown() has been called.
The request is invalid because Shutdown() has been called.
BufferingManager.UpdateState read blocking -> False duration 00:00:11.7492556 size 188104 memory 2,43 MiB
  Audio count 34 size 188104 newest 00:00:29.4196445 oldest 00:00:17.6703889
---- Completed http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment5_0_a.ts?null=&id=AgBSjDTwUO/3n+pAIVS9Rw0J5HwYu6KUhzYL/38Lvk/SdkognEUBw/k7vagAP0DbTfNJJ4sbjn+egw==&hdntl=exp=1411551850~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=645f6fb6dbdf63a05808a186ec378c199c42d66c6b367d8590e3b63e2981e678 at 23.09.2014 11:44:24 +02:00 (00:00:13.0460180).  Total memory: 2,90 MiB
++++ Starting http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment6_0_a.ts?null=&id=AgBSjDTwUO/3n+pAIVS9Rw0J5HwYu6KUhzYL/38Lvk/SdkognEUBw/k7vagAP0DbTfNJJ4sbjn+egw==&hdntl=exp=1411551850~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=645f6fb6dbdf63a05808a186ec378c199c42d66c6b367d8590e3b63e2981e678 at 23.09.2014 11:44:24 +02:00.  Total memory: 2,92 MiB
BufferingManager.UpdateState read blocking -> True duration 00:00:32.0898889 size 512277 memory 2,92 MiB
  Audio count 93 size 512277 newest 00:00:49.7602778 oldest 00:00:17.6703889
Segment AES-128: key 14-41-BA-EE-EB-41-FC-56-33-CC-1D-7C-D2-1F-D2-24 iv 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-06


__Failed with error code System.Exception: The request is invalid because Shutdown() has been called.
The request is invalid because Shutdown() has been called.__


---- Completed http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment6_0_a.ts?null=&id=AgBSjDTwUO/3n+pAIVS9Rw0J5HwYu6KUhzYL/38Lvk/SdkognEUBw/k7vagAP0DbTfNJJ4sbjn+egw==&hdntl=exp=1411551850~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=645f6fb6dbdf63a05808a186ec378c199c42d66c6b367d8590e3b63e2981e678 at 23.09.2014 11:44:25 +02:00 (00:00:00.3091976).  Total memory: 2,02 MiB
++++ Starting http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment7_0_a.ts?null=&id=AgBSjDTwUO/3n+pAIVS9Rw0J5HwYu6KUhzYL/38Lvk/SdkognEUBw/k7vagAP0DbTfNJJ4sbjn+egw==&hdntl=exp=1411551850~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=645f6fb6dbdf63a05808a186ec378c199c42d66c6b367d8590e3b63e2981e678 at 23.09.2014 11:44:25 +02:00.  Total memory: 2,02 MiB
Segment AES-128: key 14-41-BA-EE-EB-41-FC-56-33-CC-1D-7C-D2-1F-D2-24 iv 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-07
<14,00MiB/20,00MiB>
<14,00MiB/20,00MiB>
I attached the example Project.
First click on button start the BackgroundMediaPlayer, second click start the stream.

BackgroundMediaPlayerExample.zip
Coordinator
Sep 23, 2014 at 1:33 PM
I think you need to do the "deferral = taskInstance.GetDeferral();" thing in your Run() implementation. (And clean it up with "deferral.Complete();" when done.)

Also, there are build warnings about some NuGet packages not being referenced by some of the projects during the build. That reminds me that I need to pull the HttpClient stuff out of SM.Media. That package shouldn't be required unless one is actually using it..
Sep 23, 2014 at 2:26 PM
Thanks for the answer.

Yes i forgot the taskInstance.GetDeferral thing in the example project. Thanks for the hint.
But it continue to stop the stream after around 10- 15 seconds and the media Player throws a exception:
BufferingManager.UpdateState read blocking -> False duration 00:00:11.7492556 size 188104 memory 2,45 MiB
  Audio count 34 size 188104 newest 00:00:29.4196445 oldest 00:00:17.6703889
BufferingManager.UpdateState read blocking -> True duration 00:00:31.7415889 size 508202 memory 2,93 MiB
  Audio count 92 size 508202 newest 00:00:49.4119778 oldest 00:00:17.6703889
---- Completed http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment5_0_a.ts?null=&id=AgBSjDTwUO/YtWJ/IVTt4Hz/Sb1rTkvGFxVLun7zw0b/kP4jRepnU0J+gK6EuO3dsuNNRSBtOZhU7w==&hdntl=exp=1411567842~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=4b1f592a7796daea8b0081b73c6568df30085489dbe70e5a7cf1a0a9cf9aa3ee at 23.09.2014 16:10:56 +02:00 (00:00:12.9459896).  Total memory: 2,94 MiB
++++ Starting http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment6_0_a.ts?null=&id=AgBSjDTwUO/YtWJ/IVTt4Hz/Sb1rTkvGFxVLun7zw0b/kP4jRepnU0J+gK6EuO3dsuNNRSBtOZhU7w==&hdntl=exp=1411567842~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=4b1f592a7796daea8b0081b73c6568df30085489dbe70e5a7cf1a0a9cf9aa3ee at 23.09.2014 16:10:56 +02:00.  Total memory: 2,95 MiB
Segment AES-128: key 5A-4F-4D-C1-68-F3-6B-F3-AB-77-D0-BA-E0-C6-34-FD iv 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-06
---- Completed http://webstream-vh.akamaihd.net/i/139/108/432/155/audio.mp4/segment6_0_a.ts?null=&id=AgBSjDTwUO/YtWJ/IVTt4Hz/Sb1rTkvGFxVLun7zw0b/kP4jRepnU0J+gK6EuO3dsuNNRSBtOZhU7w==&hdntl=exp=1411567842~acl=/i/139/108/432/155/audio.mp4*~data=hdntl~hmac=4b1f592a7796daea8b0081b73c6568df30085489dbe70e5a7cf1a0a9cf9aa3ee at 23.09.2014 16:10:57 +02:00 (00:00:00.3353052).  Total memory: 2,05 MiB
An error occurred while decoding the media resource : HRESULT=0xC00D3E85

It is always in the sampe place, don´t know exactly how to handle it..
Coordinator
Sep 23, 2014 at 11:07 PM
This sounds more like it has something to do with the stream than with background audio. Does the stream work if you play it in HlsView? If it is the stream, try playing it with the console SimulatedPlayer and Fiddler. If you want me to take a look at the stream, send me a PM with a Fiddler trace (.saz file) or with the actual files (the .m3u8, .ts, and either the .key file and/or the debug output log).
Sep 24, 2014 at 6:08 AM
The stream works in HlsView and plays the track to the end. i send you a pm with the track Information.