dcpparser.cpp 50.7 KB
Newer Older
1 2 3 4 5 6
/*****************************************************************************
 * Copyright (C) 2013 VLC authors and VideoLAN
 *
 * Authors:
 *          Nicolas Bertrand <nico@isf.cc>
 *          Jean-Baptiste Kempf <jb@videolan.org>
Valentin Vetter's avatar
Valentin Vetter committed
7 8 9 10 11
 *          Guillaume Gonnaud
 *          Valentin Vetter <vvetter@outlook.com>
 *          Anthony Giniers
 *          Ludovic Hoareau
 *          Loukmane Dessai
12
 *          Simona-Marinela Prodea <simona dot marinela dot prodea at gmail dot com>
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
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/**
 * @file dcpparser.cpp
 * @brief Parsing of DCP XML files
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* VLC core API headers */
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_xml.h>
#include <vlc_url.h>

#include <iostream>
#include <string>
#include <list>
#include <vector>

#include "dcpparser.h"

using namespace std;

typedef enum {
    CHUNK_UNKNOWN = 0,
    CHUNK_PATH,
    CHUNK_VOL_INDEX,
    CHUNK_OFFSET,
    CHUNK_LENGTH
} ChunkTag_t;


typedef enum {
    ASSET_UNKNOWN = 0,
    ASSET_ID,
    ASSET_ANNOTATION_TEXT,
    ASSET_PACKING_LIST,
    ASSET_CHUNK_LIST,
    ASSET_HASH,
    ASSET_SIZE,
    ASSET_TYPE,
    ASSET_ORIGINAL_FILENAME
} AssetTag_t;

static const string g_asset_names[] = {
    "Id",
    "AnnotationText",
    "PackingList",
    "ChunkList",
    "Hash",
    "Size",
    "Type",
    "OriginalFileName"
};


typedef enum {
    PKL_UNKNOWN = 0,
    PKL_ID,
    PKL_ISSUE_DATE,
    PKL_ISSUER,
    PKL_CREATOR,
    PKL_ASSET_LIST,
    PKL_ANNOTATION_TEXT, /* start of optional tags */
    PKL_ICON_ID,
    PKL_GROUP_ID,
    PKL_SIGNER,
    PKL_SIGNATURE,
} PKLTag_t;

typedef enum {
    CPL_UNKNOWN = 0,
    CPL_ID,
    CPL_ANNOTATION_TEXT,        /* optional */
    CPL_ICON_ID,                /* optional */
    CPL_ISSUE_DATE,
    CPL_ISSUER,                 /* optional */
    CPL_CREATOR,                /* optional */
    CPL_CONTENT_TITLE,
    CPL_CONTENT_KIND,
    CPL_CONTENT_VERSION,        /* not optional, but not always present*/
    CPL_RATING_LIST,            /* not handled */
    CPL_REEL_LIST,
    CPL_SIGNER,                 /* optional - not handled */
    CPL_SIGNATURE               /* optional - not handled */
} CPLTag_t;


class ChunkList: public std::list<Chunk> {
public :
    ChunkList();
    ~ChunkList();
};

/*
 * Chunk Class
 */
int Chunk::Parse( xml_reader_t *p_xmlReader, string p_node, int p_type){
    string node;
    int type;
    string s_value;
    static const string names[] = {"Path", "VolumeIndex", "Offset",
                               "Length"};
    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "Chunk")
        return -1;
    /* loop on Chunks Node */
137
    while( ( type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node ) ) > 0 ) {
138 139 140 141 142 143 144
        switch (type) {
            case XML_READER_STARTELEM:
            {
                ChunkTag_t chunk_tag = CHUNK_UNKNOWN;
                for(ChunkTag_t i = CHUNK_PATH; i <= CHUNK_LENGTH; i = ChunkTag_t(i+1)) {
                    if( node == names[i-1]) {
                        chunk_tag = i;
145
                        if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
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
                            return -1;
                        switch (chunk_tag) {
                            case CHUNK_PATH:
                                this->s_path = s_value;
                                break;
                            case CHUNK_VOL_INDEX:
                                this->i_vol_index = atoi(s_value.c_str());
                                break;
                            case CHUNK_OFFSET:
                                this->i_offset = atoi(s_value.c_str());
                                break;
                            case CHUNK_LENGTH:
                                this->i_length = atoi(s_value.c_str());
                                break;
                            case CHUNK_UNKNOWN:
                            default:
                                break;
                        }
                        /* break the for loop as a tag is found*/
                        break;
                    }
                }
                if(chunk_tag == CHUNK_UNKNOWN)
                    return -1;
                break;
            }
            case XML_READER_TEXT:
                s_value = node;
                if (unlikely(node.empty()))
                    return -1;
                break;
            case XML_READER_ENDELEM:
                /* Verify if we reach Chuk endelem  */
                if ( node == p_node) {
                    /* verify path */
                    if ( this->s_path.empty() ) {
                        msg_Err(this->p_demux, "Chunk::Parse No path found");
                        return -1;
                    }
                    if ( this->i_vol_index != 1 ) {
186
                        msg_Err(this->p_demux, "Only one VOLINDEX supported. Patch welcome.");
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                        return -1;
                    }
                    /* end of chunk tag parse */
                    return 0;
                }
                break;
        }
    }
    return -1;
}
/*
 * AssetMap Class
 */

AssetMap::~AssetMap() { }

