<commit_before><?php

/**
 * Enable serving live conversion profile to the Wowza servers as XML
 * @service liveConversionProfile
 * @package plugins.wowza
 * @subpackage api.services
 */
class LiveConversionProfileService extends KalturaBaseService
{
	const MINIMAL_DEFAULT_FRAME_RATE = 12.5;
	const WIDTH = 'width';
	const HEIGHT = 'height';
	const MATCH_SOURCE = 'match-source';
	const KILO = 1000;
	const DEFAULT_NAME_GROUP = 'all';
	
	/* (non-PHPdoc)
	 * @see KalturaBaseService::initService()
	 */

	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);
		
		$this->applyPartnerFilterForClass('conversionProfile2');
		if (kCurrentContext::getCurrentPartnerId() != Partner::MEDIA_SERVER_PARTNER_ID)
			$this->applyPartnerFilterForClass('assetParams');
	}

	/**
	 * Serve XML rendition of the Kaltura Live Transcoding Profile usable by the Wowza transcoding add-on
	 *
	 * @action serve
	 * @param string $streamName the id of the live entry with it's stream suffix
	 * @param string $hostname the media server host name
	 * @param string $extraParams is a json object containing the stream parameters transfered by the encoder
	 * @return file
	 *
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws WowzaErrors::INVALID_STREAM_NAME
	 * @throws KalturaErrors::INGEST_NOT_FOUND_IN_CONVERSION_PROFILE
	 */
	public function serveAction($streamName, $hostname = null, $extraParams = null)
	{
		$streamParametersArray = array(
			'streamname' => $streamName,
			'hostname' => $hostname,
			'audiodatarate' => 0,
			'videodatarate' => 0,
			'width' => 0,
			'height' => 0,
			'framerate' => 0,
			'videocodecidstring' => "",
			'audiocodecidstring' => ""
		);
		
		$matches = null;
		if(!preg_match('/^(\d_.{8})_(\d+)$/', $streamName, $matches))
			throw new KalturaAPIException(WowzaErrors::INVALID_STREAM_NAME, $streamName);
		
		$entryId = $matches[1];
		$suffix = $matches[2];
		
		$entry = null;
		if (!kCurrentContext::$ks)
		{
			kEntitlementUtils::initEntitlementEnforcement(null, false);
			$entry = kCurrentContext::initPartnerByEntryId($entryId);
			
			if (!$entry || $entry->getStatus() == entryStatus::DELETED)
				throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
			
			// enforce entitlement
			$this->setPartnerFilters(kCurrentContext::getCurrentPartnerId());
		}
		else
		{
			$entry = entryPeer::retrieveByPK($entryId);
		}
		
		// Check if to perform smart transcoding for partner
		$isSmartTranscodingDisabled = PermissionPeer::isValidForPartner(PermissionName::FEATURE_KALTURA_LIVE_DISABLE_SMART_TRANSCODING, kCurrentContext::getCurrentPartnerId());
		if ($extraParams !== "" && $this->isValidJson($extraParams) && !$isSmartTranscodingDisabled)
		{
			$streamParametersArray = array_merge($streamParametersArray, json_decode($extraParams, true));
		}
		
		if (!$entry || $entry->getType() != KalturaEntryType::LIVE_STREAM || !in_array($entry->getSource(), array(KalturaSourceType::LIVE_STREAM, KalturaSourceType::LIVE_STREAM_ONTEXTDATA_CAPTIONS)))
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		
		$mediaServer = null;
		if($hostname)
			$mediaServer = ServerNodePeer::retrieveActiveMediaServerNode($hostname);
		
		$conversionProfileId = $entry->getConversionProfileId();
		$liveParams = assetParamsPeer::retrieveByProfile($conversionProfileId);<commit_msg>Move check for media partner in action<commit_after><?php

/**
 * Enable serving live conversion profile to the Wowza servers as XML
 * @service liveConversionProfile
 * @package plugins.wowza
 * @subpackage api.services
 */
class LiveConversionProfileService extends KalturaBaseService
{
	const MINIMAL_DEFAULT_FRAME_RATE = 12.5;
	const WIDTH = 'width';
	const HEIGHT = 'height';
	const MATCH_SOURCE = 'match-source';
	const KILO = 1000;
	const DEFAULT_NAME_GROUP = 'all';
	
	/* (non-PHPdoc)
	 * @see KalturaBaseService::initService()
	 */

	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);
		
		$this->applyPartnerFilterForClass('conversionProfile2');
		$this->applyPartnerFilterForClass('assetParams');
	}

	/**
	 * Serve XML rendition of the Kaltura Live Transcoding Profile usable by the Wowza transcoding add-on
	 *
	 * @action serve
	 * @param string $streamName the id of the live entry with it's stream suffix
	 * @param string $hostname the media server host name
	 * @param string $extraParams is a json object containing the stream parameters transfered by the encoder
	 * @return file
	 *
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws WowzaErrors::INVALID_STREAM_NAME
	 * @throws KalturaErrors::INGEST_NOT_FOUND_IN_CONVERSION_PROFILE
	 */
	public function serveAction($streamName, $hostname = null, $extraParams = null)
	{
		$streamParametersArray = array(
			'streamname' => $streamName,
			'hostname' => $hostname,
			'audiodatarate' => 0,
			'videodatarate' => 0,
			'width' => 0,
			'height' => 0,
			'framerate' => 0,
			'videocodecidstring' => "",
			'audiocodecidstring' => ""
		);
		
		$matches = null;
		if(!preg_match('/^(\d_.{8})_(\d+)$/', $streamName, $matches))
			throw new KalturaAPIException(WowzaErrors::INVALID_STREAM_NAME, $streamName);
		
		$entryId = $matches[1];
		$suffix = $matches[2];
		
		$entry = null;
		if (!kCurrentContext::$ks || (kCurrentContext::getCurrentPartnerId() == Partner::MEDIA_SERVER_PARTNER_ID))
		{
			kEntitlementUtils::initEntitlementEnforcement(null, false);
			$entry = kCurrentContext::initPartnerByEntryId($entryId);
			
			if (!$entry || $entry->getStatus() == entryStatus::DELETED)
				throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
			
			// enforce entitlement
			$this->setPartnerFilters(kCurrentContext::getCurrentPartnerId());
		}
		else
		{
			$entry = entryPeer::retrieveByPK($entryId);
		}
		
		// Check if to perform smart transcoding for partner
		$isSmartTranscodingDisabled = PermissionPeer::isValidForPartner(PermissionName::FEATURE_KALTURA_LIVE_DISABLE_SMART_TRANSCODING, kCurrentContext::getCurrentPartnerId());
		if ($extraParams !== "" && $this->isValidJson($extraParams) && !$isSmartTranscodingDisabled)
		{
			$streamParametersArray = array_merge($streamParametersArray, json_decode($extraParams, true));
		}
		
		if (!$entry || $entry->getType() != KalturaEntryType::LIVE_STREAM || !in_array($entry->getSource(), array(KalturaSourceType::LIVE_STREAM, KalturaSourceType::LIVE_STREAM_ONTEXTDATA_CAPTIONS)))
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		
		$mediaServer = null;
		if($hostname)
			$mediaServer = ServerNodePeer::retrieveActiveMediaServerNode($hostname);
		
		$conversionProfileId = $entry->getConversionProfileId();
		$liveParams = assetParamsPeer::retrieveByProfile($conversionProfileId);
{'fraction_non_alphanumeric': 0.12692867540029112, 'fraction_numerical': 0.004657933042212519, 'mean_word_length': 5.581417624521073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1888517', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2161, 'n_words': 436}