2 Copyright (c) 2013-2014, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file ST2052_TimedText.cpp
29 \brief AS-DCP library, PCM essence reader and writer implementation
33 #include "AS_02_internal.h"
37 using namespace ASDCP;
39 using Kumu::DefaultLogSink;
41 const char* c_tt_namespace_name = "http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt";
43 //------------------------------------------------------------------------------------------
45 typedef std::map<Kumu::UUID, ASDCP::TimedText::MIMEType_t> ResourceTypeMap_t;
47 class AS_02::TimedText::ST2052_TextParser::h__TextParser
50 ResourceTypeMap_t m_ResourceTypes;
53 ASDCP_NO_COPY_CONSTRUCT(h__TextParser);
56 std::string m_Filename;
58 TimedTextDescriptor m_TDesc;
59 ASDCP::mem_ptr<ASDCP::TimedText::LocalFilenameResolver> m_DefaultResolver;
61 h__TextParser() : m_Root("**ParserRoot**")
63 memset(&m_TDesc.AssetID, 0, UUIDlen);
68 ASDCP::TimedText::IResourceResolver* GetDefaultResolver()
70 if ( m_DefaultResolver.empty() )
72 ASDCP::TimedText::LocalFilenameResolver *resolver = new ASDCP::TimedText::LocalFilenameResolver;
73 resolver->OpenRead(PathDirname(m_Filename));
74 m_DefaultResolver = resolver;
77 return m_DefaultResolver;
80 Result_t OpenRead(const std::string& filename);
81 Result_t OpenRead(const std::string& xml_doc, const std::string& filename);
82 Result_t ReadAncillaryResource(const UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
83 const ASDCP::TimedText::IResourceResolver& Resolver) const;
88 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& filename)
90 Result_t result = ReadFileIntoString(filename, m_XMLDoc);
92 if ( KM_SUCCESS(result) )
94 m_Filename = filename;
103 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& xml_doc, const std::string& filename)
106 m_Filename = filename;
112 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead()
114 if ( ! m_Root.ParseString(m_XMLDoc.c_str()) )
115 return RESULT_FORMAT;
117 m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8
118 m_TDesc.ResourceList.clear();
119 m_TDesc.ContainerDuration = 0;
120 const XMLNamespace* ns = m_Root.Namespace();
124 DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_tt_namespace_name);
125 m_TDesc.NamespaceName = c_tt_namespace_name;
129 m_TDesc.NamespaceName = ns->Name();
136 //------------------------------------------------------------------------------------------
138 AS_02::TimedText::ST2052_TextParser::ST2052_TextParser()
142 AS_02::TimedText::ST2052_TextParser::~ST2052_TextParser()
146 // Opens the stream for reading, parses enough data to provide a complete
147 // set of stream metadata for the MXFWriter below.
149 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& filename) const
151 const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
153 Result_t result = m_Parser->OpenRead(filename);
155 if ( ASDCP_FAILURE(result) )
156 const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
161 // Parses an XML document to provide a complete set of stream metadata for the MXFWriter below.
163 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& xml_doc, const std::string& filename) const
165 const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
167 Result_t result = m_Parser->OpenRead(xml_doc, filename);
169 if ( ASDCP_FAILURE(result) )
170 const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
177 AS_02::TimedText::ST2052_TextParser::FillTimedTextDescriptor(TimedTextDescriptor& TDesc) const
179 if ( m_Parser.empty() )
182 TDesc = m_Parser->m_TDesc;
186 // Reads the complete Timed Text Resource into the given string.
188 AS_02::TimedText::ST2052_TextParser::ReadTimedTextResource(std::string& s) const
190 if ( m_Parser.empty() )
193 s = m_Parser->m_XMLDoc;
199 AS_02::TimedText::ST2052_TextParser::ReadAncillaryResource(const Kumu::UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
200 const ASDCP::TimedText::IResourceResolver* Resolver) const
202 return RESULT_NOTIMPL;
207 // end ST2052_TextParser.cpp