-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathmedia-overlays_smil_model.cpp
1253 lines (1032 loc) · 50 KB
/
media-overlays_smil_model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// media-overlays_smil_model.cpp
// ePub3
//
// Created by Daniel Weck on 2013-09-15.
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
#include "media-overlays_smil_model.h"
#include "package.h"
#include <ePub3/media-overlays_smil_utils.h>
#include <ePub3/media-overlays_smil_data.h>
#include "error_handler.h"
#include "xpath_wrangler.h"
//#include <iostream>
#include <chrono>
//#include "make_unique.h"
//std::unique_ptr<KLASS> obj = make_unique<KLASS>(constructor_params);
EPUB3_BEGIN_NAMESPACE
class Timer
{
private:
typedef std::chrono::high_resolution_clock CLOCK;
typedef std::chrono::duration<double, std::ratio<1>> SECOND;
std::chrono::time_point<CLOCK> BEGIN;
public:
Timer() : BEGIN(CLOCK::now())
{}
void reset()
{
BEGIN = CLOCK::now();
}
double elapsed() const
{
return std::chrono::duration_cast<SECOND>(CLOCK::now() - BEGIN).count();
}
};
// static inline FORCE_INLINE
// void HandleError_(EPUBError __code, const std::string & __msg)
// {
// std::cerr << "HANDLE ERROR" << std::endl;
//
// //throw std::invalid_argument(std::string("parseSMILs TRACE HERE")); //std::runtime_error ///err.what()
//
// HandleError(__code, __msg);
// }
/*
void debugTreeAudio(const SMILData::Audio * audio)
{
printf("-- DEBUG TREE AUDIO\n");
printf("CHECK SMIL DATA TREE MEDIA SRC %s\n", audio->_src_file.c_str());
printf("CHECK SMIL DATA TREE AUDIO: %ld --> %ld\n", (long) audio->_clipBeginMilliseconds, (long) audio->_clipEndMilliseconds);
}
void debugTreeText(const SMILData::Text * text)
{
printf("-- DEBUG TREE TEXT\n");
printf("CHECK SMIL DATA TREE MEDIA SRC FILE %s\n", text->_src_file.c_str());
printf("CHECK SMIL DATA TREE MEDIA SRC FRAGID %s\n", text->_src_fragmentID.c_str());
printf("CHECK SMIL DATA TREE TEXT\n");
}
void debugTreePar(const SMILData::Parallel * par)
{
printf("-- DEBUG TREE PAR\n");
printf("CHECK SMIL DATA TREE TEXTREF FILE %s\n", par->_textref_file.c_str());
printf("CHECK SMIL DATA TREE TEXTREF FRAGID %s\n", par->_textref_fragmentID.c_str());
printf("CHECK SMIL DATA TREE TYPE %s\n", par->_type.c_str());
if (par->_text != nullptr)
{
debugTreeText(par->_text);
}
if (par->_audio != nullptr)
{
debugTreeAudio(par->_audio);
}
}
void debugTreeSeq(const SMILData::Sequence * seqq)
{
printf("-- DEBUG TREE SEQ\n");
printf("CHECK SMIL DATA TREE TEXTREF FILE %s\n", seqq->_textref_file.c_str());
printf("CHECK SMIL DATA TREE TEXTREF FRAGID %s\n", seqq->_textref_fragmentID.c_str());
printf("CHECK SMIL DATA TREE TYPE %s\n", seqq->_type.c_str());
for (int i = 0; i < seqq->_children.size(); i++)
{
SMILData::TimeContainer * container = seqq->_children[i];
const SMILData::Sequence * seq = dynamic_cast<const SMILData::Sequence *>(container);
if (seq != nullptr)
{
debugTreeSeq(seq);
continue;
}
const SMILData::Parallel * par = dynamic_cast<const SMILData::Parallel *>(container);
if (par != nullptr)
{
debugTreePar(par);
continue;
}
throw std::invalid_argument("WTF?");
}
}
void debugSmilData(std::vector<std::shared_ptr<SMILData>> & smilDatas)
{
for (int i = 0; i < smilDatas.size(); i++)
{
const std::shared_ptr<SMILData> smilData = smilDatas.at(i);
printf("}}}}}}} CHECK SMIL DATA TREE %s duration (milliseconds): %ld\n", smilData->ManifestItem()->Identifier().c_str(), (long) smilData->DurationMilliseconds());
debugTreeSeq(smilData->Body());
}
}
*/
MediaOverlaysSmilModel::~MediaOverlaysSmilModel()
{
//printf("~MediaOverlaysSmilModel()\n");
}
MediaOverlaysSmilModel::MediaOverlaysSmilModel(const std::shared_ptr<Package> & package) : OwnedBy(package), _totalDuration(0), _smilDatas(std::vector<std::shared_ptr<SMILData>>())
{
}
void MediaOverlaysSmilModel::Initialize()
{
resetData();
populateData();
}
void MediaOverlaysSmilModel::resetData()
{
_totalDuration = 0;
// std::vector automatically releases the contained smart shared pointers (reference count--)
//_smilDatas.erase(_smilDatas.begin(), _smilDatas.end());
_smilDatas.clear();
}
void MediaOverlaysSmilModel::populateData()
{
parseMetadata();
uint32_t totalDurationFromSMILs = parseSMILs();
if (_totalDuration != totalDurationFromSMILs)
{
std::stringstream s;
s << "Media Overlays total duration mismatch (milliseconds): METADATA " << (long) _totalDuration << " != SMILs " << (long) totalDurationFromSMILs;
const std::string & str = _Str(s.str());
//printf("%s\n", str.c_str());
//_totalDuration = totalDurationFromSMILs;
HandleError(EPUBError::MediaOverlayMismatchDurationMetadata, str);
}
else
{
//printf("Media Overlays SMILs parsed, total duration checked okay (milliseconds): %ld\n", (long) totalDurationFromSMILs);
}
//debugSmilData(_smilDatas);
}
void MediaOverlaysSmilModel::parseMetadata()
{
//const string & _narrator = Narrator();
//printf("Media Overlays NARRATOR: %s\n", _narrator.c_str());
//const string & _activeClass = ActiveClass();
//printf("Media Overlays ACTIVE CLASS: %s\n", _activeClass.c_str());
//const string & _playbackActiveClass = PlaybackActiveClass();
//printf("Media Overlays PLAYBACK ACTIVE CLASS: %s\n", _playbackActiveClass.c_str());
std::shared_ptr<Package> package = Owner(); // internally: std::weak_ptr<Package>.lock()
if (package == nullptr)
{
return;
}
const string & durationStr = package->MediaOverlays_DurationTotal();
//printf("Media Overlays TOTAL DURATION (string): %s\n", durationStr.c_str());
_totalDuration = 0;
if (!durationStr.empty())
{
try
{
_totalDuration = SmilClockValuesParser::ToWholeMilliseconds(durationStr);
//printf("Media Overlays TOTAL DURATION (milliseconds): %ld\n", (long) _totalDuration);
}
catch (const std::invalid_argument & error)
{
HandleError(EPUBError::MediaOverlayInvalidSmilClockValue, _Str("OPF package -- media:duration=", durationStr, " => invalid SMIL Clock Value syntax (", error.what(), ")"));
}
//catch (...)
//{
//}
}
std::shared_ptr<MediaOverlaysSmilModel> sharedMe = std::enable_shared_from_this<MediaOverlaysSmilModel>::shared_from_this();
//std::weak_ptr<MediaOverlaysSmilModel> weakSharedMe = sharedMe; // Not needed: smart shared pointer passed as reference, then onto OwnedBy()
uint32_t accumulatedDurationMilliseconds = 0;
// const ManifestTable & manifestTable = package->Manifest();
// for (ManifestTable::const_iterator iter = manifestTable.begin(); iter != manifestTable.end(); iter++)
// std::shared_ptr<ManifestItem> item = iter->second;
bool allFake = true;
std::shared_ptr<SpineItem> spineItem = package->FirstSpineItem();
while (spineItem != nullptr)
{
ManifestItemPtr item = spineItem->ManifestItem();
//string
//const ManifestItem::MimeType & mediaType = item->MediaType();
//if (mediaType != "application/smil+xml")
//const string & mediaOverlayID = item->MediaOverlayID();
//printf("--- Media Overlays ID: %s\n", mediaOverlayID.c_str());
item = item->MediaOverlay();
if (item == nullptr)
{
const std::shared_ptr<SMILData> smilData = std::make_shared<class SMILData>(sharedMe, nullptr, spineItem, 0);
_smilDatas.push_back(smilData); // creates a *copy* of the shared smart pointer (reference count++)
spineItem = spineItem->Next();
continue;
}
//printf("Media Overlays SMIL HREF: %s\n", item->Href().c_str());
const string & itemDurationStr = package->MediaOverlays_DurationItem(item);
//printf("Media Overlays SMIL DURATION (string): %s\n", itemDurationStr.c_str());
if (itemDurationStr.empty())
{
allFake = false;
const std::shared_ptr<SMILData> smilData = std::make_shared<class SMILData>(sharedMe, item, spineItem, 0);
_smilDatas.push_back(smilData); // creates a *copy* of the shared smart pointer (reference count++)
HandleError(EPUBError::MediaOverlayMissingDurationMetadata, _Str(item->Href(), " => missing media:duration metadata"));
}
else
{
uint32_t durationWholeMilliseconds = 0;
try
{
durationWholeMilliseconds = SmilClockValuesParser::ToWholeMilliseconds(itemDurationStr);
//printf("Media Overlays SMIL DURATION (milliseconds): %ld\n", (long) durationWholeMilliseconds);
allFake = false;
const std::shared_ptr<SMILData> smilData = std::make_shared<class SMILData>(sharedMe, item, spineItem, durationWholeMilliseconds);
_smilDatas.push_back(smilData); // creates a *copy* of the shared smart pointer (reference count++)
accumulatedDurationMilliseconds += durationWholeMilliseconds;
}
catch (const std::invalid_argument & error)
{
allFake = false;
const std::shared_ptr<SMILData> smilData = std::make_shared<class SMILData>(sharedMe, item, spineItem, 0);
_smilDatas.push_back(smilData); // creates a *copy* of the shared smart pointer (reference count++)
HandleError(EPUBError::MediaOverlayInvalidSmilClockValue, _Str(item->Href(), " -- media:duration=", itemDurationStr, " => invalid SMIL Clock Value syntax (", error.what(), ")"));
}
//catch (...)
//{
//}
}
spineItem = spineItem->Next();
}
if (accumulatedDurationMilliseconds != _totalDuration)
{
if (_totalDuration == 0)
{
HandleError(EPUBError::MediaOverlayMissingDurationMetadata, _Str("OPF package", " => missing media:duration metadata"));
}
else
{
std::stringstream s;
s << "Media Overlays metadata duration mismatch (milliseconds): TOTAL " << (long) _totalDuration << " != ACCUMULATED " << (long) accumulatedDurationMilliseconds;
const std::string & str = _Str(s.str());
//printf("%s\n", str.c_str());
HandleError(EPUBError::MediaOverlayMismatchDurationMetadata, str);
}
}
else
{
//printf("Media Overlays SMIL DURATION check okay.\n");
}
if (allFake)
{
// std::vector automatically releases the contained smart shared pointers (reference count--)
//_smilDatas.erase(_smilDatas.begin(), _smilDatas.end());
_smilDatas.clear();
}
else
{
ForEachSmilData([](const std::shared_ptr<SMILData> & data)
{
if (data->SmilManifestItem() != nullptr)
{
return;
}
printf("SMIL placeholder for 'blank' MO page: %s\n", data->XhtmlSpineItem()->ManifestItem()->Href().c_str());
data->_root = std::make_shared<SMILData::Sequence>(nullptr, "", "", nullptr, "", data);
shared_ptr<SMILData::Sequence> sequence = std::const_pointer_cast<SMILData::Sequence>(data->Body());
shared_ptr<SMILData::Parallel> par = std::make_shared<SMILData::Parallel>(sequence, "", data);
sequence->_children.push_back(par);
par->_text = std::make_shared<SMILData::Text>(par, data->XhtmlSpineItem()->ManifestItem()->Href(), "", nullptr, data);
});
}
}
uint32_t MediaOverlaysSmilModel::parseSMILs()
{
std::shared_ptr<Package> package = Owner(); // internally: std::weak_ptr<Package>.lock()
if (package == nullptr)
{
return 0;
}
//Timer timer;
//uint counter = 0;
std::map<std::shared_ptr<ManifestItem>, string> cache_manifestItemToAbsolutePath;
uint32_t accumulatedDurationMilliseconds = 0;
shared_ptr<SpineItem> spineItem = package->FirstSpineItem();
while (spineItem != nullptr)
{
ManifestItemPtr item = spineItem->ManifestItem();
//string
//const ManifestItem::MimeType & mediaType = item->MediaType();
//if (mediaType != "application/smil+xml")
//const string & mediaOverlayID = item->MediaOverlayID();
//printf("--- Media Overlays ID: %s\n", mediaOverlayID.c_str());
item = item->MediaOverlay();
if (item == nullptr)
{
spineItem = spineItem->Next();
continue;
}
// counter++;
// if (counter > 10)
// {
// break;
// }
//printf("Media Overlays SMIL PARSING: %s\n", item->Href().c_str());
//unique_ptr<ArchiveXmlReader> xmlReader = package->XmlReaderForRelativePath(item->Href());
shared_ptr<xml::Document> doc = item->ReferencedDocument();
if (!bool(doc))
{
HandleError(EPUBError::MediaOverlayCannotParseSMILXML, _Str("Cannot parse XML: ", item->Href().c_str()));
}
#if EPUB_COMPILER_SUPPORTS(CXX_INITIALIZER_LISTS)
XPathWrangler xpath(doc, {{"epub", ePub3NamespaceURI}, {"smil", SMILNamespaceURI}});
#else
XPathWrangler::NamespaceList __ns;
__ns["epub"] = ePub3NamespaceURI;
__ns["smil"] = SMILNamespaceURI;
XPathWrangler xpath(doc, __ns);
#endif
xpath.NameDefaultNamespace("smil");
xml::NodeSet nodes = xpath.Nodes("/smil:smil");
if (nodes.empty())
{
HandleError(EPUBError::MediaOverlayInvalidRootElement, _Str("'smil' root element not found: ", item->Href().c_str()));
}
else if (nodes.size() > 1)
{
HandleError(EPUBError::MediaOverlayInvalidRootElement, _Str("Multiple 'smil' root elements found: ", item->Href().c_str()));
}
if (nodes.size() != 1)
return 0;
shared_ptr<xml::Node> smil = nodes[0];
string version = _getProp(smil, "version", SMILNamespaceURI);
if (version.empty())
{
HandleError(EPUBError::MediaOverlayVersionMissing, _Str("SMIL version not found: ", item->Href().c_str()));
}
else if (version != "3.0")
{
HandleError(EPUBError::MediaOverlayInvalidVersion, _Str("Invalid SMIL version (", version, "): ", item->Href().c_str()));
}
nodes = xpath.Nodes("./smil:head", smil);
if (nodes.empty())
{
// OKAY
}
else if (nodes.size() == 1)
{
//TODO: check head placement
//HandleError(EPUBError::MediaOverlayHeadIncorrectlyPlaced, _Str("'head' element incorrectly placed: ", item->Href().c_str()));
}
else if (nodes.size() > 1)
{
HandleError(EPUBError::MediaOverlayHeadIncorrectlyPlaced, _Str("multiple 'head' elements found: ", item->Href().c_str()));
return 0;
}
nodes = xpath.Nodes("./smil:body", smil);
if (nodes.empty())
{
HandleError(EPUBError::MediaOverlayNoBody, _Str("'body' element not found: ", item->Href().c_str()));
}
else if (nodes.size() > 1)
{
HandleError(EPUBError::MediaOverlayMultipleBodies, _Str("multiple 'body' elements found: ", item->Href().c_str()));
}
if (nodes.size() != 1)
{
return 0;
}
shared_ptr<SMILData> smilData = nullptr;
string id = item->Identifier();
for (shared_vector<SMILData>::size_type i = 0; i < _smilDatas.size(); i++)
{
const std::shared_ptr<SMILData> data = _smilDatas.at(i); // does not make a copy of the smart pointer (NO reference count++)
if (data->SmilManifestItem() == nullptr)
{
continue;
}
if (data->SmilManifestItem()->Identifier() == id)
{
auto seq = data->Body();
if (seq != nullptr)
{
continue;
}
if (data->XhtmlSpineItem()->ManifestItem()->Identifier() != spineItem->ManifestItem()->Identifier())
{
printf("XHTML spine item's manifest item ID mismatch?!\n");
}
smilData = data;
break;
}
}
shared_ptr<xml::Node> body = nodes[0];
std::map<string, std::shared_ptr<ManifestItem>> cache_smilRelativePathToManifestItem;
//// TIMER START
//timer.reset();
uint32_t smilDur = parseSMIL(smilData, nullptr, nullptr, item, body, cache_manifestItemToAbsolutePath, cache_smilRelativePathToManifestItem);
//printf("Media Overlays SMIL DURATION (milliseconds): %ld\n", (long) smilDur);
//// TIMER END
//double seconds = timer.elapsed();
//std::stringstream s;
//s << "TIMER (" << counter << "): " << seconds;
//const std::string & str = _Str(s.str());
//printf("%s\n", str.c_str());
////std::cout << t << std::endl;
uint32_t metaDur = smilData->DurationMilliseconds_Metadata();
if (metaDur != smilDur)
{
std::stringstream s;
s << "Media Overlays SMIL duration mismatch (milliseconds): METADATA " << (long) metaDur << " != SMIL " << (long) smilDur << " (" << item->Href().c_str() << ")";
const std::string & str = _Str(s.str());
//printf("%s\n", str.c_str());
//smilData->_duration = smilDur;
HandleError(EPUBError::MediaOverlayMismatchDurationMetadata, str);
}
accumulatedDurationMilliseconds += smilDur;
spineItem = spineItem->Next();
}
return accumulatedDurationMilliseconds;
}
static std::vector<string> splitFileFragmentId; // = std::vector<string>(2);
void splitIriFileFragmentID(const string & iri, std::vector<string> &splitFileFragmentId)
{
//printf("=========== IRI: %s\n", iri.c_str());
splitFileFragmentId.clear();
const char * str = iri.c_str();
size_t size = strlen(str);
for (size_t j = 0; j < size; j++)
{
char c = str[j];
if (c == '#')
{
splitFileFragmentId.push_back(string(str, j));
j++;
if (size > j)
{
splitFileFragmentId.push_back(string(str + j));
}
else
{
splitFileFragmentId.push_back("");
}
return;
}
}
splitFileFragmentId.push_back(iri);
splitFileFragmentId.push_back("");
//
// // FILE
// string::size_type i = iri.find_first_of('#');
// if (i != string::npos && i > 0)
// {
// splitFileFragmentId.push_back(iri.substr(0, i));
// }
// else
// {
// splitFileFragmentId.push_back(iri); //string(iri)
// }
//
// // FRAGMENT ID
// if (i != string::npos && i < (iri.length() - 1))
// {
// string::size_type n = iri.length() - i - 1;
// splitFileFragmentId.push_back(iri.substr(i + 1, n));
// }
// else
// {
// splitFileFragmentId.push_back("");
// }
//
// //printf("=========== IRI FILE: %s\n", split.at(0).c_str());
// //printf("=========== IRI FRAGID: %s\n", split.at(1).c_str());
//
// // RVO Return Value Optimisation should take care of moving the object reference from the local stack that of the callee's context?
// //return split;
}
std::shared_ptr<ManifestItem> getReferencedManifestItem(const std::shared_ptr<Package> & package, string filepathInSmil, const std::shared_ptr<ManifestItem> & smilItem, std::map<std::shared_ptr<ManifestItem>, string> & cache_manifestItemToAbsolutePath)
{
if (filepathInSmil.empty())
{
return nullptr;
}
std::map<std::shared_ptr<ManifestItem>, string>::iterator iterator = cache_manifestItemToAbsolutePath.find(smilItem);
string smilOPFPath;
if (iterator != cache_manifestItemToAbsolutePath.end()
//cache_manifestItemToAbsolutePath.count(smilItem)
)
{
//smilOPFPath = cache_manifestItemToAbsolutePath[smilItem];
smilOPFPath = iterator->second;
//printf("=========== smilOPFPath FROM CACHE: %s\n", smilOPFPath.c_str());
}
else
{
smilOPFPath = smilItem->AbsolutePath();
cache_manifestItemToAbsolutePath[smilItem] = smilOPFPath;
//printf("=========== smilOPFPath CACHING: %s\n", smilOPFPath.c_str());
}
//printf("=========== smilOPFPath: %s\n", smilOPFPath.c_str());
string::size_type i = smilOPFPath.find_last_of('/');
string smilParentFolder = "/";
if (i != string::npos && i > 0)
{
smilParentFolder = _Str(smilOPFPath.substr(0, i), '/');
}
//printf("=========== smilParentFolder: %s\n", smilParentFolder.c_str());
std::string refOPFPath = _Str(smilParentFolder, filepathInSmil);
//std::string & refOPFPath = _Str("/.././void1/../", smilParentFolder, "./void2/.././", filepathInSmil);
//std::string & refOPFPath = _Str(smilParentFolder, "void/../", filepathInSmil);
//std::string & refOPFPath = _Str("../", smilParentFolder, filepathInSmil);
//printf("=========== refOPFPath BEFORE: %s\n", refOPFPath.c_str());
std::string::size_type j = 0;
while ((j = refOPFPath.find("../")) != string::npos)
{
//printf("=========== REPLACE 1: %d\n", j);
std::string::size_type k = j < 2 ? 0 : refOPFPath.rfind('/', j - 2);
//std::string::size_type k = refOPFPath.find_first_of('/', j);
if (k == string::npos || k == 0)
{
refOPFPath.replace(0, (j + 2) + 1, "");
//printf("=========== REPLACE K1: %d\n", k);
}
else
{
refOPFPath.replace(k + 1, (j + 2) - k, "");
//printf("=========== REPLACE K2: %d\n", k);
}
}
j = 0;
while ((j = refOPFPath.find("./")) != string::npos)
{
refOPFPath.replace(j, 2, "");
//printf("=========== REPLACE 2: %d\n", j);
}
//printf("=========== refOPFPath AFTER: %s\n", refOPFPath.c_str());
// When content does not have a root folder such as "OEBPS" or "EPUB", manifest absolute paths have a '/' prefix!!
// //std::shared_ptr<Package> package = Owner(); // internally: std::weak_ptr<Package>.lock()
// if (package != nullptr)
// {
// string basePath = package->BasePath();
// printf("=========== basePath: %s\n", basePath.c_str());
// }
if (smilOPFPath.at(0) == '/' && refOPFPath.at(0) != '/')
{
//refOPFPath = _Str(smilOPFPath.substr(0, 1), refOPFPath);
refOPFPath.insert(0, "/");
}
const ManifestTable & manifestTable = package->Manifest();
for (ManifestTable::const_iterator iter = manifestTable.begin(); iter != manifestTable.end(); iter++)
{
std::shared_ptr<ManifestItem> manItem = iter->second;
string abs;
iterator = cache_manifestItemToAbsolutePath.find(manItem);
if (iterator != cache_manifestItemToAbsolutePath.end()
//cache_manifestItemToAbsolutePath.count(manItem)
)
{
//abs = cache_manifestItemToAbsolutePath[manItem];
abs = iterator->second;
//printf("=========== abs FROM CACHE: %s\n", abs.c_str());
}
else
{
abs = manItem->AbsolutePath();
cache_manifestItemToAbsolutePath[manItem] = abs;
//printf("=========== abs CACHING: %s\n", abs.c_str());
}
if (abs.compare(refOPFPath) == 0)
{
//printf("------> manifest item path: %s\n", abs.c_str());
return manItem; //unless RVO (Return Value Optimization), creates a copy of the smart pointer (reference count++), and deallocates the local variable (reference count--)
}
}
//printf("------> manifest item path NOT FOUND??!: [%s] %s\n", smilOPFPath.c_str(), filepathInSmil.c_str());
return nullptr;
}
uint32_t MediaOverlaysSmilModel::parseSMIL(SMILDataPtr smilData, shared_ptr<SMILData::Sequence> sequence, shared_ptr<SMILData::Parallel> parallel, const ManifestItemPtr item, shared_ptr<xml::Node> element, std::map<std::shared_ptr<ManifestItem>, string> & cache_manifestItemToAbsolutePath, std::map<string, std::shared_ptr<ManifestItem>> & cache_smilRelativePathToManifestItem)
{
if (!bool(element) || !element->IsElementNode())
{
return 0;
}
uint32_t accumulatedDurationMilliseconds = 0;
string elementName = element->Name();
string textref_ = string(_getProp(element, "textref", ePub3NamespaceURI));
string textref_file;
string textref_fragmentID;
if (!textref_.empty())
{
//printf("=========== TEXTREF: %s\n", textref.c_str());
splitIriFileFragmentID(textref_, splitFileFragmentId);
textref_file = splitFileFragmentId[0];
textref_fragmentID = splitFileFragmentId[1];
//
// printf("=========== TEXTREF FILE: %s\n", textref_file.c_str());
// printf("=========== TEXTREF FRAGID: %s\n", textref_fragmentID.c_str());
}
string src_ = string(_getProp(element, "src", SMILNamespaceURI));
string src_file;
string src_fragmentID;
if (!src_.empty())
{
//printf("=========== SRC: %s\n", src.c_str());
splitIriFileFragmentID(src_, splitFileFragmentId);
src_file = splitFileFragmentId[0];
src_fragmentID = splitFileFragmentId[1];
//
// printf("=========== SRC FILE: %s\n", src_file.c_str());
// printf("=========== SRC FRAGID: %s\n", src_fragmentID.c_str());
}
std::shared_ptr<Package> package = Owner(); // internally: std::weak_ptr<Package>.lock()
if (package == nullptr)
{
return 0;
}
std::map<string, std::shared_ptr<ManifestItem>>::iterator iterator = cache_smilRelativePathToManifestItem.find(src_file);
std::shared_ptr<ManifestItem> srcManifestItem;
if (iterator != cache_smilRelativePathToManifestItem.end()
//cache_smilRelativePathToManifestItem.count(src_file)
)
{
//srcManifestItem = cache_smilRelativePathToManifestItem[src_file];
srcManifestItem = iterator->second;
//printf("=========== srcManifestItem FROM CACHE: %s\n", src_file.c_str());
}
else
{
srcManifestItem = getReferencedManifestItem(package, src_file, item, cache_manifestItemToAbsolutePath);
cache_smilRelativePathToManifestItem[src_file] = srcManifestItem;
//printf("=========== srcManifestItem CACHING: %s\n", src_file.c_str());
}
iterator = cache_smilRelativePathToManifestItem.find(textref_file);
std::shared_ptr<ManifestItem> textrefManifestItem;
if (iterator != cache_smilRelativePathToManifestItem.end()
//cache_smilRelativePathToManifestItem.count(textref_file)
)
{
//textrefManifestItem = cache_smilRelativePathToManifestItem[textref_file];
textrefManifestItem = iterator->second;
//printf("=========== textrefManifestItem FROM CACHE: %s\n", textref_file.c_str());
}
else
{
textrefManifestItem = getReferencedManifestItem(package, textref_file, item, cache_manifestItemToAbsolutePath);
cache_smilRelativePathToManifestItem[textref_file] = textrefManifestItem;
//printf("=========== textrefManifestItem CACHING: %s\n", textref_file.c_str());
}
string type = string(_getProp(element, "type", ePub3NamespaceURI));
if (elementName == "body")
{
if (!textref_file.empty() && textrefManifestItem == nullptr)
{
//printf("Media Overlays TEXT REF error: %s [%s]\n", textref_file.c_str(), item->Href().c_str());
HandleError(EPUBError::MediaOverlayInvalidTextRefSource, _Str(item->Href().c_str(), " [", textref_file.c_str(), "] => text ref manifest cannot be found"));
}
smilData->_root = std::make_shared<SMILData::Sequence>(nullptr, textref_file, textref_fragmentID, textrefManifestItem, type, smilData);
sequence = std::const_pointer_cast<SMILData::Sequence>(smilData->Body());
parallel = nullptr;
}
else if (elementName == "seq")
{
if (!textref_file.empty() && textrefManifestItem == nullptr)
{
//printf("Media Overlays TEXT REF error: %s\n", textref_file.c_str());
HandleError(EPUBError::MediaOverlayInvalidTextRefSource, _Str(item->Href().c_str(), " [", textref_file.c_str(), "] => text ref manifest cannot be found"));
}
if (sequence == nullptr)
{
HandleError(EPUBError::MediaOverlaySMILSequenceSequenceParent, _Str(item->Href().c_str(), " => parent of sequence time container must be sequence"));
}
shared_ptr<SMILData::Sequence> seq = std::make_shared<SMILData::Sequence>(sequence, textref_file, textref_fragmentID, textrefManifestItem, type, smilData);
sequence->_children.push_back(seq);
sequence = seq;
parallel = nullptr;
}
else if (elementName == "par")
{
if (!textref_file.empty() && textrefManifestItem == nullptr)
{
//printf("Media Overlays TEXT REF error: %s\n", textref_file.c_str());
HandleError(EPUBError::MediaOverlayInvalidTextRefSource, _Str(item->Href().c_str(), " [", textref_file.c_str(), "] => text ref manifest cannot be found"));
}
if (sequence == nullptr)
{
HandleError(EPUBError::MediaOverlaySMILParallelSequenceParent, _Str(item->Href().c_str(), " => parent of parallel time container must be sequence"));
}
shared_ptr<SMILData::Parallel> par = std::make_shared<SMILData::Parallel>(sequence, type, smilData);
sequence->_children.push_back(par);
sequence = nullptr;
parallel = par;
}
else if (elementName == "audio")
{
if (parallel == nullptr)
{
HandleError(EPUBError::MediaOverlaySMILAudioParallelParent, _Str(item->Href().c_str(), " => parent of audio must be parallel time container"));
}
if (src_file.empty())
{
HandleError(EPUBError::MediaOverlayInvalidAudio, _Str(item->Href().c_str(), " => audio source is empty"));
}
else if (srcManifestItem == nullptr)
{
HandleError(EPUBError::MediaOverlayInvalidAudioSource, _Str(item->Href().c_str(), " [", src_file.c_str(), "] => audio source manifest cannot be found"));
}
else if (srcManifestItem->MediaType() != "audio/mpeg" && srcManifestItem->MediaType() != "audio/mp4") //package->CoreMediaTypes.find(mediaType) == package->CoreMediaTypes.end()
{
HandleError(EPUBError::MediaOverlayInvalidAudioType, _Str(item->Href().c_str(), " [", src_file.c_str(), " (", srcManifestItem->MediaType().c_str(), ")] => audio source type is invalid"));
}
uint32_t clipBeginMilliseconds = 0;
uint32_t clipEndMilliseconds = 0;
string clipBeginStr = _getProp(element, "clipBegin", SMILNamespaceURI);
//printf("Media Overlays CLIP BEGIN: %s\n", clipBeginStr.c_str());
if (!clipBeginStr.empty())
{
try
{
clipBeginMilliseconds = SmilClockValuesParser::ToWholeMilliseconds(clipBeginStr);
}
catch (const std::invalid_argument & error)
{
HandleError(EPUBError::MediaOverlayInvalidSmilClockValue, _Str(item->Href().c_str(), " -- clipBegin=", clipBeginStr, " => invalid SMIL Clock Value syntax (", error.what(), ")"));
}
//catch (...)
//{
//}
}
string clipEndStr = _getProp(element, "clipEnd", SMILNamespaceURI);
//printf("Media Overlays CLIP END: %s\n", clipEndStr.c_str());
if (!clipEndStr.empty())
{
try
{
clipEndMilliseconds = SmilClockValuesParser::ToWholeMilliseconds(clipEndStr);
}
catch (const std::invalid_argument & error)
{
HandleError(EPUBError::MediaOverlayInvalidSmilClockValue, _Str(item->Href().c_str(), " -- clipEnd=", clipEndStr, " => invalid SMIL Clock Value syntax (", error.what(), ")"));
}
//catch (...)
//{
//}
}
if (clipEndStr.empty() || clipEndMilliseconds <= clipBeginMilliseconds)
{
//TODO: get intrinsic audio file duration
clipEndMilliseconds = 0; // means NULL
}
int32_t clipDuration = clipEndMilliseconds - clipBeginMilliseconds;
if (clipDuration <= 0)
{
//HandleError(EPUBError::MediaOverlayInvalidSmilClockValue, _Str(item->Href().c_str(), " -- clipBegin=", clipBeginStr, ", clipEnd=", clipEndStr, " => invalid time values"));
}
else
{
accumulatedDurationMilliseconds += clipDuration;
}
parallel->_audio = std::make_shared<SMILData::Audio>(parallel, src_file, srcManifestItem, clipBeginMilliseconds, clipEndMilliseconds, smilData);
sequence = nullptr;
parallel = nullptr;
}
else if (elementName == "text")
{
if (parallel == nullptr)
{
HandleError(EPUBError::MediaOverlaySMILTextParallelParent, _Str(item->Href().c_str(), " => parent of text must be parallel time container"));
}
if (src_file.empty())
{
HandleError(EPUBError::MediaOverlayInvalidText, _Str(item->Href().c_str(), " => text source is empty"));
}
else if (srcManifestItem == nullptr)
{
HandleError(EPUBError::MediaOverlayInvalidTextSource, _Str(item->Href().c_str(), " [", src_file.c_str(), "] => text source manifest cannot be found"));
}
else if (src_fragmentID.empty())
{
HandleError(EPUBError::MediaOverlayTextSrcFragmentMissing, _Str(item->Href().c_str(), " [", src_file.c_str(), "] => text source fragment identifier is empty"));
}
std::shared_ptr<ManifestItem> spineManifestItem = smilData->_spineItem->ManifestItem();
if (srcManifestItem != spineManifestItem)
{
printf("Media Overlays TEXT SRC mismatch (SMIL[1] with XHTML[1+]): %s (%s) [%s]\n", srcManifestItem->Href().c_str(), spineManifestItem->Href().c_str(), item->Href().c_str());
_excludeAudioDuration = true;
}
parallel->_text = std::make_shared<SMILData::Text>(parallel, src_file, src_fragmentID, srcManifestItem, smilData);
sequence = nullptr;
parallel = nullptr;
}
else