public VideoDownloadManager(HohoemaApp hohoemaApp, VideoCacheManager mediaManager) { HohoemaApp = hohoemaApp; MediaManager = mediaManager; // ダウンロード完了をバックグラウンドで処理 CoreApplication.BackgroundActivated += CoreApplication_BackgroundActivated; CoreApplication.Suspending += CoreApplication_Suspending; }
public NicoVideo(string rawVideoid, NiconicoContentProvider contentProvider, NiconicoContext context, VideoCacheManager manager) { RawVideoId = rawVideoid; _ContentProvider = contentProvider; _Context = context; CacheManager = manager; CommentClient = new CommentClient(_Context, RawVideoId); }
static internal async Task <VideoCacheManager> Create(HohoemaApp app) { var man = new VideoCacheManager(app); await man.Initialize(); // await man.RetrieveCacheCompletedVideos(); return(man); }
private async void OnDownloadProgress(object sender, DownloadOperation op) { Debug.WriteLine($"{op.RequestedUri}:{op.Progress.TotalBytesToReceive}"); var req = VideoCacheManager.CacheRequestInfoFromFileName(op.ResultFile); var progress = await GetCacheProgress(req.RawVideoId, req.Quality); progress.DownloadOperation = op; DownloadProgress?.Invoke(this, progress); UpdateProgressToast(req, op); }
/// <summary> /// キャッシュダウンロードのタスクから /// ダウンロードの情報を復元します /// </summary> /// <returns></returns> private async Task RestoreBackgroundDownloadTask() { // TODO: ユーザーのログイン情報を更新してダウンロードを再開する必要がある? // ユーザー情報の有効期限が切れていた場合には最初からダウンロードし直す必要があるかもしれません var tasks = await BackgroundDownloader.GetCurrentDownloadsForTransferGroupAsync(_NicoCacheVideoBGTransferGroup); foreach (var task in tasks) { NicoVideoCacheProgress info = null; try { var _info = VideoCacheManager.CacheRequestInfoFromFileName(task.ResultFile); var nicoVideo = new NicoVideo(_info.RawVideoId, _HohoemaApp.ContentProvider, _HohoemaApp.NiconicoContext, _HohoemaApp.CacheManager); var session = await nicoVideo.CreateVideoStreamingSession(_info.Quality, forceDownload : true); if (session?.Quality == _info.Quality) { continue; } info = new NicoVideoCacheProgress(_info, task, session); await RestoreDonloadOperation(info, task); Debug.WriteLine($"実行中のキャッシュBGDLを補足: {info.RawVideoId} {info.Quality}"); } catch { Debug.WriteLine(task.ResultFile + "のキャッシュダウンロード操作を復元に失敗しました"); continue; } try { task.Resume(); } catch { if (task.Progress.Status != BackgroundTransferStatus.Running) { await RemoveDownloadOperation(info); // ダウンロード再開に失敗したらキャッシュリクエストに積み直します // 失敗の通知はここではなくバックグラウンドタスクの 後処理 として渡されるかもしれません DownloadProgress?.Invoke(this, info); } } } }
public DmcQualityNicoVideo(NicoVideoQuality quality, NicoVideo nicoVideo, VideoCacheManager context) : base(quality, nicoVideo, context) { }
public OriginalQualityNicoVideo(NicoVideo nicoVideo, VideoCacheManager context) : base(NicoVideoQuality.Smile_Original, nicoVideo, context) { }
public LowQualityNicoVideo(NicoVideo nicoVideo, VideoCacheManager context) : base(NicoVideoQuality.Smile_Low, nicoVideo, context) { }
// 次のキャッシュダウンロードを試行 // ダウンロード用の本数 private async Task TryNextCacheRequestedVideoDownload() { if (State != CacheManagerState.Running) { return; } NicoVideoCacheRequest nextDownloadItem = null; using (var releaser = await _CacheRequestProcessingLock.LockAsync()) { while (CanAddDownloadLine) { if (_DownloadOperations.Count == 0) { nextDownloadItem = _CacheDownloadPendingVideos .FirstOrDefault(); } if (nextDownloadItem == null) { break; } if (_DownloadOperations.Any(x => x.RawVideoId == nextDownloadItem.RawVideoId && x.Quality == nextDownloadItem.Quality)) { _CacheDownloadPendingVideos.Remove(nextDownloadItem); break; } Debug.WriteLine($"キャッシュ準備を開始: {nextDownloadItem.RawVideoId} {nextDownloadItem.Quality}"); // 動画ダウンロードURLを取得 var nicoVideo = new NicoVideo(nextDownloadItem.RawVideoId, _HohoemaApp.ContentProvider, _HohoemaApp.NiconicoContext, _HohoemaApp.CacheManager); var videoInfo = await _HohoemaApp.ContentProvider.GetNicoVideoInfo(nextDownloadItem.RawVideoId); // DownloadSessionを保持して、再生完了時にDisposeさせる必要がある var downloadSession = await nicoVideo.CreateVideoStreamingSession(nextDownloadItem.Quality, forceDownload : true); var uri = await downloadSession.GetDownloadUrlAndSetupDonwloadSession(); var downloader = new BackgroundDownloader() { TransferGroup = _NicoCacheVideoBGTransferGroup }; downloader.SuccessToastNotification = MakeSuccessToastNotification(videoInfo); downloader.FailureToastNotification = MakeFailureToastNotification(videoInfo); // 保存先ファイルの確保 var filename = VideoCacheManager.MakeCacheVideoFileName( videoInfo.Title, nextDownloadItem.RawVideoId, videoInfo.MovieType, downloadSession.Quality ); var videoFolder = await _HohoemaApp.GetVideoCacheFolder(); var videoFile = await videoFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); // ダウンロード操作を作成 var operation = downloader.CreateDownload(uri, videoFile); var progress = new NicoVideoCacheProgress(nextDownloadItem, operation, downloadSession); await AddDownloadOperation(progress); Debug.WriteLine($"キャッシュ準備完了: {nextDownloadItem.RawVideoId} {nextDownloadItem.Quality}"); // ダウンロードを開始 /* * if (Helpers.ApiContractHelper.IsFallCreatorsUpdateAvailable) * { * operation.IsRandomAccessRequired = true; * } */ var action = operation.StartAsync(); action.Progress = OnDownloadProgress; var task = action.AsTask(); TaskIdToCacheProgress.Add(task.Id, progress); var _ = task.ContinueWith(OnDownloadCompleted); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { CacheState = NicoVideoCacheState.Downloading, Request = progress }); Debug.WriteLine($"キャッシュ開始: {nextDownloadItem.RawVideoId} {nextDownloadItem.Quality}"); SendUpdatableToastWithProgress(videoInfo.Title, nextDownloadItem); // DL作業を作成できたらDL待ちリストから削除 _CacheDownloadPendingVideos.Remove(nextDownloadItem); await SaveDownloadRequestItems(); } } }
private async Task RetrieveCacheCompletedVideos() { var videoFolder = await _HohoemaApp.GetVideoCacheFolder(); if (videoFolder != null) { var files = await videoFolder.GetFilesAsync(); foreach (var file in files) { if (!(file.FileType == ".mp4" || file.FileType == ".flv")) { continue; } // ファイル名の最後方にある[]の中身の文字列を取得 // (動画タイトルに[]が含まれる可能性に配慮) var match = NicoVideoIdRegex.Match(file.Name); var id = match.Groups[1]?.Value; NicoVideoQuality quality = NicoVideoQuality.Unknown; if (string.IsNullOrEmpty(id)) { // 外部キャッシュとして取得可能かをチェック match = ExternalCachedNicoVideoIdRegex.Match(file.Name); if (match.Groups.Count > 0) { id = match.Groups[match.Groups.Count - 1].Value; } // 動画IDを抽出不可だった場合はスキップ if (string.IsNullOrEmpty(id)) { continue; } } else { quality = VideoCacheManager.GetQualityFromFileName(file.Name); } var info = new NicoVideoCacheInfo() { RawVideoId = id, Quality = quality, FilePath = file.Path, RequestAt = file.DateCreated.DateTime }; _CacheVideos.AddOrUpdate(info.RawVideoId, (x) => { return(new List <NicoVideoCacheInfo>() { info }); }, (x, y) => { var tempinfo = y.FirstOrDefault(z => z.Quality == info.Quality); if (tempinfo == null) { y.Add(info); } else { tempinfo.RequestAt = info.RequestAt; tempinfo.FilePath = info.FilePath; } return(y); }); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = info, CacheState = NicoVideoCacheState.Cached }); Debug.Write("."); } } }