Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?


C#
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.

C#
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...
Posted
Updated 30-Oct-14 1:19am
v6
Comments
BillWoodruff 28-Oct-14 23:16pm    
More specifics about what you have tried and how that is not working, please.
BatataSharp 29-Oct-14 7:26am    
Thanks for your repply Bill. I already improving my question, sorry for immaturity, but i i'm a beginner with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900