changeset 6872:305778086d85 streaming

introduction of DicomSequentialReader::Factory
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 28 May 2026 17:06:57 +0200
parents c6830eae8586
children 42ca9122d003
files OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp OrthancFramework/Sources/DataSource/DicomSequentialReader.h OrthancServer/Sources/OrthancGetRequestHandler.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h OrthancServer/Sources/ServerJobs/ArchiveJob.cpp
diffstat 6 files changed, 146 insertions(+), 145 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Thu May 28 17:06:57 2026 +0200
@@ -29,6 +29,8 @@
 #include "../OrthancException.h"
 #include "../StringMemoryBuffer.h"
 #include "BaseDataIdentifier.h"
+#include "DataSourceReader.h"
+#include "DataSourceSequentialReader.h"
 #include "DicomDataSource.h"
 #include "StorageAreaDataSource.h"
 #include "TranscoderDataSource.h"
@@ -191,7 +193,7 @@
     mode_(mode),
     hasLossyQuality_(hasLossyQuality),
     lossyQuality_(lossyQuality),
-    reader_(executor, reader, new Disconnector(sourceType), windowSize, windowCapacity)
+    reader_(new DataSourceSequentialReader(executor, reader, new Disconnector(sourceType), windowSize, windowCapacity))
   {
   }
 
@@ -226,62 +228,7 @@
       dynamic_cast<BaseDataIdentifier&>(*id).SetUserData(protection.release());
     }
 
-    reader_.Submit(id.release());
-  }
-
-
-  DicomSequentialReader* DicomSequentialReader::CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor,
-                                                                  ExpectedAnswer expectedAnswer,
-                                                                  const boost::shared_ptr<DataSourceReader>& dicomReader,
-                                                                  unsigned int windowSize,
-                                                                  uint64_t windowCapacity)
-  {
-    switch (expectedAnswer)
-    {
-      case ExpectedAnswer_ParsedDicomFile:
-        return new DicomSequentialReader(
-          SourceType_Dicom, executor,
-          DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew,  /* dummy values */
-          false /* no lossy */, 0 /* unused lossy quality */,
-          dicomReader, windowSize, windowCapacity);
-
-      case ExpectedAnswer_RawMemoryBuffer:
-        return new DicomSequentialReader(
-          SourceType_StorageArea, executor,
-          DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew,  /* dummy values */
-          false /* no lossy */, 0 /* unused lossy quality */,
-          dicomReader, windowSize, windowCapacity);
-
-      default:
-        throw OrthancException(ErrorCode_NotImplemented);
-    }
-  }
-
-
-  DicomSequentialReader* DicomSequentialReader::CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor,
-                                                                    ExpectedAnswer expectedAnswer,
-                                                                    DicomTransferSyntax targetSyntax,
-                                                                    TranscodingSopInstanceUidMode mode,
-                                                                    bool hasLossyQuality,
-                                                                    unsigned int lossyQuality,
-                                                                    const boost::shared_ptr<DataSourceReader>& transcoderReader,
-                                                                    unsigned int windowSize,
-                                                                    uint64_t windowCapacity)
-  {
-    switch (expectedAnswer)
-    {
-      case ExpectedAnswer_RawMemoryBuffer:
-        // TODO
-
-      case ExpectedAnswer_ParsedDicomFile:
-        return new DicomSequentialReader(
-          SourceType_Transcoder, executor,
-          targetSyntax, mode, hasLossyQuality, lossyQuality,
-          transcoderReader, windowSize, windowCapacity);
-
-      default:
-        throw OrthancException(ErrorCode_NotImplemented);
-    }
+    reader_->Submit(id.release());
   }
 
 
@@ -305,19 +252,19 @@
 
   void DicomSequentialReader::Start()
   {
-    reader_.Start();
+    reader_->Start();
   }
 
 
   bool DicomSequentialReader::HasNext()
   {
-    return reader_.HasNext();
+    return reader_->HasNext();
   }
 
 
   DicomSequentialReader::Item* DicomSequentialReader::Next()
   {
-    std::unique_ptr<DataSourceSequentialReader::Item> source(reader_.Next());
+    std::unique_ptr<DataSourceSequentialReader::Item> source(reader_->Next());
 
     std::unique_ptr<Item> target(dynamic_cast<Item*>(source->ReleaseValue()));
 
@@ -328,4 +275,74 @@
 
     return target.release();
   }