int AssetMap::Parse ( )
{
    int type = 0;
206
    int retval;
Valentin Vetter's avatar
Valentin Vetter committed
207 208 209 210
    int reel_nbr = 0;
    int index = 0;
    int sum_duration_vid = 0;
    int sum_duration_aud = 0;
211
    string node;
212
    char *psz_kdm_path;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

    CPL  *cpl;
    Reel *reel;
    PKL  *pkl;
    AssetList *_p_asset_list = NULL;

    vector<string> pklfiles;

    /* init XML parser */
    if( this->OpenXml() ) {
        msg_Err( p_demux, "Failed to initialize Assetmap XML parser" );
        return -1;
    }

    /* reading ASSETMAP file to get the asset_list */
    msg_Dbg( p_demux, "reading ASSETMAP file..." );
229 230 231 232 233 234
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) ) {
        if( type == -1 )
        {
            this->CloseXml();
            return -1;
        }
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
        if ( (type == XML_READER_STARTELEM) && ( node =="AssetList")) {
            _p_asset_list =  new (nothrow) AssetList();
            if ( unlikely(_p_asset_list == NULL) ) {
                this->CloseXml();
                return -1;
            }
            p_dcp->p_asset_list = _p_asset_list;
            if (this->ParseAssetList(p_xmlReader, node, type )) {
                this->CloseXml();
                return -1;
            }
            /* asset list found so break*/
            break;
        }
    }

    /* Look for PKLs path */
    if ( (_p_asset_list == NULL) ||  (_p_asset_list->size() == 0) ) {
        msg_Err( p_demux, "Asset list empty" );
        this->CloseXml();
        return -1;
    }

    for (AssetList::iterator iter = _p_asset_list->begin();
            iter != _p_asset_list->end() ; ++iter) {
        string s_filepath;
        s_filepath = (*iter)->getPath();
        if (s_filepath.empty()) {
            msg_Err( p_demux, "No path element for asset" );
            continue;
        }
        /* set an absolute file path */
        s_filepath = p_dcp->path + s_filepath;

        /* case of packing list */
        if ((*iter)->isPackingList()) {
            pklfiles.push_back( s_filepath );
        }
    }

    /* TODO: case of only on PKL managed.
     * Future work needed for managing severals
     * the PKL vector will be used to select the required PKL  */
    if( (pklfiles.size() == 0)  || (pklfiles[0].empty()) )
    {
        msg_Err( p_demux, "Could not find PKL file in ASSETMAP" );
        this->CloseXml();
        return -1;
    }

    /* Create the first PKL */
    pkl = new (nothrow) PKL(p_demux, pklfiles[0], _p_asset_list, p_dcp->path);
    if ( unlikely(pkl == NULL) ) {
        this->CloseXml();
        return -1;
        }
    if (pkl->Parse()) {
        delete pkl;
        this->CloseXml();
        return -1;
        }
    p_dcp->pkls.push_back( pkl );

    /* Now, CPL */
    if ( pkl->FindCPLs() <= 0 ) {
        msg_Err(p_demux, " No CPL found");
        this->CloseXml();
        return -1;
    }
    /* TODO: Only one CPL managed.
     * Future work needed for managing severals
     */

    cpl = pkl->getCPL(0);
    if( cpl == NULL ) {
        msg_Err(p_demux, " No CPL found");
        this->CloseXml();
        return -1;
    }
    if ( cpl->Parse() ) {
        this->CloseXml();
        return -1;
    }

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    /* KDM, if needed */
    for( AssetList::iterator iter = _p_asset_list->begin(); iter != _p_asset_list->end(); ++iter )
        if( ! (*iter)->getKeyId().empty() )
        {
            msg_Dbg( p_demux, "DCP is encrypted, searching KDM file...");
            psz_kdm_path = var_InheritString( p_demux, "kdm" );
            if( !psz_kdm_path || !*psz_kdm_path )
            {
                msg_Err( p_demux, "cryptographic key IDs found in CPL and no path to KDM given");
                free( psz_kdm_path );
                this->CloseXml();
                return VLC_EGENERIC;
            }
            KDM p_kdm( p_demux, psz_kdm_path, p_dcp );
            free( psz_kdm_path );
            if( ( retval = p_kdm.Parse() ) )
            {
                this->CloseXml();
                return retval;
            }
            break;
        }

Valentin Vetter's avatar
Valentin Vetter committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    reel_nbr = cpl->getReelList().size();
    for(index = 0; index != reel_nbr; ++index)
    {
        reel = cpl->getReel(index);

        Asset *asset;
        struct info_reel video;
        struct info_reel audio;

        /* Get picture */
        asset = reel->getTrack(TRACK_PICTURE);
        if (asset != NULL)
        {
            sum_duration_vid += asset->getDuration();
            video.filename = p_dcp->path + asset->getPath();
            video.i_entrypoint = asset->getEntryPoint();
            video.i_duration = asset->getDuration();
            video.i_correction = video.i_entrypoint - sum_duration_vid + video.i_duration;
            video.i_absolute_end = sum_duration_vid;
361
            video.p_key = asset->getAESKeyById( p_dcp->p_key_list, asset->getKeyId() );
Valentin Vetter's avatar
Valentin Vetter committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
            p_dcp->video_reels.push_back(video);
            msg_Dbg( this->p_demux, "Video Track: %s",asset->getPath().c_str());
            msg_Dbg( this->p_demux, "Entry point: %i",asset->getEntryPoint());
        }
        /* Get audio */
        asset = reel->getTrack(TRACK_SOUND);
        if (asset != NULL)
        {
            /*if (!p_dcp->audio_reels.empty())
            {
                sum_duration_aud = 0;
                for (int i = 0; i != p_dcp->audio_reels.size(); ++i)
                {
                    sum_duration_aud += p_dcp->audio_reels(i).i_duration;
                }
            }*/
            sum_duration_aud += asset->getDuration();
            audio.filename = p_dcp->path + asset->getPath();
            audio.i_entrypoint = asset->getEntryPoint();
            audio.i_duration = asset->getDuration();
            audio.i_correction = audio.i_entrypoint - sum_duration_aud + audio.i_duration;
            audio.i_absolute_end = sum_duration_aud;
384
            audio.p_key = asset->getAESKeyById( p_dcp->p_key_list, asset->getKeyId() );
Valentin Vetter's avatar
Valentin Vetter committed
385 386 387 388 389
            p_dcp->audio_reels.push_back(audio);
            msg_Dbg( this->p_demux, "Audio Track: %s",asset->getPath().c_str());
            msg_Dbg( this->p_demux, "Entry point: %i",asset->getEntryPoint());
        }
    }
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    /* free memory */
    this->CloseXml();
    return VLC_SUCCESS;
}



