package com.viaccessorca.voplayerdemo.mediamgt;

import android.util.Log;

import com.viaccessorca.voplayerdemo.Constants;
import com.viaccessorca.voplayerdemo.Utils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import static com.viaccessorca.voplayerdemo.Constants.DRM_AGENT_TYPE;
import static com.viaccessorca.voplayerdemo.Constants.DRM_CONTENT_IDENTIFIER;
import static com.viaccessorca.voplayerdemo.Constants.DRM_LICENSE_COOKIE_SKEY;
import static com.viaccessorca.voplayerdemo.Constants.DRM_LICENSE_CUSTOM_DATA_SKEY;
import static com.viaccessorca.voplayerdemo.Constants.DRM_LICENSE_URL_PARAMETERS_SKEY;
import static com.viaccessorca.voplayerdemo.Constants.DRM_LICENSE_URL_SKEY;
import static com.viaccessorca.voplayerdemo.Constants.DRM_LICENSE_USE_VOSECURITYGATEWAY;
import static com.viaccessorca.voplayerdemo.Constants.DRM_PERSONALIZATION_APPVERSION;
import static com.viaccessorca.voplayerdemo.Constants.DRM_PERSONALIZATION_CUSTOM_DATA;
import static com.viaccessorca.voplayerdemo.Constants.DRM_PERSONALIZATION_URL;


public class MediaDescriptionParser {

    private static final String TAG = "MediaDescriptionParser";


    public static StreamingMediaItem parseMediaItemFromXMLElement(Element curElement, String mediaId, String sslCertificatesRootPath) {

        StreamingMediaItem mediaItem = null;

        String strUrl = null;
        if (0 < curElement.getElementsByTagName("URL").getLength()) {
            Element URLElement = (Element) curElement.getElementsByTagName("URL").item(0);
            Node valURL = URLElement.getFirstChild();
            strUrl = valURL.getNodeValue();
            //Log.d(TAG, "setupStreamFromOpFile name: op url:" + strUrl);
        }

        if (strUrl != null) {
            strUrl = strUrl.trim();
            String displayName = mediaId;

            mediaItem = new StreamingMediaItem(Utils.getMD5Hash(displayName), strUrl, displayName, false);
            mediaItem.setDescriptionFilePath("directData");

            /**
             * Parse parameters in file.op
             */
            NodeList list = null;

            // Boot address
            list = curElement.getElementsByTagName("BootAddress");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String bootAddress = node.getNodeValue();
                    if (null != bootAddress) {
                        mediaItem.setBootAddress(bootAddress);
                        Log.d(TAG, "setupStreamFromOpFile bootAddress: " + bootAddress);
                    }
                }
            }

