示例#1
0
        public static MediaFoundationStreamingSources CreateFromニコ動(string user_id, string password, string video_id, WaveFormat soundDeviceFormat)
        {
            var sources = new MediaFoundationStreamingSources();

            #region " ニコ動から SourceReaderEx を生成する。"
            //----------------
            if (null == _HttpClient)
            {
                _HttpClient = new HttpClient();
            }

            // ログインする。
            var content = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "mail", user_id },
                { "password", password },
                { "next_url", string.Empty },
            });
            using (var responseLogin = _HttpClient.PostAsync("https://secure.nicovideo.jp/secure/login?site=niconico", content).Result)
            {
            }

            // 動画ページにアクセスする。(getflvより前に)
            var responseWatch = _HttpClient.GetStringAsync($"http://www.nicovideo.jp/watch/{video_id}").Result;

            // 動画情報を取得する。
            var responseGetFlv = _HttpClient.GetStringAsync($"http://flapi.nicovideo.jp/api/getflv/{video_id}").Result;
            var flvmap         = HttpUtility.ParseQueryString(responseGetFlv);
            var flvurl         = flvmap["url"];

            // 動画の長さを取得する。
            ulong  長さbyte      = 0;
            string contentType = "";
            using (var requestMovie = new HttpRequestMessage(HttpMethod.Get, flvurl))
                using (var responseMovie = _HttpClient.SendAsync(requestMovie, HttpCompletionOption.ResponseHeadersRead).Result)
                {
                    長さbyte      = (ulong)(responseMovie.Content.Headers.ContentLength);
                    contentType = responseMovie.Content.Headers.ContentType.MediaType;
                }

            // IMFByteStream を生成する。
            sources._ByteStream                = new ByteStream(IntPtr.Zero);
            sources._HttpRandomAccessStream    = new HttpRandomAccessStream(_HttpClient, 長さbyte, flvurl);
            sources._unkHttpRandomAccessStream = new ComObject(Marshal.GetIUnknownForObject(sources._HttpRandomAccessStream));
            MediaFactory.CreateMFByteStreamOnStreamEx(sources._unkHttpRandomAccessStream, sources._ByteStream);
            using (var 属性 = sources._ByteStream.QueryInterfaceOrNull <MediaAttributes>())
            {
                // content-type を設定する。
                属性.Set(ByteStreamAttributeKeys.ContentType, contentType);
            }

            // SourceResolver で IMFByteStream から MediaSouce を取得する。
            using (var sourceResolver = new SourceResolver())
                using (var unkMediaSource = sourceResolver.CreateObjectFromStream(sources._ByteStream, null, SourceResolverFlags.MediaSource))
                {
                    sources._MediaSource = unkMediaSource.QueryInterface <MediaSource>();

                    // MediaSource から SourceReaderEx を生成する。
                    using (var 属性 = new MediaAttributes())
                    {
                        // DXVAに対応しているGPUの場合には、それをデコードに利用するよう指定する。
                        属性.Set(SourceReaderAttributeKeys.D3DManager, グラフィックデバイス.Instance.MFDXGIDeviceManager);

                        // 追加のビデオプロセッシングを有効にする。
                        属性.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true); // 真偽値が bool だったり

                        // 追加のビデオプロセッシングを有効にしたら、こちらは無効に。
                        属性.Set(SinkWriterAttributeKeys.ReadwriteDisableConverters, 0);         // int だったり

                        // 属性を使って、SourceReaderEx を生成。
                        using (var sourceReader = new SourceReader(sources._MediaSource, 属性))
                        {
                            sources._SourceReaderEx = sourceReader.QueryInterfaceOrNull <SourceReaderEx>();
                        }
                    }
                }
            //----------------
            #endregion

            #region " WaveFormat を生成。"
            //----------------
            sources._Audioのフォーマット = new WaveFormat(
                soundDeviceFormat.SampleRate,
                32,
                soundDeviceFormat.Channels,
                AudioEncoding.IeeeFloat);
            //----------------
            #endregion

            sources._SourceReaderEx生成後の初期化();

            return(sources);
        }
