1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use anyhow::Result;
use fui_core::*;
use gstreamer::prelude::*;
use media_gstreamer;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::mpsc::*;
use std::sync::{Arc, Mutex};
pub struct Player {
pub texture: PlayerTexture,
pipeline: Option<gstreamer::Pipeline>,
//dispatcher: Arc<Mutex<dyn Dispatcher>>,
receiver: Option<Receiver<Vec<u8>>>,
}
impl Player {
pub fn new(drawing_context: Rc<RefCell<FuiDrawingContext>>) -> Self {
gstreamer::init().unwrap();
Player {
texture: PlayerTexture::new(drawing_context),
pipeline: None,
//dispatcher: Arc::new(Mutex::new(Dispatcher>:for_current_thread())),
receiver: None,
}
}
pub fn open(&mut self) {
println!("Main thread id: {:?}", std::thread::current().id());
let (sender, receiver) = channel();
self.receiver = Some(receiver);
let _sender = Arc::new(Mutex::new(sender));
// Create the elements
//let (pipeline, video_app_sink) = pipeline_factory::create_pipeline_videotest();
//self.texture.set_size(320, 240);
let (_pipeline, _video_app_sink) = media_gstreamer::create_appsink_pipeline_url(
"http://ftp.nluug.nl/pub/graphics/blender/demo/movies/Sintel.2010.720p.mkv",
);
self.texture.set_size(1280, 544);
//let dispatcher_clone = self.dispatcher.clone();
/*video_app_sink.set_callbacks(
gstreamer_app::AppSinkCallbacks::builder()
.new_sample(move |app_sink| {
let timespec = time::OffsetDateTime::now_utc();
let mills: f64 = timespec.second() as f64
+ (timespec.nanosecond() as f64 / 1000.0 / 1000.0 / 1000.0);
println!(
"New sample thread id: {:?}, time: {:?}",
std::thread::current().id(),
mills
);
let sample = match app_sink.pull_sample() {
Err(_) => return Err(gstreamer::FlowError::Eos),
Ok(sample) => sample,
};
//let caps = sample.caps().unwrap();
//let s = caps.structure(0).unwrap();
//let width: i32 = s.get("width").unwrap();
//let height: i32 = s.get("height").unwrap();
let buffer = sample.buffer().unwrap();
let map = buffer.map_readable().unwrap();
let data = map.as_slice();
//print!("AppSink: New sample ({}x{}, size: {})\n", width, height, data.len());
sender.lock().unwrap().send(Vec::from(data)).unwrap();
//dispatcher_clone.lock().unwrap().send_async(|| {
//texture_clone.lock().unwrap().update_texture();
//});
Ok(gstreamer::FlowSuccess::Ok)
})
.build(),
);*/
self.pipeline = None; //Some(pipeline);
}
pub fn play(&mut self) {
// Start playing
if let Some(ref pipeline) = self.pipeline {
let ret = pipeline.set_state(gstreamer::State::Playing);
assert_ne!(ret, Err(gstreamer::StateChangeError));
}
}
pub fn on_loop_interation(&mut self) -> Result<()> {
if let Some(ref receiver) = self.receiver {
while let Ok(buffer) = receiver.try_recv() {
let timespec = time::OffsetDateTime::now_utc();
let mills: f64 = timespec.second() as f64
+ (timespec.nanosecond() as f64 / 1000.0 / 1000.0 / 1000.0);
println!(
"buffer size: {}, thread id: {:?}, time: {:?}",
buffer.len(),
std::thread::current().id(),
mills
);
self.texture.update_texture(buffer)?
}
}
Ok(())
}
pub fn stop(&mut self) {
// Shutdown pipeline
if let Some(ref pipeline) = self.pipeline {
let ret = pipeline.set_state(gstreamer::State::Null);
assert_ne!(ret, Err(gstreamer::StateChangeError));
}
}
}
pub struct PlayerTexture {
pub updated: Callback<i32>,
texture_id: i32,
width: u16,
height: u16,
//drawing_context: Rc<RefCell<fui_app::DrawingContext>>,
}
impl PlayerTexture {
pub fn new(_drawing_context: Rc<RefCell<FuiDrawingContext>>) -> Self {
PlayerTexture {
updated: Callback::empty(),
texture_id: -1,
width: 0,
height: 0,
//drawing_context,
}
}
pub fn set_size(&mut self, width: u16, height: u16) {
self.width = width;
self.height = height;
}
fn update_texture(&mut self, _buffer: Vec<u8>) -> Result<()> {
let timespec = time::OffsetDateTime::now_utc();
let mills: f64 =
timespec.second() as f64 + (timespec.nanosecond() as f64 / 1000.0 / 1000.0 / 1000.0);
println!(
"Dispatcher, thread id: {:?}, time: {:?}",
std::thread::current().id(),
mills
);
/*if self.texture_id == -1 {
let mut drawing_context = self.drawing_context.borrow_mut();
self.texture_id = drawing_context.create_texture(
&buffer,
self.width,
self.height,
ColorFormat::RGBA,
true,
)?;
} else {
let mut drawing_context = self.drawing_context.borrow_mut();
drawing_context.update_texture(
self.texture_id,
&buffer,
0,
0,
self.width,
self.height,
)?;
}
self.updated.emit(self.texture_id);*/
Ok(())
}
}