<commit_before>// ===================================================================================================
// Copyright (C) 2017 Kaltura Inc.
//
// Licensed under the AGPLv3 license, unless a different license for a 
// particular library is specified in the applicable library path.
//
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
// ===================================================================================================

import Foundation
import AVFoundation

typealias ReadyCallback = (Error?, AVURLAsset?) -> Void
typealias RefreshCallback = (Bool) -> Void

class WidevineClassicAssetHandler: RefreshableAssetHandler {
    
    var readyCallback: ReadyCallback?
    
    static let sourceFilter = { (_ src: MediaSource) -> Bool in
        
        // FIXME: extension is not the best criteria here, use format when that's available.
        guard let ext = src.contentUrl?.pathExtension else {
            return false
        }
        
        // DRM is not supported on simulators
        if src.drmData != nil && TARGET_OS_SIMULATOR != 0 {
            PKLog.warning("DRM is not supported on simulators")
            return false
        }
        
        // Only support wvm files
        if ext == "wvm" {
            return true
        }
        
        return false
    }
    
    func shouldRefreshAsset(mediaSource: MediaSource, refreshCallback: @escaping RefreshCallback) {
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            refreshCallback(false)
            return
        }
        
        guard (mediaSource.drmData?.first?.licenseUri) != nil else {
            PKLog.error("Missing licenseUri")
            refreshCallback(false)
            return
        }
        
        WidevineClassicHelper.shouldRefreshAsset(contentUrl.absoluteString) { (shouldRefresh) in
            if shouldRefresh {
                refreshCallback(true)
            }
        }
    }
    
    func refreshAsset(mediaSource: MediaSource) {
        
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            return
        }
        
        guard let licenseUri = mediaSource.drmData?.first?.licenseUri else {
            PKLog.error("Missing licenseUri")
            return
        }
        
        WidevineClassicHelper.playAsset(contentUrl.absoluteString, withLicenseUri: licenseUri.absoluteString) {  (_ playbackURL:String?)->Void  in
            if playbackURL == "" {
                PKLog.error("Invalid media: no url")
                self.readyCallback?(AssetError.invalidContentUrl(nil), nil)
                return
                
            }
            
            guard let playbackURL = playbackURL else {
                PKLog.error("Invalid media: no url")
                self.readyCallback?(AssetError.invalidContentUrl(nil), nil)
                return
            }
            
            DispatchQueue.main.async {
                PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                self.readyCallback?(nil, AVURLAsset(url: URL(string: playbackURL)!))
            }
        }
    }
    
    internal func buildAsset(mediaSource: MediaSource, readyCallback: @escaping ReadyCallback) {
        self.readyCallback = readyCallback
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            readyCallback(AssetError.invalidContentUrl(nil), nil)
            return
        }
        
        if let localSource = mediaSource as? LocalMediaSource {
            PKLog.debug("Creating local asset")

            WidevineClassicHelper.playLocalAsset(localSource.contentUrl?.absoluteString) { (_ playbackURL:String?) in
                guard let playbackURL = playbackURL else {
                    PKLog.error("Invalid media: no url")
                    readyCallback(AssetError.invalidContentUrl(nil), nil)
                    return
                }
                
                DispatchQueue.main.async {
                    PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                    readyCallback(nil, AVURLAsset(url: URL(string: playbackURL)!))
                }
            }
            
            return
        }
        
        guard let drmData = mediaSource.drmData?.first else {
            PKLog.error("Invalid drm data")
            readyCallback(AssetError.noPlayableSources, nil)
            return
        }
        
        guard let licenseUri = drmData.licenseUri else {
            PKLog.error("Missing licenseUri")
            readyCallback(AssetError.noLicenseUri, nil)
            return
        }
        
        PKLog.trace("playAsset:: url: \(contentUrl.absoluteString), uri: \(licenseUri.absoluteString)")
       
        WidevineClassicHelper.playAsset(contentUrl.absoluteString, withLicenseUri: licenseUri.absoluteString) {  (_ playbackURL:String?)->Void  in
            
            guard let playbackURL = playbackURL else {
                PKLog.error("Invalid media: no url")
                readyCallback(AssetError.invalidContentUrl(nil), nil)
                return
            }
            
            DispatchQueue.main.async {
                PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                readyCallback(nil, AVURLAsset(url: URL(string: playbackURL)!))
            }
        }
    }
    
    required init() {}
}


<commit_msg>FEM-1603 #comment fix for offline mode (#212)<commit_after>// ===================================================================================================
// Copyright (C) 2017 Kaltura Inc.
//
// Licensed under the AGPLv3 license, unless a different license for a 
// particular library is specified in the applicable library path.
//
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
// ===================================================================================================

