async void MSS_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { MediaStreamSourceSampleRequest request = args.Request; // check if the sample requested byte offset is within the file size if (byteOffset + sampleSize <= mssStream.Size) { MediaStreamSourceSampleRequestDeferral deferal = request.GetDeferral(); IInputStream inputStream = mssStream.GetInputStreamAt(byteOffset); // create the MediaStreamSample and assign to the request object. // You could also create the MediaStreamSample using createFromBuffer(...) MediaStreamSample sample = await MediaStreamSample.CreateFromStreamAsync(inputStream, sampleSize, timeOffset); sample.Duration = sampleDuration; sample.KeyFrame = true; // increment the time and byte offset byteOffset += sampleSize; timeOffset = timeOffset.Add(sampleDuration); request.Sample = sample; deferal.Complete(); } }
void InitializeMediaPlayer() { int iWidth = (int)Window.Current.Bounds.Width; int iHeight = (int)Window.Current.Bounds.Height; // Even frame size with a 16:9 ratio iWidth = Math.Min(iWidth, ((iHeight * 16 / 9) >> 1) * 2); iHeight = Math.Min(iHeight, ((iWidth * 9 / 16) >> 1) * 2); VideoEncodingProperties videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, (uint)iWidth, (uint)iHeight); _videoDesc = new VideoStreamDescriptor(videoProperties); _videoDesc.EncodingProperties.FrameRate.Numerator = c_frameRateN; _videoDesc.EncodingProperties.FrameRate.Denominator = c_frameRateD; _videoDesc.EncodingProperties.Bitrate = (uint)(c_frameRateN * c_frameRateD * iWidth * iHeight * 4); _mss = new Windows.Media.Core.MediaStreamSource(_videoDesc); TimeSpan spanBuffer = new TimeSpan(0, 0, 0, 0, 250); _mss.BufferTime = spanBuffer; _mss.Starting += _mss_Starting; _mss.SampleRequested += _mss_SampleRequested; _sampleGenerator = new DXSurfaceGenerator.SampleGenerator(); mediaPlayer.AutoPlay = false; mediaPlayer.CurrentStateChanged += mediaPlayer_CurrentStateChanged; mediaPlayer.SetMediaStreamSource(_mss); _hasSetMediaSource = true; }
public void InitialiseMp4FileMedia(string path) { try { VideoEncodingProperties videoProperties = VideoEncodingProperties.CreateH264(); _videoDesc = new VideoStreamDescriptor(videoProperties); _videoDesc.EncodingProperties.FrameRate.Numerator = FRAME_RATE; _videoDesc.EncodingProperties.FrameRate.Denominator = 1; //_videoDesc.EncodingProperties.Bitrate = (uint)(1 * FRAME_RATE * MP4_WIDTH * MP4_HEIGHT * 4); _mss = new Windows.Media.Core.MediaStreamSource(_videoDesc); TimeSpan spanBuffer = new TimeSpan(0); _mss.BufferTime = spanBuffer; _mss.Starting += mp4_Starting; _mss.SampleRequested += mp4_SampleRequested; _mp4Sampler = new SurfaceGenerator.Mp4Sampler(); _remoteVideo.MediaFailed += _remoteVideo_MediaFailed; _remoteVideo.SetMediaStreamSource(_mss); _remoteVideo.Play(); } catch (Exception excp) { Debug.WriteLine("Exception InitialiseMp4FileMedia. " + excp); } }
async void MSS_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { MediaStreamSourceStartingRequest request = args.Request; if ((request.StartPosition != null) && request.StartPosition.Value <= MSS.Duration) { UInt64 sampleOffset = (UInt64)request.StartPosition.Value.Ticks / (UInt64)sampleDuration.Ticks; timeOffset = new TimeSpan((long)sampleOffset * sampleDuration.Ticks); byteOffset = sampleOffset * sampleSize; } // create the RandomAccessStream for the input file for the first time if (mssStream == null) { MediaStreamSourceStartingRequestDeferral deferal = request.GetDeferral(); try { mssStream = await inputMP3File.OpenAsync(FileAccessMode.Read); request.SetActualStartPosition(timeOffset); deferal.Complete(); } catch (Exception) { MSS.NotifyError(MediaStreamSourceErrorStatus.FailedToOpenFile); deferal.Complete(); } } else { request.SetActualStartPosition(timeOffset); } }
void mss_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { if (args.Request.StreamDescriptor is VideoStreamDescriptor) { _sampleMaker.GenerateSample(args.Request); } }
void mss_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { Debug.WriteLine("Starting."); _sampleMaker.Initialize(_mss, _videoDesc); args.Request.SetActualStartPosition(new TimeSpan(0)); }
void MSS_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { if (!m_hasSetMediaSource || advanced_media_source == null) { return; } advanced_media_source.Initialize(media_stream_source, videoDesc, audioDesc); args.Request.SetActualStartPosition(new TimeSpan(0)); }
void MSS_Closed(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceClosedEventArgs args) { sender.Starting -= MSS_Starting; sender.Closed -= MSS_Closed; sender.SampleRequested -= MSS_SampleRequested; if (sender == media_stream_source) { media_stream_source = null; } }
void mp4_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { Debug.WriteLine("Starting."); //_sampleMaker.Initialize(_mss, _videoDesc); var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; _mp4Sampler.Initialise(folder.Path + @"\Assets\big_buck_bunny.mp4"); args.Request.SetActualStartPosition(new TimeSpan(0)); }
void mp4_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { try { if (args.Request.StreamDescriptor is VideoStreamDescriptor) { _mp4Sampler.GetSample(args.Request); } } catch (Exception excp) { Debug.WriteLine("Exception mp4_SampleRequeste. " + excp.Message); } }
async private void InitializeMediaStreamSource() { // initialize Parsing Variables byteOffset = 0; timeOffset = new TimeSpan(0); // get the encoding properties of the input MP3 file List <string> encodingPropertiesToRetrieve = new List <string>(); encodingPropertiesToRetrieve.Add("System.Audio.SampleRate"); encodingPropertiesToRetrieve.Add("System.Audio.ChannelCount"); encodingPropertiesToRetrieve.Add("System.Audio.EncodingBitrate"); IDictionary <string, object> encodingProperties = await inputMP3File.Properties.RetrievePropertiesAsync(encodingPropertiesToRetrieve); uint sampleRate = (uint)encodingProperties["System.Audio.SampleRate"]; uint channelCount = (uint)encodingProperties["System.Audio.ChannelCount"]; uint bitRate = (uint)encodingProperties["System.Audio.EncodingBitrate"]; // get the common music properties of the input MP3 file MusicProperties mp3FileProperties = await inputMP3File.Properties.GetMusicPropertiesAsync(); songDuration = mp3FileProperties.Duration; // creating the AudioEncodingProperties for the MP3 file AudioEncodingProperties audioProps = AudioEncodingProperties.CreateMp3(sampleRate, channelCount, bitRate); // creating the AudioStreamDescriptor for the MP3 file AudioStreamDescriptor audioDescriptor = new AudioStreamDescriptor(audioProps); // creating the MediaStreamSource for the MP3 file MSS = new Windows.Media.Core.MediaStreamSource(audioDescriptor); MSS.CanSeek = true; MSS.MusicProperties.Title = mp3FileProperties.Title; MSS.Duration = songDuration; // hooking up the MediaStreamSource event handlers MSS.Starting += MSS_Starting; MSS.SampleRequested += MSS_SampleRequested; MSS.Closed += MSS_Closed; mediaPlayer.SetMediaStreamSource(MSS); rootPage.NotifyUser("MediaStreamSource created and set as source", NotifyType.StatusMessage); }
void MSS_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { if (!m_hasSetMediaSource || advanced_media_source == null) { return; } if (args.Request.StreamDescriptor is VideoStreamDescriptor) { advanced_media_source.GenerateVideoSample(args.Request); } else if (args.Request.StreamDescriptor is AudioStreamDescriptor) { advanced_media_source.GenerateAudioSample(args.Request); } }
void MSS_Closed(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceClosedEventArgs args) { // close the MediaStreamSource and remove the MediaStreamSource event handlers if (mssStream != null) { mssStream.Dispose(); mssStream = null; } sender.SampleRequested -= MSS_SampleRequested; sender.Starting -= MSS_Starting; sender.Closed -= MSS_Closed; if (sender == MSS) { MSS = null; } }
public MainPage() { this.InitializeComponent(); var videoProperties = VideoEncodingProperties.CreateH264();//.CreateUncompressed(MediaEncodingSubtypes.H264, 720, 480); var vd = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.H264, 720, 480); videoDesc = new VideoStreamDescriptor(videoProperties); videoDesc.EncodingProperties.FrameRate.Numerator = 29970; videoDesc.EncodingProperties.FrameRate.Denominator = 1000; videoDesc.EncodingProperties.Width = 720; videoDesc.EncodingProperties.Height = 480; mss = new MediaStreamSource(videoDesc); mss.CanSeek = false; //mss.BufferTime = new TimeSpan(0, 0, 0, 0, 250); mss.Starting += mss_Starting; mss.SampleRequested += Mss_SampleRequested; mss.SampleRendered += Mss_SampleRendered; //initialize some buffers buff = new Windows.Storage.Streams.Buffer(1024 * 4); bStream = buff.AsStream(); //this seems needed for start-up threadSync = new System.Threading.AutoResetEvent(false); //get the frame time in ms double ms = 1000.0 * videoDesc.EncodingProperties.FrameRate.Denominator / videoDesc.EncodingProperties.FrameRate.Numerator; //get the frame time in ticks T0 = System.TimeSpan.FromTicks((long)(ms * System.TimeSpan.TicksPerMillisecond)); //our demuxer extractor = new MpegTS.BufferExtractor(); running = true; //give the file IO a head start Task.Run(() => RunreadFromFile()); }
public void InitialiseRovingSquareSampleMedia() { VideoEncodingProperties videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, (uint)WIDTH, (uint)HEIGHT); _videoDesc = new VideoStreamDescriptor(videoProperties); _videoDesc.EncodingProperties.FrameRate.Numerator = FRAME_RATE; _videoDesc.EncodingProperties.FrameRate.Denominator = 1; _videoDesc.EncodingProperties.Bitrate = (uint)(1 * FRAME_RATE * WIDTH * HEIGHT * 4); _mss = new Windows.Media.Core.MediaStreamSource(_videoDesc); TimeSpan spanBuffer = new TimeSpan(0); _mss.BufferTime = spanBuffer; _mss.Starting += mss_Starting; _mss.SampleRequested += mss_SampleRequested; _sampleMaker = new SurfaceGenerator.SampleMaker(); _remoteVideo.MediaFailed += _remoteVideo_MediaFailed; _remoteVideo.SetMediaStreamSource(_mss); _remoteVideo.Play(); }
public async Task <bool> ConnectAsync() { var response = await EstablishConnectionAsync(); connected = response.Item1; if (connected == false) { return(false); } AudioEncodingProperties obtainedProperties = await GetEncodingPropertiesAsync(response.Item2); MediaStreamSource = new Windows.Media.Core.MediaStreamSource(new AudioStreamDescriptor(obtainedProperties)); MediaStreamSource.SampleRequested += MediaStreamSource_SampleRequested; MediaStreamSource.CanSeek = false; MediaStreamSource.Starting += MediaStreamSource_Starting; MediaStreamSource.Closed += MediaStreamSource_Closed; connected = true; return(connected); }
public async Task ConnectAsync(uint sampleRate = 44100, string relativePath = ";") { await HandleConnection(relativePath); //Surprised that this commented-out-bit is broken. //AudioEncodingProperties obtainedProperties = await GetEncodingPropertiesAsync(); switch (contentType) { case StreamAudioFormat.MP3: { MediaStreamSource = new Windows.Media.Core.MediaStreamSource(new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(sampleRate, 2, (uint)bitRate))); //MediaStreamSource.AddStreamDescriptor(new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(48000, 2, (uint)bitRate))); //MediaStreamSource.AddStreamDescriptor(new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(32000, 2, (uint)bitRate))); //MediaStreamSource.AddStreamDescriptor(new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(24000, 2, (uint)bitRate))); //MediaStreamSource.AddStreamDescriptor(new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(22050, 2, (uint)bitRate))); } break; case StreamAudioFormat.AAC: { MediaStreamSource = new MediaStreamSource(new AudioStreamDescriptor(AudioEncodingProperties.CreateAac(sampleRate, 2, (uint)bitRate))); } break; } MediaStreamSource.SampleRequested += MediaStreamSource_SampleRequested; MediaStreamSource.CanSeek = false; MediaStreamSource.Starting += MediaStreamSource_Starting; MediaStreamSource.Closed += MediaStreamSource_Closed; }
void _mss_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { _sampleGenerator.GenerateSample(args.Request); }
void _mss_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { _sampleGenerator.Initialize(_mss, _videoDesc); args.Request.SetActualStartPosition(new TimeSpan(0)); }
async private void InitializeMediaStreamSource() { // initialize Parsing Variables byteOffset = 0; timeOffset = new TimeSpan(0); // get the encoding properties of the input MP3 file List<string> encodingPropertiesToRetrieve = new List<string>(); encodingPropertiesToRetrieve.Add("System.Audio.SampleRate"); encodingPropertiesToRetrieve.Add("System.Audio.ChannelCount"); encodingPropertiesToRetrieve.Add("System.Audio.EncodingBitrate"); IDictionary<string, object> encodingProperties = await inputMP3File.Properties.RetrievePropertiesAsync(encodingPropertiesToRetrieve); uint sampleRate = (uint)encodingProperties["System.Audio.SampleRate"]; uint channelCount = (uint)encodingProperties["System.Audio.ChannelCount"]; uint bitRate = (uint)encodingProperties["System.Audio.EncodingBitrate"]; // get the common music properties of the input MP3 file MusicProperties mp3FileProperties = await inputMP3File.Properties.GetMusicPropertiesAsync(); songDuration = mp3FileProperties.Duration; // creating the AudioEncodingProperties for the MP3 file AudioEncodingProperties audioProps = AudioEncodingProperties.CreateMp3(sampleRate, channelCount, bitRate); // creating the AudioStreamDescriptor for the MP3 file AudioStreamDescriptor audioDescriptor = new AudioStreamDescriptor(audioProps); // creating the MediaStreamSource for the MP3 file MSS = new Windows.Media.Core.MediaStreamSource(audioDescriptor); MSS.CanSeek = true; MSS.MusicProperties.Title = mp3FileProperties.Title; MSS.Duration = songDuration; // hooking up the MediaStreamSource event handlers MSS.Starting += MSS_Starting; MSS.SampleRequested += MSS_SampleRequested; MSS.Closed += MSS_Closed; mediaPlayer.SetMediaStreamSource(MSS); rootPage.NotifyUser("MediaStreamSource created and set as source", NotifyType.StatusMessage); }
private async void MediaStreamSource_SampleRequested(Windows.Media.Core.MediaStreamSource sender, Windows.Media.Core.MediaStreamSourceSampleRequestedEventArgs args) { var request = args.Request; if (!IsInternetConnected() || !connected) { connected = false; Disconnect(); sender.NotifyError(MediaStreamSourceErrorStatus.ConnectionToServerLost); return; } var deferral = request.GetDeferral(); try { MediaStreamSample sample = null; uint sampleLength = 0; //request.ReportSampleProgress(25); //if metadataPos is less than mp3_sampleSize away from metadataInt if (metadataInt - metadataPos <= (AudioInfo.AudioFormat == StreamAudioFormat.MP3 ? mp3_sampleSize : aac_adts_sampleSize) && metadataInt - metadataPos > 0) { //parse part of the frame. byte[] partialFrame = new byte[metadataInt - metadataPos]; var read = await socketReader.LoadAsync(metadataInt - metadataPos); if (read == 0) { Disconnect(); MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.ConnectionToServerLost); return; } socketReader.ReadBytes(partialFrame); metadataPos += metadataInt - metadataPos; switch (AudioInfo.AudioFormat) { case StreamAudioFormat.MP3: { Tuple <MediaStreamSample, uint> result = await ParseMP3SampleAsync(partial : true, partialBytes : partialFrame); sample = result.Item1; sampleLength = result.Item2; } break; case StreamAudioFormat.AAC_ADTS: case StreamAudioFormat.AAC: { Tuple <MediaStreamSample, uint> result = await ParseAACSampleAsync(partial : true, partialBytes : partialFrame); sample = result.Item1; sampleLength = result.Item2; } break; } } else { await HandleMetadata(); //request.ReportSampleProgress(50); switch (AudioInfo.AudioFormat) { case StreamAudioFormat.MP3: { //mp3 Tuple <MediaStreamSample, uint> result = await ParseMP3SampleAsync(); sample = result.Item1; sampleLength = result.Item2; } break; case StreamAudioFormat.AAC_ADTS: case StreamAudioFormat.AAC: { Tuple <MediaStreamSample, uint> result = await ParseAACSampleAsync(); sample = result.Item1; sampleLength = result.Item2; } break; } try { if (sample == null || sampleLength == 0) //OLD bug: on RELEASE builds, sample.Buffer causes the app to die due to a possible .NET Native bug { MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.DecodeError); deferral.Complete(); return; } else { metadataPos += sampleLength; } } catch (Exception) { } } if (sample != null) { request.Sample = sample; } //request.ReportSampleProgress(100); } catch (Exception) { MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.Other); } deferral.Complete(); }