queues.c 8.49 KB
Newer Older
Cedric Roux's avatar
Cedric Roux committed
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
#include "queues.h"
#include "mobipass.h"

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>

#define QSIZE 10000

struct mobipass_header {
  uint16_t flags;
  uint16_t fifo_status;
  unsigned char seqno;
  unsigned char ack;
  uint32_t word0;
  uint32_t timestamp;
} __attribute__((__packed__));

struct queue {
  unsigned char buf[QSIZE][14+14+640*2];
  volatile int start;
  volatile int len;
  pthread_mutex_t mutex;
  pthread_cond_t cond;
};

typedef struct {
  struct queue to_mobipass;
  struct queue from_mobipass;
  int samples_per_1024_frames;

  /* variables used by dequeue_from_mobipass */
  int dequeue_from_mobipass_seqno;

  /* variables used to manage logging of "missing samples"
   * coming from mobipass. (Yes, this can happen, mostly
   * at startup.)
   * This idea is to print some logs, but not too much to
   * flood stdout. (This is already a bad idea to call
   * printf in a 'realtime' thread.)
   */
  int no_sample_log_running;
  uint32_t no_sample_log_start_sample;
  uint32_t no_sample_log_next_sample;
} queue_state_t;

static void enqueue(void *data, struct queue *q)
{
  int pos;

  if (pthread_mutex_lock(&q->mutex)) abort();
  if (q->len == QSIZE) {
    printf("mobipass: WARNING: enqueue: full\n");
    goto done;
  }

  pos = (q->start + q->len) % QSIZE;
  memcpy(q->buf[pos], data, 14+14+640*2);
  q->len++;

done:
  if (pthread_cond_signal(&q->cond)) abort();
  if (pthread_mutex_unlock(&q->mutex)) abort();
}

void enqueue_to_mobipass(void *_qstate, void *data)
{
  queue_state_t *qstate = _qstate;
  enqueue(data, &qstate->to_mobipass);
}

void dequeue_to_mobipass(void *_qstate, uint32_t timestamp, void *data)
{
  queue_state_t *qstate = _qstate;
  if (pthread_mutex_lock(&qstate->to_mobipass.mutex)) abort();
  while (qstate->to_mobipass.len == 0) {
    if (pthread_cond_wait(&qstate->to_mobipass.cond, &qstate->to_mobipass.mutex)) abort();
  }

  memcpy(data, qstate->to_mobipass.buf[qstate->to_mobipass.start], 14+14+640*2);
  qstate->to_mobipass.len--;
  qstate->to_mobipass.start = (qstate->to_mobipass.start + 1) % QSIZE;

  if (pthread_mutex_unlock(&qstate->to_mobipass.mutex)) abort();
}

void enqueue_from_mobipass(void *_qstate, void *data)
{
  queue_state_t *qstate = _qstate;
  struct mobipass_header *mh = (struct mobipass_header *)((char*)data+14);
  mh->timestamp = htonl(ntohl(mh->timestamp) % qstate->samples_per_1024_frames);
//printf("from mobipass! timestamp %u seqno %d\n", ntohl(mh->timestamp), mh->seqno);
  enqueue(data, &qstate->from_mobipass);
}

static int cmp_timestamps(uint32_t a, uint32_t b, int samples_per_1024_frames)
{
  if (a == b) return 0;
  if (a < b) {
    if (b-a > samples_per_1024_frames/2) return 1;
    return -1;
  }
  if (a-b > samples_per_1024_frames/2) return -1;
  return 1;
}

/*************************************************/
/* missing samples logging management begin      */
/*************************************************/

static void log_flush(queue_state_t *qstate)
{
  /* print now if there is something to print */
  if (qstate->no_sample_log_running == 0)
    return;
  qstate->no_sample_log_running = 0;
  printf("mobipass: WARNING: missing samples [%u-%u]\n",
      qstate->no_sample_log_start_sample,
      (uint32_t)(qstate->no_sample_log_next_sample-1));
}

static void log_missed_sample(queue_state_t *qstate, uint32_t timestamp)
{
  /* collect data, print if there is a discontinuity */
  if (qstate->no_sample_log_running == 0 ||
      timestamp != qstate->no_sample_log_next_sample) {
    log_flush(qstate);
    qstate->no_sample_log_start_sample = timestamp;
  }

  qstate->no_sample_log_next_sample = timestamp+1;
  qstate->no_sample_log_running = 1;
}

static void log_flush_if_old(queue_state_t *qstate, uint32_t timestamp)
{
  /* log every second (more or less), if we have to */
  /* note that if mobipass stopped, it may take much more
   * than one second to log, due to the sleeps done while
   * waiting for samples (that never come)
   */
  if (qstate->no_sample_log_running == 1 &&
      labs(timestamp-qstate->no_sample_log_start_sample) > qstate->samples_per_1024_frames/10)
    log_flush(qstate);
}

/*************************************************/
/* missing samples logging management end        */
/*************************************************/

