public void ProcessBuffer(byte[] buffer) { byte[] packetBuffer = new byte[188]; using (MemoryStream tsStream = new MemoryStream(buffer)) { while (tsStream.Position < tsStream.Length) { _bytesInLastSecond += 188; tsStream.Read(packetBuffer, 0, 188); TransportStreamPacket tsPacket = PacketParsing.ParsePacket(packetBuffer); if (tsPacket.packetID == 0x60 || tsPacket.packetID == 0x61) { if (_currentFwPackets.ContainsKey(tsPacket.packetID)) { if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6)) { // We have started a new packet using (BinaryReaderEndian packetReader = new BinaryReaderEndian(_currentFwPackets[tsPacket.packetID], false)) { packetReader.DontDispose = true; packetReader.BaseStream.Seek(0, SeekOrigin.Begin); FwPacket fwPacket; try { fwPacket = ProcessNextFwPacket(packetReader); } catch (EndOfStreamException) { // Probably a better way of doing this! (When 0x00 0xB5 exists at the beginning of a packet naturaly) _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length); continue; } string fwName = string.Format("{0:X2}-{1:X2}-{2:X2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion); lock (CurrentFiles) { CurrentFiles[tsPacket.packetID] = string.Format("{0:X2}{1:X2}-r{2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion); } if (fwPacket.SectionType == 1 && !CompletedFiles.ContainsKey(fwName)) { if (!InProgressFiles.ContainsKey(fwName)) { InProgressFiles.Add(fwName, new FirmwareFile(fwPacket, _extractFolder)); InProgressFileList.Add(InProgressFiles[fwName]); } else { InProgressFiles[fwName].AddPacket(fwPacket); } if (InProgressFiles[fwName].isComplete) { _completedListHasChanged = true; CompletedFiles.Add(fwName, InProgressFiles[fwName]); CompletedFileList.Add(InProgressFiles[fwName]); InProgressFileList.Remove(InProgressFiles[fwName]); InProgressFiles.Remove(fwName); } } } _currentFwPackets[tsPacket.packetID].Close(); _currentFwPackets[tsPacket.packetID] = new MemoryStream(); } _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length); } else if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6)) { _currentFwPackets.Add(tsPacket.packetID, new MemoryStream()); _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length); } } } } }
private void buttonCapture_Click(object sender, RoutedEventArgs e) { _extractFolder = txtTsLocation.Text; if (Directory.Exists(_extractFolder)) { if (Properties.Settings.Default.ExtractDirectory != _extractFolder) { Properties.Settings.Default.ExtractDirectory = _extractFolder; Properties.Settings.Default.Save(); } txtTsLocation.IsEnabled = false; buttonBrowse.IsEnabled = false; // List already downloaded items DirectoryInfo extractDir = new DirectoryInfo(_extractFolder); foreach (DirectoryInfo fwFolder in extractDir.EnumerateDirectories()) { FirmwareFile fwFile = new FirmwareFile(fwFolder.FullName); CompletedFiles.Add(string.Format("{0:X2}-{1:X2}-{2:X2}", fwFile.VendorId, fwFile.ModelId, fwFile.VersionId), fwFile); CompletedFileList.Add(fwFile); } timer.Interval = new TimeSpan(0, 0, 1); timer.Tick += new EventHandler(timer_Tick); timer.Start(); try { // Start BDA Capture int hr = 0; IDVBSTuningSpace tuningSpace; tuningSpace = (IDVBSTuningSpace) new DVBSTuningSpace(); hr = tuningSpace.put_UniqueName("DVBS TuningSpace"); hr = tuningSpace.put_FriendlyName("DVBS TuningSpace"); hr = tuningSpace.put__NetworkType(typeof(DVBSNetworkProvider).GUID); hr = tuningSpace.put_SystemType(DVBSystemType.Satellite); hr = tuningSpace.put_LowOscillator(9750000); hr = tuningSpace.put_HighOscillator(10600000); ITuneRequest tr = null; hr = tuningSpace.CreateTuneRequest(out tr); DsError.ThrowExceptionForHR(hr); IDVBTuneRequest tuneRequest = (IDVBTuneRequest)tr; hr = tuneRequest.put_ONID(2); hr = tuneRequest.put_TSID(2004); hr = tuneRequest.put_SID(4190); IDVBSLocator locator = (IDVBSLocator) new DVBSLocator(); hr = locator.put_CarrierFrequency(11778000); hr = locator.put_SymbolRate(27500000); hr = locator.put_Modulation(ModulationType.ModQpsk); hr = (locator as IDVBSLocator).put_SignalPolarisation(Polarisation.LinearV); hr = (locator as IDVBSLocator).put_InnerFEC(FECMethod.Viterbi); hr = (locator as IDVBSLocator).put_InnerFECRate(BinaryConvolutionCodeRate.Rate2_3); hr = (locator as IDVBSLocator).put_OuterFEC(FECMethod.Viterbi); hr = (locator as IDVBSLocator).put_OuterFECRate(BinaryConvolutionCodeRate.Rate2_3); hr = tr.put_Locator(locator as ILocator); Marshal.ReleaseComObject(locator); this.bdaGraphBuilder = new BDAGraphBuilder(); this.bdaGraphBuilder.BuildGraph(tuningSpace); this.bdaGraphBuilder.SubmitTuneRequest(tr); // We have to do this to make it actually tune! this.bdaGraphBuilder.RunGraph(); this.bdaGraphBuilder.StopGraph(); TsGrabber grabber = new TsGrabber(); grabber.Callback = new TsGrabber.ProcessBufferDelegate(ProcessBuffer); this.bdaGraphBuilder.SetUpForTs(grabber, 1); this.bdaGraphBuilder.RunGraph(); this.buttonCapture.IsEnabled = false; } catch (Exception ex) { MessageBox.Show(string.Format("Error connecting to DVB-S:\n{0}", ex), "Connection error", MessageBoxButton.OK, MessageBoxImage.Error); Close(); } } else { MessageBox.Show("Cannot find extract directory specified!"); } }
public AppModel() { this.listener = new Lazy <AppModelTraceListener>(() => Trace.Listeners.OfType <AppModelTraceListener>().FirstOrDefault()); this.uMonitor = new UTorrentMonitor(Application.Current.Dispatcher); this.uMonitor.ReadyTorrents += OnTorrentReady; this.WorkQueue = new BlockingCollection <EpisodeFile>(); this.RunningFiles = new ObservableCollection <EpisodeFile>(); this.Files = new ObservableCollection <EpisodeFile>(); this.CompletedFiles = new ObservableCollection <EpisodeFile>(); this.Files.CollectionChanged += (s, e) => { switch (e.Action) { case NotifyCollectionChangedAction.Remove: foreach (var item in e.OldItems.Cast <EpisodeFile>()) { item.Cancel(); } break; case NotifyCollectionChangedAction.Reset: foreach (var item in this.Files) { item.Cancel(); } break; } OnPropertyChanged("IsFileSetEmpty", "Status"); }; this.RunningFiles.CollectionChanged += (s, e) => { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (var item in e.NewItems.Cast <EpisodeFile>()) { item.Status = "Waiting ..."; this.WorkQueue.Add(item); } } }; Task.Factory.StartNew(() => { foreach (var item in WorkQueue.GetConsumingEnumerable()) { var file = item; // do the processing file.Status = "Processing ..."; var errors = new List <string>(); // first do copy foreach ( var action in file.Actions.Where(a => a.Command == EpisodeFileActionCommand.Copy && a.IsEnabled)) { try { item.Status = action.TargetCommand; var dir = Path.GetDirectoryName(action.TargetPath); if ((dir != null) && (!Directory.Exists(dir))) { Directory.CreateDirectory(dir); } var fo = new InteropSHFileOperation() { pFrom = item.FilePath, pTo = action.TargetPath, wFunc = InteropSHFileOperation.FO_Func.FO_COPY, fFlags = new InteropSHFileOperation.FILEOP_FLAGS() { FOF_RENAMEONCOLLISION = true } }; fo.Execute(); //File.Copy(item.FilePath, action.TargetPath, false); action.IsEnabled = false; } catch (Exception ex) { errors.Add(ex.Message); } } var renameAction = file.Actions.FirstOrDefault(a => a.Command == EpisodeFileActionCommand.Move && a.IsEnabled); if (errors.Count == 0 && renameAction != null) { try { file.Status = renameAction.TargetCommand; var dir = Path.GetDirectoryName(renameAction.TargetPath); if ((dir != null) && (!Directory.Exists(dir))) { Directory.CreateDirectory(dir); } var fo = new InteropSHFileOperation() { pFrom = item.FilePath, pTo = renameAction.TargetPath, wFunc = InteropSHFileOperation.FO_Func.FO_MOVE, fFlags = new InteropSHFileOperation.FILEOP_FLAGS() { FOF_RENAMEONCOLLISION = true } }; fo.Execute(); renameAction.IsEnabled = false; } catch (Exception ex) { errors.Add(ex.Message); } } if (errors.Count == 0) { file.Status = "Done"; try { //detect if this directory contains any more video files or files bigger then 1MB // if not, delete it var dir = Path.GetDirectoryName(file.FilePath); if (!Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories).Select(fl => new FileInfo(fl)).Any(fl => ((fl.Length > 1024 * 1024) || Settings.GetVideoExtensions().Any(ex => string.Equals(fl.Extension, ex, StringComparison.CurrentCultureIgnoreCase))) )) { // delete directory Directory.Delete(dir, true); } } catch { } Application.Current.Dispatcher.BeginInvoke(new Action(() => { file.Errors.Clear(); RunningFiles.Remove(file); CompletedFiles.Add(file); })); } else { file.Status = "Waiting for retry"; Application.Current.Dispatcher.BeginInvoke(new Action(() => { file.Errors.Clear(); foreach (var err in errors) { file.Errors.Add(err); } })); file.RetryTimer = new DispatcherTimer(TimeSpan.FromMinutes(5), DispatcherPriority.Normal, (s, e) => { file.Errors.Clear(); file.RetryTimer.Stop(); file.Status = "Pending ..."; this.WorkQueue.Add(file); }, Application.Current.Dispatcher); } } }); this.RunWatcher(); //this.Watcher.EnableRaisingEvents = this.Settings.RunMonitor; this.Settings.SettingsSaving += Settings_SettingsSaving; }