mptsd reads mpegts streams from udp/multicast or http and combines them into one multiple program stream that is suitable for outputting to DVB-C modulator. Tested with Dektec DTE-3114 Quad QAM Modulator and used in production in small DVB-C networks. https://georgi.unixsol.org/programs/mptsd/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

output_mix.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <errno.h>
  7. #include "libfuncs/libfuncs.h"
  8. #include "libtsfuncs/tsfuncs.h"
  9. #include "data.h"
  10. #include "config.h"
  11. #include "input.h"
  12. void output_show_programs(CONFIG *conf) {
  13. LNODE *lr, *lrtmp;
  14. list_for_each(conf->inputs, lr, lrtmp) {
  15. INPUT *r = lr->data;
  16. if (r->input_ready == 1) {
  17. LOGf("OUTPUT: [%-12s] Service %d appeared.\n", r->channel->id, r->channel->service_id);
  18. r->input_ready++;
  19. }
  20. }
  21. }
  22. void * output_handle_mix(void *_config) {
  23. LNODE *lr, *lrtmp;
  24. LNODE *inpt; // Track last used input
  25. CONFIG *conf = _config;
  26. OUTPUT *o = conf->output;
  27. int buf_in_use = 0;
  28. unsigned int o_datasize, o_packets, packets;
  29. unsigned int o_maxpackets = o->obuf[0].size / TS_PACKET_SIZE;
  30. signal(SIGPIPE, SIG_IGN);
  31. inpt = conf->inputs->tail; // Next is the first one
  32. while (!o->dienow) {
  33. OBUF *curbuf = &o->obuf[buf_in_use];
  34. usleep(o->obuf_ms); // Fill interval
  35. output_show_programs(conf);
  36. while (curbuf->status != obuf_empty) {
  37. if (o->dienow)
  38. goto OUT;
  39. //LOGf("MIX: Waiting for obuf %d\n", buf_in_use);
  40. usleep(1);
  41. }
  42. list_lock(conf->inputs);
  43. o_datasize = o->psibuf->input - o->psibuf->output; // PSI data
  44. list_for_each(conf->inputs, lr, lrtmp) { // INPUT data
  45. INPUT *r = lr->data;
  46. o_datasize += r->buf->input - r->buf->output;
  47. }
  48. o_packets = o_datasize / TS_PACKET_SIZE;
  49. packets = min(o_packets, o_maxpackets);
  50. double null_per_data = 1;
  51. double data_per_null = 0;
  52. if (o_maxpackets - packets) {
  53. data_per_null = (double)packets / (o_maxpackets-packets);
  54. if (data_per_null < 1) {
  55. null_per_data = (double)(o_maxpackets-packets) / packets;
  56. data_per_null = 1;
  57. }
  58. }
  59. curbuf->status = obuf_filling; // Mark buffer as being filled
  60. if (conf->debug) {
  61. LOGf("MIX[%2d]: Data:%6u | Bufsz:%6d | Packs:%4u | D/N:%5.2f/%5.2f\n",
  62. buf_in_use,
  63. o_datasize,
  64. curbuf->size,
  65. packets,
  66. ((double)packets / o_maxpackets) * 100,
  67. (double)100 - ((double)packets / o_maxpackets) * 100
  68. );
  69. LOGf("datapacks:%5d maxpacks:%5d null:%5d (%5.2f) | null_per_data:%5.2f data_per_null:%5.2f\n",
  70. packets,
  71. o_maxpackets,
  72. o_maxpackets-packets,
  73. 100-((double)packets / o_maxpackets)*100,
  74. null_per_data,
  75. data_per_null
  76. );
  77. }
  78. unsigned int nulls=0, null_packets_count = o_maxpackets - packets;
  79. // The is no data in the input buffer, send only NULLs
  80. if (null_packets_count == o_maxpackets) {
  81. // Increase sended packets
  82. list_for_each(conf->inputs, lr, lrtmp) {
  83. INPUT *r = lr->data;
  84. r->outputed_packets += o_maxpackets;
  85. }
  86. goto NEXT_BUFFER;
  87. }
  88. unsigned int data_packets;
  89. int data_size;
  90. uint8_t *data;
  91. for (data_packets=0;data_packets<packets;data_packets++) {
  92. if (o->dienow)
  93. break;
  94. // Try the PSI data first
  95. data = cbuf_get(o->psibuf, TS_PACKET_SIZE, &data_size);
  96. if (data && data_size == TS_PACKET_SIZE)
  97. goto SEND_PACKET;
  98. // Loop over inputs
  99. int inputs_left = conf->inputs->items;
  100. while (inputs_left--) {
  101. inpt = inpt->next;
  102. INPUT *r = inpt->data;
  103. if (!r || !r->buf)
  104. continue;
  105. // Move pcrs || Move & rewrite prcs
  106. if (conf->pcr_mode == 1 || conf->pcr_mode == 3) {
  107. // Is there any data in this input?
  108. data = cbuf_peek(r->buf, TS_PACKET_SIZE, &data_size);
  109. if (data_size == TS_PACKET_SIZE) {
  110. uint16_t pid = ts_packet_get_pid(data);
  111. // Do we have PCR packet?
  112. if (pid == r->output_pcr_pid && ts_packet_has_pcr(data)) {
  113. if (r->output_pcr_packets_needed > 0 && r->outputed_packets < r->output_pcr_packets_needed) {
  114. data = NULL;
  115. data_size = 0;
  116. continue;
  117. }
  118. /*
  119. LOGf("%10s | pcr:%15llu last_pcr:%15llu diff:%10lld packets:%5d needed_packs:%d diff:%d\n",
  120. r->channel->id,
  121. r->output_pcr,
  122. r->output_last_pcr,
  123. r->output_pcr - r->output_last_pcr,
  124. r->outputed_packets,
  125. r->output_pcr_packets_needed,
  126. r->outputed_packets - r->output_pcr_packets_needed
  127. );
  128. */
  129. uint64_t last_last_pcr = r->output_last_pcr;
  130. r->output_last_pcr = r->output_pcr;
  131. r->output_pcr = ts_packet_get_pcr(data);
  132. if (last_last_pcr)
  133. r->output_pcr_packets_needed = round(conf->output_bitrate / 8 * (r->output_pcr - r->output_last_pcr) / 27000000 / 188);
  134. r->outputed_packets = 0;
  135. }
  136. data = cbuf_get(r->buf, TS_PACKET_SIZE, &data_size);
  137. if (data_size == TS_PACKET_SIZE) // We have our data, no need to look at other inputs
  138. break;
  139. }
  140. // Do not move PCRs
  141. } else {
  142. data = cbuf_get(r->buf, TS_PACKET_SIZE, &data_size);
  143. if (data_size == TS_PACKET_SIZE) // We have our data, no need to look at other inputs
  144. break;
  145. }
  146. } // while (inputs_left--)
  147. // We have data. Mix it with NULLs and stuff it in the output buffer
  148. // If the have no data, the output buffer will automaticaly be left
  149. // with NULL packets
  150. SEND_PACKET:
  151. if (data && data_size == TS_PACKET_SIZE) {
  152. // Mix data with NULLs
  153. if (nulls < null_packets_count) {
  154. if (round(nulls * data_per_null) < round(data_packets * null_per_data)) {
  155. nulls += round(data_packets * null_per_data) - round(nulls * data_per_null);
  156. }
  157. if (nulls > null_packets_count)
  158. nulls = null_packets_count;
  159. }
  160. if (data_packets+nulls >= o_maxpackets) { // Can't happen
  161. LOGf("wtf: %d packets:%d\n", data_packets+nulls, o_maxpackets);
  162. break;
  163. }
  164. uint8_t *bufptr = curbuf->buf + ((data_packets + nulls) * TS_PACKET_SIZE);
  165. memcpy(bufptr, data, TS_PACKET_SIZE);
  166. }
  167. // Increase sended packets
  168. list_for_each(conf->inputs, lr, lrtmp) {
  169. INPUT *r = lr->data;
  170. r->outputed_packets++;
  171. }
  172. }
  173. NEXT_BUFFER:
  174. list_unlock(conf->inputs);
  175. curbuf->status = obuf_full; // Mark buffer as full
  176. buf_in_use = buf_in_use ? 0 : 1; // Switch buffer
  177. }
  178. OUT:
  179. LOG("OUTPUT: MIX thread stopped.\n");
  180. o->dienow++;
  181. LNODE *l, *tmp;
  182. list_for_each(conf->inputs, l, tmp) {
  183. INPUT *r = l->data;
  184. r->dienow = 1;
  185. }
  186. return 0;
  187. }