Move _started from PictureAssetWriter into AssetWriter
authorCarl Hetherington <cth@carlh.net>
Tue, 15 Sep 2015 21:14:04 +0000 (22:14 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 15 Sep 2015 21:15:13 +0000 (22:15 +0100)
and don't start the SoundAssetWriter until the first
thing is written to it.

src/asset_writer.cc
src/asset_writer.h
src/picture_asset_writer.cc
src/picture_asset_writer.h
src/sound_asset_writer.cc

index 4611b38a4c10cccf7dc11e65097c3e5c1007747a..db2d4c53da073295625858a1a4a4c50c868ae13f 100644 (file)
@@ -38,6 +38,7 @@ AssetWriter::AssetWriter (MXF* mxf, boost::filesystem::path file)
        , _file (file)
        , _frames_written (0)
        , _finalized (false)
+       , _started (false)
        , _encryption_context (0)
 {
        if (mxf->key ()) {
index 31d2f7ae0aeb67ce05738ebbad96f63ed58885ff..66d7396f4faaf3bacbec6970de8c314f0afe98b9 100644 (file)
@@ -59,6 +59,8 @@ protected:
        int64_t _frames_written;
        /** true if finalize() has been called on this object */
        bool _finalized;
+       /** true if something has been written to this asset */
+       bool _started;
        ASDCP::AESEncContext* _encryption_context;
 };
 
index b47aaabd2b66f10fd5840fae511da0f97b5042db..3bb6a59782f617a717f6fd9206a022370cbb9317 100644 (file)
@@ -34,7 +34,6 @@ using namespace dcp;
 PictureAssetWriter::PictureAssetWriter (PictureAsset* asset, boost::filesystem::path file, Standard standard, bool overwrite)
        : AssetWriter (asset, file)
        , _picture_asset (asset)
-       , _started (false)
        , _standard (standard)
        , _overwrite (overwrite)
 {
index e263d479f1bb67435f59afefeae06655d4deb668..a815fbcda405bd3696ae753dc45f6cc5d883e234 100644 (file)
@@ -74,7 +74,6 @@ protected:
        PictureAssetWriter (PictureAsset *, boost::filesystem::path, Standard standard, bool);
 
        PictureAsset* _picture_asset;
-       bool _started;
        Standard _standard;
        bool _overwrite;
 };
index 4a27639e31738fe8eaa89e1e34c45835b6ff6119..f24605bbf48217956b2b99519f40465474fba7ad 100644 (file)
@@ -56,13 +56,6 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path f
        memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
 
        _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard);
-
-       Kumu::Result_t r = _state->mxf_writer.OpenWrite (file.string().c_str(), _state->writer_info, _state->audio_desc);
-       if (ASDCP_FAILURE (r)) {
-               boost::throw_exception (FileError ("could not open audio MXF for writing", file.string(), r));
-       }
-
-       _sound_asset->set_file (file);
 }
 
 void
@@ -70,6 +63,16 @@ SoundAssetWriter::write (float const * const * data, int frames)
 {
        DCP_ASSERT (!_finalized);
 
+       if (!_started) {
+               Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
+               if (ASDCP_FAILURE (r)) {
+                       boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
+               }
+
+               _sound_asset->set_file (_file);
+               _started = true;
+       }
+
        for (int i = 0; i < frames; ++i) {
 
                byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
@@ -112,7 +115,7 @@ SoundAssetWriter::finalize ()
                write_current_frame ();
        }
 
-       if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
+       if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
                boost::throw_exception (MiscError ("could not finalise audio MXF"));
        }