+
+
+  DicomSequentialReader::Factory::Factory(const boost::shared_ptr<IExecutorService>& executor,
+                                          const boost::shared_ptr<DataSourceReader>& storageAreaReader,
+                                          const boost::shared_ptr<DataSourceReader>& dicomReader,
+                                          const boost::shared_ptr<DataSourceReader>& transcoderReader,
+                                          unsigned int windowSize,
+                                          uint64_t windowCapacity) :
+    executor_(executor),
+    storageAreaReader_(storageAreaReader),
+    dicomReader_(dicomReader),
+    transcoderReader_(transcoderReader),
+    windowSize_(windowSize),
+    windowCapacity_(windowCapacity)
+  {
+    if (!executor)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    if (windowSize == 0)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::Factory::CreateForOriginalRawMemoryBuffer() const
+  {
+    return new DicomSequentialReader(
+      DicomSequentialReader::SourceType_StorageArea, executor_,
+      DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew,  /* dummy values */
+      false /* no lossy */, 0 /* unused lossy quality */,
+      storageAreaReader_, windowSize_, windowCapacity_);
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::Factory::CreateForTranscodedRawMemoryBuffer(DicomTransferSyntax targetSyntax,
+                                                                                            TranscodingSopInstanceUidMode mode,
+                                                                                            bool hasLossyQuality,
+                                                                                            unsigned int lossyQuality) const
+  {
+    // TODO
+    return new DicomSequentialReader(
+      DicomSequentialReader::SourceType_Transcoder, executor_,
+      targetSyntax, mode, hasLossyQuality, lossyQuality,
+      transcoderReader_, windowSize_, windowCapacity_);
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::Factory::CreateForOriginalParsedDicomFile() const
+  {
+    return new DicomSequentialReader(
+      DicomSequentialReader::SourceType_Dicom, executor_,
+      DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew,  /* dummy values */
+      false /* no lossy */, 0 /* unused lossy quality */,
+      dicomReader_, windowSize_, windowCapacity_);
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::Factory::CreateForTranscodedParsedDicomFile(DicomTransferSyntax targetSyntax,
+                                                                                            TranscodingSopInstanceUidMode mode,
+                                                                                            bool hasLossyQuality,
+                                                                                            unsigned int lossyQuality) const
+  {
+    return new DicomSequentialReader(
+      DicomSequentialReader::SourceType_Transcoder, executor_,
+      targetSyntax, mode, hasLossyQuality, lossyQuality,
+      transcoderReader_, windowSize_, windowCapacity_);
+  }
 }
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Thu May 28 17:06:57 2026 +0200
@@ -27,22 +27,17 @@
 #include "../FileStorage/FileInfo.h"
 #include "../IMemoryBuffer.h"
 #include "../MultiThreading/IExecutorService.h"
-#include "DataSourceSequentialReader.h"
 
 
 namespace Orthanc
 {
+  class DataSourceReader;
+  class DataSourceSequentialReader;
   class ParsedDicomFile;
 
   class ORTHANC_PUBLIC DicomSequentialReader : public boost::noncopyable
   {
   public:
-    enum ExpectedAnswer
-    {
-      ExpectedAnswer_ParsedDicomFile,
-      ExpectedAnswer_RawMemoryBuffer
-    };
-
     class ORTHANC_PUBLIC Item : public IDynamicObject
     {
     private:
@@ -82,12 +77,12 @@
       SourceType_Transcoder
     };
 
-    SourceType                     sourceType_;
-    DicomTransferSyntax            targetSyntax_;
-    TranscodingSopInstanceUidMode  mode_;
-    bool                           hasLossyQuality_;
-    unsigned int                   lossyQuality_;
-    DataSourceSequentialReader     reader_;
+    SourceType                                     sourceType_;
+    DicomTransferSyntax                            targetSyntax_;
+    TranscodingSopInstanceUidMode                  mode_;
+    bool                                           hasLossyQuality_;
+    unsigned int                                   lossyQuality_;
+    boost::shared_ptr<DataSourceSequentialReader>  reader_;  // boost::shared_ptr<> for PImpl
 
     DicomSequentialReader(SourceType sourceType,
                           const boost::shared_ptr<IExecutorService>& executor,
@@ -103,21 +98,50 @@
                         IDynamicObject* userData);
 
   public:
-    static DicomSequentialReader* CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor,
-                                                    ExpectedAnswer expectedAnswer,
-                                                    const boost::shared_ptr<DataSourceReader>& dicomReader,
-                                                    unsigned int windowSize,
-                                                    uint64_t windowCapacity);
+    // The factory can be called from multiple threads
+    class ORTHANC_PUBLIC Factory : public boost::noncopyable
+    {
+    private:
+      boost::shared_ptr<IExecutorService>  executor_;
+      boost::shared_ptr<DataSourceReader>  storageAreaReader_;
+      boost::shared_ptr<DataSourceReader>  dicomReader_;
+      boost::shared_ptr<DataSourceReader>  transcoderReader_;
+      unsigned int                         windowSize_;
+      uint64_t                             windowCapacity_;
+
+    public:
+      // The readers are allowed to be NULL, for unit tests
+      Factory(const boost::shared_ptr<IExecutorService>& executor,
+              const boost::shared_ptr<DataSourceReader>& storageAreaReader,
+              const boost::shared_ptr<DataSourceReader>& dicomReader,
+              const boost::shared_ptr<DataSourceReader>& transcoderReader,
+              unsigned int windowSize,
+              uint64_t windowCapacity);
 
-    static DicomSequentialReader* CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor,
-                                                      ExpectedAnswer expectedAnswer,
-                                                      DicomTransferSyntax targetSyntax,
-                                                      TranscodingSopInstanceUidMode mode,
-                                                      bool hasLossyQuality,
-                                                      unsigned int lossyQuality,
-                                                      const boost::shared_ptr<DataSourceReader>& transcoderReader,
-                                                      unsigned int windowSize,
-                                                      uint64_t windowCapacity);
+      /**
+       * Methods below prioritize the downloading of the raw DICOM
+       * files (and avoid DICOM parsing if possible).
+       **/
+
+      DicomSequentialReader* CreateForOriginalRawMemoryBuffer() const;
+
+      DicomSequentialReader* CreateForTranscodedRawMemoryBuffer(DicomTransferSyntax targetSyntax,
+                                                                TranscodingSopInstanceUidMode mode,
+                                                                bool hasLossyQuality,
+                                                                unsigned int lossyQuality) const;
+
+      /**
+       * Methods below prioritize the parsing of the DICOM files (and
+       * exploit the DICOM cache if possible).
+       **/
+
+      DicomSequentialReader* CreateForOriginalParsedDicomFile() const;
+
+      DicomSequentialReader* CreateForTranscodedParsedDicomFile(DicomTransferSyntax targetSyntax,
+                                                                TranscodingSopInstanceUidMode mode,
+                                                                bool hasLossyQuality,
+                                                                unsigned int lossyQuality) const;
+    };
 
     void Submit(const FileInfo& attachment);
 
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Thu May 28 17:06:57 2026 +0200
@@ -619,7 +619,7 @@
     }
 
 
-    instancesLoader_.reset(context_.CreateDicomSequentialReader(true /* we want ParsedDicomFile */));
+    instancesLoader_.reset(context_.GetDicomSequentialReaderFactory().CreateForOriginalParsedDicomFile());
 
     for (size_t i = 0; i < instancesIds_.size(); ++i)
     {
--- a/OrthancServer/Sources/ServerContext.cpp	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Thu May 28 17:06:57 2026 +0200
@@ -622,7 +622,11 @@
         pool->SetDequeueTimeout(100);
         pool->Start();
 
-        sequentialReaderThreadPool_.reset(pool.release());
+        boost::shared_ptr<IExecutorService> executor(pool.release());
+        dicomSequentialReaderFactory_.reset(new DicomSequentialReader::Factory(
+                                              executor, storageAreaReader_, dicomReader_, transcoderReader_,
+                                              4 /* TODO-Streaming: Parameter */,
+                                              0  /* TODO-Streaming: Parameter */));
       }
     }
     catch (OrthancException&)
@@ -2172,39 +2176,8 @@
   }
 
 