/*
 * Asset Class
 */
Asset::~Asset() {}

int Asset::Parse( xml_reader_t *p_xmlReader, string p_node, int p_type)
{
    string node;
    int type;
    string s_value;
    const string s_root_node = "Asset";

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != s_root_node)
        return -1;
    /* loop on Assets Node */
414
    while( ( type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node ) ) > 0 ) {
415 416 417 418 419 420 421 422 423 424
        switch (type) {
            case XML_READER_STARTELEM:
                {
                    AssetTag_t _tag = ASSET_UNKNOWN;
                    for(AssetTag_t i = ASSET_ID; i <= ASSET_ORIGINAL_FILENAME; i = AssetTag_t(i+1)) {
                        if( node == g_asset_names[i-1]) {
                            _tag = i;
                            switch(_tag) {
                                /* Case of complex nodes */
                                case ASSET_PACKING_LIST:
425
                                    /* case of <PackingList/> tag, bur not compliant with SMPTE-429-9 2007*/
426
                                    if (xml_ReaderIsEmptyElement( p_xmlReader))
427
                                    {
428
                                        this->b_is_packing_list = true;
429
                                    }
430
                                    else if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
431 432
                                    {
                                        msg_Err(this->p_demux, "Missing end node in %s", node.c_str());
433
                                        return -1;
434
                                    }
435 436 437 438 439
                                    if ( s_value == "true" )
                                        this->b_is_packing_list = true;
                                    break;
                                case ASSET_CHUNK_LIST:
                                    if ( this->parseChunkList(p_xmlReader, node, type ) )
440 441
                                    {
                                        msg_Err(this->p_demux, "Error parsing chunk list: %s", node.c_str());
442
                                        return -1;
443
                                    }
444 445 446
                                    this->s_path = this->chunk_vec[0].getPath();
                                    break;
                                case ASSET_ID:
447
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
448 449
                                    {
                                        msg_Err(this->p_demux, "Missing end node in %s", node.c_str());
450
                                        return -1;
451
                                    }
452 453 454
                                    this->s_id = s_value;
                                    break;
                                case ASSET_ANNOTATION_TEXT:
455
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
456 457
                                    {
                                        msg_Err(this->p_demux, "Missing end node in %s", node.c_str());
458
                                        return -1;
459
                                    }
460 461 462 463 464 465 466 467
                                    this->s_annotation = s_value;
                                    break;
                                case ASSET_ORIGINAL_FILENAME:
                                case ASSET_HASH:
                                case ASSET_TYPE:
                                case ASSET_SIZE:
                                    /* Asset tags not in AssetMap */
                                    break;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
468
                                default:
469
                                    msg_Warn(this->p_demux, "Unknown ASSET_TAG: %i", _tag );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
470
                                    break;
471 472 473 474 475 476
                            }
                            /* break the for loop as a tag is found*/
                            break;
                        }
                    }
                    if( _tag == ASSET_UNKNOWN )
477 478
                    {
                        msg_Err(this->p_demux, "Unknown ASSET_TAG: %s", node.c_str());
479
                        return -1;
480
                    }
481 482 483
                    break;
                }
            case XML_READER_TEXT:
484
                msg_Err(this->p_demux, " Text element found in Asset");
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
                return -1;
            case XML_READER_ENDELEM:
                if ( node != s_root_node) {
                    msg_Err(this->p_demux,
                            "Something goes wrong in Asset parsing on Assetmap (node %s)", node.c_str());
                    return -1;
                } else {
                    /*Check Presence of Id and Chunklist */
                    if ( this->s_id.empty() ) {
                        msg_Err(this->p_demux, " No Id element found in Asset");
                        return -1;
                    }
                    if ( this->s_path.empty() ) {
                        msg_Err(this->p_demux, " No path element found in Asset");
                        return -1;
                    }
                    return 0;
                }
                break;
        }
    }
    return -1;
}



