Small bits of pre-release tidying.
[libdcp.git] / src / atmos_asset_writer.cc
1 /*
2     Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34
35 #include "atmos_asset_writer.h"
36 #include "atmos_asset.h"
37 #include "exceptions.h"
38 #include "dcp_assert.h"
39 #include "compose.hpp"
40 #include "crypto_context.h"
41 #include <asdcp/AS_DCP.h>
42
43
44 using std::min;
45 using std::max;
46 using std::shared_ptr;
47 using namespace dcp;
48
49
50 struct AtmosAssetWriter::ASDCPState
51 {
52         ASDCP::ATMOS::MXFWriter mxf_writer;
53         ASDCP::DCData::FrameBuffer frame_buffer;
54         ASDCP::WriterInfo writer_info;
55         ASDCP::ATMOS::AtmosDescriptor desc;
56 };
57
58
59 AtmosAssetWriter::AtmosAssetWriter (AtmosAsset* asset, boost::filesystem::path file)
60         : AssetWriter (asset, file)
61         , _state (new AtmosAssetWriter::ASDCPState)
62         , _asset (asset)
63 {
64         _state->desc.EditRate = ASDCP::Rational (_asset->edit_rate().numerator, _asset->edit_rate().denominator);
65         _state->desc.FirstFrame = _asset->first_frame ();
66         _state->desc.MaxChannelCount = _asset->max_channel_count ();
67         _state->desc.MaxObjectCount = _asset->max_object_count ();
68
69         unsigned int c;
70         Kumu::hex2bin (_asset->atmos_id().c_str(), _state->desc.AtmosID, ASDCP::UUIDlen, &c);
71         DCP_ASSERT (c == ASDCP::UUIDlen);
72
73         _state->desc.AtmosVersion = _asset->atmos_version ();
74
75         _asset->fill_writer_info (&_state->writer_info, _asset->id());
76 }
77
78
79 void
80 AtmosAssetWriter::write (shared_ptr<const AtmosFrame> frame)
81 {
82         write (frame->data(), frame->size());
83 }
84
85
86 void
87 AtmosAssetWriter::write (uint8_t const * data, int size)
88 {
89         DCP_ASSERT (!_finalized);
90
91         if (!_started) {
92                 auto r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->desc);
93                 if (ASDCP_FAILURE(r)) {
94                         boost::throw_exception (FileError ("could not open atmos MXF for writing", _file.string(), r));
95                 }
96
97                 _asset->set_file (_file);
98                 _started = true;
99         }
100
101         _state->frame_buffer.Capacity (size);
102         _state->frame_buffer.Size (size);
103         memcpy (_state->frame_buffer.Data(), data, size);
104
105         auto const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _crypto_context->context(), _crypto_context->hmac());
106         if (ASDCP_FAILURE(r)) {
107                 boost::throw_exception (MiscError(String::compose("could not write atmos MXF frame (%1)", static_cast<int>(r))));
108         }
109
110         ++_frames_written;
111 }
112
113 bool
114 AtmosAssetWriter::finalize ()
115 {
116         if (_started && ASDCP_FAILURE(_state->mxf_writer.Finalize())) {
117                 boost::throw_exception (MiscError("could not finalise atmos MXF"));
118         }
119
120         _asset->_intrinsic_duration = _frames_written;
121         return AssetWriter::finalize ();
122 }