/// <summary> /// SaveManifest /// Save manifest on disk /// </summary> public async Task <bool> SaveManifest(ManifestCache cache) { bool bResult = false; using (var releaser = await internalManifestDiskLock.WriterLockAsync()) { System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Writer Enter for Uri: " + cache.ManifestUri.ToString()); try { using (MemoryStream ms = new MemoryStream()) { if (ms != null) { System.Runtime.Serialization.DataContractSerializer ser = new System.Runtime.Serialization.DataContractSerializer(typeof(ManifestCache)); ser.WriteObject(ms, cache); bResult = await Save(Path.Combine(Path.Combine(root, cache.StoragePath), manifestFileName), ms.ToArray()); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Writer exception for Uri: " + cache.ManifestUri.ToString() + " Exception: " + e.Message); } System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Writer Exit for Uri: " + cache.ManifestUri.ToString()); } 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); }