            // Company name
            list = curElement.getElementsByTagName("CompanyName");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String companyName = node.getNodeValue();
                    if (null != companyName) {
                        mediaItem.setCompanyName(companyName);
                        Log.d(TAG, "setupStreamFromOpFile companyName: " + companyName);
                    }
                }
            }

            list = curElement.getElementsByTagName("SecondaryContentUri");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String secondaryContentUri = node.getNodeValue();
                    if (null != secondaryContentUri) {
                        mediaItem.setSecondaryContentUri(secondaryContentUri);
                        Log.d(TAG, "setupStreamFromOpFile secondaryContentUri: " + secondaryContentUri);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_AGENT_TYPE);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmAgentType = node.getNodeValue();
                    if (null != drmAgentType) {
                        mediaItem.setDrmAgentType(drmAgentType);
                        Log.d(TAG, "setupStreamFromOpFile drmAgentType: " + drmAgentType);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_PERSONALIZATION_URL);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmPersonalizationUrl = node.getNodeValue();
                    if (null != drmPersonalizationUrl) {
                        mediaItem.setDrmPersonalizationUrl(drmPersonalizationUrl);
                        Log.d(TAG, "setupStreamFromOpFile drmPersonalizationUrl: " + drmPersonalizationUrl);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_PERSONALIZATION_APPVERSION);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmPersonalizationAppVersion = node.getNodeValue();
                    if (null != drmPersonalizationAppVersion) {
                        mediaItem.setDrmPersonalizationAppVersion(drmPersonalizationAppVersion);
                        Log.d(TAG, "setupStreamFromOpFile DrmPersonalizationAppVersion: " + drmPersonalizationAppVersion);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_PERSONALIZATION_CUSTOM_DATA);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmPersonalizationCustomData = node.getNodeValue();
                    if (null != drmPersonalizationCustomData) {
                        mediaItem.setDrmPersonalizationCustomData(drmPersonalizationCustomData);
                        Log.d(TAG, "setupStreamFromOpFile drmPersonalizationCustomData: " + drmPersonalizationCustomData);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_LICENSE_URL_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmLicenseAcquisitionUrl = node.getNodeValue();
                    if (null != drmLicenseAcquisitionUrl) {
                        mediaItem.setDRMLicenseAcquisitionUrl(drmLicenseAcquisitionUrl);
                        Log.d(TAG, "setupStreamFromOpFile setDRMLicenseAcquisitionUrl: " + drmLicenseAcquisitionUrl);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_LICENSE_URL_PARAMETERS_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmLicenseAcquisitionUrlParameters = node.getNodeValue();
                    if (null != drmLicenseAcquisitionUrlParameters) {
                        mediaItem.setDRMLicenseAcquisitionUrlParameters(drmLicenseAcquisitionUrlParameters);
                        Log.d(TAG, "setupStreamFromOpFile setDRMLicenseAcquisitionUrlParameters: " + drmLicenseAcquisitionUrlParameters);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_LICENSE_CUSTOM_DATA_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmLicenseAcquisitionCustomData = node.getNodeValue();
                    if (null != drmLicenseAcquisitionCustomData) {
                        mediaItem.setDRMLicenseAcquisitionCustomData(drmLicenseAcquisitionCustomData);
                        Log.d(TAG, "setupStreamFromOpFile drmLicenseAcquisitionCustomData: " + drmLicenseAcquisitionCustomData);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_LICENSE_COOKIE_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmLicenseAcquisitionCookie = node.getNodeValue();
                    if (null != drmLicenseAcquisitionCookie) {
                        mediaItem.setDRMLicenseAcquisitionCookie(drmLicenseAcquisitionCookie);
                        Log.d(TAG, "setupStreamFromOpFile drmLicenseAcquisitionCookie: " + drmLicenseAcquisitionCookie);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_CONTENT_IDENTIFIER);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String drmContentId = node.getNodeValue();
                    if (null != drmContentId) {
                        mediaItem.setDrmContentIdentifier(drmContentId);
                        Log.d(TAG, "setupStreamFromOpFile setDrmContentIdentifier: " + drmContentId);
                    }
                }
            }

            list = curElement.getElementsByTagName(DRM_LICENSE_USE_VOSECURITYGATEWAY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String useSecurityGate = node.getNodeValue();
                    if (null != useSecurityGate) {
                        boolean value = useSecurityGate.equalsIgnoreCase("true") ? true : false;
                        mediaItem.setDrmUseVOSecurityGateway(value);
                        Log.d(TAG, "setupStreamFromOpFile setDrmUseVOSecurityGateway: " + value);
                    }
                }
            }

            // Marlin protection
            list = curElement.getElementsByTagName(Constants.MARLIN_PROTECTED_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String marlinProtectedStr = node.getNodeValue();
                    if (null != marlinProtectedStr && marlinProtectedStr.equalsIgnoreCase("yes")) {
                        mediaItem.setMarlinProtected(true);
                        Log.d(TAG, "setupStreamFromOpFile marlin protected");
                    }
                }
            }

            // Vast url
            list = curElement.getElementsByTagName(Constants.VAST_URL_INTENT_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String vastUrlStr = node.getNodeValue();
                    if (null != vastUrlStr) {
                        mediaItem.setVastUrl(vastUrlStr);
                        Log.d(TAG, "setupStreamFromOpFile vastUrl " + vastUrlStr);
                    }
                }
            }

            // Vmap url
            list = curElement.getElementsByTagName(Constants.VMAP_URL_INTENT_SKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String vmapUrlStr = node.getNodeValue();
                    if (null != vmapUrlStr) {
                        mediaItem.setVmapUrl(vmapUrlStr);
                        Log.d(TAG, "setupStreamFromOpFile vmapUrl " + vmapUrlStr);
                    }
                }
            }

            list = curElement.getElementsByTagName(Constants.FAST_STARTUP_BKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String fastStartupStr = node.getNodeValue();
                    if (null != fastStartupStr && fastStartupStr.equalsIgnoreCase("yes")) {
                        mediaItem.setFastStartup(true);
                        Log.d(TAG, "setupStreamFromOpFile fast startup");
                    }
                }
            }

            list = curElement.getElementsByTagName(Constants.LOCAL_SERVER_DEMO_BKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String localServerDemoStr = node.getNodeValue();
                    if (null != localServerDemoStr && localServerDemoStr.equalsIgnoreCase("yes")) {
                        mediaItem.setLocalServerDemo(true);
                        Log.d(TAG, "setupStreamFromOpFile local server demo");
                    }
                }
            }

            // FAAS offline url
            list = curElement.getElementsByTagName("dbFaasOffline");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String dbFaasOffline = node.getNodeValue();
                    if (null != dbFaasOffline) {
                        mediaItem.setDbFaasOffline(dbFaasOffline);
                        Log.d(TAG, "setupStreamFromOpFile dbFaasOffline: " + dbFaasOffline);
                    }
                }
            }

            // Authentication user name
            list = curElement.getElementsByTagName("HttpAuthUsername");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String HttpAuthUsername = node.getNodeValue();
                    if (null != HttpAuthUsername) {
                        mediaItem.setHttpAuthUsername(HttpAuthUsername);
                        Log.d(TAG, "setupStreamFromOpFile HttpAuthUsername: " + HttpAuthUsername);
                    }
                }
            }

            // Authentication password
            list = curElement.getElementsByTagName("HttpAuthPassword");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String HttpAuthPassword = node.getNodeValue();
                    if (null != HttpAuthPassword) {
                        mediaItem.setHttpAuthPassword(HttpAuthPassword);
                        Log.d(TAG, "setupStreamFromOpFile HttpAuthPassword: " + HttpAuthPassword);
                    }
                }
            }

            // SSL CA cert file path
            list = curElement.getElementsByTagName("SSLCACertFilePath");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node && null != sslCertificatesRootPath) {
                    if (null != sslCertificatesRootPath) {
                        String SSLCACertFilePath = sslCertificatesRootPath + "/" + node.getNodeValue();
                        if (null != SSLCACertFilePath) {
                            mediaItem.setSSLCACertFilePath(SSLCACertFilePath);
                            Log.d(TAG, "setupStreamFromOpFile SSLCACertFilePath: " + SSLCACertFilePath);
                        }
                    }
                }
            }

            // SSL CA cert file type
            list = curElement.getElementsByTagName("SSLCACertFileType");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String SSLCACertFileType = node.getNodeValue();
                    if (null != SSLCACertFileType) {
                        mediaItem.setSSLCACertFileType(SSLCACertFileType);
                        Log.d(TAG, "setupStreamFromOpFile SSLCACertFileType: " + SSLCACertFileType);
                    }
                }
            }

            // SSL Client cert file path
            list = curElement.getElementsByTagName("SSLClientCertFilePath");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node && null != sslCertificatesRootPath) {
                    String SSLClientCertFilePath = sslCertificatesRootPath + "/" + node.getNodeValue();
                    if (null != SSLClientCertFilePath) {
                        mediaItem.setSSLClientCertFilePath(SSLClientCertFilePath);
                        Log.d(TAG, "setupStreamFromOpFile SSLClientCertFilePath: " + SSLClientCertFilePath);
                    }
                }
            }

            // SSL Client cert file type
            list = curElement.getElementsByTagName("SSLClientCertFileType");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String SSLClientCertFileType = node.getNodeValue();
                    if (null != SSLClientCertFileType) {
                        mediaItem.setSSLClientCertFileType(SSLClientCertFileType);
                        Log.d(TAG, "setupStreamFromOpFile SSLClientCertFileType: " + SSLClientCertFileType);
                    }
                }
            }

            // SSL Client Key file Path
            list = curElement.getElementsByTagName("SSLClientKeyFilePath");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node && null != sslCertificatesRootPath) {
                    String SSLClientKeyFilePath = sslCertificatesRootPath + "/" + node.getNodeValue();
                    if (null != SSLClientKeyFilePath) {
                        mediaItem.setSSLClientKeyFilePath(SSLClientKeyFilePath);
                        Log.d(TAG, "setupStreamFromOpFile SSLClientKeyFilePath: " + SSLClientKeyFilePath);
                    }
                }
            }

            // SSL Client Key file type
            list = curElement.getElementsByTagName("SSLClientKeyFileType");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String SSLClientKeyFileType = node.getNodeValue();
                    if (null != SSLClientKeyFileType) {
                        mediaItem.setSSLClientKeyFileType(SSLClientKeyFileType);
                        Log.d(TAG, "setupStreamFromOpFile SSLClientKeyFileType: " + SSLClientKeyFileType);
                    }
                }
            }

            // Http Custom header
            list = curElement.getElementsByTagName("HttpCustomHeader");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String HttpCustomHeader = node.getNodeValue();
                    if (null != HttpCustomHeader) {
                        mediaItem.setHttpCustomHeader(HttpCustomHeader);
                        Log.d(TAG, "setupStreamFromOpFile HttpCustomHeader: " + HttpCustomHeader);
                    }
                }
            }

            // alternate bitrates
            list = curElement.getElementsByTagName("AlternateBitrates");
            if (0 < list.getLength()) {

                Element altsNode = ((Element) list.item(0));
                list = altsNode.getElementsByTagName("Bitrate");

                int bitrates[] = new int[list.getLength()];
                for (int a = 0; a < list.getLength(); a++) {

                    Node node = ((Element) list.item(a)).getFirstChild();
                    if (null != node) {
                        String bitrate = node.getNodeValue();
                        bitrates[a] = Integer.parseInt(bitrate);
                    }
                }
                mediaItem.setStreamBitrates(bitrates);
            }


            // Additional url to remote thumbnail zip
            list = curElement.getElementsByTagName("RemoteThumbnailsZipUrl");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String remoteZipUrl = node.getNodeValue();
                    if (null != remoteZipUrl) {
                        mediaItem.setThumbnailsZipUrl(remoteZipUrl);
                        Log.d(TAG, "setupStreamFromOpFile RemoteThumbnailsZipUrl: " + remoteZipUrl);
                    }
                }
            }

            // AbrSensitivityValue
            list = curElement.getElementsByTagName("AbrSensitivity");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AbrSensitivityValue = node.getNodeValue();
                    if (null != AbrSensitivityValue) {
                        mediaItem.setAbrSensitivityValue(AbrSensitivityValue);
                        Log.d(TAG, "setupStreamFromOpFile setAbrSensitivityValue: " + AbrSensitivityValue);
                    }
                }
            }

            //Live latency
            list = curElement.getElementsByTagName(Constants.ABR_LIVE_LATENCY_IKEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String LiveLatency = node.getNodeValue();
                    int LiveLatencyValue = Integer.parseInt(LiveLatency);
                    if (0 <= LiveLatencyValue) {
                        mediaItem.setLiveLatencyValue(LiveLatencyValue);
                        Log.d(TAG, "setupStreamFromOpFile setLiveLatencyValue: " + LiveLatencyValue);
                    }
                }
            }

            //Force Update Manifest Mode
            list = curElement.getElementsByTagName("ForceMSSUpdateManifest");
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String forceUpdateManifest = node.getNodeValue();
                    boolean forceUpdateManifestValue = "yes".equalsIgnoreCase(forceUpdateManifest);
                    mediaItem.setForceMSSUpdateManifest(forceUpdateManifestValue);
                    Log.d(TAG, "setupStreamFromOpFile ForceMSSUpdateManifest: " + forceUpdateManifestValue);
                }
            }

            //ChromeCast Alternate URL
            list = curElement.getElementsByTagName(Constants.CCAST_ALTERNATE_URL_KEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AlternateUrl = node.getNodeValue();
                    if (null != AlternateUrl) {
                        mediaItem.setChromeCastAlternateUrl(AlternateUrl);
                        Log.d(TAG, "setupStreamFromOpFile setChromeCastAlternateUrl: " + AlternateUrl);
                    }
                }
            }

            //ChromeCast Alternate DRM Agent Type
            list = curElement.getElementsByTagName(Constants.CCAST_ALTERNATE_DRM_KEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AlternateDrmAgentType = node.getNodeValue();
                    if (null != AlternateDrmAgentType) {
                        mediaItem.setChromeCastAlternateDrmAgentType(AlternateDrmAgentType);
                        Log.d(TAG, "setupStreamFromOpFile setChromeCastAlternateDrmAgentType: " + AlternateDrmAgentType);
                    }
                }
            }

            //ChromeCast Alternate DRM Licenser URL
            list = curElement.getElementsByTagName(Constants.CCAST_ALTERNATE_DRM_LICENSER_URL_KEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AlternateDrmLicenserUrl = node.getNodeValue();
                    if (null != AlternateDrmLicenserUrl) {
                        mediaItem.setChromeCastAlternateDrmLicenserUrl(AlternateDrmLicenserUrl);
                        Log.d(TAG, "setupStreamFromOpFile setChromeCastAlternateLicenserUrl: " + AlternateDrmLicenserUrl);
                    }
                }
            }

            //ChromeCast Alternate DRM Custom Data
            list = curElement.getElementsByTagName(Constants.CCAST_ALTERNATE_DRM_CUSTOM_DATA_KEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AlternateDrmCustomData = node.getNodeValue();
                    if (null != AlternateDrmCustomData) {
                        mediaItem.setChromeCastAlternateDrmCustomData(AlternateDrmCustomData);
                        Log.d(TAG, "setupStreamFromOpFile setChromeCastAlternateDrmCustomData: " + AlternateDrmCustomData);
                    }
                }
            }

            //ChromeCast Alternate DRM cookie
            list = curElement.getElementsByTagName(Constants.CCAST_ALTERNATE_DRM_COOKIE_KEY);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String AlternateDrmCookie = node.getNodeValue();
                    if (null != AlternateDrmCookie) {
                        mediaItem.setChromeCastAlternateDrmCookie(AlternateDrmCookie);
                        Log.d(TAG, "setupStreamFromOpFile setChromeCastAlternateDrmCookie: " + AlternateDrmCookie);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SERVER);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String SrubbingOptionServer = node.getNodeValue();
                    if (null != SrubbingOptionServer) {
                        mediaItem.setScrubbingOptionServer(SrubbingOptionServer);
                        Log.d(TAG, "setupStreamFromOpFile SrubbingOptionServer: " + SrubbingOptionServer);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_CHANNEL);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionChannel = node.getNodeValue();
                    if (null != ScrubbingOptionChannel) {
                        mediaItem.setScrubbingOptionChannel(ScrubbingOptionChannel);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionChannel: " + ScrubbingOptionChannel);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_ASSET);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionAsset = node.getNodeValue();
                    if (null != ScrubbingOptionAsset) {
                        mediaItem.setScrubbingOptionAsset(ScrubbingOptionAsset);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionAsset: " + ScrubbingOptionAsset);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SERVICE_TYPE);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionServiceType = node.getNodeValue();
                    if (null != ScrubbingOptionServiceType) {
                        mediaItem.setScrubbingOptionServiceType(ScrubbingOptionServiceType);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionServiceType: " + ScrubbingOptionServiceType);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_START);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionStart = node.getNodeValue();
                    if (null != ScrubbingOptionStart) {
                        mediaItem.setScrubbingOptionStart(ScrubbingOptionStart);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionStart: " + ScrubbingOptionStart);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_END);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionEnd = node.getNodeValue();
                    if (null != ScrubbingOptionEnd) {
                        mediaItem.setScrubbingOptionEnd(ScrubbingOptionEnd);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionEnd: " + ScrubbingOptionEnd);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SPRITE_RESOLUTION);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionSpriteResolution = node.getNodeValue();
                    if (null != ScrubbingOptionSpriteResolution) {
                        mediaItem.setScrubbingOptionSpriteResolution(ScrubbingOptionSpriteResolution);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionSpriteResolution: " + ScrubbingOptionSpriteResolution);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SPRITE_COLUMS);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionSpriteColums = node.getNodeValue();
                    if (null != ScrubbingOptionSpriteColums) {
                        mediaItem.setScrubbingOptionSpriteColums(ScrubbingOptionSpriteColums);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionSpriteColums: " + ScrubbingOptionSpriteColums);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SPRITE_LINES);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionSpriteLines = node.getNodeValue();
                    if (null != ScrubbingOptionSpriteLines) {
                        mediaItem.setScrubbingOptionSpriteLines(ScrubbingOptionSpriteLines);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionSpriteLines: " + ScrubbingOptionSpriteLines);
                    }
                }
            }

            //Scrubbing Parameter
            list = curElement.getElementsByTagName(Constants.SCRUBBING_OPTION_SPRITE_INTERVAL);
            if (0 < list.getLength()) {
                Node node = ((Element) list.item(0)).getFirstChild();
                if (null != node) {
                    String ScrubbingOptionSpriteInterval = node.getNodeValue();
                    if (null != ScrubbingOptionSpriteInterval) {
                        mediaItem.setScrubbingOptionSpriteInterval(ScrubbingOptionSpriteInterval);
                        Log.d(TAG, "setupStreamFromOpFile ScrubbingOptionSpriteInterval: " + ScrubbingOptionSpriteInterval);
                    }
                }
            }
        }
        return mediaItem;
    }

    public static MediaItem readMediaItemFromXMLDesc(String mediaId, InputStream xmlIntputStream, String sslCertificatesRootPath) throws Exception {

        Log.v(TAG, "readMediaItemFromXMLDesc");
        StreamingMediaItem mediaItem = null;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlIntputStream);
        doc.getDocumentElement().normalize();

        NodeList elements = doc.getElementsByTagName("Stream");
        int nb_elements = elements.getLength();

        //Old .op file format support
        if (nb_elements == 0) {
            elements = doc.getElementsByTagName("ContentAccessDescriptor");
            nb_elements = elements.getLength();
        }

        if (nb_elements == 0) {
            Log.e(TAG, "readMediaItemFromXMLDesc - No element in media description ");
            throw new Exception("readMediaItemFromXMLDesc - No element in media description");
        }

        StreamingMediaList mediaList = new StreamingMediaList(Utils.getMD5Hash(mediaId), mediaId);

        for (int i = 0 ; i < nb_elements ; i++) {
            Element curElement = (Element) elements.item(i);

            mediaItem = parseMediaItemFromXMLElement( curElement, mediaId, sslCertificatesRootPath);

            if(null != mediaItem) {
                mediaList.addItemToPlaylist(mediaItem);
            } else {
                Log.e(TAG, "readMediaItemFromXMLDesc - No tag URL in directData");
                throw new Exception("readMediaItemFromXMLDesc - No tag URL in directData");
            }
        }

        if (1 == nb_elements){
            return mediaItem;
        }
        else {
            mediaList.setPlaylistUri();
            return mediaList;
        }
    }
}