-  static DicomSequentialReader::ExpectedAnswer ConvertToExpectedAnswer(bool asParsedDicomFile)
-  {
-    if (asParsedDicomFile)
-    {
-      return DicomSequentialReader::ExpectedAnswer_ParsedDicomFile;
-    }
-    else
-    {
-      return DicomSequentialReader::ExpectedAnswer_RawMemoryBuffer;
-    }
-  }
-
-
-  DicomSequentialReader* ServerContext::CreateDicomSequentialReader(bool asParsedDicomFile)
+  DicomSequentialReader::Factory& ServerContext::GetDicomSequentialReaderFactory()
   {
-    return DicomSequentialReader::CreateForOriginal(
-      sequentialReaderThreadPool_, ConvertToExpectedAnswer(asParsedDicomFile),
-      asParsedDicomFile ? dicomReader_ : storageAreaReader_,
-      4 /* TODO-Streaming: Parameter */,
-      0  /* TODO-Streaming: Parameter */);
-  }
-
-
-  DicomSequentialReader* ServerContext::CreateTranscodedSequentialReader(bool asParsedDicomFile,
-                                                                         DicomTransferSyntax targetSyntax,
-                                                                         TranscodingSopInstanceUidMode mode,
-                                                                         bool hasLossyQuality,
-                                                                         unsigned int lossyQuality)
-  {
-    return DicomSequentialReader::CreateForTranscoded(
-      sequentialReaderThreadPool_, ConvertToExpectedAnswer(asParsedDicomFile),
-      targetSyntax, mode, hasLossyQuality, lossyQuality, transcoderReader_,
-      4 /* TODO-Streaming: Parameter */,
-      0  /* TODO-Streaming: Parameter */);
+    return *dicomSequentialReaderFactory_;
   }
 }
