/// <summary>Constructor</summary>
 /// <param name="encodingJobs">EncodingJobs handle.</param>
 public AFWorkerThreadBase(string threadName, AFServerMainThread mainThread, AFServerConfig serverConfig, EncodingJobs encodingJobs)
 {
     ThreadName   = threadName;
     MainThread   = mainThread;
     Config       = serverConfig;
     EncodingJobs = encodingJobs;
 }
Beispiel #2
0
 protected override void ThreadLoop(EncodingJobs encodingJobs, object[] objects = null)
 {
     while (_shutdown == false)
     {
         Thread.Sleep(5000);
     }
 }
        private void AddEncodingJob(VideoSourceData sourceData, string sourceDirectoryPath, string destinationDirectoryPath)
        {
            // Only add encoding job is file is ready.
            if (CheckFileReady(sourceData.FullPath))
            {
                EncodingJob encodingJob = new EncodingJob()
                {
                    Name                = sourceData.FileName,
                    SourceFullPath      = sourceData.FullPath,
                    DestinationFullPath = sourceData.FullPath.Replace(sourceDirectoryPath, destinationDirectoryPath)
                };

                EncodingJobs.AddEncodingJob(encodingJob);
            }
        }
Beispiel #4
0
 public EncodingThread(AFServerMainThread mainThread, AFServerConfig serverConfig, EncodingJobs encodingJobs)
     : base("EncodingThread", mainThread, serverConfig, encodingJobs)
 {
 }
 protected abstract void ThreadLoop(EncodingJobs encodingJobs, object[] objects = null);
        protected override void ThreadLoop(EncodingJobs encodingJobs, object[] threadObjects)
        {
            Dictionary <string, SearchDirectory> searchDirectories = (Dictionary <string, SearchDirectory>)threadObjects[0];

            while (_shutdown == false)
            {
                try
                {
                    Status = AFWorkerThreadStatus.PROCESSING;
                    if (_directoryUpdate)
                    {
                        UpdateSearchDirectories(searchDirectories);
                    }

                    bool bFoundEncodingJob = false;
                    foreach (KeyValuePair <string, SearchDirectory> entry in searchDirectories)
                    {
                        if (Directory.Exists(entry.Value.Source))
                        {
                            // TV Show structured directories
                            if (entry.Value.TVShowStructure)
                            {
                                List <VideoSourceData> newEncodingJobs = new List <VideoSourceData>();
                                lock (_showSourceFileLock)
                                {
                                    _showSourceFiles[entry.Key] = new List <ShowSourceData>();
                                    List <string> sourceShows      = Directory.GetDirectories(entry.Value.Source).ToList();
                                    List <string> destinationFiles = Directory.GetFiles(entry.Value.Destination, "*.*", SearchOption.AllDirectories).ToList()
                                                                     .Where(file => Config.ServerSettings.VideoFileExtensions.Any(file.ToLower().EndsWith)).Select(file => file = Path.GetFileNameWithoutExtension(file)).ToList();
                                    // Show
                                    foreach (string showPath in sourceShows)
                                    {
                                        string         showName = showPath.Replace(entry.Value.Source, "").RemoveLeadingSlash();
                                        ShowSourceData showData = new ShowSourceData(showName);
                                        List <string>  seasons  = Directory.GetDirectories(showPath).ToList();
                                        // Season
                                        foreach (string seasonPath in seasons)
                                        {
                                            string           season     = seasonPath.Replace(showPath, "").RemoveLeadingSlash();
                                            SeasonSourceData seasonData = new SeasonSourceData(season);
                                            List <string>    episodes   = Directory.GetFiles(seasonPath, "*.*", SearchOption.AllDirectories)
                                                                          .Where(file => Config.ServerSettings.VideoFileExtensions.Any(file.ToLower().EndsWith)).ToList();
                                            // Episode
                                            foreach (string episodePath in episodes)
                                            {
                                                string          episode     = episodePath.Replace(seasonPath, "").RemoveLeadingSlash();
                                                VideoSourceData episodeData = new VideoSourceData()
                                                {
                                                    FileName = episode,
                                                    FullPath = episodePath,
                                                    Encoded  = destinationFiles.Contains(Path.GetFileNameWithoutExtension(episodePath))
                                                };
                                                seasonData.Episodes.Add(episodeData);
                                                if (episodeData.Encoded == false && entry.Value.Automated == true)
                                                {
                                                    bFoundEncodingJob = true;
                                                    newEncodingJobs.Add(episodeData);
                                                }
                                            }
                                            showData.Seasons.Add(seasonData);
                                        }
                                        _showSourceFiles[entry.Key].Add(showData);
                                    }
                                }
                                newEncodingJobs.ForEach(x => AddEncodingJob(x, entry.Value.Source, entry.Value.Destination));
                            }
                            else
                            {
                                List <VideoSourceData> newEncodingJobs = new List <VideoSourceData>();
                                lock (_videoSourceFileLock)
                                {
                                    _videoSourceFiles[entry.Key] = new List <VideoSourceData>();
                                    List <string> sourceFiles = Directory.GetFiles(entry.Value.Source, "*.*", SearchOption.AllDirectories)
                                                                .Where(file => Config.ServerSettings.VideoFileExtensions.Any(file.ToLower().EndsWith)).ToList();
                                    List <string> destinationFiles = Directory.GetFiles(entry.Value.Destination, "*.*", SearchOption.AllDirectories)
                                                                     .Where(file => Config.ServerSettings.VideoFileExtensions.Any(file.ToLower().EndsWith)).Select(file => file = Path.GetFileNameWithoutExtension(file)).ToList();
                                    foreach (string sourceFile in sourceFiles)
                                    {
                                        // Handles files in subdirectories
                                        string filename = sourceFile.Replace(entry.Value.Source, "").RemoveLeadingSlash();

                                        VideoSourceData sourceData = new VideoSourceData()
                                        {
                                            FileName = filename,
                                            FullPath = sourceFile,
                                            Encoded  = destinationFiles.Contains(Path.GetFileNameWithoutExtension(sourceFile))
                                        };
                                        _videoSourceFiles[entry.Key].Add(sourceData);

                                        // If the source file has not been encoded already and it's an automated directory, add to encoding job list
                                        if (sourceData.Encoded == false && entry.Value.Automated == true)
                                        {
                                            bFoundEncodingJob = true;
                                            newEncodingJobs.Add(sourceData);
                                        }
                                    }
                                }

                                newEncodingJobs.ForEach(x => AddEncodingJob(x, entry.Value.Source, entry.Value.Destination));
                            }
                        }
                        else
                        {
                            // TODO Logging
                            Console.WriteLine($"{entry.Value.Source} does not exist.");
                        }
                    }

                    if (bFoundEncodingJob == false)
                    {
                        Sleep();
                    }
                }
                catch (Exception ex)
                {
                    // TODO Logging
                    Debug.WriteLine($"[{ThreadName}] ERROR: {ex.Message}");
                }
            }
        }
 /// <summary>Constructor</summary>
 /// <param name="mainThread">AFServerMainThread</param>
 /// <param name="serverConfig">AFServerConfig</param>
 /// <param name="encodingJobs">EncodingJobs</param>
 public EncodingJobFinderThread(AFServerMainThread mainThread, AFServerConfig serverConfig, EncodingJobs encodingJobs)
     : base("EncodingJobFinderThread", mainThread, serverConfig, encodingJobs)
 {
     _searchDirectories = Config.Directories.ToDictionary(x => x.Key, x => (SearchDirectory)x.Value.Clone());
 }