I am trying to get my MediaStreamSource to work on Windows Phone 7.1.
For smaller resolutions like 256*256 or 512*512 it works but at some resolution it is Closed immediately after I called ReportOpenMediaCompleted.
I don’t get any Exceptions or such.
Here is my code:
public class BitmapStreamSource : MediaStreamSource
{
MediaStreamDescription bitmapMediaStream;
const int width = 1024;
const int height = 1024;
protected override void OpenMediaAsync()
{
Dictionary<MediaSourceAttributesKeys, string> mediaStreamAttributes;
List<MediaStreamDescription> availableMediaStreams;
Dictionary<MediaStreamAttributeKeys, string> bitmapStreamAttributes;
mediaStreamAttributes = new Dictionary<MediaSourceAttributesKeys, string>(2);
mediaStreamAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString();
mediaStreamAttributes[MediaSourceAttributesKeys.Duration] = "-1";
bitmapStreamAttributes = new Dictionary<MediaStreamAttributeKeys,string>(3);
bitmapStreamAttributes[MediaStreamAttributeKeys.VideoFourCC]= "RGBA";
bitmapStreamAttributes[MediaStreamAttributeKeys.Width]= width.ToString();
bitmapStreamAttributes[MediaStreamAttributeKeys.Height] = height.ToString();
bitmapMediaStream = new MediaStreamDescription(MediaStreamType.Video, bitmapStreamAttributes);
availableMediaStreams = new List<MediaStreamDescription>(1);
availableMediaStreams.Add(bitmapMediaStream);
ReportOpenMediaCompleted(mediaStreamAttributes, availableMediaStreams);
}
protected override void SeekAsync(long seekToTime)
{
ReportSeekCompleted(seekToTime);
}
int sampleCount = 0;
protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
int pixel = width * height;
var buffer = new byte[pixel * 4];
for (int i = 0; i < pixel; i++)
{
byte value = (byte)((i + sampleCount) % 256);
var k = i * 4;
//buffer[k] = 255; // B
buffer[k + 1] = value; // G
buffer[k + 2] = 255; // R
//buffer[k + 3] = value; // A
}
var stream = new MemoryStream(buffer);
var keys = new Dictionary<MediaSampleAttributeKeys, string>();
var sample = new MediaStreamSample(bitmapMediaStream, stream, 0, buffer.Length, 0, keys);
sampleCount++;
ReportGetSampleCompleted(sample);
}
protected override void CloseMedia()
{
Debug.WriteLine("CloseMedia()");
}
protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind)
{
throw new NotImplementedException();
}
protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription)
{
throw new NotImplementedException();
}
}
You are going beyond the media capabilities for your Windows Phone 7.x device (or emulator).
In general the maximum resolution and which video codecs are supported depends on the SoC chip inside the phone and is documented here on MSDN for both Windows Phone 7.x and 8.0.
In particular the chips inside Windows Phone 7.x handsets typically limit resolution at 1280×720 or 800×480 resolution. Windows Phone 8 handsets may support higher resolutions.
If you are using the emulator this gets more complicated – it will typically simulate typical device capabilities, but you will need to test how media behaves on real devices to be sure.