Skip to content

Commit

Permalink
[2/2] handlers - SBC pod launcher JSON based configuration parser
Browse files Browse the repository at this point in the history
handlers - SBC pod launcher JSON based configuration parser
where functions that handled the key/value pass in the json config
file are implemented. for now, they're simple functions but with
more corner cases will be important to isolate and expand the handlers
table.

Signed-off-by: Aly, Walid <[email protected]>
  • Loading branch information
walidbarakat committed Jan 10, 2025
1 parent 3f8160a commit c24ffc0
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions launcher/config_parser/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"errors"
"fmt"
)

// HandlerFunc is a function type that handles a key-value pair and returns a formatted string
type HandlerFunc func(key string, value interface{}) (string, error)

// handlers is a map where the key is a string and the value is a HandlerFunc
var handlers = map[string]HandlerFunc{
// per stream options
"codec": handleCodecKeyValue,
"filter": handleKeyValue,
"format": handleFormatKeyValue,

// video options
"height": handleKeyValue,
"width": handleKeyValue,

// common options for MCM, MTL
"payload_type": handleKeyValue,
"video_size": handleKeyValue,
"pixel_format": handleKeyValue,

// MCM Muxer/Demuxer
"ip_addr": handleKeyValue,
"port": handleKeyValue,
"protocol_type": handleKeyValue,
"frame_rate": handleFrameRate,
"socket_name": handleKeyValue,
"interface_id": handleKeyValue,

// MTL Device Arguments
"p_port": handleKeyValue,
"p_sip": handleKeyValue,
"dma_dev": handleKeyValue,

// Tx/Rx Port Encoding/Decoding Arguments
"p_tx_ip": handleKeyValue,
"p_rx_ip": handleKeyValue,
"udp_port": handleKeyValue,

// MTL st20p Muxer/Demuxer
"fb_cnt": handleKeyValue,
"pix_fmt": handleKeyValue,
"fps": handleKeyValue,
"timeout_s": handleKeyValue,

// MTL st22p Muxer/Demuxer
"bpp": handleKeyValue,
"codec_thread_cnt": handleKeyValue,
"st22_codec": handleKeyValue,

// MTL st30p Muxer/Demuxer
"at": handleKeyValue,
"ar": handleKeyValue,
"ac": handleKeyValue,
"pcm_fmt": handleKeyValue,

// JPEG XS encoder/decoder
"decomp_v": handleKeyValue,
"decomp_h": handleKeyValue,
"threads": handleKeyValue,
"slice_height": handleKeyValue,
"quantization": handleKeyValue,
"coding-signs": handleKeyValue,
"coding-sigf": handleKeyValue,
"coding-vpred": handleKeyValue,
}

// handleFormatKeyValue handles the key "format", the value passed to ffmpeg as -f <format value>
func handleFormatKeyValue(key string, value interface{}) (string, error) {
return handlePrefixedKeyValue("-f", key, value)
}

// handleCodecKeyValue handles the key "codec", the value passed to ffmpeg as -c <codec value>
func handleCodecKeyValue(key string, value interface{}) (string, error) {
return handlePrefixedKeyValue("-c", key, value)
}

// handlePrefixedKeyValue handles the key and value with a prefix, prints them, and returns a formatted string
func handlePrefixedKeyValue(prefix, key string, value interface{}) (string, error) {
if value == nil {
return "", fmt.Errorf("key %s has no value", key)
}
valueStr := fmt.Sprintf("%v", value)
if valueStr == "" {
return "", fmt.Errorf("key %s has an empty value", key)
}
fmt.Printf("%s: %v\n", prefix, value)
return fmt.Sprintf("%s %v", prefix, value), nil
}

// handleKeyValue handles the key and value, prints them, and returns a formatted string
func handleKeyValue(key string, value interface{}) (string, error) {
return handlePrefixedKeyValue("-"+key, key, value)
}

// handleFrameRate handles the frame_rate key by calculating the frame rate and returning a formatted string
func handleFrameRate(key string, value interface{}) (string, error) {
if frameRateMap, ok := value.(map[string]interface{}); ok {
numerator, numOk := frameRateMap["numerator"].(float64)
denominator, denOk := frameRateMap["denominator"].(float64)
if numOk && denOk && denominator != 0 {
frameRate := int(numerator) / int(denominator)
fmt.Printf("%s: %d\n", key, frameRate)
return fmt.Sprintf("-%s %d", key, frameRate), nil
}
return "", errors.New("invalid frame_rate: numerator or denominator missing or denominator is zero")
}
return "", errors.New("invalid frame_rate format")
}

0 comments on commit c24ffc0

Please sign in to comment.