forked from mirrors/gstreamer-rs
Use byte-slice-cast trait instead of our own minimal version of it
This commit is contained in:
parent
e66bcf8a5d
commit
56847216a0
6 changed files with 17 additions and 194 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -43,6 +43,11 @@ name = "bitflags"
|
|||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "byte-slice-cast"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.1.0"
|
||||
|
@ -107,6 +112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
name = "examples"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byte-slice-cast 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"gio 0.1.3 (git+https://github.com/gtk-rs/gio)",
|
||||
"glib 0.1.3 (git+https://github.com/gtk-rs/glib)",
|
||||
|
@ -669,6 +675,7 @@ dependencies = [
|
|||
"checksum atk-sys 0.3.4 (git+https://github.com/gtk-rs/sys)" = "<none>"
|
||||
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
|
||||
"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
|
||||
"checksum byte-slice-cast 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5a865e7bfa6c3b79216ccba767d4dc66e4f9f65f1ed4639e73faff3c4a2485d7"
|
||||
"checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d"
|
||||
"checksum bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8b24f16593f445422331a5eed46b72f7f171f910fead4f2ea8f17e727e9c5c14"
|
||||
"checksum c_vec 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6237ac5a4b1e81c213c24c6437964c61e646df910a914b4ab1487b46df20bd13"
|
||||
|
|
|
@ -15,6 +15,7 @@ gio = { version = "0.1.3", git = "https://github.com/gtk-rs/gio" }
|
|||
futures = "0.1"
|
||||
tokio-core = "0.1"
|
||||
send-cell = "0.1"
|
||||
byte-slice-cast = "0.1"
|
||||
|
||||
[features]
|
||||
gst-player = ["gstreamer-player"]
|
||||
|
|
|
@ -6,6 +6,9 @@ extern crate gstreamer_audio as gst_audio;
|
|||
|
||||
extern crate glib;
|
||||
|
||||
extern crate byte_slice_cast;
|
||||
use byte_slice_cast::*;
|
||||
|
||||
use std::u64;
|
||||
use std::i16;
|
||||
use std::i32;
|
||||
|
@ -58,12 +61,10 @@ fn create_pipeline() -> Result<Pipeline, utils::ExampleError> {
|
|||
.map_readable()
|
||||
.expect("Unable to map buffer for reading");
|
||||
|
||||
let data =
|
||||
gst_audio::AudioData::new(map.as_slice(), gst_audio::AUDIO_FORMAT_S16).unwrap();
|
||||
let samples = if let gst_audio::AudioData::S16(samples) = data {
|
||||
let samples = if let Ok(samples) = map.as_slice().as_slice_of::<i16>() {
|
||||
samples
|
||||
} else {
|
||||
return FlowReturn::NotNegotiated;
|
||||
return FlowReturn::Error;
|
||||
};
|
||||
|
||||
let sum: f64 = samples
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
extern crate gstreamer as gst;
|
||||
use gst::*;
|
||||
|
||||
extern crate gstreamer_audio as gst_audio;
|
||||
|
||||
extern crate byte_slice_cast;
|
||||
use byte_slice_cast::*;
|
||||
|
||||
use std::u64;
|
||||
use std::i16;
|
||||
|
||||
|
@ -26,9 +28,7 @@ fn main() {
|
|||
if let Some(PadProbeData::Buffer(ref buffer)) = probe_info.data {
|
||||
let map = buffer.map_readable().unwrap();
|
||||
|
||||
let data =
|
||||
gst_audio::AudioData::new(map.as_slice(), gst_audio::AUDIO_FORMAT_S16).unwrap();
|
||||
let samples = if let gst_audio::AudioData::S16(samples) = data {
|
||||
let samples = if let Ok(samples) = map.as_slice().as_slice_of::<i16>() {
|
||||
samples
|
||||
} else {
|
||||
return PadProbeReturn::Ok;
|
||||
|
|
|
@ -1,184 +0,0 @@
|
|||
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::slice;
|
||||
use std::fmt;
|
||||
use libc::uintptr_t;
|
||||
|
||||
use std::error::Error as StdError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
WrongAlignment,
|
||||
WrongEndianness,
|
||||
IncompleteSamples,
|
||||
UnsupportedFormat,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
f.write_str(self.description())
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
use self::Error::*;
|
||||
|
||||
match *self {
|
||||
WrongAlignment => "Wrong Alignment",
|
||||
WrongEndianness => "Wrong Endianness",
|
||||
IncompleteSamples => "Incomplete Samples",
|
||||
UnsupportedFormat => "Unsupported Format",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum AudioData<'a> {
|
||||
S8(&'a [i8]),
|
||||
U8(&'a [u8]),
|
||||
S16(&'a [i16]),
|
||||
U16(&'a [u16]),
|
||||
S32(&'a [i32]),
|
||||
U32(&'a [u32]),
|
||||
F32(&'a [f32]),
|
||||
F64(&'a [f64]),
|
||||
}
|
||||
|
||||
impl<'a> AudioData<'a> {
|
||||
pub fn new(data: &'a [u8], format: ::AudioFormat) -> Result<AudioData<'a>, Error> {
|
||||
use AudioFormat::*;
|
||||
|
||||
let alignment = if (data.as_ptr() as uintptr_t) % 8 == 0 {
|
||||
8
|
||||
} else if (data.as_ptr() as uintptr_t) % 4 == 0 {
|
||||
4
|
||||
} else if (data.as_ptr() as uintptr_t) % 2 == 0 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
let format_info = ::AudioFormatInfo::from_format(format);
|
||||
let width = (format_info.width() / 8) as usize;
|
||||
|
||||
if width != 1 && cfg!(target_endian = "big") && format_info.is_little_endian() {
|
||||
return Err(Error::WrongEndianness);
|
||||
} else if width != 1 && cfg!(target_endian = "little") && format_info.is_big_endian() {
|
||||
return Err(Error::WrongEndianness);
|
||||
}
|
||||
|
||||
if alignment < width {
|
||||
return Err(Error::WrongAlignment);
|
||||
}
|
||||
|
||||
if data.len() % width != 0 {
|
||||
return Err(Error::IncompleteSamples);
|
||||
}
|
||||
|
||||
match format {
|
||||
S8 => Ok(AudioData::S8(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len())
|
||||
})),
|
||||
U8 => Ok(AudioData::U8(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len())
|
||||
})),
|
||||
S16le | S16be => Ok(AudioData::S16(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 2)
|
||||
})),
|
||||
U16le | U16be => Ok(AudioData::U16(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 2)
|
||||
})),
|
||||
S32le | S2432le | S32be | S2432be => Ok(AudioData::S32(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 4)
|
||||
})),
|
||||
U32le | U2432le | U32be | U2432be => Ok(AudioData::U32(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 4)
|
||||
})),
|
||||
F32le | F32be => Ok(AudioData::F32(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 4)
|
||||
})),
|
||||
F64le | F64be => Ok(AudioData::F64(unsafe {
|
||||
slice::from_raw_parts(data.as_ptr() as *const _, data.len() / 8)
|
||||
})),
|
||||
_ => Err(Error::UnsupportedFormat),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum AudioDataMut<'a> {
|
||||
S8(&'a mut [i8]),
|
||||
U8(&'a mut [u8]),
|
||||
S16(&'a mut [i16]),
|
||||
U16(&'a mut [u16]),
|
||||
S32(&'a mut [i32]),
|
||||
U32(&'a mut [u32]),
|
||||
F32(&'a mut [f32]),
|
||||
F64(&'a mut [f64]),
|
||||
}
|
||||
|
||||
impl<'a> AudioDataMut<'a> {
|
||||
pub fn new(data: &'a mut [u8], format: ::AudioFormat) -> Result<AudioDataMut<'a>, Error> {
|
||||
use AudioFormat::*;
|
||||
|
||||
let alignment = if (data.as_ptr() as uintptr_t) % 8 == 0 {
|
||||
8
|
||||
} else if (data.as_ptr() as uintptr_t) % 4 == 0 {
|
||||
4
|
||||
} else if (data.as_ptr() as uintptr_t) % 2 == 0 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
let format_info = ::AudioFormatInfo::from_format(format);
|
||||
let width = (format_info.width() / 8) as usize;
|
||||
|
||||
if width != 1 && cfg!(target_endian = "big") && format_info.is_little_endian() {
|
||||
return Err(Error::WrongEndianness);
|
||||
} else if width != 1 && cfg!(target_endian = "little") && format_info.is_big_endian() {
|
||||
return Err(Error::WrongEndianness);
|
||||
}
|
||||
|
||||
if alignment < width {
|
||||
return Err(Error::WrongAlignment);
|
||||
}
|
||||
|
||||
if data.len() % width != 0 {
|
||||
return Err(Error::IncompleteSamples);
|
||||
}
|
||||
|
||||
match format {
|
||||
S8 => Ok(AudioDataMut::S8(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len())
|
||||
})),
|
||||
U8 => Ok(AudioDataMut::U8(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len())
|
||||
})),
|
||||
S16le | S16be => Ok(AudioDataMut::S16(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 2)
|
||||
})),
|
||||
U16le | U16be => Ok(AudioDataMut::U16(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 2)
|
||||
})),
|
||||
S32le | S2432le | S32be | S2432be => Ok(AudioDataMut::S32(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 4)
|
||||
})),
|
||||
U32le | U2432le | U32be | U2432be => Ok(AudioDataMut::U32(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 4)
|
||||
})),
|
||||
F32le | F32be => Ok(AudioDataMut::F32(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 4)
|
||||
})),
|
||||
F64le | F64be => Ok(AudioDataMut::F64(unsafe {
|
||||
slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 8)
|
||||
})),
|
||||
_ => Err(Error::UnsupportedFormat),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -49,8 +49,6 @@ mod audio_info;
|
|||
pub use audio_info::*;
|
||||
mod audio_channel_position;
|
||||
pub use audio_channel_position::*;
|
||||
mod audio_data;
|
||||
pub use audio_data::{AudioData, AudioDataMut};
|
||||
|
||||
use glib::translate::{from_glib_full, ToGlibPtr};
|
||||
pub fn audio_buffer_clip(
|
||||
|
|
Loading…
Reference in a new issue