示例#2
0
        /// <summary>
        /// Create a topology to be played with a MediaSession from a filepath.
        /// </summary>
        internal static Topology CreateTopology(ByteStream mediaInputStream, out MediaSource mediaSource)
        {
            // collector to dispose all the created Media Foundation native objects.
            var collector = new ObjectCollector();

            // Get the MediaSource object.
            var sourceResolver = new SourceResolver();

            collector.Add(sourceResolver);
            ComObject mediaSourceObject;

            // Try to load music
            try
            {
                mediaSourceObject = sourceResolver.CreateObjectFromStream(mediaInputStream, null, SourceResolverFlags.MediaSource | SourceResolverFlags.ContentDoesNotHaveToMatchExtensionOrMimeType);
            }
            catch (SharpDXException)
            {
                collector.Dispose();
                throw new InvalidOperationException("Music stream format not supported");
            }

            Topology retTopo;

            try
            {
                mediaSource = mediaSourceObject.QueryInterface <MediaSource>();
                collector.Add(mediaSourceObject);

                // Get the PresentationDescriptor
                PresentationDescriptor presDesc;
                mediaSource.CreatePresentationDescriptor(out presDesc);
                collector.Add(presDesc);

                // Create the topology
                MediaFactory.CreateTopology(out retTopo);
                for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
                {
                    RawBool          selected;
                    StreamDescriptor desc;
                    presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);
                    collector.Add(desc);

                    if (selected)
                    {
                        // Test that the audio file data is valid and supported.
                        var typeHandler = desc.MediaTypeHandler;
                        collector.Add(typeHandler);

                        var majorType = typeHandler.MajorType;

                        if (majorType != MediaTypeGuids.Audio)
                        {
                            throw new InvalidOperationException("The music stream is not a valid audio stream.");
                        }

                        for (int mType = 0; mType < typeHandler.MediaTypeCount; mType++)
                        {
                            MediaType type;
                            typeHandler.GetMediaTypeByIndex(mType, out type);
                            collector.Add(type);

                            var nbChannels = type.Get(MediaTypeAttributeKeys.AudioNumChannels);
                            if (nbChannels > 2)
                            {
                                throw new InvalidOperationException("The provided audio stream has more than 2 channels.");
                            }
                        }

                        // create the topology (source,...)
                        TopologyNode sourceNode;
                        MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);
                        collector.Add(sourceNode);

                        sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                        sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                        sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);

                        TopologyNode outputNode;
                        MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);
                        collector.Add(outputNode);

                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);
                        collector.Add(activate);
                        outputNode.Object = activate;

                        retTopo.AddNode(sourceNode);
                        retTopo.AddNode(outputNode);
                        sourceNode.ConnectOutput(0, outputNode, 0);
                    }
                }
            }
            finally
            {
                collector.Dispose();
            }

            return(retTopo);
        }
示例#3
0
文件: Video.cs 项目: ttou73/IronStar
        /// <summary>
        ///
        /// </summary>
        private void PlatformInitialize(byte[] bytes, Stream stream, string url)
        {
            if (Topology != null)
            {
                return;
            }

            MediaFactory.CreateTopology(out _topology);

            SharpDX.MediaFoundation.MediaSource mediaSource;
            {
                SourceResolver resolver = new SourceResolver();

                ObjectType otype;
                ComObject  source = null;

                if (url != null)
                {
                    source = resolver.CreateObjectFromURL(url, SourceResolverFlags.MediaSource, null, out otype);
                }

                if (stream != null)
                {
                    var bs = new ByteStream(stream);
                    source = resolver.CreateObjectFromStream(bs, null, SourceResolverFlags.MediaSource, null, out otype);
                }

                if (bytes != null)
                {
                    var bs = new ByteStream(bytes);
                    source = resolver.CreateObjectFromStream(bs, null, SourceResolverFlags.MediaSource | SourceResolverFlags.ContentDoesNotHaveToMatchExtensionOrMimeType, null, out otype);
                }

                if (source == null)
                {
                    throw new ArgumentException("'stream' and 'url' are null!");
                }

                mediaSource = source.QueryInterface <SharpDX.MediaFoundation.MediaSource>();


                resolver.Dispose();
                source.Dispose();
            }


            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                RawBool          selected = false;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);

                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);


                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;

                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        sampleGrabber = new VideoSampleGrabber();

                        _mediaType = new MediaType();

                        _mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        _mediaType.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));

                        MediaFactory.CreateSampleGrabberSinkActivate(_mediaType, SampleGrabber, out activate);
                        outputNode.Object = activate;


                        long frameSize = desc.MediaTypeHandler.CurrentMediaType.Get <long>(MediaTypeAttributeKeys.FrameSize);

                        Width  = (int)(frameSize >> 32);
                        Height = (int)(frameSize & 0x0000FFFF);
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    _topology.AddNode(sourceNode);
                    _topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);


                    Duration = new TimeSpan(presDesc.Get <long>(PresentationDescriptionAttributeKeys.Duration));


                    sourceNode.Dispose();
                    outputNode.Dispose();
                }

                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();


            videoFrame = new DynamicTexture(Game.Instance.RenderSystem, Width, Height, typeof(ColorBGRA), false, false);
        }