-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stabilize the interface + minimal v4l example
- Loading branch information
Showing
6 changed files
with
162 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = [ "copper", "copper_derive", "copper_derive_test", "examples/pluginload", "examples/camreader"] | ||
[workspace] | ||
members = ["copper", "copper_derive", "copper_derive_test", "examples/pluginload", "examples/v4lsrc"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use linux_video::{Device, Stream}; | ||
use linux_video::types::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use copper::config::NodeConfig; | ||
use copper::cutask::{CuResult, CuSrcTask}; | ||
use copper::serde::arrays; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct ImageMsg { | ||
#[serde(with = "arrays")] | ||
buffer: [[u8; 1920]; 1200], | ||
} | ||
|
||
impl Default for ImageMsg { | ||
fn default() -> Self { | ||
ImageMsg { | ||
buffer: [[0; 1920]; 1200], | ||
} | ||
} | ||
} | ||
|
||
impl ImageMsg { | ||
fn copy_from(&mut self, buff_src: &[u8]) { | ||
let mut x = 0usize; | ||
let mut y = 0usize; | ||
|
||
for el in buff_src { | ||
self.buffer[y][x] = *el; | ||
x += 1; | ||
if x == 1920 { | ||
x = 0; | ||
y += 1; | ||
if y == 1200 { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct Video4LinuxSource { | ||
device: Device, | ||
stream: Option<Stream<In, Mmap>>, | ||
} | ||
|
||
impl CuSrcTask for Video4LinuxSource { | ||
type Msg = ImageMsg; | ||
|
||
fn new(config: NodeConfig) -> CuResult<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
let device = Device::open("/dev/video0").unwrap(); | ||
Ok(Video4LinuxSource { | ||
device, | ||
stream: None, | ||
}) | ||
} | ||
|
||
fn start(&mut self) -> CuResult<()> { | ||
self.stream = Some( | ||
self.device | ||
.stream::<In, Mmap>(ContentType::Video, 4) | ||
.unwrap(), | ||
); | ||
Ok(()) | ||
} | ||
|
||
fn process(&mut self, empty_msg: &mut Self::Msg) -> CuResult<()> { | ||
let stream = self.stream.as_ref().unwrap(); | ||
if let Ok(buffer) = stream.next() { | ||
let buffer = buffer.lock(); | ||
empty_msg.copy_from(buffer.as_ref()); | ||
} | ||
Ok(()) | ||
} | ||
fn stop(&mut self) -> CuResult<()> { | ||
self.stream = None; // This will trigger the Drop implementation on Stream | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::mem; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn emulate_runtime() -> CuResult<()> { | ||
println!("Build config"); | ||
let config = NodeConfig::default(); | ||
println!("Build task"); | ||
let mut task = Video4LinuxSource::new(config)?; | ||
println!("Build img"); | ||
// emulates the inplace behavior of copper's runtime. | ||
let size_of_image_msg = mem::size_of::<ImageMsg>(); | ||
let mut memory: Vec<u8> = vec![0; size_of_image_msg]; | ||
let ptr = memory.as_mut_ptr() as *mut ImageMsg; | ||
let img = unsafe { &mut *ptr }; | ||
|
||
println!("Start"); | ||
task.start()?; | ||
println!("Process"); | ||
task.process(img)?; | ||
println!("First byte: {}", img.buffer[0][0]); | ||
println!("Stop"); | ||
task.stop()?; | ||
Ok(()) | ||
} | ||
} |