Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generic srt socket options set/get. #4325

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/opam/liquidsoap-core-windows.opam
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ conflicts: [
"shine-windows" {< "0.2.0"}
"soundtouch-windows" {< "0.1.9"}
"speex-windows" {< "0.4.0"}
"srt-windows" {< "0.3.2"}
"srt-windows" {< "0.3.3"}
"ssl-windows" {< "0.5.2"}
"sdl-liquidsoap-windows" {< "2"}
"tsdl-image-windows" {< "0.3.2"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Bindings for posix sockets"
description:
"posix-socket provides the types and bindings of posix sockets APIs available on both unix and windows."
maintainer: ["[email protected]"]
authors: ["Romain Beauxis"]
license: "MIT"
homepage: "https://github.com/savonet/ocaml-posix"
bug-reports: "https://github.com/savonet/ocaml-posix/issues"
depends: [
"dune" {>= "2.9"}
"ocaml-windows"
"ctypes"
"ctypes-windows"
]
build: [
[
"dune"
"build"
"-p"
"posix-base"
"-x"
"windows"
"-j"
jobs
"@install"
]
]
dev-repo: "git+https://github.com/savonet/ocaml-posix.git"
url {
src: "https://github.com/savonet/ocaml-posix/archive/main.tar.gz"
}
33 changes: 33 additions & 0 deletions .github/opam/packages/posix-socket/posix-socket-windows.2.2.0/opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Bindings for posix sockets"
description:
"posix-socket provides the types and bindings of posix sockets APIs available on both unix and windows."
maintainer: ["[email protected]"]
authors: ["Romain Beauxis"]
license: "MIT"
homepage: "https://github.com/savonet/ocaml-posix"
bug-reports: "https://github.com/savonet/ocaml-posix/issues"
depends: [
"dune" {>= "2.9"}
"ocaml-windows"
"ctypes"
"ctypes-windows"
]
build: [
[
"dune"
"build"
"-p"
"posix-socket"
"-x"
"windows"
"-j"
jobs
"@install"
]
]
dev-repo: "git+https://github.com/savonet/ocaml-posix.git"
url {
src: "https://github.com/savonet/ocaml-posix/archive/main.tar.gz"
}
43 changes: 43 additions & 0 deletions .github/opam/packages/srt-windows/srt-windows.0.3.3/opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Binding for the Secure, Reliable, Transport protocol library"
description: """
Secure Reliable Transport (SRT) is an open source transport technology
that optimizes streaming performance across unpredictable networks, such
as the Internet.
This package provides OCaml bindings to the C implementation library.
"""
maintainer: ["The Savonet Team <[email protected]>"]
authors: ["The Savonet Team <[email protected]>"]
license: "GPL-2.0-only"
homepage: "https://github.com/savonet/ocaml-srt"
bug-reports: "https://github.com/savonet/ocaml-srt/issues"
depends: [
"conf-pkg-config" {build}
"dune" {> "2.0"}
"dune-configurator" {build}
"ctypes-foreign-windows"
"integers-windows"
"posix-socket-windows" {>= "2.2.0"}
"posix-socket"
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
"srt"
"-x"
"windows"
"-j"
jobs
"@install"
]
]
depexts: [
["libsrt"] {os-distribution = "mxe"}
]
url {
src: "https://github.com/savonet/ocaml-srt/archive/main.tar.gz"
}
12 changes: 10 additions & 2 deletions .github/scripts/build-win32.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ echo "::group::Installing deps"

eval "$(opam config env)"
opam repository set-url windows https://github.com/ocaml-cross/opam-cross-windows.git
opam update
opam install -y srt-windows.0.3.2
opam update windows

cd /tmp
rm -rf ocaml-posix
git clone https://github.com/savonet/ocaml-posix.git
cd ocaml-posix
opam pin -ny .
opam install -y posix-socket.2.2.0 posix-base.2.2.0

opam install -y srt-windows.0.3.3

echo "::endgroup::"

Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Changed:
option (#3906)
- Add full explicit support for `ipv4` vs. `ipv6` resolution in SRT inputs and outputs,
add global `settings.srt.prefer_address` and `settings.icecast.prefer_address` (#4317)
- Added generic SRT socket get/set API. Added new socket options, including `latency`
and `ipv6only`.

Fixed:

Expand Down
92 changes: 84 additions & 8 deletions src/core/builtins/builtins_srt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,37 @@ exception Done
exception Not_connected

module Socket_value = struct
let read_only_socket_options_specs = [("read_data", `Int Srt.rcvdata)]
let read_only_socket_options_specs = [("rcvdata", `Int Srt.rcvdata)]

let write_only_socket_options_specs =
[
("messageapi", `Bool Srt.messageapi);
("payloadsize", `Int Srt.payloadsize);
("conntimeo", `Int Srt.conntimeo);
("passphrase", `String Srt.passphrase);
("enforced_encryption", `Bool Srt.enforced_encryption);
]

let read_write_socket_options_specs =
[
("read_timeout", `Int Srt.rcvtimeo);
("write_timeout", `Int Srt.sndtimeo);
("rcvsyn", `Bool Srt.rcvsyn);
("sndsyn", `Bool Srt.sndsyn);
("rcvtimeout", `Int Srt.rcvtimeo);
("sndtimeout", `Int Srt.sndtimeo);
("reuseaddr", `Bool Srt.reuseaddr);
("rcvbuf", `Int Srt.rcvbuf);
("sndbuf", `Int Srt.sndbuf);
("udp_rcvbuf", `Int Srt.udp_rcvbuf);
("udp_sndbuf", `Int Srt.udp_sndbuf);
("streamid", `String Srt.streamid);
("pbkeylen", `Int Srt.pbkeylen);
("read_latency", `Int Srt.rcvlatency);
("ipv6only", `Bool Srt.ipv6only);
("rcvlatency", `Int Srt.rcvlatency);
("peerlatency", `Int Srt.peerlatency);
("latency", `Int Srt.latency);
]

let mk_socket_option name socket_opt =
let mk_read_socket_option name socket_opt =
let t =
match socket_opt with
| `Int _ -> Lang.int_t
Expand All @@ -61,12 +80,52 @@ module Socket_value = struct
let bt = Printexc.get_raw_backtrace () in
Lang.raise_as_runtime ~bt ~kind:"srt" exn) )

let mk_write_socket_option name socket_opt =
let t =
match socket_opt with
| `Int _ -> Lang.int_t
| `Bool _ -> Lang.bool_t
| `String _ -> Lang.string_t
in
( "set_" ^ name,
([], Lang.fun_t [(false, "", t)] Lang.unit_t),
"Set " ^ name ^ " option",
fun s ->
Lang.val_fun
[("", "", None)]
(fun p ->
let v = List.assoc "" p in
try
(match socket_opt with
| `Int socket_opt ->
Srt.setsockflag s socket_opt (Lang.to_int v)
| `Bool socket_opt ->
Srt.setsockflag s socket_opt (Lang.to_bool v)
| `String socket_opt ->
Srt.setsockflag s socket_opt (Lang.to_string v));
Lang.unit
with exn ->
let bt = Printexc.get_raw_backtrace () in
Lang.raise_as_runtime ~bt ~kind:"srt" exn) )

