/// <summary> /// GetChunkBuffer /// Return the chunk buffer from the disk /// </summary> private async Task <byte[]> GetChunkBuffer(bool isVideo, string path, ulong time) { byte[] buffer = null; string dir = Path.Combine(root, path); if (!string.IsNullOrEmpty(dir)) { string indexFile = Path.Combine(dir, (isVideo == true ? videoIndexFileName : audioIndexFileName)); string contentFile = Path.Combine(dir, (isVideo == true ? videoContentFileName : audioContentFileName)); if ((!string.IsNullOrEmpty(contentFile)) && (!string.IsNullOrEmpty(indexFile))) { using (var releaser = (isVideo == true ? await internalVideoDiskLock.ReaderLockAsync(): await internalAudioDiskLock.ReaderLockAsync())) { ulong offset = 0; ulong size = 20; ulong fileSize = await GetFileSize(indexFile); while (offset < fileSize) { byte[] b = await Restore(indexFile, offset, size); IndexCache ic = new IndexCache(b); if (ic != null) { if (ic.Time == time) { buffer = await Restore(contentFile, ic.Offset, ic.Size); break; } } offset += size; } } } } return(buffer); }
/// <summary> /// GetChunkBuffer /// Return the chunk buffer from the disk /// </summary> private async Task<byte[]> GetChunkBuffer(bool isVideo, string path, ulong time) { byte[] buffer = null; string dir = Path.Combine(root, path); if (!string.IsNullOrEmpty(dir)) { string indexFile = Path.Combine(dir, (isVideo == true ? videoIndexFileName : audioIndexFileName)); string contentFile = Path.Combine(dir, (isVideo == true ? videoContentFileName : audioContentFileName)); if ((!string.IsNullOrEmpty(contentFile))&& (!string.IsNullOrEmpty(indexFile))) { using (var releaser = (isVideo == true ? await internalVideoDiskLock.ReaderLockAsync(): await internalAudioDiskLock.ReaderLockAsync())) { ulong offset = 0; ulong size = 20; ulong fileSize = await GetFileSize(indexFile); while (offset < fileSize) { byte[] b = await Restore(indexFile, offset, size); IndexCache ic = new IndexCache(b); if (ic != null) { if (ic.Time == time) { buffer = await Restore(contentFile, ic.Offset, ic.Size); break; } } offset += size; } } } } return buffer; }
/// <summary> /// SaveVideoChunks /// Save video chunks on disk /// </summary> public async Task <bool> SaveVideoChunks(ManifestCache cache) { bool bResult = false; string VideoIndexFile = Path.Combine(Path.Combine(root, cache.StoragePath), videoIndexFileName); string VideoContentFile = Path.Combine(Path.Combine(root, cache.StoragePath), videoContentFileName); if ((!string.IsNullOrEmpty(VideoIndexFile)) && (!string.IsNullOrEmpty(VideoContentFile))) { using (var releaser = await internalVideoDiskLock.WriterLockAsync()) { ulong VideoOffset = await GetFileSize(VideoContentFile); ulong InitialVideoOffset = VideoOffset; // delete the initial files /* * await DeleteFile(VideoIndexFile); * await DeleteFile(VideoContentFile); * cache.VideoSavedChunks = 0; */ for (int Index = (int)cache.VideoSavedChunks; Index < (int)cache.VideoDownloadedChunks; Index++) //foreach (var cc in cache.VideoChunkList) { var cc = cache.VideoChunkList[Index]; if ((cc != null) && (cc.GetLength() > 0)) { IndexCache ic = new IndexCache(cc.Time, VideoOffset, cc.GetLength()); if (ic != null) { ulong res = await Append(VideoContentFile, cc.chunkBuffer); if (res == cc.GetLength()) { VideoOffset += res; ulong result = await Append(VideoIndexFile, ic.GetByteData()); if (result == indexSize) { cache.VideoSavedChunks++; cache.VideoSavedBytes += res; // free buffer cc.chunkBuffer = null; // cache.VideoChunkList[Index].chunkBuffer = null; } } } } else { break; } } if (InitialVideoOffset < VideoOffset) { bResult = true; } if (cache.VideoSavedChunks == cache.VideoDownloadedChunks) { bResult = true; } } } return(bResult); }
/// <summary> /// SaveVideoChunks /// Save video chunks on disk /// </summary> public async Task<bool> SaveVideoChunks(ManifestCache cache) { bool bResult = false; string VideoIndexFile = Path.Combine(Path.Combine(root, cache.StoragePath), videoIndexFileName); string VideoContentFile = Path.Combine(Path.Combine(root, cache.StoragePath), videoContentFileName); if ((!string.IsNullOrEmpty(VideoIndexFile)) && (!string.IsNullOrEmpty(VideoContentFile))) { using (var releaser = await internalVideoDiskLock.WriterLockAsync()) { ulong VideoOffset = await GetFileSize(VideoContentFile); ulong InitialVideoOffset = VideoOffset; // delete the initial files /* await DeleteFile(VideoIndexFile); await DeleteFile(VideoContentFile); cache.VideoSavedChunks = 0; */ for (int Index = (int)cache.VideoSavedChunks; Index < (int)cache.VideoDownloadedChunks; Index++) //foreach (var cc in cache.VideoChunkList) { var cc = cache.VideoChunkList[Index]; if ((cc != null) && (cc.GetLength() > 0)) { IndexCache ic = new IndexCache(cc.Time, VideoOffset, cc.GetLength()); if (ic != null) { ulong res = await Append(VideoContentFile, cc.chunkBuffer); if (res == cc.GetLength()) { VideoOffset += res; ulong result = await Append(VideoIndexFile, ic.GetByteData()); if (result == indexSize) { cache.VideoSavedChunks++; cache.VideoSavedBytes += res; // free buffer cc.chunkBuffer = null; // cache.VideoChunkList[Index].chunkBuffer = null; } } } } else break; } if (InitialVideoOffset < VideoOffset) bResult = true; if (cache.VideoSavedChunks == cache.VideoDownloadedChunks) bResult = true; } } return bResult; }