import Foundation
import AVFoundation

typealias ReadyCallback = (Error?, AVURLAsset?) -> Void
typealias RefreshCallback = (Bool) -> Void

class WidevineClassicAssetHandler: RefreshableAssetHandler {
    
    var readyCallback: ReadyCallback?
    
    static let sourceFilter = { (_ src: MediaSource) -> Bool in
        
        // FIXME: extension is not the best criteria here, use format when that's available.
        guard let ext = src.contentUrl?.pathExtension else {
            return false
        }
        
        // DRM is not supported on simulators
        if src.drmData != nil && TARGET_OS_SIMULATOR != 0 {
            PKLog.warning("DRM is not supported on simulators")
            return false
        }
        
        // Only support wvm files
        if ext == "wvm" {
            return true
        }
        
        return false
    }
    
    func shouldRefreshAsset(mediaSource: MediaSource, refreshCallback: @escaping RefreshCallback) {
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            refreshCallback(false)
            return
        }
        
        guard (mediaSource.drmData?.first?.licenseUri) != nil else {
            PKLog.error("Missing licenseUri")
            refreshCallback(false)
            return
        }
        
        WidevineClassicHelper.shouldRefreshAsset(contentUrl.absoluteString) { (shouldRefresh) in
            if shouldRefresh {
                refreshCallback(true)
            }
        }
    }
    
    func refreshAsset(mediaSource: MediaSource) {
        
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            return
        }
        
        // WidevineClassicHandler.playAsset here will cause the stream to be closed and reopened (WV_Stop, WV_Play).
        // Since we already have LicenseUri no reason to send it again.
        WidevineClassicHelper.playAsset(contentUrl.absoluteString, withLicenseUri: nil) {  (_ playbackURL: String?) -> Void  in
            if playbackURL == "" {
                PKLog.error("Invalid media: no url")
                self.readyCallback?(AssetError.invalidContentUrl(nil), nil)
                return
            }
            
            guard let playbackURL = playbackURL else {
                PKLog.error("Invalid media: no url")
                self.readyCallback?(AssetError.invalidContentUrl(nil), nil)
                return
            }
            
            DispatchQueue.main.async {
                PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                self.readyCallback?(nil, AVURLAsset(url: URL(string: playbackURL)!))
            }
        }
    }
    
    internal func buildAsset(mediaSource: MediaSource, readyCallback: @escaping ReadyCallback) {
        self.readyCallback = readyCallback
        guard let contentUrl = mediaSource.contentUrl else {
            PKLog.error("Invalid media: no url")
            readyCallback(AssetError.invalidContentUrl(nil), nil)
            return
        }
        
        if let localSource = mediaSource as? LocalMediaSource {
            PKLog.debug("Creating local asset")

            WidevineClassicHelper.playLocalAsset(localSource.contentUrl?.absoluteString) { (_ playbackURL:String?) in
                guard let playbackURL = playbackURL else {
                    PKLog.error("Invalid media: no url")
                    readyCallback(AssetError.invalidContentUrl(nil), nil)
                    return
                }
                
                DispatchQueue.main.async {
                    PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                    readyCallback(nil, AVURLAsset(url: URL(string: playbackURL)!))
                }
            }
            
            return
        }
        
        guard let drmData = mediaSource.drmData?.first else {
            PKLog.error("Invalid drm data")
            readyCallback(AssetError.noPlayableSources, nil)
            return
        }
        
        guard let licenseUri = drmData.licenseUri else {
            PKLog.error("Missing licenseUri")
            readyCallback(AssetError.noLicenseUri, nil)
            return
        }
        
        PKLog.trace("playAsset:: url: \(contentUrl.absoluteString), uri: \(licenseUri.absoluteString)")
       
        WidevineClassicHelper.playAsset(contentUrl.absoluteString, withLicenseUri: licenseUri.absoluteString) {  (_ playbackURL:String?)->Void  in
            
            guard let playbackURL = playbackURL else {
                PKLog.error("Invalid media: no url")
                readyCallback(AssetError.invalidContentUrl(nil), nil)
                return
            }
            
            DispatchQueue.main.async {
                PKLog.debug("widevine classic:: callback url:\(playbackURL)")
                readyCallback(nil, AVURLAsset(url: URL(string: playbackURL)!))
            }
        }
    }
    
    required init() {}
}



{'fraction_non_alphanumeric': 0.11609712230215827, 'fraction_numerical': 0.002068345323741007, 'mean_word_length': 1.8706763035622096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5313392', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2654, 'n_words': 640}