Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Use AVAssetWriter for encoding on macos (#160)

Brendan Allan67a0d3e80d20
diff --git a/Cargo.lock b/Cargo.lock
index a63ba98e6..10c4f59a1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -139,6 +139,55 @@ dependencies = [
"winapi",
]
+[[package]]
+name = "anstream"
+version = "0.6.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
+dependencies = [
+ "anstyle",
+ "anstyle-parse",
+ "anstyle-query",
+ "anstyle-wincon",
+ "colorchoice",
+ "is_terminal_polyfill",
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle"
+version = "1.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
+
+[[package]]
+name = "anstyle-parse"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle-query"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
+dependencies = [
+ "windows-sys 0.59.0",
+]
+
+[[package]]
+name = "anstyle-wincon"
+version = "3.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
+dependencies = [
+ "anstyle",
+ "windows-sys 0.59.0",
+]
+
[[package]]
name = "anyhow"
version = "1.0.86"
@@ -162,7 +211,7 @@ checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -307,7 +356,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -342,7 +391,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -508,7 +557,7 @@ dependencies = [
"bitflags 1.3.2",
"cexpr 0.4.0",
"clang-sys",
- "clap",
+ "clap 2.34.0",
"env_logger",
"lazy_static",
"lazycell",
@@ -540,7 +589,7 @@ dependencies = [
"regex",
"rustc-hash 1.1.0",
"shlex 1.3.0",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -558,7 +607,7 @@ dependencies = [
"regex",
"rustc-hash 1.1.0",
"shlex 1.3.0",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -690,7 +739,7 @@ checksum = "0cc8b54b395f2fcfbb3d90c47b01c7f444d94d05bdeb775811dec868ac3bbc26"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -800,6 +849,7 @@ dependencies = [
"cap-flags",
"cap-gpu-converters",
"cap-project",
+ "cidre",
"cocoa 0.26.0",
"core-foundation 0.10.0",
"core-graphics 0.24.0",
@@ -818,6 +868,7 @@ dependencies = [
"objc2-foundation",
"ringbuf",
"scap",
+ "screencapturekit",
"serde",
"specta",
"tempfile",
@@ -996,6 +1047,21 @@ dependencies = [
"windows-targets 0.52.6",
]
+[[package]]
+name = "cidre"
+version = "0.4.0"
+source = "git+https://github.com/yury/cidre?rev=1e008bec49a0f97aeaaea6130a0ba20fe00aa03b#1e008bec49a0f97aeaaea6130a0ba20fe00aa03b"
+dependencies = [
+ "cidre-macros",
+ "parking_lot",
+ "tokio",
+]
+
+[[package]]
+name = "cidre-macros"
+version = "0.1.0"
+source = "git+https://github.com/yury/cidre?rev=1e008bec49a0f97aeaaea6130a0ba20fe00aa03b#1e008bec49a0f97aeaaea6130a0ba20fe00aa03b"
+
[[package]]
name = "clang-sys"
version = "1.8.1"
@@ -1022,6 +1088,46 @@ dependencies = [
"vec_map",
]
+[[package]]
+name = "clap"
+version = "4.5.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
+dependencies = [
+ "clap_builder",
+ "clap_derive",
+]
+
+[[package]]
+name = "clap_builder"
+version = "4.5.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
+dependencies = [
+ "anstream",
+ "anstyle",
+ "clap_lex",
+ "strsim 0.11.1",
+]
+
+[[package]]
+name = "clap_derive"
+version = "4.5.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
+dependencies = [
+ "heck 0.5.0",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.87",
+]
+
+[[package]]
+name = "clap_lex"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
+
[[package]]
name = "claxon"
version = "0.4.3"
@@ -1119,6 +1225,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
+[[package]]
+name = "colorchoice"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
+
[[package]]
name = "com"
version = "0.6.0"
@@ -1518,7 +1630,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
dependencies = [
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1528,7 +1640,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f"
dependencies = [
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
+]
+
+[[package]]
+name = "ctrlc"
+version = "3.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
+dependencies = [
+ "nix 0.29.0",
+ "windows-sys 0.59.0",
]
[[package]]
@@ -1563,7 +1685,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim 0.11.1",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1574,7 +1696,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1635,7 +1757,7 @@ checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1648,7 +1770,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustc_version",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1717,7 +1839,6 @@ dependencies = [
"uuid",
"wgpu",
"windows 0.52.0",
- "windows-capture",
]
[[package]]
@@ -1801,7 +1922,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1833,7 +1954,7 @@ checksum = "f2b99bf03862d7f545ebc28ddd33a665b50865f4dfd84031a393823879bd4c54"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -1986,7 +2107,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -2249,7 +2370,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -2373,7 +2494,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -2651,7 +2772,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -2820,7 +2941,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -3248,7 +3369,7 @@ checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -3276,6 +3397,12 @@ dependencies = [
"once_cell",
]
+[[package]]
+name = "is_terminal_polyfill"
+version = "1.70.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
+
[[package]]
name = "itertools"
version = "0.12.1"
@@ -4193,7 +4320,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -4254,7 +4381,7 @@ dependencies = [
"proc-macro-crate 2.0.2",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -4596,7 +4723,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -4846,7 +4973,7 @@ dependencies = [
"phf_shared 0.11.2",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -4893,7 +5020,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5102,7 +5229,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8021cf59c8ec9c432cfc2526ac6b8aa508ecaf29cd415f271b8406c1b851c3fd"
dependencies = [
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5687,8 +5814,8 @@ dependencies = [
[[package]]
name = "scap"
-version = "0.0.6"
-source = "git+https://github.com/CapSoftware/scap?rev=4d6be030ba2b0cea565ccaee28b1999f25b8dd5d#4d6be030ba2b0cea565ccaee28b1999f25b8dd5d"
+version = "0.0.7"
+source = "git+https://github.com/CapSoftware/scap?rev=b1e140a3fe90#b1e140a3fe905c19b845dfea66b3b1aea02f0472"
dependencies = [
"cocoa 0.25.0",
"core-graphics-helmer-fork",
@@ -5700,7 +5827,7 @@ dependencies = [
"screencapturekit-sys",
"sysinfo",
"tao-core-video-sys",
- "windows 0.52.0",
+ "windows 0.58.0",
"windows-capture",
]
@@ -5737,7 +5864,7 @@ dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5855,7 +5982,7 @@ checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5866,7 +5993,7 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5899,7 +6026,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -5950,7 +6077,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -6162,7 +6289,7 @@ dependencies = [
"Inflector",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -6333,9 +6460,9 @@ dependencies = [
[[package]]
name = "syn"
-version = "2.0.85"
+version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56"
+checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [
"proc-macro2",
"quote",
@@ -6474,7 +6601,7 @@ checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -6587,7 +6714,7 @@ dependencies = [
"serde",
"serde_json",
"sha2",
- "syn 2.0.85",
+ "syn 2.0.87",
"tauri-utils",
"thiserror",
"time",
@@ -6605,7 +6732,7 @@ dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
"tauri-codegen",
"tauri-utils",
]
@@ -6934,7 +7061,7 @@ dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -7044,22 +7171,22 @@ checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"
[[package]]
name = "thiserror"
-version = "1.0.63"
+version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
+checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
-version = "1.0.63"
+version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
+checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -7145,7 +7272,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -7316,7 +7443,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -7534,6 +7661,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
+[[package]]
+name = "utf8parse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+
[[package]]
name = "uuid"
version = "1.10.0"
@@ -7672,7 +7805,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
"wasm-bindgen-shared",
]
@@ -7706,7 +7839,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@@ -7875,7 +8008,7 @@ checksum = "1d228f15bba3b9d56dde8bddbee66fa24545bd17b48d5128ccf4a8742b18e431"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -8136,14 +8269,16 @@ dependencies = [
[[package]]
name = "windows-capture"
-version = "1.2.0"
+version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "15e2f94b43842205eb84814f505badade792e7b8cd132e436636ccbf7baa08fc"
+checksum = "308113a004a94ea5fb83dc7d19e2c66050879b6767f1eb10231af2e77b66475d"
dependencies = [
+ "clap 4.5.20",
+ "ctrlc",
"parking_lot",
"rayon",
"thiserror",
- "windows 0.56.0",
+ "windows 0.58.0",
]
[[package]]
@@ -8207,7 +8342,7 @@ checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -8218,7 +8353,7 @@ checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -8229,7 +8364,7 @@ checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -8240,7 +8375,7 @@ checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
@@ -8705,7 +8840,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.85",
+ "syn 2.0.87",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 580dd7d74..0bf66bc19 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,7 +20,7 @@ tokio = { version = "1.39.3", features = [
] }
tauri = { version = "2.0.0" }
specta = { version = "=2.0.0-rc.20" }
-scap = { git = "https://github.com/CapSoftware/scap", rev = "4d6be030ba2b0cea565ccaee28b1999f25b8dd5d" }
+scap = { git = "https://github.com/CapSoftware/scap", rev = "b1e140a3fe90" }
nokhwa = { git = "https://github.com/CapSoftware/nokhwa", rev = "c5c7e2298764", features = [
"input-native",
"serialize",
diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml
index 5be87f7c5..31b11e587 100644
--- a/apps/desktop/src-tauri/Cargo.toml
+++ b/apps/desktop/src-tauri/Cargo.toml
@@ -96,9 +96,6 @@ windows = { version = "0.52.0", features = [
"Win32_UI_WindowsAndMessaging",
"Win32_Graphics_Gdi",
] }
-# Lock the version of windows-capture used by scap@691bd88798d3
-# TODO: Remove this once scap uses the latest version of `windows` and `windows-capture`
-windows-capture = "=1.2.0"
[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", features = ["fs"] }
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index cbad0e821..b613e1f21 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -5,10 +5,10 @@ use serde::Serialize;
use specta::Type;
use std::collections::HashMap;
use std::fs::File;
+use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
-use std::path::PathBuf;
use tokio::sync::oneshot;
use crate::cursor::spawn_cursor_recorder;
@@ -20,13 +20,13 @@ pub const FPS: u32 = 30;
#[tauri::command(async)]
#[specta::specta]
pub fn list_capture_screens() -> Vec<CaptureScreen> {
- ScreenCaptureSource::list_screens()
+ ScreenCaptureSource::<AVFrameCapture>::list_screens()
}
#[tauri::command(async)]
#[specta::specta]
pub fn list_capture_windows() -> Vec<CaptureWindow> {
- ScreenCaptureSource::list_targets()
+ ScreenCaptureSource::<AVFrameCapture>::list_targets()
}
#[tauri::command(async)]
@@ -201,22 +201,47 @@ pub async fn start(
let mut audio_output_path = None;
let mut camera_output_path = None;
- let screen_source =
- ScreenCaptureSource::init(dbg!(&recording_options.capture_target), None, None);
- let screen_config = screen_source.info();
- let screen_bounds = screen_source.bounds;
-
- let output_config = screen_config.scaled(1920, 30);
- let screen_filter = VideoFilter::init("screen", screen_config, output_config)?;
- let screen_encoder = H264Encoder::init(
- "screen",
- output_config,
- Output::File(display_output_path.clone()),
- )?;
- pipeline_builder = pipeline_builder
- .source("screen_capture", screen_source)
- .pipe("screen_capture_filter", screen_filter)
- .sink("screen_capture_encoder", screen_encoder);
+ #[cfg(target_os = "macos")]
+ {
+ let screen_source = ScreenCaptureSource::<cap_media::sources::CMSampleBufferCapture>::init(
+ dbg!(&recording_options.capture_target),
+ None,
+ None,
+ );
+ let screen_config = screen_source.info();
+
+ let output_config = screen_config.scaled(1920, 30);
+ let screen_encoder = cap_media::encoders::H264AVAssetWriterEncoder::init(
+ "screen",
+ output_config,
+ Output::File(display_output_path.clone()),
+ )?;
+ pipeline_builder = pipeline_builder
+ .source("screen_capture", screen_source)
+ .sink("screen_capture_encoder", screen_encoder);
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let screen_source = ScreenCaptureSource::<AVFrameCapture>::init(
+ dbg!(&recording_options.capture_target),
+ None,
+ None,
+ );
+ let screen_config = screen_source.info();
+ let screen_bounds = screen_source.bounds;
+
+ let output_config = screen_config.scaled(1920, 30);
+ let screen_filter = VideoFilter::init("screen", screen_config, output_config)?;
+ let screen_encoder = H264Encoder::init(
+ "screen",
+ output_config,
+ Output::File(display_output_path.clone()),
+ )?;
+ pipeline_builder = pipeline_builder
+ .source("screen_capture", screen_source)
+ // .pipe("screen_capture_filter", screen_filter)
+ .sink("screen_capture_encoder", screen_encoder);
+ }
if let Some(mic_source) = recording_options
.audio_input_name
@@ -263,9 +288,11 @@ pub async fn start(
let stop_signal = Arc::new(AtomicBool::new(false));
// Initialize default values for cursor channels
- let (mouse_moves, mouse_clicks) = if FLAGS.record_mouse {
- spawn_cursor_recorder(stop_signal.clone(), screen_bounds, content_dir, cursors_dir)
- } else {
+ let (mouse_moves, mouse_clicks) =
+ // if FLAGS.record_mouse
+ {
+ // spawn_cursor_recorder(stop_signal.clone(), screen_bounds, content_dir, cursors_dir)
+ // } else {
// Create dummy channels that will never receive data
let (move_tx, move_rx) = oneshot::channel();
let (click_tx, click_rx) = oneshot::channel();
diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml
index 6e5b2d22e..046b05885 100644
--- a/crates/media/Cargo.toml
+++ b/crates/media/Cargo.toml
@@ -39,6 +39,8 @@ objc = "0.2.7"
objc-foundation = "0.1.1"
objc2-foundation = { version = "0.2.2", features = ["NSValue"] }
nokhwa-bindings-macos.workspace = true
+cidre = { git = "https://github.com/yury/cidre", rev = "1e008bec49a0f97aeaaea6130a0ba20fe00aa03b" }
+screencapturekit = "0.2.8"
[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.52.0", features = [
diff --git a/crates/media/src/encoders/h264.rs b/crates/media/src/encoders/h264.rs
index 819c8efba..fc4c355cf 100644
--- a/crates/media/src/encoders/h264.rs
+++ b/crates/media/src/encoders/h264.rs
@@ -6,6 +6,7 @@ use crate::{
use ffmpeg::{
codec::{codec::Codec, context, encoder},
format::{self},
+ software,
threading::Config,
Dictionary,
};
@@ -17,11 +18,13 @@ pub struct H264Encoder {
encoder: encoder::Video,
output_ctx: format::context::Output,
last_pts: Option<i64>,
+ config: VideoInfo,
}
impl H264Encoder {
pub fn init(tag: &'static str, config: VideoInfo, output: Output) -> Result<Self, MediaError> {
let Output::File(destination) = output;
+
let mut output_ctx = format::output(&destination)?;
let (codec, options) = get_codec_and_options(&config)?;
@@ -37,7 +40,14 @@ impl H264Encoder {
encoder.set_format(config.pixel_format);
encoder.set_time_base(config.frame_rate.invert());
encoder.set_frame_rate(Some(config.frame_rate));
- encoder.set_bit_rate(5_000_000);
+
+ if codec.name() == "h264_videotoolbox" {
+ encoder.set_bit_rate(1_200_000);
+ encoder.set_max_bit_rate(120_000);
+ } else {
+ encoder.set_bit_rate(8_000_000);
+ encoder.set_max_bit_rate(8_000_000);
+ }
let video_encoder = encoder.open_with(options)?;
@@ -52,6 +62,7 @@ impl H264Encoder {
encoder: video_encoder,
output_ctx,
last_pts: None,
+ config,
})
}
@@ -109,7 +120,9 @@ impl PipelineSinkTask for H264Encoder {
fn get_codec_and_options(config: &VideoInfo) -> Result<(Codec, Dictionary), MediaError> {
let encoder_name = {
if cfg!(target_os = "macos") {
- "h264_videotoolbox"
+ "libx264"
+ // looks terrible rn :(
+ // "h264_videotoolbox"
} else {
"libx264"
}
@@ -117,17 +130,22 @@ fn get_codec_and_options(config: &VideoInfo) -> Result<(Codec, Dictionary), Medi
if let Some(codec) = encoder::find_by_name(encoder_name) {
let mut options = Dictionary::new();
- let keyframe_interval_secs = 2;
- let keyframe_interval = keyframe_interval_secs * config.frame_rate.numerator();
- let keyframe_interval_str = keyframe_interval.to_string();
-
- options.set("preset", "ultrafast");
- options.set("tune", "zerolatency");
- options.set("vsync", "1");
- options.set("g", &keyframe_interval_str);
- options.set("keyint_min", &keyframe_interval_str);
- // TODO: Is it worth limiting quality? Maybe make this configurable
- // options.set("crf", "23");
+ if encoder_name == "h264_videotoolbox" {
+ // options.set("constant_bit_rate", "true");
+ options.set("realtime", "true");
+ } else {
+ let keyframe_interval_secs = 2;
+ let keyframe_interval = keyframe_interval_secs * config.frame_rate.numerator();
+ let keyframe_interval_str = keyframe_interval.to_string();
+
+ options.set("preset", "ultrafast");
+ options.set("tune", "zerolatency");
+ options.set("vsync", "1");
+ options.set("g", &keyframe_interval_str);
+ options.set("keyint_min", &keyframe_interval_str);
+ // // TODO: Is it worth limiting quality? Maybe make this configurable
+ // options.set("crf", "14");
+ }
return Ok((codec, options));
}
diff --git a/crates/media/src/encoders/h264_avassetwriter.rs b/crates/media/src/encoders/h264_avassetwriter.rs
new file mode 100644
index 000000000..c9f91b394
--- /dev/null
+++ b/crates/media/src/encoders/h264_avassetwriter.rs
@@ -0,0 +1,130 @@
+use crate::{data::VideoInfo, pipeline::task::PipelineSinkTask, MediaError};
+
+use super::Output;
+use arc::Retained;
+use cidre::{objc::Obj, *};
+
+pub struct H264AVAssetWriterEncoder {
+ tag: &'static str,
+ last_pts: Option<i64>,
+ config: VideoInfo,
+ asset_writer: Retained<av::AssetWriter>,
+ video_input: Retained<av::AssetWriterInput>,
+ first_timestamp: Option<cm::Time>,
+ last_timestamp: Option<cm::Time>,
+}
+
+impl H264AVAssetWriterEncoder {
+ pub fn init(tag: &'static str, config: VideoInfo, output: Output) -> Result<Self, MediaError> {
+ let Output::File(destination) = output;
+
+ let mut asset_writer = av::AssetWriter::with_url_and_file_type(
+ cf::Url::with_path(&destination.as_path(), false)
+ .unwrap()
+ .as_ns(),
+ av::FileType::mp4(),
+ )
+ .unwrap();
+
+ let assistant =
+ av::OutputSettingsAssistant::with_preset(av::OutputSettingsPreset::h264_3840x2160())
+ .unwrap();
+
+ let mut output_settings = assistant.video_settings().unwrap().copy_mut();
+
+ output_settings.insert(
+ av::video_settings_keys::width(),
+ ns::Number::with_u32(config.width).as_id_ref(),
+ );
+
+ output_settings.insert(
+ av::video_settings_keys::height(),
+ ns::Number::with_u32(config.height).as_id_ref(),
+ );
+
+ output_settings.insert(
+ av::video_settings_keys::compression_props(),
+ ns::Dictionary::with_keys_values(
+ &[unsafe { AVVideoAverageBitRateKey }],
+ &[ns::Number::with_u32(10_000_000).as_id_ref()],
+ )
+ .as_id_ref(),
+ );
+
+ let mut video_input = av::AssetWriterInput::with_media_type_and_output_settings(
+ av::MediaType::video(),
+ Some(output_settings.as_ref()),
+ )
+ .unwrap();
+ video_input.set_expects_media_data_in_real_time(true);
+
+ asset_writer.add_input(&video_input).unwrap();
+
+ asset_writer.start_writing();
+
+ Ok(Self {
+ tag,
+ last_pts: None,
+ config,
+ asset_writer,
+ video_input,
+ first_timestamp: None,
+ last_timestamp: None,
+ })
+ }
+
+ fn queue_frame(&mut self, frame: screencapturekit::cm_sample_buffer::CMSampleBuffer) {
+ let sample_buf = unsafe {
+ let ptr = &*frame.sys_ref as *const _ as *const cm::SampleBuf;
+ &*ptr
+ };
+
+ let time = sample_buf.pts();
+
+ if self.first_timestamp.is_none() {
+ self.asset_writer.start_session_at_src_time(time);
+ self.first_timestamp = Some(time);
+ }
+
+ self.last_timestamp = Some(time);
+
+ self.video_input.append_sample_buf(sample_buf).unwrap();
+ }
+
+ fn process_frame(&mut self) {}
+
+ fn finish(&mut self) {
+ self.asset_writer
+ .end_session_at_src_time(self.last_timestamp.take().unwrap_or(cm::Time::zero()));
+ self.video_input.mark_as_finished();
+ self.asset_writer.finish_writing();
+ }
+}
+
+impl PipelineSinkTask for H264AVAssetWriterEncoder {
+ type Input = screencapturekit::cm_sample_buffer::CMSampleBuffer;
+
+ fn run(
+ &mut self,
+ ready_signal: crate::pipeline::task::PipelineReadySignal,
+ input: flume::Receiver<Self::Input>,
+ ) {
+ println!("Starting {} video encoding thread", self.tag);
+ ready_signal.send(Ok(())).unwrap();
+
+ while let Ok(frame) = input.recv() {
+ self.queue_frame(frame);
+ self.process_frame();
+ }
+
+ println!("Received last {} frame. Finishing up encoding.", self.tag);
+ self.finish();
+
+ println!("Shutting down {} video encoding thread", self.tag);
+ }
+}
+
+#[link(name = "AVFoundation", kind = "framework")]
+extern "C" {
+ static AVVideoAverageBitRateKey: &'static cidre::ns::String;
+}
diff --git a/crates/media/src/encoders/mod.rs b/crates/media/src/encoders/mod.rs
index bc184da53..27d9ba0cd 100644
--- a/crates/media/src/encoders/mod.rs
+++ b/crates/media/src/encoders/mod.rs
@@ -1,9 +1,13 @@
use std::path::PathBuf;
mod h264;
+#[cfg(target_os = "macos")]
+mod h264_avassetwriter;
mod mp3;
pub use h264::*;
+#[cfg(target_os = "macos")]
+pub use h264_avassetwriter::*;
pub use mp3::*;
pub enum Output {
diff --git a/crates/media/src/sources/screen_capture.rs b/crates/media/src/sources/screen_capture.rs
index 277f5eb20..03034291c 100644
--- a/crates/media/src/sources/screen_capture.rs
+++ b/crates/media/src/sources/screen_capture.rs
@@ -1,4 +1,6 @@
use cap_flags::FLAGS;
+use cidre::cm;
+use core_foundation::base::{kCFAllocatorDefault, CFAllocatorRef};
use flume::Sender;
use scap::{
capturer::{get_output_frame_size, Area, Capturer, Options, Point, Resolution, Size},
@@ -7,15 +9,17 @@ use scap::{
};
use serde::{Deserialize, Serialize};
use specta::Type;
-use std::collections::HashMap;
+use std::{
+ collections::HashMap,
+ ffi::c_void,
+ path::PathBuf,
+ ptr::{null, null_mut},
+};
use crate::{
data::{FFVideo, RawVideoFormat, VideoInfo},
- platform::{Bounds, Window},
-};
-use crate::{
pipeline::{clock::*, control::Control, task::PipelineSourceTask},
- platform,
+ platform::{self, Bounds, Window},
};
static EXCLUDED_WINDOWS: [&str; 4] = [
@@ -60,14 +64,15 @@ impl PartialEq<Target> for ScreenCaptureTarget {
}
}
-pub struct ScreenCaptureSource {
+pub struct ScreenCaptureSource<T> {
options: Options,
video_info: VideoInfo,
target: ScreenCaptureTarget,
pub bounds: Bounds,
+ phantom: std::marker::PhantomData<T>,
}
-impl ScreenCaptureSource {
+impl<T> ScreenCaptureSource<T> {
pub const DEFAULT_FPS: u32 = 30;
pub fn init(
@@ -81,13 +86,9 @@ impl ScreenCaptureSource {
let excluded_targets: Vec<scap::Target> = targets
.iter()
- .filter(|target| match target {
- Target::Window(scap_window)
- if EXCLUDED_WINDOWS.contains(&scap_window.title.as_str()) =>
- {
- true
- }
- _ => false,
+ .filter(|target| {
+ matches!(target, Target::Window(scap_window)
+ if EXCLUDED_WINDOWS.contains(&scap_window.title.as_str()))
})
.cloned()
.collect();
@@ -141,6 +142,7 @@ impl ScreenCaptureSource {
target: capture_target.clone(),
bounds,
video_info: VideoInfo::from_raw(RawVideoFormat::Nv12, frame_width, frame_height, fps),
+ phantom: Default::default(),
}
}
@@ -208,7 +210,9 @@ impl ScreenCaptureSource {
}
}
-impl PipelineSourceTask for ScreenCaptureSource {
+pub struct AVFrameCapture;
+
+impl PipelineSourceTask for ScreenCaptureSource<AVFrameCapture> {
type Clock = RealTimeClock<RawNanoseconds>;
type Output = FFVideo;
@@ -317,3 +321,81 @@ impl PipelineSourceTask for ScreenCaptureSource {
println!("Shutting down screen capture source thread.");
}
}
+
+#[cfg(target_os = "macos")]
+pub struct CMSampleBufferCapture;
+
+#[cfg(target_os = "macos")]
+impl PipelineSourceTask for ScreenCaptureSource<CMSampleBufferCapture> {
+ type Clock = RealTimeClock<RawNanoseconds>;
+ type Output = screencapturekit::cm_sample_buffer::CMSampleBuffer;
+
+ fn run(
+ &mut self,
+ _: Self::Clock,
+ ready_signal: crate::pipeline::task::PipelineReadySignal,
+ mut control_signal: crate::pipeline::control::PipelineControlSignal,
+ output: Sender<Self::Output>,
+ ) {
+ use cidre::*;
+
+ println!("Preparing screen capture source thread...");
+
+ let maybe_capture_window_id = match &self.target {
+ ScreenCaptureTarget::Window(window) => Some(window.id),
+ _ => None,
+ };
+ let mut capturer = Capturer::new(dbg!(self.options.clone()));
+ let mut capturing = false;
+ ready_signal.send(Ok(())).unwrap();
+
+ loop {
+ match control_signal.last() {
+ Some(Control::Play) => {
+ if !capturing {
+ if let Some(window_id) = maybe_capture_window_id {
+ crate::platform::bring_window_to_focus(window_id);
+ }
+ capturer.start_capture();
+ capturing = true;
+
+ println!("Screen recording started.");
+ }
+
+ match capturer.raw().get_next_pixel_buffer() {
+ Ok(pixel_buffer) => {
+ if pixel_buffer.height() == 0 || pixel_buffer.width() == 0 {
+ continue;
+ }
+
+ if let Err(_) = output.send(pixel_buffer.into()) {
+ eprintln!("Pipeline is unreachable. Shutting down recording.");
+ break;
+ }
+ }
+ Err(error) => {
+ eprintln!("Capture error: {error}");
+ break;
+ }
+ }
+ }
+ Some(Control::Pause) => {
+ println!("Received pause signal");
+ if capturing {
+ capturer.stop_capture();
+ capturing = false;
+ }
+ }
+ Some(Control::Shutdown) | None => {
+ println!("Received shutdown signal");
+ if capturing {
+ capturer.stop_capture();
+ }
+ break;
+ }
+ }
+ }
+
+ println!("Shutting down screen capture source thread.");
+ }
+}
diff --git a/crates/rendering/src/decoder.rs b/crates/rendering/src/decoder.rs
index 996e1065f..0c5ad774e 100644
--- a/crates/rendering/src/decoder.rs
+++ b/crates/rendering/src/decoder.rs
@@ -6,8 +6,8 @@ use std::{
use ffmpeg::{
codec,
- format::{self, context::input::PacketIter},
- frame, rescale, Codec, Packet, Rational, Rescale, Stream,
+ format::{self},
+ frame, rescale, Codec, Rational, Rescale,
};
use ffmpeg_hw_device::{CodecContextExt, HwDevice};
use ffmpeg_sys_next::{avcodec_find_decoder, AVHWDeviceType};
@@ -18,13 +18,16 @@ enum VideoDecoderMessage {
GetFrame(u32, tokio::sync::oneshot::Sender<Option<Arc<Vec<u8>>>>),
}
-fn ts_to_frame(ts: i64, time_base: Rational, frame_rate: Rational) -> u32 {
- // dbg!((ts, time_base, frame_rate));
- ((ts * time_base.numerator() as i64 * frame_rate.numerator() as i64)
- / (time_base.denominator() as i64 * frame_rate.denominator() as i64)) as u32
+fn pts_to_frame(pts: i64, time_base: Rational) -> u32 {
+ (FPS as f64 * ((pts as f64 * time_base.numerator() as f64) / (time_base.denominator() as f64)))
+ .round() as u32
}
const FRAME_CACHE_SIZE: usize = 50;
+// TODO: Allow dynamic FPS values by either passing it into `spawn`
+// or changing `get_frame` to take the requested time instead of frame number,
+// so that the lookup can be done by PTS instead of frame number.
+const FPS: u32 = 30;
pub struct AsyncVideoDecoder;
@@ -90,97 +93,113 @@ impl AsyncVideoDecoder {
let mut temp_frame = ffmpeg::frame::Video::empty();
- let render_more_margin = (FRAME_CACHE_SIZE / 4) as u32;
-
let mut cache = BTreeMap::<u32, Arc<Vec<u8>>>::new();
// active frame is a frame that triggered decode.
// frames that are within render_more_margin of this frame won't trigger decode.
let mut last_active_frame = None::<u32>;
let mut last_decoded_frame = None::<u32>;
-
- struct PacketStuff<'a> {
- packets: PacketIter<'a>,
- skipped_packet: Option<(Stream<'a>, Packet)>,
- }
+ let mut last_sent_frame = None::<(u32, DecodedFrame)>;
let mut peekable_requests = PeekableReceiver { rx, peeked: None };
let mut packets = input.packets();
- // let mut packet_stuff = PacketStuff {
- // packets: input.packets(),
- // skipped_packet: None,
- // };
while let Ok(r) = peekable_requests.recv() {
match r {
- VideoDecoderMessage::GetFrame(frame_number, sender) => {
- // println!("retrieving frame {frame_number}");
-
- let mut sender = if let Some(cached) = cache.get(&frame_number) {
- // println!("sending frame {frame_number} from cache");
+ VideoDecoderMessage::GetFrame(requested_frame, sender) => {
+ let mut sender = if let Some(cached) = cache.get(&requested_frame) {
sender.send(Some(cached.clone())).ok();
+ last_sent_frame = Some((requested_frame, cached.clone()));
continue;
} else {
Some(sender)
};
- let cache_min = frame_number.saturating_sub(FRAME_CACHE_SIZE as u32 / 2);
- let cache_max = frame_number + FRAME_CACHE_SIZE as u32 / 2;
+ let cache_min = requested_frame.saturating_sub(FRAME_CACHE_SIZE as u32 / 2);
+ let cache_max = requested_frame + FRAME_CACHE_SIZE as u32 / 2;
- if frame_number <= 0
- || last_decoded_frame
- .map(|f| {
- frame_number < f ||
+ if requested_frame <= 0
+ || last_sent_frame
+ .as_ref()
+ .map(|last| {
+ requested_frame < last.0 ||
// seek forward for big jumps. this threshold is arbitrary but should be derived from i-frames in future
- frame_number - f > FRAME_CACHE_SIZE as u32
+ requested_frame - last.0 > FRAME_CACHE_SIZE as u32
})
.unwrap_or(true)
{
let timestamp_us =
- ((frame_number as f32 / frame_rate.numerator() as f32)
+ ((requested_frame as f32 / frame_rate.numerator() as f32)
* 1_000_000.0) as i64;
let position = timestamp_us.rescale((1, 1_000_000), rescale::TIME_BASE);
- println!("seeking to {position} for frame {frame_number}");
+ println!("seeking to {position} for frame {requested_frame}");
decoder.flush();
input.seek(position, ..position).unwrap();
cache.clear();
last_decoded_frame = None;
+ last_sent_frame = None;
packets = input.packets();
}
- last_active_frame = Some(frame_number);
+ // handle when requested_frame == last_decoded_frame or last_decoded_frame > requested_frame.
+ // the latter can occur when there are skips in frame numbers.
+ // in future we should alleviate this by using time + pts values instead of frame numbers.
+ if let Some((_, last_sent_frame)) = last_decoded_frame
+ .zip(last_sent_frame.as_ref())
+ .filter(|(last_decoded_frame, last_sent_frame)| {
+ last_sent_frame.0 < requested_frame
+ && requested_frame < *last_decoded_frame
+ })
+ {
+ if let Some(sender) = sender.take() {
+ sender.send(Some(last_sent_frame.1.clone())).ok();
+ continue;
+ }
+ }
+
+ last_active_frame = Some(requested_frame);
loop {
if peekable_requests.peek().is_some() {
break;
}
let Some((stream, packet)) = packets.next() else {
+ sender.take().map(|s| s.send(None));
break;
};
if stream.index() == input_stream_index {
let start_offset = stream.start_time();
- let packet_frame =
- ts_to_frame(packet.pts().unwrap(), time_base, frame_rate);
- // println!("sending frame {packet_frame} packet");
decoder.send_packet(&packet).ok(); // decode failures are ok, we just fail to return a frame
let mut exit = false;
while decoder.receive_frame(&mut temp_frame).is_ok() {
- let current_frame = ts_to_frame(
+ let current_frame = pts_to_frame(
temp_frame.pts().unwrap() - start_offset,
time_base,
- frame_rate,
);
- // println!("processing frame {current_frame}");
+
last_decoded_frame = Some(current_frame);
+ // we repeat the similar section as above to do the check per-frame instead of just per-request
+ if let Some((_, last_sent_frame)) = last_decoded_frame
+ .zip(last_sent_frame.as_ref())
+ .filter(|(last_decoded_frame, last_sent_frame)| {
+ last_sent_frame.0 <= requested_frame
+ && requested_frame < *last_decoded_frame
+ })
+ {
+ if let Some(sender) = sender.take() {
+ sender.send(Some(last_sent_frame.1.clone())).ok();
+ }
+ }
+
let exceeds_cache_bounds = current_frame > cache_max;
let too_small_for_cache_bounds = current_frame < cache_min;
@@ -223,18 +242,22 @@ impl AsyncVideoDecoder {
let frame = Arc::new(frame_buffer);
- if current_frame == frame_number {
+ if current_frame == requested_frame {
if let Some(sender) = sender.take() {
+ last_sent_frame = Some((current_frame, frame.clone()));
sender.send(Some(frame.clone())).ok();
+
+ break;
}
}
if !too_small_for_cache_bounds {
if cache.len() >= FRAME_CACHE_SIZE {
if let Some(last_active_frame) = &last_active_frame {
- let frame = if frame_number > *last_active_frame {
+ let frame = if requested_frame > *last_active_frame
+ {
*cache.keys().next().unwrap()
- } else if frame_number < *last_active_frame {
+ } else if requested_frame < *last_active_frame {
*cache.keys().next_back().unwrap()
} else {
let min = *cache.keys().min().unwrap();
@@ -265,8 +288,8 @@ impl AsyncVideoDecoder {
}
}
- if sender.is_some() {
- println!("failed to send frame {frame_number}");
+ if let Some(s) = sender.take() {
+ s.send(None);
}
}
}
@@ -283,10 +306,10 @@ pub struct AsyncVideoDecoderHandle {
}
impl AsyncVideoDecoderHandle {
- pub async fn get_frame(&self, frame_number: u32) -> Option<Arc<Vec<u8>>> {
+ pub async fn get_frame(&self, time: u32) -> Option<Arc<Vec<u8>>> {
let (tx, rx) = tokio::sync::oneshot::channel();
self.sender
- .send(VideoDecoderMessage::GetFrame(frame_number, tx))
+ .send(VideoDecoderMessage::GetFrame(time, tx))
.unwrap();
rx.await.ok().flatten()
}