1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Vidtv serves as a reference DVB driver and helps validate the existing APIs
4 * in the media subsystem. It can also aid developers working on userspace
7 * This file contains the muxer logic for TS packets from different
10 * Loosely based on libavcodec/mpegtsenc.c
12 * Copyright (C) 2020 Daniel W. S. Almeida
18 #include <linux/hashtable.h>
19 #include <linux/types.h>
20 #include <linux/workqueue.h>
22 #include <media/dvb_frontend.h>
24 #include "vidtv_psi.h"
27 * struct vidtv_mux_timing - Timing related information
29 * This is used to decide when PCR or PSI packets should be sent. This will also
30 * provide storage for the clock, which is used to compute the value for the PCR.
32 * @start_jiffies: The value of 'jiffies' when we started the mux thread.
33 * @current_jiffies: The value of 'jiffies' for the current iteration.
34 * @past_jiffies: The value of 'jiffies' for the past iteration.
35 * @clk: A 27Mhz clock from which we will drive the PCR. Updated proportionally
37 * @pcr_period_usecs: How often we should send PCR packets.
38 * @si_period_usecs: How often we should send PSI packets.
40 struct vidtv_mux_timing {
52 * struct vidtv_mux_si - Store the PSI context.
54 * This is used to store the PAT, PMT sections and SDT in use by the muxer.
56 * The muxer acquire these by looking into the hardcoded channels in
57 * vidtv_channel and then periodically sends the TS packets for them>
59 * @pat: The PAT in use by the muxer.
60 * @pmt_secs: The PMT sections in use by the muxer. One for each program in the PAT.
61 * @sdt: The SDT in use by the muxer.
62 * @eit: the EIT in use by the muxer.
66 struct vidtv_psi_table_pat *pat;
67 struct vidtv_psi_table_pmt **pmt_secs; /* the PMT sections */
68 struct vidtv_psi_table_sdt *sdt;
69 struct vidtv_psi_table_nit *nit;
70 struct vidtv_psi_table_eit *eit;
74 * struct vidtv_mux_pid_ctx - Store the context for a given TS PID.
76 * @cc: The continuity counter for this PID. It is incremented on every TS
77 * pack and it will wrap around at 0xf0. If the decoder notices a sudden jump in
78 * this counter this will trigger a discontinuity state.
79 * @h: This is embedded in a hash table, mapping pid -> vidtv_mux_pid_ctx
81 struct vidtv_mux_pid_ctx {
83 u8 cc; /* continuity counter */
88 * struct vidtv_mux - A muxer abstraction loosely based in libavcodec/mpegtsenc.c
89 * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
90 * @timing: Keeps track of timing related information.
91 * @pid_ctx: A hash table to keep track of per-PID metadata.
92 * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
93 * @mux_buf: A pointer to a buffer for this muxer. TS packets are stored there
94 * and then passed on to the bridge driver.
95 * @mux_buf_sz: The size for 'mux_buf'.
96 * @mux_buf_offset: The current offset into 'mux_buf'.
97 * @channels: The channels associated with this muxer.
98 * @si: Keeps track of the PSI context.
99 * @num_streamed_pcr: Number of PCR packets streamed.
100 * @num_streamed_si: The number of PSI packets streamed.
101 * @mpeg_thread: Thread responsible for the muxer loop.
102 * @streaming: whether 'mpeg_thread' is running.
103 * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
105 * @transport_stream_id: The transport stream ID
106 * @network_id: The network ID
107 * @network_name: The network name
108 * @priv: Private data.
111 struct dvb_frontend *fe;
114 struct vidtv_mux_timing timing;
116 u32 mux_rate_kbytes_sec;
118 DECLARE_HASHTABLE(pid_ctx, 3);
120 void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
126 struct vidtv_channel *channels;
128 struct vidtv_mux_si si;
129 u64 num_streamed_pcr;
132 struct work_struct mpeg_thread;
136 u16 transport_stream_id;
143 * struct vidtv_mux_init_args - Arguments used to inix the muxer.
144 * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
145 * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
146 * @mux_buf_sz: The size for 'mux_buf'.
147 * @pcr_period_usecs: How often we should send PCR packets.
148 * @si_period_usecs: How often we should send PSI packets.
149 * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
151 * @transport_stream_id: The transport stream ID
152 * @channels: an optional list of channels to use
153 * @network_id: The network ID
154 * @network_name: The network name
155 * @priv: Private data.
157 struct vidtv_mux_init_args {
158 u32 mux_rate_kbytes_sec;
159 void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
161 u64 pcr_period_usecs;
164 u16 transport_stream_id;
165 struct vidtv_channel *channels;
171 struct vidtv_mux *vidtv_mux_init(struct dvb_frontend *fe,
173 struct vidtv_mux_init_args *args);
174 void vidtv_mux_destroy(struct vidtv_mux *m);
176 void vidtv_mux_start_thread(struct vidtv_mux *m);
177 void vidtv_mux_stop_thread(struct vidtv_mux *m);