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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::sync::{Arc, Mutex};
use std::thread;

use fnv::FnvHashMap;
use log::{trace, warn};
use webrender_api::ExternalImageId;
use webrender_traits::{WebrenderExternalImageRegistry, WebrenderImageHandlerType};

use crate::media_channel::{glplayer_channel, GLPlayerSender};
/// GL player threading API entry point that lives in the
/// constellation.
use crate::{GLPlayerMsg, GLPlayerMsgForward};

/// A GLPlayerThread manages the life cycle and message demultiplexing of
/// a set of video players with GL render.
pub struct GLPlayerThread {
    /// Map of live players.
    players: FnvHashMap<u64, GLPlayerSender<GLPlayerMsgForward>>,
    /// List of registered webrender external images.
    /// We use it to get an unique ID for new players.
    external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
}

impl GLPlayerThread {
    pub fn new(external_images: Arc<Mutex<WebrenderExternalImageRegistry>>) -> Self {
        GLPlayerThread {
            players: Default::default(),
            external_images,
        }
    }

    pub fn start(
        external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
    ) -> GLPlayerSender<GLPlayerMsg> {
        let (sender, receiver) = glplayer_channel::<GLPlayerMsg>().unwrap();
        thread::Builder::new()
            .name("GLPlayer".to_owned())
            .spawn(move || {
                let mut renderer = GLPlayerThread::new(external_images);
                loop {
                    let msg = receiver.recv().unwrap();
                    let exit = renderer.handle_msg(msg);
                    if exit {
                        return;
                    }
                }
            })
            .expect("Thread spawning failed");

        sender
    }

    /// Handles a generic GLPlayerMsg message
    #[inline]
    fn handle_msg(&mut self, msg: GLPlayerMsg) -> bool {
        trace!("processing {:?}", msg);
        match msg {
            GLPlayerMsg::RegisterPlayer(sender) => {
                let id = self
                    .external_images
                    .lock()
                    .unwrap()
                    .next_id(WebrenderImageHandlerType::Media)
                    .0;
                self.players.insert(id, sender.clone());
                sender.send(GLPlayerMsgForward::PlayerId(id)).unwrap();
            },
            GLPlayerMsg::UnregisterPlayer(id) => {
                self.external_images
                    .lock()
                    .unwrap()
                    .remove(&ExternalImageId(id));
                if self.players.remove(&id).is_none() {
                    warn!("Tried to remove an unknown player");
                }
            },
            GLPlayerMsg::Lock(id, handler_sender) => {
                if let Some(sender) = self.players.get(&id) {
                    sender.send(GLPlayerMsgForward::Lock(handler_sender)).ok();
                }
            },
            GLPlayerMsg::Unlock(id) => {
                if let Some(sender) = self.players.get(&id) {
                    sender.send(GLPlayerMsgForward::Unlock()).ok();
                }
            },
            GLPlayerMsg::Exit => return true,
        }

        false
    }
}