-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recorder: initial file recording support
Needs metadata variables to really be useful, but a good start
- Loading branch information
Showing
8 changed files
with
145 additions
and
18 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
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,43 @@ | ||
use std::io::{self, Write}; | ||
|
||
use anyhow::{ensure, Result}; | ||
|
||
use crate::{player::Player, recorder::Recorder}; | ||
|
||
pub struct CombinedWriter { | ||
player: Option<Player>, | ||
recorder: Option<Recorder>, | ||
} | ||
|
||
impl Write for CombinedWriter { | ||
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> { | ||
unimplemented!() | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | ||
if let Some(ref mut player) = self.player { | ||
player.write_all(buf)?; | ||
} | ||
|
||
if let Some(ref mut recorder) = self.recorder { | ||
recorder.write_all(buf)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl CombinedWriter { | ||
pub fn new(player: Option<Player>, recorder: Option<Recorder>) -> Result<Self> { | ||
ensure!( | ||
player.is_some() || recorder.is_some(), | ||
"Player or recording must be set" | ||
); | ||
|
||
Ok(Self { player, recorder }) | ||
} | ||
} |
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,63 @@ | ||
use std::{ | ||
fs::File, | ||
io::{self, Write}, | ||
}; | ||
|
||
use anyhow::Result; | ||
use log::{debug, info}; | ||
|
||
use crate::args::{ArgParser, Parser}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct Args { | ||
path: Option<String>, | ||
overwrite: bool, | ||
} | ||
|
||
impl ArgParser for Args { | ||
fn parse(&mut self, parser: &mut Parser) -> Result<()> { | ||
parser.parse_fn_cfg(&mut self.path, "-r", "record", Parser::parse_opt_string)?; | ||
parser.parse_switch(&mut self.overwrite, "--overwrite")?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct Recorder { | ||
file: File, | ||
} | ||
|
||
impl Write for Recorder { | ||
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> { | ||
unimplemented!(); | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
debug!("Finished writing segment"); | ||
Ok(()) | ||
} | ||
|
||
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | ||
self.file.write_all(buf) | ||
} | ||
} | ||
|
||
impl Recorder { | ||
pub fn new(args: &Args) -> Result<Option<Self>> { | ||
if let Some(ref path) = args.path { | ||
info!("Recording to {path}"); | ||
|
||
if args.overwrite { | ||
return Ok(Some(Self { | ||
file: File::create(path)?, | ||
})); | ||
} | ||
|
||
return Ok(Some(Self { | ||
file: File::options().write(true).create_new(true).open(path)?, | ||
})); | ||
} | ||
|
||
Ok(None) | ||
} | ||
} |
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