MediaStreamSourceをWindowsPhone7.1で動作させようとしています。256*256や512*512などの小さい解像度の場合は機能しますが、一部の解像度では、ReportOpenMediaCompletedを呼び出した直後に閉じられます。例外などは発生しません。
これが私のコードです:
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();
}
}