let socket_options_meths =
let read_meths =
List.fold_left
(fun cur (name, socket_opt) ->
mk_read_socket_option name socket_opt :: cur)
(List.fold_left
(fun cur (name, socket_opt) ->
mk_read_socket_option name socket_opt :: cur)
[] read_only_socket_options_specs)
read_write_socket_options_specs
in
List.fold_left
(fun cur (name, socket_opt) -> mk_socket_option name socket_opt :: cur)
(fun cur (name, socket_opt) ->
mk_write_socket_option name socket_opt :: cur)
(List.fold_left
(fun cur (name, socket_opt) -> mk_socket_option name socket_opt :: cur)
[] read_only_socket_options_specs)
(fun cur (name, socket_opt) ->
mk_write_socket_option name socket_opt :: cur)
read_meths write_only_socket_options_specs)
read_write_socket_options_specs

let stats_specs =
Expand Down Expand Up @@ -263,6 +322,10 @@ module Socket_value = struct
let meths =
socket_options_meths
@ [
( "id",
([], Lang.int_t),
"Socket ID",
fun s -> Lang.int (Srt.socket_id s) );
( "status",
([], Lang.fun_t [] Lang.string_t),
"Socket status",
Expand Down Expand Up @@ -327,9 +390,22 @@ module Socket_value = struct
(List.map (fun (n, _, fn) -> (n, fn stats)) stats_specs)) );
]

let base_t = t

let t =
Lang.method_t t (List.map (fun (lbl, t, descr, _) -> (lbl, t, descr)) meths)

let to_base_value = to_value

let to_value s =
Lang.meth (to_value s) (List.map (fun (lbl, _, _, m) -> (lbl, m s)) meths)
end

let srt = Lang.add_module "srt"

let clock =
Lang.add_builtin "socket" ~base:srt ~category:`Liquidsoap
~descr:"Decorate a srt socket with all its methods."
[("", Socket_value.base_t, None, None)]
Socket_value.t
(fun p -> Socket_value.(to_value (of_value (List.assoc "" p))))
Loading
Loading