Skip to content

Commit

Permalink
Issue KN-1096 feat: Azure MediaKind(MK.IO) service enabled to generat…
Browse files Browse the repository at this point in the history
…e streaming URL
  • Loading branch information
pallakartheekreddy committed Jun 13, 2024
1 parent 5470dbe commit 5209f06
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ azure {
}
}

azure_mediakind{
project_name="subcitionname"
auth_token="authToken"
account_name="media servcie accountname"
api {
endpoint="https://api.mk.io/api"
}
transform {
default = "media_transform_default"
}
stream {
base_url = "https://ep-default-mkservicepoc.japaneast.streaming.mediakind.com"
endpoint_name = "default"
protocol = "Hls"
policy_name = "Predefined_ClearStreamingOnly"
}
}

azure_tenant="tenant"
azure_subscription_id="subscription id"
azure_account_name="account name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.sunbird.job.videostream.helpers

object AzureRequestBody {

val create_asset = " {\"properties\": {\"description\": \"assetDescription\",\"alternateId\" : \"assetId\"}}"
val create_asset = " {\"properties\": {\"storageAccountName\": \"assetStorageAccountName\",\"description\": \"assetDescription\",\"alternateId\" : \"assetId\"}}"
val submit_job = "{\"properties\": {\"input\": {\"@odata.type\": \"#Microsoft.Media.JobInputHttp\",\"baseUri\": \"baseInputUrl\",\"files\": [\"inputVideoFile\"]},\"outputs\": [{\"@odata.type\": \"#Microsoft.Media.JobOutputAsset\",\"assetName\": \"assetId\"}]}}"
val create_stream_locator="{\"properties\":{\"assetName\": \"assetId\",\"streamingPolicyName\": \"policyName\"}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import scala.collection.immutable.HashMap
abstract class AzureMediaService extends IMediaService {

private var API_ACCESS_TOKEN: String = ""

private def getToken()(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): String = {
val tenant = config.getSystemConfig("azure.tenant")
val clientKey = config.getSystemConfig("azure.token.client_key")
Expand Down Expand Up @@ -51,9 +50,11 @@ abstract class AzureMediaService extends IMediaService {
}

protected def createAsset(assetId: String, jobId: String)(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): MediaResponse = {
val accountName: String = config.getConfig("azure_mediakind.account_name")
val url = getApiUrl("asset").replace("assetId", assetId)
val requestBody = AzureRequestBody.create_asset.replace("assetId", assetId)
.replace("assetDescription", "Output Asset for " + jobId)
.replace("assetStorageAccountName", accountName)
val response:MediaResponse = Response.getResponse(httpUtil.put(url, requestBody, getDefaultHeader()))
if(response.responseCode == "OK"){
response
Expand All @@ -64,7 +65,7 @@ abstract class AzureMediaService extends IMediaService {

protected def createStreamingLocator(streamingLocatorName: String, assetName: String)(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): MediaResponse = {
val url = getApiUrl("stream_locator").replace("streamingLocatorName", streamingLocatorName)
val streamingPolicyName = config.getConfig("azure.stream.policy_name")
val streamingPolicyName = config.getConfig("azure_mediakind.stream.policy_name")
val reqBody = AzureRequestBody.create_stream_locator.replace("assetId", assetName).replace("policyName", streamingPolicyName)
Response.getResponse(httpUtil.put(url, reqBody, getDefaultHeader()))
}
Expand All @@ -90,42 +91,34 @@ abstract class AzureMediaService extends IMediaService {
}

protected def getApiUrl(apiName: String)(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): String = {
val subscriptionId: String = config.getSystemConfig("azure.subscription_id")
val resourceGroupName: String = config.getSystemConfig("azure.resource_group_name")
val accountName: String = config.getSystemConfig("azure.account_name")
val apiVersion: String = config.getConfig("azure.api.version")
val transformName: String = config.getConfig("azure.transform.default")
val transformName: String = config.getConfig("azure_mediakind.transform.default")
val projectName:String = config.getConfig("azure_mediakind.project_name")

val baseUrl: String = new StringBuilder().append(config.getConfig("azure.api.endpoint")+"/subscriptions/")
.append(subscriptionId)
.append("/resourceGroups/")
.append(resourceGroupName)
.append("/providers/Microsoft.Media/mediaServices/")
.append(accountName).mkString
val baseUrl: String = new StringBuilder().append(config.getConfig("azure_mediakind.api.endpoint")+"/ams/")
.append(projectName).mkString


apiName.toLowerCase() match {
case "asset" => baseUrl + "/assets/assetId?api-version=" + apiVersion
case "job" => baseUrl + "/transforms/" + transformName + "/jobs/jobIdentifier?api-version=" + apiVersion
case "stream_locator" => baseUrl + "/streamingLocators/streamingLocatorName?api-version=" + apiVersion
case "list_paths" => baseUrl + "/streamingLocators/streamingLocatorName/listPaths?api-version=" + apiVersion
case "asset" => baseUrl + "/assets/assetId"
case "job" => baseUrl + "/transforms/" + transformName + "/jobs/jobIdentifier"
case "stream_locator" => baseUrl + "/streamingLocators/streamingLocatorName"
case "list_paths" => baseUrl + "/streamingLocators/streamingLocatorName/listPaths"
case _ => throw new MediaServiceException("ERR_INVALID_API_NAME", "Please Provide Valid Media Service API Name")
}
}

protected def getDefaultHeader()(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): Map[String, String] = {
val accessToken = if (StringUtils.isNotBlank(API_ACCESS_TOKEN)) API_ACCESS_TOKEN else getToken()
val authToken = "Bearer " + accessToken
val authToken = config.getConfig("azure_mediakind.auth_token")
HashMap[String, String](
"Content-Type" -> "application/json",
"Accept" -> "application/json",
"Authorization" -> authToken
"content-type" -> "application/json",
"accept" -> "application/json",
"x-mkio-token" -> authToken
)
}

protected def prepareStreamingUrl(streamLocatorName: String, jobId: String)(implicit config: VideoStreamGeneratorConfig, httpUtil: HttpUtil): Map[String, AnyRef] = {
val streamType = config.getConfig("azure.stream.protocol")
val streamHost = config.getConfig("azure.stream.base_url")
val streamType = config.getConfig("azure_mediakind.stream.protocol")
val streamHost = config.getConfig("azure_mediakind.stream.base_url")
var url = ""
val listPathResponse = getStreamUrls(streamLocatorName)
if (listPathResponse.responseCode.equalsIgnoreCase("OK")) {
Expand All @@ -146,4 +139,4 @@ abstract class AzureMediaService extends IMediaService {
HashMap[String, AnyRef]("streamUrl" -> streamUrl)
}
}
}
}

0 comments on commit 5209f06

Please sign in to comment.