shithub: opus-tools

Download patch

ref: 4aceec44d74880c18a266d0a8d37dbdf885fe0d2
parent: 4b0f423d0009c9616fbe11050e2b3959b5709eaa
author: Mark Harris <mark.hsj@gmail.com>
date: Sat Apr 28 08:28:04 EDT 2018

opusrtp: Avoid global variables for options

--- a/src/opusrtp.c
+++ b/src/opusrtp.c
@@ -66,8 +66,6 @@
 
 #define SNIFF_DEVICE "lo0"
 
-static uint8_t opus_payload_type = 120;
-
 /* state struct for passing around our handles */
 typedef struct {
   ogg_stream_state *stream;
@@ -75,6 +73,7 @@
   int seq;
   ogg_int64_t granulepos;
   int linktype;
+  int payload_type;
 } state;
 
 /* helper, write a little-endian 32 bit int to memory */
@@ -109,11 +108,8 @@
   p[1] = v & 0xff;
 }
 
-static int samplerate = 48000;
-static int channels = 2;
-
 /* manufacture a generic OpusHead packet */
-ogg_packet *op_opushead(void)
+ogg_packet *op_opushead(int samplerate, int channels)
 {
   int size = 19;
   unsigned char *data = malloc(size);
@@ -633,7 +629,8 @@
   return ret;
 }
 
-int rtp_send_file(const char *filename, const char *dest, int port)
+int rtp_send_file(const char *filename, const char *dest, int port,
+        int payload_type)
 {
   rtp_header rtp;
   int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -668,7 +665,7 @@
   }
 
   rtp.version = 2;
-  rtp.type = opus_payload_type;
+  rtp.type = payload_type;
   rtp.pad = 0;
   rtp.ext = 0;
   rtp.cc = 0;
@@ -773,10 +770,12 @@
   return 0;
 }
 #else /* _WIN32 */
-int rtp_send_file(const char *filename, const char *addr, int port)
+int rtp_send_file(const char *filename, const char *dest, int port,
+        int payload_type)
 {
   fprintf(stderr, "Cannot send '%s to %s:%d'. Socket support not available.\n",
-          filename, addr, port);
+          filename, dest, port);
+  (void)payload_type;
   return -2;
 }
 #endif
@@ -898,7 +897,7 @@
   }
   params->seq = rtp.seq;
 
-  if (rtp.type != opus_payload_type) {
+  if (rtp.type != params->payload_type) {
     fprintf(stderr, "skipping non-opus packet\n");
     return;
   }
@@ -922,7 +921,8 @@
 }
 
 /* use libpcap to capture packets and write them to a file */
-int sniff(const char *input_file, const char *output_file)
+int sniff(const char *input_file, const char *output_file, int payload_type,
+        int samplerate, int channels)
 {
   state *params;
   pcap_t *pcap;
@@ -968,6 +968,7 @@
   params->out = NULL;
   params->seq = 0;
   params->granulepos = 0;
+  params->payload_type = payload_type;
 
   if (output_file) {
     if (strcmp(output_file, "-") == 0) {
@@ -983,7 +984,7 @@
       return 1;
     }
     /* write stream headers */
-    op = op_opushead();
+    op = op_opushead(samplerate, channels);
     ogg_stream_packetin(params->stream, op);
     op_free(op);
     op = op_opustags();
@@ -1053,6 +1054,9 @@
   int pcap_mode = 0;
 #endif
   int port = 1234;
+  int payload_type = 120;
+  int samplerate = 48000;
+  int channels = 2;
   struct option long_options[] = {
     {"help", no_argument, NULL, 'h'},
     {"version", no_argument, NULL, 'V'},
@@ -1123,7 +1127,7 @@
         break;
       case 't':
         if (optarg)
-            opus_payload_type = atoi(optarg);
+            payload_type = atoi(optarg);
         break;
       case 'h':
         usage(argv[0]);
@@ -1145,7 +1149,7 @@
     }
 #endif
     for (i = optind; i < argc; i++) {
-      rtp_send_file(argv[i], dest, port);
+      rtp_send_file(argv[i], dest, port, payload_type);
     }
     return 0;
   }
@@ -1152,7 +1156,7 @@
 
 #ifdef HAVE_PCAP
   if (pcap_mode) {
-    return sniff(input_pcap, output_file);
+    return sniff(input_pcap, output_file, payload_type, samplerate, channels);
   }
 #endif