int Asset::ParsePKL( xml_reader_t *p_xmlReader)
{
    string node;
    int type;
    string s_value;
    const string s_root_node = "Asset";

518
    while( ( type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node ) ) > 0 ) {
519 520 521 522 523 524 525 526 527
        switch (type) {
            case XML_READER_STARTELEM:
                {
                    AssetTag_t _tag = ASSET_UNKNOWN;
                    for(AssetTag_t i = ASSET_ID; i <= ASSET_ORIGINAL_FILENAME; i = AssetTag_t(i+1)) {
                        if( node == g_asset_names[i-1]) {
                            _tag = i;
                            switch(_tag) {
                                case ASSET_ANNOTATION_TEXT:
528
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
529 530 531 532 533 534 535
                                        return -1;
                                    if ( this->s_annotation.empty() )
                                        this->s_annotation = s_value;
                                    else
                                        this->s_annotation = this->s_annotation + "--" + s_value;
                                    break;
                                case ASSET_HASH:
536
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
537 538 539 540
                                        return -1;
                                    this->s_hash = s_value;
                                    break;
                                case ASSET_SIZE:
541
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
542 543 544 545
                                        return -1;
                                    this->ui_size = atol(s_value.c_str());
                                    break;
                                case ASSET_TYPE:
546
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
547 548 549 550
                                        return -1;
                                    this->s_type = s_value;
                                    break;
                                case ASSET_ORIGINAL_FILENAME:
551
                                    if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
552 553 554 555 556 557 558 559
                                        return -1;
                                    this->s_original_filename = s_value;
                                    break;
                                case ASSET_ID: /* already verified */
                                case ASSET_PACKING_LIST:
                                case ASSET_CHUNK_LIST:
                                    /* Asset tags not in PKL */
                                    break;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
560 561 562
                                default:
                                    msg_Warn(this->p_demux, "Unknow ASSET_TAG: %i", _tag );
                                    break;
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
                            }
                            /* break the for loop as a tag is found*/
                            break;
                        }
                    }
                    if( _tag == ASSET_UNKNOWN )
                        return -1;
                break;
            }
            case XML_READER_TEXT:
                return -1;
            case XML_READER_ENDELEM:
                if ( node != s_root_node) {
                    msg_Err(this->p_demux,
                            "Something goes wrong in Asset parsing on PKL (node %s)", node.c_str());
                    return -1;
                } else {
                    /* Verify that mandatory attributes are filled */
                    if (this->s_hash.empty()) {
                        msg_Err(this->p_demux,"Asset Hash tag invalid");
                        return -1;
                    }
                    if (this->ui_size == 0) {
                        msg_Err(this->p_demux,"Asset Size tag invalid");
                        return -1;
                    }
                    if (this->s_type.empty()) {
                        msg_Err(this->p_demux,"Asset Type tag invalid");
                        return -1;
                    }
                    return 0;
                }
                break;
        }

    }

    return -1;
}

void Asset::Dump()
{
    msg_Dbg(this->p_demux,"Id              = %s", this->s_id.c_str());
    msg_Dbg(this->p_demux,"Path            = %s", this->s_path.c_str());
    msg_Dbg(this->p_demux,"Is PKL          = %s", this->b_is_packing_list ? "True" : "False");
    msg_Dbg(this->p_demux,"Hash            = %s", this->s_hash.c_str());
    msg_Dbg(this->p_demux,"Size            = %i", this->ui_size);
    msg_Dbg(this->p_demux,"Type            = %s", this->s_type.c_str());
    msg_Dbg(this->p_demux,"OrignalFileName = %s", this->s_original_filename.c_str());
    msg_Dbg(this->p_demux,"AnnotationText  = %s", this->s_annotation.c_str());
}

int Asset::parseChunkList( xml_reader_t *p_xmlReader, string p_node, int p_type)
{
    string node;
    int type;
    string s_value;
    std::vector<Chunk> chunk_vec;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "ChunkList" )
        return -1;
    /* loop on Assets Node */
627
    while( ( type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node ) ) > 0 ) {
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
         switch (type) {
            case XML_READER_STARTELEM:
                {
                    Chunk chunk(this->p_demux);
                    if (node != "Chunk" )
                        return -1;
                    if ( chunk.Parse(p_xmlReader, node, type) )
                        return -1;
                    chunk_vec.push_back(chunk);
                    break;
                }
            case XML_READER_ENDELEM:
                if ( node == p_node) {
                    if (chunk_vec.size() != 1 ) {
                        msg_Err(this->p_demux, "chunklist of size greater than one not supported");
                        return -1;
                        }
                    this->chunk_vec = chunk_vec;
                    return 0;
                }
                break;
        }
    }
    return -1;
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667
AESKey * Asset::getAESKeyById( AESKeyList* p_key_list, const string s_id )
{
    /* return NULL if DCP is not encrypted */
    if( !p_key_list || s_id.empty() )
        return NULL;

    for( AESKeyList::iterator index = p_key_list->begin(); index != p_key_list->end(); ++index )
        if( (*index)->getKeyId() == s_id )
            return *index;

    return NULL;
}


668 669 670 671 672 673 674 675 676 677 678
int AssetMap::ParseAssetList (xml_reader_t *p_xmlReader, const string p_node, int p_type)
{
    string node;
    int type;
    Asset *asset;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "AssetList" )
        return -1;
    /* loop on AssetList nodes */
679
    while( ( type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node ) ) > 0 ) {
680 681 682 683 684 685 686 687
        switch (type) {
            case XML_READER_STARTELEM:
                if (node != "Asset" )
                    return -1;
                asset = new (nothrow) Asset(this->p_demux);
                if ( unlikely(asset == NULL) )
                    return -1;
                if (asset->Parse(p_xmlReader, node, type)){
688
                    msg_Err(this->p_demux, "Error parsing Asset in AssetMap");
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
                    delete asset;
                    return -1;
                }
                p_dcp->p_asset_list->push_back(asset);
                break;

            case XML_READER_ENDELEM:
                if (node == p_node )
                    return 0;
                break;
            default:
            case XML_READER_TEXT:
                msg_Err(this->p_demux, "Error parsing AssetList in AssetMap");
                return -1;
        }
    }
    return -1;
}

