0

Afaik that using MediaStreamSource i can playback h264 file in windows phone, but i dont find anything about it. The only sample that i saw and really help me to starts, was it: http://msdn.microsoft.com/en-us/library/hh180779(v=vs.95).aspx But i wasnt luck... Please help me, i need to implement it more quickly as possible!

For a moment i tried to implement the MediaStreamSource override methods to be called when i use mediaElement.SetSource(). What i'm doing wrong?

`public class VideoMediaStreamSource : MediaStreamSource { private MediaStreamDescription _videoDesc; private MediaStreamType _mediaType;

    private Dictionary<MediaStreamAttributeKeys, string> _streamAttributes;
    private Dictionary<MediaSourceAttributesKeys, string> _sourceAttributes;
    private Dictionary<MediaSampleAttributeKeys, string> _sampleAttributes;
    private Dictionary<MediaSampleAttributeKeys, string> _emptySampleDict;

    private List<MediaStreamDescription> _listDesc;

    private MemoryStream _frameStream;

    private int _framePixelSize;
    private int _frameBufferSize;
    private int _currentReadyFrame;
    private int _currentBufferFrame;
    private int _frameTime;

    private long _currentVideoTimeStamp;

    private byte[][] _frames = new byte[2][];

    private const int frameHeightOriginal = 384;
    private const int FrameWidthOriginal = 288;
    private const int BYTESPERPIXEL = 4;

    #region PUBLIC CONST
    public int FRAME_HEIGHT_ORIGINAL
    {
        get
        {
            return frameHeightOriginal;
        }
    }

    public int FRAME_WIDTH_ORIGINAL
    {
        get
        {
            return FrameWidthOriginal;
        }
    }
    #endregion
    public VideoMediaStreamSource()
    {
        _framePixelSize = FRAME_WIDTH_ORIGINAL * frameHeightOriginal;
        _frameBufferSize = _framePixelSize * BYTESPERPIXEL;

        // PAL is 50 frames per second
        _frameTime = (int)TimeSpan.FromSeconds((double)1 / 50).Ticks;

        _frames[0] = new byte[_frameBufferSize];
        _frames[1] = new byte[_frameBufferSize];

        _currentBufferFrame = 0;
        _currentReadyFrame = 1;
    }

    protected override void OpenMediaAsync()
    {
        _sourceAttributes = new Dictionary<MediaSourceAttributesKeys, string>();
        _streamAttributes  = new Dictionary<MediaStreamAttributeKeys, string>();

        _listDesc = new List<MediaStreamDescription>();

        _mediaType = MediaStreamType.Video;

        _frameStream = new MemoryStream();

        _streamAttributes[MediaStreamAttributeKeys.VideoFourCC] = "H264";
        _streamAttributes[MediaStreamAttributeKeys.Height] = frameHeightOriginal.ToString();
        _streamAttributes[MediaStreamAttributeKeys.Width] = FRAME_WIDTH_ORIGINAL.ToString();           

        _sourceAttributes[MediaSourceAttributesKeys.Duration] = TimeSpan.FromSeconds(0).Ticks.ToString(CultureInfo.InvariantCulture);
        _sourceAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString();

        _videoDesc = new MediaStreamDescription(_mediaType,_streamAttributes);
        _listDesc.Add(_videoDesc);

        ReportOpenMediaCompleted(_sourceAttributes,_listDesc);
    }

    protected override void CloseMedia()
    {
        throw new NotImplementedException(); 
    }

    protected override void SeekAsync(long seekToTime)
    {
        throw new NotImplementedException();
    }

    protected override void GetSampleAsync(MediaStreamType mediaStreamType)
    {
        if (mediaStreamType == MediaStreamType.Video)
        {
            GetVideoSample();
        }
    }
    private void GetVideoSample()
    {
        _frameStream = new MemoryStream();
        _frameStream.Write(_frames[_currentReadyFrame], 0, _frameBufferSize);

        _emptySampleDict = new Dictionary<MediaSampleAttributeKeys, string>();

        MediaStreamSample msSamp = new MediaStreamSample(
            _videoDesc,
            _frameStream,
            0,
            _frameBufferSize,
            _currentVideoTimeStamp,
            _emptySampleDict);

        _currentVideoTimeStamp += _frameTime;

        ReportGetSampleCompleted(msSamp);
    }
    protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind)
    {
        throw new NotImplementedException();
    }

    protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription)
    {
        throw new NotImplementedException();
    }`

and here, is my code that i'm trying to do now, that is to call the OpenMediaAsync within MediaElement. The "slamtv60.h264" file is in my solution.

`private void PlayMediaStreamSource() { MediaElement media = new MediaElement(); VideoMediaStreamSource videoMedia = new VideoMediaStreamSource(); media.Source = new Uri("/slamtv60.h264", UriKind.RelativeOrAbsolute); media.SetSource(videoMedia); media.AutoPlay = true;

        media.Width = videoMedia.FRAME_WIDTH_ORIGINAL;
        media.Height = videoMedia.FRAME_HEIGHT_ORIGINAL;

        media.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        media.VerticalAlignment = System.Windows.VerticalAlignment.Center;

        LayoutRoot.Children.Add(media);
    }`

Please, help me...

  • There's a lot of content here. Consider shortening your code snippets and telling us what is exactly wrong. – Compass Oct 30 '14 at 16:07
  • Compass, What i need to do to invoke the GetVideoSample method? Because, my application only is opening the OpenMediaAsync method, and the ReportOpenMediaCompleted method doesnt seem work to me. No exception is displayed to me when i run the code, but the result in the end is the black screen. What happen? – Diego Moura Oct 30 '14 at 18:35
  • I don't know C# or video encoding. You will likely get more answers if you can make it easier for those who understand the language smaller pieces to work with. – Compass Oct 30 '14 at 18:48

0 Answers0