<?php

//$aFiles = 

//header('Content-Type: application/dash+xml');

$sDir = '/tank0/videoserver/'.$_REQUEST['dir'];
    $sAdoptionSetAudio = "";
    $sAdoptionSetVideo = "";
    $sMaxSegmentDuration = 'PT0H0M2.360S';
    $sMediaPresentationDuration = 'PT0H2M30.080S';
    $nMaxWidth = 0;
    $nMaxHeight = 0;
    $nMaxFrameRate = 0;

if (is_dir($sDir)) {


    //echo $sDir." gefunden<br/>";
    $aFiles = glob($sDir.'/*.mpd');
    $nRepresentation = 1;
    foreach ($aFiles as $sFile) {
        $sContent = file_get_contents($sFile);
        $sContent = str_replace(' id="1" ',' id="'.$nRepresentation.'" ',$sContent);
        if (strpos($sFile,'_audio_') > 0) {
            //echo "<br />audio:".$sFile;
            $sAdoptionSetAudio .= GetRepresentation($sContent);
        }
        else {
            $sMediaPresentationDuration = GetMediaPresentationDuration($sContent);

            
            //echo "MPD:".$sMediaPresentationDuration;
            //echo "<br />video:".$sFile;
            $sAdoptionSetVideo .= GetRepresentation($sContent);
            //echo '<br />Dimensions:'.json_encode(GetDimensions($sContent));
            $aMaxValues = GetDimensions($sContent);
            if ($aMaxValues->maxFrameRate > $nMaxFrameRate) { 
                $nMaxFrameRate = $aMaxValues->maxFrameRate; 
            }
            if ($aMaxValues->maxHeight > $nMaxHeight)    { 
                $nMaxHeight = $aMaxValues->maxHeight; 
            }
            if ($aMaxValues->maxWidth > $nMaxWidth)     { 
                $nMaxWidth = $aMaxValues->maxWidth; 
            }
        }
        $nRepresentation++;
    }
    
    //echo "<br />--".$nMaxWidth;
    //echo "<br />--".$nMaxHeight;
    //echo "<br />--".$nMaxFrameRate;
    
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
    echo '<MPD xmlns:ns2="http://www.w3.org/1999/xlink"  xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT2.500S" type="static"';
    echo ' mediaPresentationDuration="'.$sMediaPresentationDuration.'"';
    //echo ' maxSegmentDuration="'.$sMaxSegmentDuration.'" ';
    echo ' profiles="urn:mpeg:dash:profile:full:2011">';
    echo ' <Period duration="'.$sMediaPresentationDuration.'">';
    //echo ' <BaseURL>http://stream.allesedv.at/'.$_REQUEST['dir'].'/<BaseURL>';
    echo '  <AdaptationSet segmentAlignment="true" lang="und">';
    echo $sAdoptionSetAudio;
    echo '  </AdaptationSet>';
    echo '  <AdaptationSet segmentAlignment="true" maxWidth="'.$nMaxWidth.'" maxHeight="'.$nMaxHeight.'" maxFrameRate="'.$nMaxFrameRate.'" par="16:9" lang="und">';
    echo $sAdoptionSetVideo;
    echo '  </AdaptationSet>';
    echo ' </Period>';
    echo '</MPD>';
}
else {
    echo "DIR NOT FOUND";
}

function GetRepresentation($sContent) {
    //echo $sContent;
    $sReturn = $sContent;
    $sReturn = substr($sReturn,strpos($sReturn,'<Representation')-1);
    $sReturn = substr($sReturn,1,strpos($sReturn,'</Representation')+16);
    $sReturn = str_replace('sourceURL="','sourceURL="/'.$_REQUEST['dir'].'/',$sReturn);
    $sReturn = str_replace('media="','media="/'.$_REQUEST['dir'].'/',$sReturn);
    return $sReturn;
}
function GetDimensions($sContent) {
    $sAdoptionSet = $sContent;
    $sAdoptionSet = substr($sAdoptionSet,strpos($sAdoptionSet,'<AdaptationSet'));
    $sAdoptionSet = substr($sAdoptionSet,1,strpos($sAdoptionSet,'>'));
    
    $nMaxFrameRate = $sAdoptionSet;
    $nMaxFrameRate = substr($nMaxFrameRate,strpos($nMaxFrameRate,'maxFrameRate')+14);
    $nMaxFrameRate = substr($nMaxFrameRate,0,strpos($nMaxFrameRate,'"'));
    
    $nMaxHeight = $sAdoptionSet;
    $nMaxHeight = substr($nMaxHeight,strpos($nMaxHeight,'maxHeight')+11);
    $nMaxHeight = substr($nMaxHeight,0,strpos($nMaxHeight,'"'));
    
    $nMaxWidth = $sAdoptionSet;
    $nMaxWidth = substr($nMaxWidth,strpos($nMaxWidth,'maxWidth')+10);
    $nMaxWidth = substr($nMaxWidth,0,strpos($nMaxWidth,'"'));
    
    $aReturn = array();
    $aReturn['maxFrameRate'] = $nMaxFrameRate;
    $aReturn['maxHeight'] = $nMaxHeight;
    $aReturn['maxWidth'] = $nMaxWidth;
    
    return (object)$aReturn;
}

function GetMediaPresentationDuration($sContent) {
    $sMPD = $sContent;
    $sMPD = str_replace('mediaPresentationDuration="PT','mediaPresentationDuration="P0Y0M0DT',$sMPD);
    $sMPD = substr($sMPD,strpos($sMPD,'mediaPresentationDuration="')+27);
    $sMPD = substr($sMPD,0,strpos($sMPD,'"')-0);
    return $sMPD;
}

?>
