This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MythTV.h
169 lines (134 loc) · 3.98 KB
/
MythTV.h
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
/* -*- Mode: c++ -*-
*
* Copyright (C) John Poet 2018
*
* This file is part of HauppaugeUSB.
*
* HauppaugeUSB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HauppaugeUSB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HauppaugeUSB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _MythTV_H_
#define _MythTV_H_
#include "Common.h"
#include "HauppaugeDev.h"
#include "USBif.h"
#include <atomic>
#include <string>
#include <vector>
#include <queue>
#include <condition_variable>
#include <thread>
#include <mutex>
class MythTV;
class Buffer
{
public:
enum constants {MAX_QUEUE = 500};
Buffer(MythTV * parent);
~Buffer(void) {
m_run = false;
if(m_thread.joinable()) m_thread.join();
}
void Start(void) {
m_thread = std::thread(&Buffer::Run, this);
}
void Join(void) {
if (m_thread.joinable())
m_thread.join();
}
void SetBlockSize(uint32_t sz) { m_block_size = sz; }
void Fill(void * data, size_t len);
DataTransfer::callback_t & getWriteCallBack(void) { return m_cb; }
std::chrono::time_point<std::chrono::system_clock> HeartBeat(void) const
{ return m_heartbeat; }
protected:
void Run(void);
private:
std::thread m_thread;
MythTV *m_parent;
std::atomic_bool m_run;
DataTransfer::callback_t m_cb;
uint32_t m_block_size;
using block_t = std::vector<uint8_t>;
using stack_t = std::queue<block_t>;
stack_t m_data;
std::chrono::time_point<std::chrono::system_clock> m_heartbeat;
};
class Commands
{
public:
enum { max_api_version = 2 };
Commands(MythTV * parent);
~Commands(void) {
m_run = false;
if(m_thread.joinable()) m_thread.join();
}
void Start(void) {
m_thread = std::thread(&Commands::Run, this);
}
void Join(void) {
if (m_thread.joinable())
m_thread.join();
}
bool send_status(const std::string & cmd, const std::string & serial,
const std::string & status);
bool process_command(const std::string & cmd);
protected:
void Run(void);
private:
std::thread m_thread;
std::atomic_bool m_run;
MythTV* m_parent;
int m_api_version;
};
class MythTV
{
friend class Buffer;
friend class Commands;
public:
MythTV(const Parameters & params, const std::string & desc);
~MythTV(void);
void Terminate(void);
void Wait(void);
void OpenDev(void);
void Fatal(const std::string & msg);
void BlockSize(uint32_t blocksz) { m_buffer.SetBlockSize(blocksz); }
DataTransfer::callback_t & getWriteCallBack(void)
{ return m_buffer.getWriteCallBack(); }
USBWrapper_t::callback_t & getErrorCallBack(void)
{ return m_error_cb; }
bool StartEncoding(std::string & resultmsg);
bool StopEncoding(std::string & resultmsg, bool soft = false);
void USBError(void) { Fatal("Detected Error with USB."); }
protected:
std::string m_desc;
uint32_t m_buffer_max;
uint32_t m_block_size;
Buffer m_buffer;
Commands m_commands;
const Parameters &m_params;
HauppaugeDev *m_dev;
USBWrapper_t m_usbio;
std::atomic<bool> m_run;
std::mutex m_run_mutex;
std::condition_variable m_run_cond;
std::string m_fatal_msg;
bool m_fatal;
std::timed_mutex m_flow_mutex;
std::condition_variable m_flow_cond;
std::atomic<bool> m_streaming;
std::atomic<bool> m_xon;
std::atomic<bool> m_ready;
USBWrapper_t::callback_t m_error_cb;
};
#endif