Esempio n. 1
0
        public DividedQualityNicoVideo GetDividedQualityNicoVideo(NicoVideoQuality quality)
        {
            DividedQualityNicoVideo qualityDividedVideo = null;

            if (_InterfaceByQuality.ContainsKey(quality))
            {
                qualityDividedVideo = _InterfaceByQuality[quality];
            }
            else
            {
                switch (quality)
                {
                case NicoVideoQuality.Original:
                    qualityDividedVideo = new OriginalQualityNicoVideo(this, _NiconicoMediaManager);
                    break;

                case NicoVideoQuality.Low:
                    qualityDividedVideo = new LowQualityNicoVideo(this, _NiconicoMediaManager);
                    break;

                case NicoVideoQuality.Dmc_High:
                    qualityDividedVideo = new DmcQualityNicoVideo(quality, this, _NiconicoMediaManager);
                    break;

                case NicoVideoQuality.Dmc_Midium:
                    qualityDividedVideo = new DmcQualityNicoVideo(quality, this, _NiconicoMediaManager);
                    break;

                case NicoVideoQuality.Dmc_Low:
                    qualityDividedVideo = new DmcQualityNicoVideo(quality, this, _NiconicoMediaManager);
                    break;

                case NicoVideoQuality.Dmc_Mobile:
                    qualityDividedVideo = new DmcQualityNicoVideo(quality, this, _NiconicoMediaManager);
                    break;

                default:
                    throw new NotSupportedException(quality.ToString());
                }

                _InterfaceByQuality.Add(quality, qualityDividedVideo);
                _QualityDividedVideos.Add(qualityDividedVideo);
            }

            return(qualityDividedVideo);
        }
Esempio n. 2
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);
        }