Asset * AssetMap::getAssetById(AssetList *asset_list, const string p_id)
{
    AssetList::iterator index = asset_list->begin() ;
    for (index = asset_list->begin(); index != asset_list->end(); ++index)
        if ((*index)->getId() == p_id )
            return *index;
    return NULL;
}

/*
 * XmlFile Class
 */
XmlFile::~XmlFile() {}

int XmlFile::OpenXml()
{
    char *psz_uri;

    psz_uri = vlc_path2uri( this->s_path.c_str(), "file" );
727
    this->p_stream = vlc_stream_NewURL(this->p_demux, psz_uri );
728 729 730 731 732
    free(psz_uri);
    if( ! this->p_stream ) {
        return -1;
    }

733
    this->p_xmlReader = xml_ReaderCreate( this->p_demux, this->p_stream);
734
    if( ! this->p_xmlReader ) {
735
        vlc_stream_Delete( this->p_stream );
736 737 738 739 740
        return -1;
    }
    return 0;
}

741
int XmlFile::ReadNextNode( demux_t *p_demux, xml_reader_t *p_xmlReader, string& p_node )
742 743
{
    const char * c_node;
744
    int i = xml_ReaderNextNode( p_xmlReader, &c_node );
745

746 747 748
    if( i <= XML_READER_NONE )
        return i;

749
    /* remove namespaces, if there are any */
750 751 752
    string s_node = c_node;
    size_t ui_pos = s_node.find( ":" );

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    if( ( i == XML_READER_STARTELEM || i == XML_READER_ENDELEM ) && ( ui_pos != string::npos ) )
    {
        try
        {
            p_node = s_node.substr( ui_pos + 1 );
        }
        catch( ... )
        {
            msg_Err( p_demux, "error while handling string" );
            return -1;
        }
    }
    else
        p_node = s_node;

768 769 770
    return i;
}

771
int XmlFile::ReadEndNode( demux_t *p_demux, xml_reader_t *p_xmlReader, string p_node, int p_type, string &s_value)
772 773 774 775 776 777 778 779 780
{
    string node;

    if ( xml_ReaderIsEmptyElement( p_xmlReader) )
            return 0;

    if (p_type != XML_READER_STARTELEM)
        return -1;

781 782
    int n = XmlFile::ReadNextNode( p_demux, p_xmlReader, node );
    if( n == XML_READER_TEXT )
783 784
    {
        s_value = node;
785 786
        n = XmlFile::ReadNextNode( p_demux, p_xmlReader, node );
        if( ( n == XML_READER_ENDELEM ) && node == p_node)
787 788
            return 0;
    }
789
    return n == XML_READER_ENDELEM ? 0 : -1;
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
/*
 * Reads first node in XML and returns
 * 1 if XML is CPL,
 * 0 if not
 * -1 on error
 */
int XmlFile::isCPL()
{
    string node;
    int type, ret = 0;

    if( this->OpenXml() )
    {
        msg_Err( this->p_demux, "Failed to open CPL XML file" );
        return -1;
    }

    /* read 1st node  and verify that is a CPL */
    type = XmlFile::ReadNextNode( this->p_demux, p_xmlReader, node );
    if( type == -1 ) /* error */
        ret = -1;
    if( type == XML_READER_STARTELEM &&  node == "CompositionPlaylist" )
        ret = 1;

    /* close xml */
    this->CloseXml();
    return ret;
}
819

820 821
void XmlFile::CloseXml() {
    if( this->p_stream )
822
        vlc_stream_Delete( this->p_stream );
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
    if( this->p_xmlReader )
        xml_ReaderDelete( this->p_xmlReader );
}

/*
 * PKL Class
 */

PKL::~PKL() {
    vlc_delete_all(vec_cpl);
}

int PKL::Parse()
{
    string node;
    int type;
    string s_value;
    const string s_root_node = "PackingList";

    static const string names[] = {
        "Id",
        "IssueDate",
        "Issuer",
        "Creator",
        "AssetList",
        "AnnotationText",
        "IconId",
        "GroupId",
        "Signer",
852
        "Signature"
853 854 855 856 857 858
    };

    if (this->OpenXml())
        return -1;

    /* read 1st node  and verify that is a PKL*/
859
    if ( ! ( ( XML_READER_STARTELEM == XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) &&
860 861 862 863
                (node == s_root_node) ) ) {
        msg_Err( this->p_demux, "Not a valid XML Packing List");
        goto error;
    }
864
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) ) {
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
        switch (type) {
            case XML_READER_STARTELEM: {
                PKLTag_t _tag = PKL_UNKNOWN;
                for(PKLTag_t i = PKL_ID; i <= PKL_SIGNATURE; i = PKLTag_t(i+1)) {
                    if( node == names[i-1]) {
                        _tag = i;
                        switch (_tag) {
                            /* case for parsing non terminal nodes */
                            case PKL_ASSET_LIST:
                                if ( this->ParseAssetList(node, type) )
                                    goto error;
                                break;
                            case PKL_SIGNER:
                                if ( this->ParseSigner(node, type) )
                                    goto error;
                                break;
                            case PKL_SIGNATURE:
                                if ( this->ParseSignature(node, type) )
                                    goto error;
                                break;
                            /* Parse simple/end nodes */
                            case PKL_ID:
887
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
888 889 890 891
                                    goto error;
                                this->s_id = s_value;
                                break;
                            case PKL_ISSUE_DATE:
892
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
893 894 895 896
                                    goto error;
                                this->s_issue_date = s_value;
                                break;
                            case PKL_ISSUER:
897
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
898 899 900 901
                                    goto error;
                                this->s_issuer = s_value;
                                break;
                            case PKL_CREATOR:
902
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
903 904 905 906
                                    goto error;
                                this->s_creator = s_value;
                                break;
                            case PKL_ANNOTATION_TEXT:
907
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
908 909 910 911
                                    goto error;
                                this->s_annotation = s_value;
                                break;
                            case PKL_ICON_ID:
912
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
913 914 915 916
                                    goto error;
                                this->s_icon_id = s_value;
                                break;
                            case PKL_GROUP_ID:
917
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
918 919 920
                                    goto error;
                                this->s_group_id = s_value;
                                break;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
921 922 923
                            default:
                                msg_Warn(this->p_demux, "Unknow PKG_TAG: %i", _tag );
                                break;
924 925 926 927 928 929 930 931 932 933
                        }
                        /* break the for loop as a tag is found*/
                        break;
                    }
                }
                if( _tag == PKL_UNKNOWN )
                    goto error;
                break;
            }
            case XML_READER_TEXT:
934
            case -1:
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
                goto error;
            case XML_READER_ENDELEM:
                if ( node != s_root_node) {
                    msg_Err(this->p_demux,
                        "Something goes wrong in PKL parsing (node %s)", node.c_str());
                    goto error;
                }
                break;
        }
    }
    /* TODO verify presence of mandatory fields*/

    /* Close PKL XML*/
    this->CloseXml();
    return 0;
