/// <summary> /// Initialize the media element for playback /// </summary> /// <param name="streamConfig">Object containing stream configuration details</param> void InitializeMediaPlayer(MoonlightStreamConfiguration streamConfig, AvStreamSource streamSource) { this._streamSource = streamSource; // This code is based upon the MS FFmpegInterop project on GitHub VideoEncodingProperties videoProps = VideoEncodingProperties.CreateH264(); videoProps.ProfileId = H264ProfileIds.High; videoProps.Width = (uint)streamConfig.GetWidth(); videoProps.Height = (uint)streamConfig.GetHeight(); videoProps.Bitrate = (uint)streamConfig.GetBitrate(); _videoMss = new MediaStreamSource(new VideoStreamDescriptor(videoProps)); _videoMss.BufferTime = TimeSpan.Zero; _videoMss.CanSeek = false; _videoMss.Duration = TimeSpan.Zero; _videoMss.SampleRequested += _videoMss_SampleRequested; XAudio2 xaudio = new XAudio2(); MasteringVoice masteringVoice = new MasteringVoice(xaudio, 2, 48000); WaveFormat format = new WaveFormat(48000, 16, 2); // Set for low latency playback StreamDisplay.RealTimePlayback = true; // Render on the full window to avoid extra compositing StreamDisplay.IsFullWindow = true; // Disable built-in transport controls StreamDisplay.AreTransportControlsEnabled = false; StreamDisplay.SetMediaStreamSource(_videoMss); AvStream.SetSourceVoice(new SourceVoice(xaudio, format)); }
/// <summary> /// Clean up resources /// </summary> private void Cleanup() { // TODO will this be okay if we haven't started a connection? LimelightCommonRuntimeComponent.StopConnection(); AvStream.Dispose(); hasMoved = false; }
/// <summary> /// Creates an input codec, based on the settings found in the dataPacket/headerPacket. /// </summary> /// <param name="dataPacket">Contains the frame Data.</param> /// <param name="headerPacket">Contains the frame header Data.</param> /// <returns></returns> private VideoInputData CreateCodecFromPacket(IResultNode dataPacket, IResultNode headerPacket) { // Load the framedata from the packet to the byte buffer if (!PacketDataToBuffer(dataPacket, headerPacket)) { #if DEBUG Log.Info("No frame header information is available."); #endif return(null); } IntPtr pInputFormatContext; if (_ffmpeg.AvOpenInputFile(out pInputFormatContext, DefraserProtocolPrefix + _convertId, IntPtr.Zero, 0, IntPtr.Zero) < 0) { #if DEBUG Log.Info("Could not open input file."); #endif return(null); } if (_ffmpeg.AvFindStreamInfo(pInputFormatContext) < 0) { #if DEBUG Log.Info("Could not find (input)stream information."); #endif return(null); } VideoInputData videoInputData = null; AvFormatContext formatContext = PtrToStructure <AvFormatContext>(pInputFormatContext); for (int i = 0; i < formatContext.NbStreams; ++i) { AvStream stream = PtrToStructure <AvStream>(formatContext.Streams[i]); AvCodecContext codec = PtrToStructure <AvCodecContext>(stream.Codec); if (codec.CodecType == CodecType.CodecTypeVideo) { AvCodecContext videoCodecContext = PtrToStructure <AvCodecContext>(stream.Codec); IntPtr pVideoCodec = _ffmpeg.AvcodecFindDecoder(videoCodecContext.CodecId); if (pVideoCodec == IntPtr.Zero) { #if DEBUG Log.Info("could not find input codec"); #endif return(null); } if (_ffmpeg.AvcodecOpen(stream.Codec, pVideoCodec) < 0) { #if DEBUG Log.Info("Could not open input codec."); #endif return(null); } // setup object used to decode (input) frames videoInputData = new VideoInputData(_ffmpeg, pInputFormatContext, stream.Codec, PtrToStructure <AvCodecContext>(stream.Codec), i); break; // stop seaching for video stream } } if (videoInputData == null) { #if DEBUG Log.Info("Could not find video stream."); #endif return(null); } return(videoInputData); }
public static void Main(string[] args) { WrapperUtils.RegisterLibrariesPathSimple("ffmpeg-x64", "ffmpeg-x86"); AvFormat.RegisterAll(); AvCodec.RegisterAll(); AvFormat.NetworkInit(); AvFormatContext format = AvFormatContext.Allocate(); if (!format.OpenInput(@"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4")) { throw new Exception("Failed to open file :("); } if (!format.FindStreamInfo()) { throw new Exception("Failed to find stream info :("); } AvStream pStream = null; foreach (AvStream avStream in format.Streams) { if (avStream.Codec.Type == AvMediaType.Video) { pStream = avStream; break; } } if (pStream == null) { throw new Exception("Could not find video stream :("); } AvCodecContext codecContext = pStream.Codec; int width = codecContext.Width; int height = codecContext.Height; AvPixelFormat sourceFormat = codecContext.PixelFormat; AvPixelFormat targetFormat = AvPixelFormat.Bgr24; SwsContext convertContext = SwsContext.Get(width, height, sourceFormat, width, height, targetFormat, SwsFlags.FastBilinear); if (convertContext == null) { throw new Exception("Could not initialize the conversion context"); } AvFrame convertedFrame = AvFrame.Allocate(); int convertedFrameBufferSize = AvPicture.GetSize(targetFormat, width, height); SByteBuffer convertedFrameBuffer = AvUtil.Malloc((ulong)convertedFrameBufferSize); ((AvPicture)convertedFrame).Fill(convertedFrameBuffer, targetFormat, width, height); AvCodec codec = AvCodec.FindDecoder(codecContext.Id); if (codec == null) { throw new Exception("Unsupported codec"); } if (codec.HasCapability(CodecCapabilities.Truncated)) { codecContext.Flags |= CodecFlags.Truncated; } if (!codecContext.Open2(codec)) { throw new Exception("Could not open codec"); } AvFrame frame = AvFrame.Allocate(); AvPacket packet = AvPacket.Create(); packet.Init(); int frameNumber = 0; while (frameNumber < 500) { if (!format.ReadFrame(packet)) { throw new Exception("Could not read frame!"); } if (packet.StreamIndex != pStream.Index) { continue; } int gotPicture; int size = codecContext.DecodeVideo2(frame, out gotPicture, packet); if (size < 0) { throw new Exception("Error while decoding frame " + frameNumber); } if (gotPicture == 1) { Console.WriteLine($"Frame: {frameNumber}"); SByteBufferArray src = frame.Data; SByteBufferArray dst = convertedFrame.Data; IntArray srcStride = frame.LineSize; IntArray dstStride = convertedFrame.LineSize; convertContext.Scale(src, srcStride, 0, height, dst, dstStride); int linesize = dstStride[0]; using ( Bitmap bitmap = new Bitmap(width, height, linesize, PixelFormat.Format24bppRgb, convertedFrame.Data0)) { bitmap.Save(@"frame.buffer." + frameNumber + ".jpg", ImageFormat.Jpeg); } frameNumber++; } } convertedFrame.Free(); convertedFrameBuffer.Free(); convertContext.Free(); frame.Free(); codecContext.Close(); format.CloseInput(); }
private void StopMediaPlayer() { AvStream.Stop(); StreamDisplay.Stop(); }
private void StartMediaPlayer() { AvStream.Start(); StreamDisplay.Play(); }
public void ArPlaySample(byte[] data) { AvStream.EnqueueAudioSample(data); }
public int DrSubmitDecodeUnit(byte[] data) { AvStream.EnqueueVideoSample(data); return(0); }
public void DrSubmitDecodeUnit(byte[] data) { AvStream.EnqueueVideoSample(data); }