--- a/OrthancServer/Sources/ServerContext.h	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.h	Thu May 28 17:06:57 2026 +0200
@@ -30,6 +30,7 @@
 #include "ServerJobs/IStorageCommitmentFactory.h"
 
 #include "../../OrthancFramework/Sources/DataSource/DicomDataSource.h"
+#include "../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h"
 #include "../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h"
 #include "../../OrthancFramework/Sources/DicomParsing/DicomModification.h"
 #include "../../OrthancFramework/Sources/JobsEngine/JobsEngine.h"
@@ -41,7 +42,6 @@
 namespace Orthanc
 {
   class DicomElement;
-  class DicomSequentialReader;
   class DicomStoreUserConnection;
   class IExecutorService;
   class OrthancPlugins;
@@ -289,11 +289,11 @@
     boost::posix_time::ptime serverStartTimeUtc_;
 
     // For streaming
-    boost::shared_ptr<DataSourceReader>  storageAreaReader_;
-    boost::shared_ptr<DataSourceReader>  dicomReader_;
-    boost::shared_ptr<ThreadPool>        transcoderThreadPool_;
-    boost::shared_ptr<DataSourceReader>  transcoderReader_;
-    boost::shared_ptr<IExecutorService>  sequentialReaderThreadPool_;
+    boost::shared_ptr<DataSourceReader>                storageAreaReader_;
+    boost::shared_ptr<DataSourceReader>                dicomReader_;
+    boost::shared_ptr<ThreadPool>                      transcoderThreadPool_;
+    boost::shared_ptr<DataSourceReader>                transcoderReader_;
+    boost::shared_ptr<DicomSequentialReader::Factory>  dicomSequentialReaderFactory_;
 
   public:
     ServerContext(IDatabaseWrapper& database,
@@ -593,18 +593,6 @@
 
     Future* SubmitTranscodingRequest(TranscodingCallable* callable);
 
-    /**
-     * If "asParsedDicomFile" is "false", the reader will prioritize
-     * the downloading of the raw DICOM files (and avoid parsing if
-     * possible). If it is "true", the reader will prioritize the
-     * parsing of the DICOM files.
-     **/
-    DicomSequentialReader* CreateDicomSequentialReader(bool asParsedDicomFile);
-
-    DicomSequentialReader* CreateTranscodedSequentialReader(bool asParsedDicomFile,
-                                                            DicomTransferSyntax targetSyntax,
-                                                            TranscodingSopInstanceUidMode mode,
-                                                            bool hasLossyQuality,
-                                                            unsigned int lossyQuality);
+    DicomSequentialReader::Factory& GetDicomSequentialReaderFactory();
   };
 }
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Wed May 27 09:03:27 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Thu May 28 17:06:57 2026 +0200
@@ -1203,13 +1203,12 @@
 
       if (transcode_)
       {
-        instancesLoader_.reset(context_.CreateTranscodedSequentialReader(
-                                 false /* prioritize raw DICOM files */, transferSyntax_,
-                                 TranscodingSopInstanceUidMode_AllowNew, hasLossyQuality_, lossyQuality_));
+        instancesLoader_.reset(context_.GetDicomSequentialReaderFactory().CreateForTranscodedRawMemoryBuffer(
+                                 transferSyntax_, TranscodingSopInstanceUidMode_AllowNew, hasLossyQuality_, lossyQuality_));
       }
       else
       {
-        instancesLoader_.reset(context_.CreateDicomSequentialReader(false /* prioritize raw DICOM files */));
+        instancesLoader_.reset(context_.GetDicomSequentialReaderFactory().CreateForOriginalRawMemoryBuffer());
       }
 
       writer_->PreloadAllCommands(*instancesLoader_);