Ejemplo n.º 1
0
        protected override async Task <MediaSource> GetPlyaingVideoMediaSource()
        {
            var videoUri = VideoUrl;

            MovieType videoContentType = MovieType.Mp4;
            var       tempStream       = await HttpSequencialAccessStream.CreateAsync(
                NiconicoSession.Context.HttpClient
                , videoUri
                );

            if (tempStream is IRandomAccessStreamWithContentType)
            {
                var contentType = (tempStream as IRandomAccessStreamWithContentType).ContentType;

                if (contentType.EndsWith("mp4"))
                {
                    videoContentType = MovieType.Mp4;
                }
                else if (contentType.EndsWith("flv"))
                {
                    videoContentType = MovieType.Flv;
                }
                else if (contentType.EndsWith("swf"))
                {
                    videoContentType = MovieType.Swf;
                }
                else
                {
                    throw new NotSupportedException($"{contentType} is not supported video format.");
                }
            }

            if (videoContentType != MovieType.Mp4)
            {
                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(tempStream, false, false);
                var mss = _VideoMSS.GetMediaStreamSource();
                mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                return(MediaSource.CreateFromMediaStreamSource(mss));
            }
            else
            {
                return(MediaSource.CreateFromStream(tempStream, (tempStream as IRandomAccessStreamWithContentType).ContentType));
            }
        }