error:
    msg_Err( this->p_demux, "PKL parsing failed");
    this->CloseXml();
    return -1;
}

int PKL::FindCPLs()
{
    if ( this->vec_cpl.size() != 0 ) {
        msg_Err(this->p_demux, "CPLs already checked");
        return -1;
    }

    for (AssetList::iterator index = this->asset_list->begin();
            index != this->asset_list->end(); ++index) {
        Asset *asset = *index;
        if ( asset->getType().find("text/xml") == string::npos) {
            /* not an xml file */
            continue;
        }

        CPL *cpl = new (nothrow) CPL(this->p_demux,
                      this->s_dcp_path + asset->getPath(),
                      this->asset_list);
        if ( unlikely(cpl == NULL) )
                    return -1;
976 977 978 979 980 981 982 983
        switch( cpl->isCPL() )
        {
            case 1:
                /* CPL Found */
                this->vec_cpl.push_back(cpl);
                break;
            case -1:
                /* error */
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
984
                delete cpl;
985 986 987 988 989 990
                return -1;
            case 0:
            default:
                delete cpl;
                break;
        }
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    }
    return this->vec_cpl.size();
}


int PKL::ParseAssetList(string p_node, int p_type) {
    string node;
    int type;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "AssetList")
        return -1;
1004
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) ) {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
        switch (type) {
            case XML_READER_STARTELEM:
                if( node =="Asset") {
                    if ( this->ParseAsset(node, type) )
                        return -1;
                } else
                    return -1;
                break;
            case XML_READER_ENDELEM:
                if ( node == p_node) {
                    /* parse of chunklist finished */
                    goto end;
                }
                break;
1019 1020 1021 1022
            case -1:
                /* error */
                return -1;
        }
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    }
end:
    return 0;
}

int PKL::ParseAsset(string p_node, int p_type) {
    string node;
    int type;
    string s_value;
    Asset *asset = NULL;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "Asset")
        return -1;

    /* 1st node shall be Id" */
1040 1041
    if( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) )
        if ( ! ( ( type == XML_READER_STARTELEM ) && ( node == "Id" ) ) || type == -1 )
1042
            return -1;
1043 1044 1045 1046
    if( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) != -1 )
    {
        if( type == XML_READER_TEXT )
        {
1047 1048 1049
            s_value = node;
            if (unlikely(node.empty()))
                return -1;
1050 1051 1052 1053 1054 1055 1056 1057 1058
        }
    }
    else
        return -1;

    if( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) != -1 )
    {
        if( type == XML_READER_ENDELEM )
        {
1059 1060 1061 1062
            asset = AssetMap::getAssetById(this->asset_list, s_value);
            if (asset  == NULL)
                return -1;
        }
1063 1064 1065 1066
    }
    else
        return -1;

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
     if (asset == NULL)
        return -1;
     if ( asset->ParsePKL(this->p_xmlReader) )
        return -1;
    return 0;
}

int PKL::ParseSigner(string p_node, int p_type)
{
    string node;
    int type;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "Signer")
        return -1;

1084
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 ) {
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        /* TODO not implemented. Just parse until end of Signer node */
            if ((node == p_node) && (type = XML_READER_ENDELEM))
                return 0;
    }

    msg_Err(this->p_demux, "Parse of Signer finished bad");
    return -1;
}

int PKL::ParseSignature(string p_node, int p_type)
{
    string node;
    int type;

    if (p_type != XML_READER_STARTELEM)
        return -1;
1101
    if( p_node != "Signature")
1102 1103
        return -1;

1104
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 ) {
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
        /* TODO not implemented. Just parse until end of Signature node */
            if ((node == p_node) && (type = XML_READER_ENDELEM))
                return 0;
    }
    msg_Err(this->p_demux, "Parse of Signature finished bad");
    return -1;
}

/*
 * Reel Class
 */
