/// <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 = GetFileSize(indexFile); while (offset < fileSize) { byte[] b = Restore(indexFile, offset, size); IndexCache ic = new IndexCache(b); if (ic != null) { if (ic.Time == time) { buffer = Restore(contentFile, ic.Offset, ic.Size); break; } } offset += size; } } } } return(buffer); }
/// <summary> /// SaveVideoChunks /// Save video chunks on disk /// </summary> public async Task <bool> SaveVideoChunks(ManifestManager 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))) { if (!DirectoryExists(Path.Combine(root, cache.StoragePath))) { CreateDirectory(Path.Combine(root, cache.StoragePath)); } using (var releaser = await internalVideoDiskLock.WriterLockAsync()) { // delete the initial files /* * await DeleteFile(VideoIndexFile); * await DeleteFile(VideoContentFile); * cache.VideoSavedChunks = 0; */ int VideoTrack = 0; foreach (var cl in cache.VideoChunkListList) { string FilePath = VideoContentFile + "_" + VideoTrack.ToString() + ".ismv"; ulong InitialVideoOffset = 0; ulong VideoOffset = GetFileSize(FilePath); if (VideoOffset == 0) { if (cl.ftypData != null) { Append(FilePath, cl.ftypData); Append(FilePath, cl.moovData); VideoOffset = GetFileSize(FilePath); } } else { InitialVideoOffset = VideoOffset; ChunkBuffer cc; while (cl.ChunksQueue.TryDequeue(out cc) == true) // foreach (var cc in cl.ChunksQueue) //for (int Index = (int)cl.OutputChunks; Index < (int)cl.InputChunks; Index++) { // var cc = cl.ChunksList.Values.ElementAt(Index); if ((cc != null) && (cc.GetLength() > 0)) { IndexCache ic = new IndexCache(cc.Time, VideoOffset, cc.GetLength()); if (ic != null) { ulong res = Append(FilePath, cc.chunkBuffer); if (res == cc.GetLength()) { VideoOffset += res; ulong result = Append(VideoIndexFile + "_" + VideoTrack.ToString(), ic.GetByteData()); if (result == indexSize) { // free buffer cc.chunkBuffer = null; cl.OutputBytes += res; cl.OutputChunks++; } else { bResult = false; System.Diagnostics.Debug.WriteLine("Error while archiving video"); } } else { System.Diagnostics.Debug.WriteLine("Error while archiving video"); } } } else { break; } } } VideoTrack++; if (InitialVideoOffset < VideoOffset) { bResult = true; } if (cache.GetOutputVideoSize() == cache.GetInputVideoSize()) { bResult = true; } } } } return(bResult); }