Ejemplo n.º 2
0
        public async Task StartPlayback(MediaPlayer player)
        {
            var videoUri = await GetVideoContentUri();

            // Note: HTML5プレイヤー移行中のFLV動画に対するフォールバック処理
            // サムネではContentType=FLV,SWFとなっていても、
            // 実際に渡される動画ストリームのContentTypeがMP4となっている場合がある

            var         videoContentType = MovieType.Mp4;
            MediaSource mediaSource      = null;

            if (!videoUri.IsFile)
            {
                // オンラインからの再生


                var tempStream = await HttpSequencialAccessStream.CreateAsync(
                    _Context.HttpClient
                    , videoUri
                    );

                if (tempStream is IRandomAccessStreamWithContentType)
                {
                    var contentType = (tempStream as IRandomAccessStreamWithContentType).ContentType;

                    if (contentType.EndsWith("mp4"))
                    {
                        videoContentType = MovieType.Mp4;
                    }
                    else if (contentType.EndsWith("flv"))
                    {
                        videoContentType = MovieType.Flv;
                    }
                    else if (contentType.EndsWith("swf"))
                    {
                        videoContentType = MovieType.Swf;
                    }
                    else
                    {
                        throw new NotSupportedException($"{contentType} is not supported video format.");
                    }
                }

                if (videoContentType != MovieType.Mp4)
                {
                    _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(tempStream, false, false);
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
                else
                {
                    tempStream.Dispose();
                    tempStream = null;

                    mediaSource = MediaSource.CreateFromUri(videoUri);
                }
            }
            else
            {
                // ローカル再生時


                var file = await StorageFile.GetFileFromPathAsync(videoUri.OriginalString);

                var stream = await file.OpenReadAsync();

                var contentType = stream.ContentType;

                if (contentType == null)
                {
                    throw new NotSupportedException("can not play video file. " + videoUri.OriginalString);
                }

                if (contentType == "video/mp4")
                {
                    mediaSource = MediaSource.CreateFromStream(stream, contentType);
                }
                else
                {
                    _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, false, false);
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
            }


            if (mediaSource != null)
            {
                player.Source       = mediaSource;
                _MediaSource        = mediaSource;
                _PlayingMediaPlayer = player;

                OnStartStreaming();
            }
            else
            {
                throw new NotSupportedException("can not play video. Video URI: " + videoUri);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 動画ストリームの取得します
        /// 他にダウンロードされているアイテムは強制的に一時停止し、再生終了後に再開されます
        /// </summary>
        public async Task <NicoVideoQuality> StartPlay(NicoVideoQuality?quality, TimeSpan?initialPosition = null)
        {
            IfVideoDeletedThrowException();

            // 再生動画画質決定
            // 指定された画質>キャッシュされた画質>デフォルト指定画質>再生可能な画質
            DividedQualityNicoVideo divided = null;

            if (quality.HasValue)
            {
                divided = GetDividedQualityNicoVideo(quality.Value);
            }

            if (divided == null || !divided.CanPlay)
            {
                var cachedQuality = GetAllQuality().Where(x => x.IsCached).FirstOrDefault();
                if (cachedQuality != null)
                {
                    divided = cachedQuality;
                }
                else if (quality == NicoVideoQuality.Original)
                {
                    divided = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                }
                else
                {
                    divided = GetDividedQualityNicoVideo(HohoemaApp.UserSettings.PlayerSettings.DefaultQuality);

                    if (!divided.CanPlay)
                    {
                        foreach (var dmcQuality in GetAllQuality().Where(x => x.Quality.IsDmc()))
                        {
                            if (dmcQuality.CanPlay)
                            {
                                divided = dmcQuality;
                                break;
                            }
                        }
                    }

                    if (divided == null || !divided.CanPlay)
                    {
                        divided = GetDividedQualityNicoVideo(NicoVideoQuality.Original);
                        if (!divided.CanPlay)
                        {
                            divided = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                        }
                    }
                }
            }

            if (divided == null || !divided.CanPlay)
            {
                throw new NotSupportedException("再生可能な動画品質を見つけられませんでした。");
            }

            Debug.WriteLine(divided.Quality);

            // キャッシュ済みの場合は
            if (divided.HasCache)
            {
                var file = await divided.GetCacheFile();

                NicoVideoCachedStream = await file.OpenReadAsync();

                await OnCacheRequested();
            }
            else if (ProtocolType == MediaProtocolType.RTSPoverHTTP)
            {
                if (Util.InternetConnection.IsInternet())
                {
                    var videoUrl = await divided.GenerateVideoContentUrl();

                    if (videoUrl == null)
                    {
                        divided  = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                        videoUrl = await divided.GenerateVideoContentUrl();
                    }
                    NicoVideoCachedStream = await HttpSequencialAccessStream.CreateAsync(
                        HohoemaApp.NiconicoContext.HttpClient
                        , videoUrl
                        );
                }
            }

            if (NicoVideoCachedStream == null)
            {
                throw new NotSupportedException();
            }

            if (initialPosition == null)
            {
                initialPosition = TimeSpan.Zero;
            }

            if (NicoVideoCachedStream is IRandomAccessStreamWithContentType)
            {
                var contentType = (NicoVideoCachedStream as IRandomAccessStreamWithContentType).ContentType;

                if (contentType.EndsWith("mp4"))
                {
                    ContentType = MovieType.Mp4;
                }
                else if (contentType.EndsWith("flv"))
                {
                    ContentType = MovieType.Flv;
                }
                else if (contentType.EndsWith("swf"))
                {
                    ContentType = MovieType.Swf;
                }
            }

            MediaSource mediaSource = null;

            if (ContentType == MovieType.Mp4)
            {
                string contentType = null;

                if (NicoVideoCachedStream is IRandomAccessStreamWithContentType)
                {
                    contentType = (NicoVideoCachedStream as IRandomAccessStreamWithContentType).ContentType;
                }

                if (contentType == null)
                {
                    throw new Exception("unknown movie content type");
                }

                mediaSource = MediaSource.CreateFromStream(NicoVideoCachedStream, contentType);
            }
            else
            {
                _VideoMSS?.Dispose();

                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(NicoVideoCachedStream, false, false);

                if (_VideoMSS != null)
                {
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            if (mediaSource != null)
            {
                HohoemaApp.MediaPlayer.Source = mediaSource;
                HohoemaApp.MediaPlayer.PlaybackSession.Position = initialPosition.Value;

                _MediaSource = mediaSource;

                var smtc = HohoemaApp.MediaPlayer.SystemMediaTransportControls;

                smtc.DisplayUpdater.Type = Windows.Media.MediaPlaybackType.Video;
                smtc.DisplayUpdater.VideoProperties.Title = Title ?? "Hohoema";
                if (ThumbnailUrl != null)
                {
                    smtc.DisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(this.ThumbnailUrl));
                }
                smtc.IsPlayEnabled  = true;
                smtc.IsPauseEnabled = true;
                smtc.DisplayUpdater.Update();

                divided.OnPlayStarted();
            }

            return(divided.Quality);
        }