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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
use adw::prelude::*;
use gst::glib::clone;
use gtk::gio::{Menu, MenuItem, SimpleAction, SimpleActionGroup};
use relm4::{WorkerController, prelude::*};
use crate::{
player::{Player, PlayerMsg, PlayerOutput},
preferences::{Preferences, PreferencesMsg},
subtitle_extractor::{
StreamIndex, SubtitleExtractor, SubtitleExtractorMsg, SubtitleExtractorOutput, TRACKS,
},
subtitle_view::{SubtitleView, SubtitleViewMsg, SubtitleViewOutput},
transcript::{Transcript, TranscriptMsg, TranscriptOutput},
util::OptionTracker,
};
const TRACK_SELECTION_ACTION_GROUP_NAME: &str = "subtitle_track_selection";
pub struct App {
url: String,
transcript: Controller<Transcript>,
player: Controller<Player>,
subtitle_view: Controller<SubtitleView>,
extractor: WorkerController<SubtitleExtractor>,
preferences: Controller<Preferences>,
subtitle_selection_menu: Menu,
subtitle_selection_action_group: SimpleActionGroup,
primary_stream_ix: Option<StreamIndex>,
primary_last_cue_ix: OptionTracker<usize>,
secondary_stream_ix: Option<StreamIndex>,
secondary_last_cue_ix: OptionTracker<usize>,
// for auto-pausing
autopaused: bool,
primary_cue_active: bool,
hovering_primary_cue: bool,
}
#[derive(Debug)]
pub enum AppMsg {
NewOrUpdatedTrackMetadata(StreamIndex),
NewCue(StreamIndex, crate::subtitle_extractor::SubtitleCue),
ExtractionComplete,
TrackSelected(StreamIndex),
PositionUpdate(gst::ClockTime),
SetHoveringSubtitleCue(bool),
ShowPreferences,
}
#[relm4::component(pub)]
impl SimpleComponent for App {
type Init = String;
type Input = AppMsg;
type Output = ();
view! {
#[root]
adw::ApplicationWindow {
set_title: Some("lleap"),
set_default_width: 800,
set_default_height: 600,
#[name(toolbar_view)]
adw::ToolbarView {
add_top_bar = &adw::HeaderBar {
pack_start = >k::MenuButton {
set_label: "Select Subtitle Track",
set_popover: Some(>k::PopoverMenu::from_model(Some(&model.subtitle_selection_menu))),
},
pack_start = >k::Button {
set_label: "Preferences",
connect_clicked => AppMsg::ShowPreferences,
add_css_class: "flat",
}
},
#[wrap(Some)]
set_content = >k::Paned {
set_orientation: gtk::Orientation::Vertical,
#[wrap(Some)]
set_start_child = >k::Paned {
set_start_child: Some(model.player.widget()),
set_end_child: Some(model.transcript.widget()),
},
set_end_child: Some(model.subtitle_view.widget()),
set_shrink_end_child: false,
}
}
}
}
fn init(
url: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let subtitle_selection_menu = Menu::new();
let subtitle_selection_action_group = SimpleActionGroup::new();
root.insert_action_group(
TRACK_SELECTION_ACTION_GROUP_NAME,
Some(&subtitle_selection_action_group),
);
Self::add_dummy_menu_item(&subtitle_selection_action_group, &subtitle_selection_menu);
let subtitle_view = SubtitleView::builder().launch(()).forward(
sender.input_sender(),
|output| match output {
SubtitleViewOutput::SetHoveringCue(val) => AppMsg::SetHoveringSubtitleCue(val),
},
);
let player = Player::builder()
.launch(())
.forward(sender.input_sender(), |output| match output {
PlayerOutput::PositionUpdate(pos) => AppMsg::PositionUpdate(pos),
});
let transcript =
Transcript::builder()
.launch(())
.forward(player.sender(), |msg| match msg {
TranscriptOutput::SeekTo(pos) => PlayerMsg::SeekTo(pos),
});
let extractor = SubtitleExtractor::builder().detach_worker(()).forward(
sender.input_sender(),
|output| match output {
SubtitleExtractorOutput::NewOrUpdatedTrackMetadata(stream_index) => {
AppMsg::NewOrUpdatedTrackMetadata(stream_index)
}
SubtitleExtractorOutput::NewCue(stream_index, cue) => {
AppMsg::NewCue(stream_index, cue)
}
SubtitleExtractorOutput::ExtractionComplete => AppMsg::ExtractionComplete,
},
);
let preferences = Preferences::builder().launch(root.clone().into()).detach();
let model = Self {
url: url.clone(), // TODO remove clone
player,
transcript,
subtitle_view,
extractor,
preferences,
subtitle_selection_menu,
subtitle_selection_action_group,
primary_stream_ix: None,
primary_last_cue_ix: OptionTracker::new(None),
secondary_stream_ix: None,
secondary_last_cue_ix: OptionTracker::new(None),
autopaused: false,
primary_cue_active: false,
hovering_primary_cue: false,
};
let widgets = view_output!();
model
.player
.sender()
.send(PlayerMsg::SetUrl(url.clone()))
.unwrap();
model
.extractor
.sender()
.send(SubtitleExtractorMsg::ExtractFromUrl(url))
.unwrap();
ComponentParts { model, widgets }
}
fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
self.primary_last_cue_ix.reset();
self.secondary_last_cue_ix.reset();
match msg {
AppMsg::NewOrUpdatedTrackMetadata(_stream_index) => {
self.update_subtitle_selection_menu(&sender);
}
AppMsg::NewCue(stream_index, cue) => {
self.transcript
.sender()
.send(TranscriptMsg::NewCue(stream_index, cue))
.unwrap();
}
AppMsg::ExtractionComplete => {
println!("Subtitle extraction complete");
}
AppMsg::TrackSelected(stream_index) => {
self.primary_stream_ix = Some(stream_index);
self.transcript
.sender()
.send(TranscriptMsg::SelectTrack(stream_index))
.unwrap();
}
AppMsg::PositionUpdate(pos) => {
if let Some(stream_ix) = self.primary_stream_ix {
let cue =
Self::get_cue_and_update_ix(stream_ix, pos, &mut self.primary_last_cue_ix);
let cue_is_some = cue.is_some();
// beginning of new subtitle
if self.primary_last_cue_ix.is_dirty()
|| (!self.primary_cue_active && cue_is_some)
{
self.subtitle_view
.sender()
.send(SubtitleViewMsg::SetPrimaryCue(cue))
.unwrap();
self.primary_cue_active = cue_is_some;
if let Some(ix) = self.primary_last_cue_ix.get() {
self.transcript
.sender()
.send(TranscriptMsg::ScrollToCue(*ix))
.unwrap();
}
self.primary_last_cue_ix.reset();
}
// end of current subtitle
if self.primary_cue_active && !cue_is_some && !self.autopaused {
if self.hovering_primary_cue {
self.player.sender().send(PlayerMsg::Pause).unwrap();
self.autopaused = true;
} else {
self.subtitle_view
.sender()
.send(SubtitleViewMsg::SetPrimaryCue(None))
.unwrap();
self.primary_cue_active = false;
}
}
}
if let Some(stream_ix) = self.secondary_stream_ix {
self.subtitle_view
.sender()
.send(SubtitleViewMsg::SetPrimaryCue(Self::get_cue_and_update_ix(
stream_ix,
pos,
&mut self.primary_last_cue_ix,
)))
.unwrap();
}
}
AppMsg::SetHoveringSubtitleCue(hovering) => {
self.hovering_primary_cue = hovering;
if !hovering && self.autopaused {
self.player.sender().send(PlayerMsg::Play).unwrap();
self.autopaused = false;
}
}
AppMsg::ShowPreferences => {
self.preferences
.sender()
.send(PreferencesMsg::Show)
.unwrap();
}
}
}
}
impl App {
fn update_subtitle_selection_menu(&mut self, sender: &ComponentSender<Self>) {
self.subtitle_selection_menu.remove_all();
for action_name in self.subtitle_selection_action_group.list_actions() {
self.subtitle_selection_action_group
.remove_action(&action_name);
}
let tracks = TRACKS.read();
if tracks.is_empty() {
Self::add_dummy_menu_item(
&self.subtitle_selection_action_group,
&self.subtitle_selection_menu,
);
} else {
for (stream_index, track) in tracks.iter() {
let unknown_string = "<unknown>".to_string();
let language = track.language_code.as_ref().unwrap_or(&unknown_string);
let label = format!("{} (Stream {})", language, stream_index);
let action_name = format!("select_{}", stream_index);
let action = SimpleAction::new(&action_name, None);
action.connect_activate(clone!(
#[strong]
sender,
#[strong]
stream_index,
move |_, _| {
let _ = sender.input(AppMsg::TrackSelected(stream_index));
}
));
self.subtitle_selection_action_group.add_action(&action);
// Create menu item
let action_target =
format!("{}.{}", TRACK_SELECTION_ACTION_GROUP_NAME, action_name);
let item = MenuItem::new(Some(&label), Some(&action_target));
self.subtitle_selection_menu.append_item(&item);
}
}
}
// Add disabled "No tracks available" item
fn add_dummy_menu_item(action_group: &SimpleActionGroup, menu: &Menu) {
let disabled_action = SimpleAction::new("no_tracks", None);
disabled_action.set_enabled(false);
action_group.add_action(&disabled_action);
let action_target = format!("{}.no_tracks", TRACK_SELECTION_ACTION_GROUP_NAME);
let item = MenuItem::new(Some("No tracks available"), Some(&action_target));
menu.append_item(&item);
}
fn get_cue_and_update_ix(
stream_ix: StreamIndex,
position: gst::ClockTime,
last_cue_ix: &mut OptionTracker<usize>,
) -> Option<String> {
let lock = TRACKS.read();
let track = lock.get(&stream_ix)?;
// try to find current cue quickly (should usually succeed during playback)
if let Some(ix) = last_cue_ix.get() {
let last_cue = track.cues.get(*ix)?;
if last_cue.start <= position && position <= last_cue.end {
return Some(last_cue.text.clone());
}
let next_cue = track.cues.get(ix + 1)?;
if last_cue.end < position && position < next_cue.start {
return None;
}
if next_cue.start <= position && position <= next_cue.end {
last_cue_ix.set(Some(ix + 1));
return Some(next_cue.text.clone());
}
}
// if we are before the first subtitle, no need to look further
if position < track.cues.first()?.start {
last_cue_ix.set(None);
return None;
}
// otherwise, search the whole track (e.g. after seeking)
let (ix, cue) = track
.cues
.iter()
.enumerate()
.rev()
.find(|(_ix, cue)| cue.start <= position)?;
last_cue_ix.set(Some(ix));
if position <= cue.end {
Some(cue.text.clone())
} else {
None
}
}
}
|