int Reel::Parse(string p_node, int p_type) {
    string node;
    int type;
    string s_value;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "Reel")
        return -1;

1126
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 ) {
1127 1128 1129
        switch (type) {
            case XML_READER_STARTELEM:
                if (node =="Id") {
1130
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1131 1132 1133
                        return -1;
                    this->s_id = s_value;
                } else if (node == "AnnotationText") {
1134
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
                        return -1;
                    this->s_annotation = s_value;
                } else if ( node =="AssetList" ) {
                    if (this->ParseAssetList(node, type))
                        return -1;
                } else {
                    /* unknown tag */
                    msg_Err(this->p_demux, "Reel::Parse, unknown tag:%s", node.c_str());
                    return -1;
                }
                break;
            case XML_READER_TEXT:
                /* Error */
                msg_Err(this->p_demux, "Reel parsing error");
                return -1;
            case XML_READER_ENDELEM:
                /* verify correctness of end node */
                if ( node == p_node) {
                    /* TODO : verify Reel id */
                    return 0;
                }
                break;
        }
    }
    return -1;
}


Asset * Reel::getTrack(TrackType_t e_track)
{
    switch (e_track) {
        case TRACK_PICTURE:
            return this->p_picture_track;
        case TRACK_SOUND:
            return this->p_sound_track;
        case TRACK_SUBTITLE:
            return this->p_subtitle_track;
        case TRACK_UNKNOWN:
        default:
            break;
    }
    return NULL;
}

int Reel::ParseAssetList(string p_node, int p_type) {
    string node;
    int type;
    string s_value;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "AssetList")
        return -1;

1189
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 )  {
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
        switch (type) {
            case XML_READER_STARTELEM:
                if (node =="MainPicture") {
                    if ( this->ParseAsset(node, type, TRACK_PICTURE) )
                        return -1;
                } else if (node =="MainSound") {
                    if ( this->ParseAsset(node, type, TRACK_SOUND) )
                        return -1;
                } else if (node =="MainSubtitle") {
                    if ( this->ParseAsset(node, type, TRACK_SUBTITLE) )
                        return -1;
                } else {
                    /* unknown tag */
                    msg_Err(this->p_demux, "Reel::ParseAssetList, unknown tag:%s", node.c_str());
                    return -1;
                }
                break;
            case XML_READER_TEXT:
                /* Parsing error */
                msg_Err(this->p_demux, "AssetList parsing error");
                return -1;
            case XML_READER_ENDELEM:
                /* verify correctness of end node */
                if ( node == p_node) {
                    /* TODO : verify id */
                    return 0;
                }
                break;
        }
    }
    return -1;
}

int Reel::ParseAsset(string p_node, int p_type, TrackType_t e_track) {
    string node;
    int type;
    string s_value;
    bool b_stop_parse = false;
    Asset *asset = NULL;

    if (p_type != XML_READER_STARTELEM)
        return -1;

    /* 1st node shall be Id */
1234 1235
    if( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) )
        if( ! ( ( type == XML_READER_STARTELEM ) && ( node == "Id" ) ) || type == -1 )
1236 1237
            return -1;

1238
    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1239 1240 1241 1242 1243 1244 1245
        return -1;

    asset = AssetMap::getAssetById(this->p_asset_list, s_value);
    if (asset  == NULL)
        return -1;

    while(  (! b_stop_parse) &&
1246
            ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) ) {
1247 1248 1249
        switch (type) {
            case XML_READER_STARTELEM:
                if (node =="EditRate") {
1250
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1251 1252
                        return -1;
                } else if (node == "AnnotationText") {
1253
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1254
                        return -1;
1255
                    asset->setAnnotation(s_value);
1256
                } else if (node == "IntrinsicDuration") {
1257
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1258
                        return -1;
1259
                    asset->setIntrinsicDuration(atoi(s_value.c_str()));
1260
                } else if (node == "EntryPoint") {
1261
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1262
                        return -1;
1263
                    asset->setEntryPoint(atoi(s_value.c_str()));
1264
                } else if (node == "Duration") {
1265
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1266
                        return -1;
1267
                    asset->setDuration(atoi(s_value.c_str()));
1268
                } else if (node == "KeyId") {
1269
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1270
                        return -1;
1271
                    asset->setKeyId( s_value );
1272
                } else if (node == "Hash") {
1273
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1274 1275
                        return -1;
                } else if (node == "FrameRate") {
1276
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1277 1278
                        return -1;
                } else if (node == "ScreenAspectRatio") {
1279
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1280 1281
                        return -1;
                } else if (node == "Language") {
1282
                    if ( XmlFile::ReadEndNode( this->p_demux, this->p_xmlReader, node, type, s_value ) )
1283 1284 1285 1286 1287 1288 1289 1290
                        return -1;
                } else {
                    /* unknown tag */
                    msg_Err(this->p_demux, "Reel::ParseAsset unknown tag:%s", node.c_str());
                    return -1;
                }
                break;
            case XML_READER_TEXT:
1291 1292
            case -1:
                /* error */
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
                return -1;
                break;
            case XML_READER_ENDELEM:
                /* verify correctness of end node */
                if ( node == p_node) {
                    /* TODO : verify id */
                    b_stop_parse = true;
                }
        }
    }
    /* store by track */
    switch (e_track) {
        case TRACK_PICTURE:
            this->p_picture_track = asset;
            break;
        case TRACK_SOUND:
            this->p_sound_track = asset;
            break;
        case TRACK_SUBTITLE:
            this->p_subtitle_track = asset;
            break;
        case TRACK_UNKNOWN:
        default:
            break;
    }
    return 0;
}

