Use a HMAC context to write HMAC stuff to encrypted MXFs.
[libdcp.git] / src / mono_picture_asset_writer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/mono_picture_asset_writer.cc
21  *  @brief MonoPictureAssetWriter class
22  */
23
24 #include "AS_DCP.h"
25 #include "KM_fileio.h"
26 #include "mono_picture_asset_writer.h"
27 #include "exceptions.h"
28 #include "picture_asset.h"
29 #include "dcp_assert.h"
30
31 #include "picture_asset_writer_common.cc"
32
33 using std::istream;
34 using std::ostream;
35 using std::string;
36 using boost::shared_ptr;
37 using namespace dcp;
38
39 struct MonoPictureAssetWriter::ASDCPState : public ASDCPStateBase
40 {
41         ASDCP::JP2K::MXFWriter mxf_writer;
42 };
43
44 /** @param a Asset to write to.  `a' must not be deleted while
45  *  this writer class still exists, or bad things will happen.
46  */
47 MonoPictureAssetWriter::MonoPictureAssetWriter (PictureAsset* asset, boost::filesystem::path file, Standard standard, bool overwrite)
48         : PictureAssetWriter (asset, file, standard, overwrite)
49         , _state (new MonoPictureAssetWriter::ASDCPState)
50 {
51
52 }
53
54 void
55 MonoPictureAssetWriter::start (uint8_t* data, int size)
56 {
57         dcp::start (this, _state, _standard, _picture_asset, data, size);
58         _picture_asset->set_frame_rate (_picture_asset->edit_rate());
59 }
60
61 FrameInfo
62 MonoPictureAssetWriter::write (uint8_t* data, int size)
63 {
64         DCP_ASSERT (!_finalized);
65
66         if (!_started) {
67                 start (data, size);
68         }
69
70         if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
71                 boost::throw_exception (MiscError ("could not parse J2K frame"));
72         }
73
74         uint64_t const before_offset = _state->mxf_writer.Tell ();
75
76         string hash;
77         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, _hmac_context, &hash);
78         if (ASDCP_FAILURE (r)) {
79                 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
80         }
81
82         ++_frames_written;
83         return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
84 }
85
86 void
87 MonoPictureAssetWriter::fake_write (int size)
88 {
89         DCP_ASSERT (_started);
90         DCP_ASSERT (!_finalized);
91
92         Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size);
93         if (ASDCP_FAILURE (r)) {
94                 boost::throw_exception (MXFFileError ("error in writing video MXF", _file.string(), r));
95         }
96
97         ++_frames_written;
98 }
99
100 bool
101 MonoPictureAssetWriter::finalize ()
102 {
103         if (_started) {
104                 Kumu::Result_t r = _state->mxf_writer.Finalize();
105                 if (ASDCP_FAILURE (r)) {
106                         boost::throw_exception (MXFFileError ("error in finalizing video MXF", _file.string(), r));
107                 }
108         }
109
110         _picture_asset->_intrinsic_duration = _frames_written;
111         return PictureAssetWriter::finalize ();
112 }