/* to be called with lock on */
static void get_sample_from_mobipass(queue_state_t *qstate, char *I, char *Q, uint32_t timestamp)
{
  unsigned char *b = NULL;
  unsigned char *data = NULL;
  struct mobipass_header *mh = NULL;
  uint32_t packet_timestamp = 0;

#if 0
uint32_t old_start = qstate->from_mobipass.start;
uint32_t old_len = qstate->from_mobipass.len;
b = qstate->from_mobipass.buf[qstate->from_mobipass.start];
mh = (struct mobipass_header *)(b+14);
uint32_t old_pts = qstate->from_mobipass.len ? ntohl(mh->timestamp) : -1;
b=NULL;
mh=NULL;
#endif

  while (qstate->from_mobipass.len) {
    b = qstate->from_mobipass.buf[qstate->from_mobipass.start];
    mh = (struct mobipass_header *)(b+14);
    data = b + 14*2;
    packet_timestamp = ntohl(mh->timestamp);
    if (cmp_timestamps(timestamp, packet_timestamp, qstate->samples_per_1024_frames) < 0) goto nodata;
    if (cmp_timestamps(timestamp, (packet_timestamp+640) % qstate->samples_per_1024_frames, qstate->samples_per_1024_frames) < 0) break;
    qstate->from_mobipass.len--;
    qstate->from_mobipass.start = (qstate->from_mobipass.start+1) % QSIZE;
  }

  if (qstate->from_mobipass.len == 0) goto nodata;

  if (timestamp == (packet_timestamp + 639) % qstate->samples_per_1024_frames) {
    qstate->from_mobipass.len--;
    qstate->from_mobipass.start = (qstate->from_mobipass.start+1) % QSIZE;
  }

  if (timestamp < packet_timestamp) timestamp += qstate->samples_per_1024_frames;

  *I = data[(timestamp - packet_timestamp) * 2];
  *Q = data[(timestamp - packet_timestamp) * 2 + 1];

  return;

nodata:
  *I = 0;
  *Q = 0;

  log_missed_sample(qstate, timestamp);

#if 0
printf("no sample timestamp %u pt %u start %d old_start %d old_pt %u len %d old len %d\n", timestamp, packet_timestamp, qstate->from_mobipass.start, old_start, old_pts, qstate->from_mobipass.len, old_len);
#endif
}

/* doesn't work with delay more than 1s */
static void wait_for_data(pthread_cond_t *cond, pthread_mutex_t *mutex, int delay_us)
{
  struct timeval now;
  struct timespec target;
  gettimeofday(&now, NULL);
  target.tv_sec = now.tv_sec;
  target.tv_nsec = (now.tv_usec + delay_us) * 1000;
  if (target.tv_nsec >= 1000 * 1000 * 1000) { target.tv_nsec -= 1000 * 1000 * 1000; target.tv_sec++; }
  int err = pthread_cond_timedwait(cond, mutex, &target);
  if (err != 0 && err != ETIMEDOUT) { printf("mobipass: ERROR: pthread_cond_timedwait: err (%d) %s\n", err, strerror(err)); abort(); }
}

/* don't block infinitely when waiting for data
 * if waiting for too long, just return some zeros
 */
void dequeue_from_mobipass(void *_qstate, uint32_t timestamp, void *data)
{
  queue_state_t *qstate = _qstate;
  int i;
//  int ts = timestamp;
  int waiting_allowed;

  if (pthread_mutex_lock(&qstate->from_mobipass.mutex)) abort();

  if (qstate->from_mobipass.len == 0) {
//printf("sleep 1\n");
    wait_for_data(&qstate->from_mobipass.cond, &qstate->from_mobipass.mutex, 2000); //1000/3);
  }

  waiting_allowed = qstate->from_mobipass.len != 0;

  for (i = 0; i < 640*2; i+=2) {
    if (qstate->from_mobipass.len == 0 && waiting_allowed) {
//printf("sleep 2\n");
      wait_for_data(&qstate->from_mobipass.cond, &qstate->from_mobipass.mutex, 2000); //1000/3);
      waiting_allowed = qstate->from_mobipass.len != 0;
    }

    get_sample_from_mobipass(qstate, (char*)data + 14*2 + i, (char*)data + 14*2 + i+1, timestamp % qstate->samples_per_1024_frames);
    timestamp++;
  }

  log_flush_if_old(qstate, timestamp);

  if (pthread_mutex_unlock(&qstate->from_mobipass.mutex)) abort();

  struct mobipass_header *mh = (struct mobipass_header *)(((char *)data) + 14);
  mh->flags = 0;
  mh->fifo_status = 0;
  mh->seqno = qstate->dequeue_from_mobipass_seqno++;
  mh->ack = 0;
  mh->word0 = 0;
  mh->timestamp = htonl(timestamp);
}

void *init_queues(int samples_per_1024_frames)
{
  queue_state_t *q;
  q = malloc(sizeof(queue_state_t));
  if (q == NULL) abort();
  memset(q, 0, sizeof(queue_state_t));

  if (pthread_mutex_init(&q->to_mobipass.mutex, NULL)) abort();
  if (pthread_mutex_init(&q->from_mobipass.mutex, NULL)) abort();
  if (pthread_cond_init(&q->to_mobipass.cond, NULL)) abort();
  if (pthread_cond_init(&q->from_mobipass.cond, NULL)) abort();

  q->samples_per_1024_frames = samples_per_1024_frames;

  return q;
}