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_write.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <signal.h>
  4. #include <sys/time.h>
  5. #include <errno.h>
  6. #include <math.h>
  7. #include "libfuncs/io.h"
  8. #include "libfuncs/log.h"
  9. #include "libfuncs/list.h"
  10. #include "libtsfuncs/tsfuncs.h"
  11. #include "sleep.h"
  12. #include "data.h"
  13. #include "config.h"
  14. #include "network.h"
  15. void increase_process_priority() {
  16. return;
  17. #ifdef __linux__
  18. struct sched_param param;
  19. param.sched_priority = 99;
  20. if (sched_setscheduler(0, SCHED_FIFO, &param)==-1) {
  21. log_perror("sched_setscheduler() failed!", errno);
  22. } else {
  23. LOGf("PRIO : sched_setschedule() succeded.\n");
  24. }
  25. #endif
  26. }
  27. void ts_frame_process(CONFIG *conf, OUTPUT *o, uint8_t *data) {
  28. int i;
  29. uint16_t pid;
  30. uint8_t *ts_packet;
  31. for (i=0; i<FRAME_PACKET_SIZE; i+=TS_PACKET_SIZE) {
  32. ts_packet = data + i;
  33. pid = ts_packet_get_pid(ts_packet);
  34. if (pid == 0x1fff) // NULL packet
  35. o->padding_period += TS_PACKET_SIZE;
  36. if (ts_packet_has_pcr(ts_packet)) {
  37. uint64_t pcr = ts_packet_get_pcr(ts_packet); // Current PCR
  38. uint64_t new_pcr = pcr;
  39. uint64_t bytes = o->traffic + i;
  40. if (o->last_pcr[pid]) {
  41. uint64_t old_pcr = o->last_pcr[pid];
  42. uint64_t old_org_pcr = o->last_org_pcr[pid];
  43. uint64_t old_bytes = o->last_traffic[pid];
  44. if (old_org_pcr < pcr) { // Detect PCR wraparound
  45. new_pcr = old_pcr + (double)((bytes - old_bytes) * 8 * 27000000) / o->output_bitrate;
  46. // Rewrite pcrs || Move pcrs & rewrite prcs
  47. if (conf->pcr_mode == 2 || conf->pcr_mode == 3) {
  48. ts_packet_set_pcr(ts_packet, new_pcr);
  49. }
  50. if (conf->debug) {
  51. uint64_t ts_rate = (double)(((bytes - old_bytes) * 8) * 27000000) / (pcr - old_org_pcr);
  52. uint64_t ts_rate_new = (double)(((bytes - old_bytes) * 8) * 27000000) / (new_pcr - old_pcr);
  53. LOGf("PCR[%03x]: old:%14llu new:%14llu pcr_diff:%8lld ts_rate:%9llu ts_rate_new:%9llu diff:%9lld | passed:%llu\n",
  54. pid,
  55. pcr,
  56. new_pcr,
  57. pcr - new_pcr,
  58. ts_rate,
  59. ts_rate_new,
  60. ts_rate - ts_rate_new,
  61. bytes - old_bytes
  62. );
  63. }
  64. }
  65. } else {
  66. // if (config->debug) {
  67. // LOGf("PCR[%03x]: %10llu init\n", pid, pcr);
  68. // }
  69. }
  70. o->last_pcr[pid] = new_pcr;
  71. o->last_org_pcr[pid] = pcr;
  72. o->last_traffic[pid] = bytes;
  73. }
  74. }
  75. }
  76. ssize_t ts_frame_write(OUTPUT *o, uint8_t *data) {
  77. ssize_t written;
  78. written = fdwrite(o->out_sock, (char *)data, FRAME_PACKET_SIZE);
  79. if (written >= 0) {
  80. o->traffic += written;
  81. o->traffic_period += written;
  82. }
  83. if (o->ofd)
  84. write(o->ofd, data, FRAME_PACKET_SIZE);
  85. return written;
  86. }
  87. void * output_handle_write(void *_config) {
  88. CONFIG *conf = _config;
  89. OUTPUT *o = conf->output;
  90. int buf_in_use = 0;
  91. unsigned int o_datasize = 0;
  92. struct timeval stats_ts, now;
  93. struct timeval start_write_ts, end_write_ts, used_ts;
  94. unsigned long long stats_interval;
  95. signal(SIGPIPE, SIG_IGN);
  96. increase_process_priority();
  97. gettimeofday(&stats_ts, NULL);
  98. while (!o->dienow) {
  99. gettimeofday(&now, NULL);
  100. OBUF *curbuf = &o->obuf[buf_in_use];
  101. while (curbuf->status != obuf_full) { // Wait untill the buffer is ready ot it is already emptying
  102. if (o->dienow)
  103. goto OUT;
  104. //LOGf("MIX: Waiting for obuf %d\n", buf_in_use);
  105. usleep(1);
  106. }
  107. curbuf->status = obuf_emptying; // Mark buffer as being filled
  108. // Show stats
  109. stats_interval = timeval_diff_msec(&stats_ts, &now);
  110. if (stats_interval > conf->timeouts.stats) {
  111. stats_ts = now;
  112. double out_kbps = (double)(o->traffic_period * 8) / 1000;
  113. double out_mbps = (double)out_kbps / 1000;
  114. double opadding = ((double)o->padding_period / o->traffic_period) * 100;
  115. if (!conf->quiet) {
  116. LOGf("STAT : Pad:%6.2f%% Traf:%5.2f Mbps | %8.2f | %7llu\n",
  117. opadding,
  118. out_mbps,
  119. out_kbps,
  120. o->traffic_period
  121. );
  122. }
  123. o->traffic_period = 0;
  124. o->padding_period = 0;
  125. o_datasize = 0;
  126. }
  127. gettimeofday(&start_write_ts, NULL);
  128. int packets_written = 0, real_sleep_time = conf->output_tmout - conf->usleep_overhead;
  129. long time_taken, time_diff, real_time, overhead = 0, overhead_total = 0;
  130. ssize_t written = 0;
  131. while (curbuf->written < curbuf->size) {
  132. if (o->dienow)
  133. goto OUT;
  134. long sleep_interval = conf->output_tmout;
  135. uint8_t *ts_frame = curbuf->buf + curbuf->written;
  136. ts_frame_process(conf, o, ts_frame); // Fix PCR and count NULL packets
  137. written += ts_frame_write(o, ts_frame); // Write packet to network/file
  138. curbuf->written += FRAME_PACKET_SIZE;
  139. if (packets_written) {
  140. time_taken = timeval_diff_usec(&start_write_ts, &used_ts);
  141. real_time = packets_written * (conf->output_tmout + conf->usleep_overhead);
  142. time_diff = real_time - time_taken;
  143. overhead = (time_taken / packets_written) - sleep_interval;
  144. overhead_total += overhead;
  145. /*
  146. LOGf("[%5d] time_taken:%5ld real_time:%5ld time_diff:%ld | overhead:%5ld overhead_total:%5ld\n",
  147. packets_written,
  148. time_taken,
  149. real_time,
  150. time_diff,
  151. overhead,
  152. overhead_total
  153. );
  154. */
  155. if (time_diff > real_sleep_time) {
  156. sleep_interval = time_diff - conf->usleep_overhead;
  157. if (sleep_interval < 0)
  158. sleep_interval = 1;
  159. // LOGf("Add sleep. time_diff: %ld sleep_interval: %ld\n", time_diff, sleep_interval);
  160. } else {
  161. //LOGf("Skip sleep %ld\n", time_diff);
  162. sleep_interval = 0;
  163. }
  164. }
  165. if (sleep_interval > 0)
  166. usleep(sleep_interval);
  167. gettimeofday(&used_ts, NULL);
  168. packets_written++;
  169. }
  170. gettimeofday(&end_write_ts, NULL);
  171. unsigned long long write_time = timeval_diff_usec(&start_write_ts, &end_write_ts);
  172. if (write_time < o->obuf_ms * 1000) {
  173. //LOGf("Writen for -%llu us less\n", o->obuf_ms*1000 - write_time);
  174. usleep(o->obuf_ms*1000 - write_time);
  175. } else {
  176. //LOGf("Writen for +%llu us more\n", write_time - o->obuf_ms*1000);
  177. }
  178. obuf_reset(curbuf); // Buffer us all used up
  179. buf_in_use = buf_in_use ? 0 : 1; // Switch buffer
  180. if (written < 0) {
  181. LOG("OUTPUT: Error writing into output socket.\n");
  182. shutdown_fd(&o->out_sock);
  183. connect_output(o);
  184. }
  185. }
  186. OUT:
  187. LOG("OUTPUT: WRITE thread stopped.\n");
  188. o->dienow++;
  189. return 0;
  190. }