/*
 * CPL Class
 */

CPL::~CPL() {
    vlc_delete_all(vec_reel);
}

int CPL::Parse()
{
    string node;
    int type;
    string s_value;
    const string s_root_node = "CompositionPlaylist";

    static const string names[] = {
        "Id",
        "AnnotationText",
        "IconId",
        "IssueDate",
        "Issuer",
        "Creator",
        "ContentTitleText",
        "ContentKind",
        "ContentVersion",
        "RatingList",
        "ReelList",
        "Signer",
1349
        "Signature"
1350 1351 1352 1353 1354 1355
    };

    if (this->OpenXml())
        return -1;

    /* read 1st node  and verify that is a CPL*/
1356
    if( ! ( ( XML_READER_STARTELEM == XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) &&
1357 1358 1359 1360 1361
                (node == s_root_node) ) ) {
        msg_Err( this->p_demux, "Not a valid XML Packing List");
        goto error;
    }

1362
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) ) {
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
        switch (type) {
            case XML_READER_STARTELEM: {
                CPLTag_t _tag = CPL_UNKNOWN;
                for(CPLTag_t i = CPL_ID; i <= CPL_SIGNATURE; i = CPLTag_t(i+1)) {
                    if( node == names[i-1]) {
                        _tag = i;
                        switch (_tag) {
                            /* case for parsing non terminal nodes */
                            case CPL_REEL_LIST:
                                if ( this->ParseReelList(node, type) )
                                    goto error;
                                break;
                            case CPL_CONTENT_VERSION:
                            case CPL_SIGNER:
                            case CPL_SIGNATURE:
                            case CPL_RATING_LIST:
                                if ( this->DummyParse(node,type) )
                                    goto error;
                                break;
                                /* Parse simple/end nodes */
                            case CPL_ID:
1384
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1385 1386 1387 1388
                                    goto error;
                                this->s_id = s_value;
                                break;
                            case CPL_ANNOTATION_TEXT:
1389
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1390 1391 1392 1393
                                    goto error;
                                this->s_annotation = s_value;
                                break;
                            case CPL_ICON_ID:
1394
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1395 1396 1397 1398
                                    goto error;
                                this->s_icon_id = s_value;
                                break;
                            case CPL_ISSUE_DATE:
1399
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1400 1401 1402 1403
                                    goto error;
                                this->s_issue_date= s_value;
                                break;
                            case CPL_ISSUER:
1404
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1405 1406 1407 1408
                                    goto error;
                                this->s_issuer = s_value;
                                break;
                            case CPL_CREATOR:
1409
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1410 1411 1412 1413
                                    goto error;
                                this->s_creator = s_value;
                                break;
                            case CPL_CONTENT_TITLE:
1414
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1415 1416 1417 1418
                                    goto error;
                                this->s_content_title = s_value;
                                break;
                            case CPL_CONTENT_KIND:
1419
                                if ( XmlFile::ReadEndNode( this->p_demux, p_xmlReader, node, type, s_value ) )
1420 1421 1422
                                    goto error;
                                this->s_content_kind = s_value;
                                break;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1423 1424 1425
                            default:
                                msg_Warn(this->p_demux, "Unknow CPL_TAG: %i", _tag );
                                break;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
                        }

                        /* break the for loop as a tag is found*/
                        break;
                    }
                }
                if( _tag == CPL_UNKNOWN )
                    goto error;
                break;
            }
            case XML_READER_TEXT:
1437
            case -1:
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
               goto error;
            case XML_READER_ENDELEM:
               if ( node != s_root_node) {
                   msg_Err(this->p_demux,
                           "Something goes wrong in CKL parsing (node %s)", node.c_str());
                   goto error;
               }
               break;
        }
    }

    /* TODO verify presence of mandatory fields*/

    /* Close CPL XML*/
    this->CloseXml();
    return 0;
error:
    this->CloseXml();
    return -1;
}

int CPL::ParseReelList(string p_node, int p_type) {
    string node;
    int type;

    if (p_type != XML_READER_STARTELEM)
        return -1;
    if( p_node != "ReelList")
        return -1;
1467
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 ) {
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
        switch (type) {
            case XML_READER_STARTELEM: {
                Reel *p_reel = new (nothrow) Reel( this->p_demux, this->asset_list, this->p_xmlReader);
                if ( unlikely(p_reel == NULL) )
                    return -1;
                if( node =="Reel") {
                    if ( p_reel->Parse(node, type) ) {
                        delete p_reel;
                        return -1;
                    }
                } else {
                    delete p_reel;
                    return -1;
                }
                this->vec_reel.push_back(p_reel);

                break;
            }
            case XML_READER_TEXT:
                /* impossible */
                break;
            case XML_READER_ENDELEM:
                if ( node == p_node)
                    return 0;
                break;
            }
    }
    return -1;
}


int CPL::DummyParse(string p_node, int p_type)
{
    string node;
    int type;

    if (p_type != XML_READER_STARTELEM)
        return -1;

    if (xml_ReaderIsEmptyElement( this->p_xmlReader))
        return 0;

1510
    while( ( type = XmlFile::ReadNextNode( this->p_demux, this->p_xmlReader, node ) ) > 0 ) {
1511 1512 1513 1514 1515 1516 1517
        /* TODO not implemented. Just pase until end of input node */
        if ((node == p_node) && (type = XML_READER_ENDELEM))
            return 0;
    }

    return -1;
}