2 Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 /** @file src/atmos_asset_writer.cc
36 * @brief AtmosAssetWriter class
40 #include "atmos_asset.h"
41 #include "atmos_asset_writer.h"
42 #include "compose.hpp"
43 #include "crypto_context.h"
44 #include "dcp_assert.h"
45 #include "exceptions.h"
46 #include "filesystem.h"
47 #include <asdcp/AS_DCP.h>
52 using std::shared_ptr;
56 struct AtmosAssetWriter::ASDCPState
58 ASDCP::ATMOS::MXFWriter mxf_writer;
59 ASDCP::DCData::FrameBuffer frame_buffer;
60 ASDCP::WriterInfo writer_info;
61 ASDCP::ATMOS::AtmosDescriptor desc;
65 AtmosAssetWriter::AtmosAssetWriter (AtmosAsset* asset, boost::filesystem::path file)
66 : AssetWriter (asset, file)
67 , _state (new AtmosAssetWriter::ASDCPState)
70 _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
71 _state->desc.FirstFrame = _asset->first_frame ();
72 _state->desc.MaxChannelCount = _asset->max_channel_count ();
73 _state->desc.MaxObjectCount = _asset->max_object_count ();
76 Kumu::hex2bin (_asset->atmos_id().c_str(), _state->desc.AtmosID, ASDCP::UUIDlen, &c);
77 DCP_ASSERT (c == ASDCP::UUIDlen);
79 _state->desc.AtmosVersion = _asset->atmos_version ();
81 _asset->fill_writer_info (&_state->writer_info, _asset->id());
86 AtmosAssetWriter::write (shared_ptr<const AtmosFrame> frame)
88 write (frame->data(), frame->size());
93 AtmosAssetWriter::write (uint8_t const * data, int size)
95 DCP_ASSERT (!_finalized);
98 auto r = _state->mxf_writer.OpenWrite(dcp::filesystem::fix_long_path(_file).string().c_str(), _state->writer_info, _state->desc);
99 if (ASDCP_FAILURE(r)) {
100 boost::throw_exception (FileError ("could not open atmos MXF for writing", _file.string(), r));
103 _asset->set_file (_file);
107 _state->frame_buffer.Capacity (size);
108 _state->frame_buffer.Size (size);
109 memcpy (_state->frame_buffer.Data(), data, size);
111 auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
112 if (ASDCP_FAILURE(r)) {
113 boost::throw_exception (MiscError(String::compose("could not write atmos MXF frame (%1)", static_cast<int>(r))));
121 AtmosAssetWriter::finalize ()
123 if (_started && ASDCP_FAILURE(_state->mxf_writer.Finalize())) {
124 boost::throw_exception (MiscError("could not finalise atmos MXF"));
127 _asset->_intrinsic_duration = _frames_written;
128 return AssetWriter::finalize ();