Changeset 5174:c24a2089056a in roaraudio for plugins/alsavs/thread.c


Ignore:
Timestamp:
10/22/11 12:11:33 (13 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added some experimental fork of the old alsa plugin using VS API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/alsavs/thread.c

    r5157 r5174  
    4242 
    4343 
     44 
     45/* 
     46 * The following function should be re-written: 
     47 * write in a loop untill all date is written, 
     48 * if vs write returns less than requested call the thread and ask it to iterate. 
     49 */ 
     50 
    4451// Writes to the FIFO buffer. Waits until there is room to write. 
    4552size_t roar_plugin_write(struct roar_alsa_pcm *self, const char *buf, size_t size) { 
     53 ssize_t ret; 
     54 
     55 ROAR_DBG("roar_plugin_write(self=%p, buf=%p, size=%llu) = ?", self, buf, (long long unsigned int)size); 
     56 
     57#if 0 
    4658 /* Wait until we have a ready buffer */ 
    4759 while (1) { 
     
    5264  //fprintf(stderr, "%d + %d : %d\n", (int)self->bufptr, (int)size, (int)self->bufsize); 
    5365  pthread_mutex_lock(&(self->lock)); 
    54   if (self->bufptr + size <= self->bufsize) { 
     66  if (roar_vs_get_avail_write(self->vss, NULL) >= (ssize_t)size) { 
    5567   pthread_mutex_unlock(&(self->lock)); 
    5668   break; 
     
    5870  pthread_mutex_unlock(&(self->lock)); 
    5971 
     72  ROAR_DBG("roar_plugin_write(self=%p, buf=%p, size=%llu): Send signal", self, buf, (long long unsigned int)size); 
    6073  pthread_cond_signal(&(self->cond)); 
    6174  /* Sleeps until we can write to the FIFO. */ 
     75  ROAR_DBG("roar_plugin_write(self=%p, buf=%p, size=%llu): Waiting for answer", self, buf, (long long unsigned int)size); 
    6276  pthread_mutex_lock(&(self->cond_lock)); 
    6377  pthread_cond_wait(&(self->cond), &(self->cond_lock)); 
    6478  pthread_mutex_unlock(&(self->cond_lock)); 
    6579 } 
    66  
    67  pthread_mutex_lock(&(self->lock)); 
    68  memcpy(self->buffer + self->bufptr, buf, size); 
    69  self->bufptr += (int)size; 
    70  pthread_mutex_unlock(&(self->lock)); 
    71  
    72  /* Send signal to thread that buffer has been updated */ 
    73  pthread_cond_signal(&(self->cond)); 
     80#endif 
     81 
     82 
     83 while (size) { 
     84  pthread_mutex_lock(&(self->lock)); 
     85  ret = roar_vs_write(self->vss, buf, size, NULL); 
     86  pthread_mutex_unlock(&(self->lock)); 
     87 
     88  if ( ret == -1 ) { 
     89   ROAR_WARN("roar_plugin_write(self=%p, buf=?, size=?): Can not write data: %s", self, roar_error2str(roar_error)); 
     90   return -1; 
     91  } 
     92 
     93  size -= ret; 
     94  buf  += ret; 
     95 
     96  /* Send signal to thread that buffer has been updated */ 
     97  ROAR_DBG("roar_plugin_write(self=%p, buf=%p, size=%llu): Send signal", self, buf, (long long unsigned int)size); 
     98  pthread_cond_signal(&(self->cond)); 
     99 } 
    74100 
    75101 return size; 
     
    83109// Attemps to drain the buffer at all times and write to libroar. 
    84110// If there is no data, it will wait for roar_plugin_write() to fill up more data. 
    85 void* roar_plugin_thread (void * thread_data) { 
     111void * roar_plugin_thread (void * thread_data) { 
    86112 /* We share data between thread and callable functions */ 
    87113 struct roar_alsa_pcm *self = thread_data; 
    88114 int rc; 
     115 int err; 
     116 
     117 ROAR_DBG("roar_plugin_thread(thread_data=%p) = ?", thread_data); 
     118 pthread_mutex_unlock(&(self->lock)); // we are now up. 
    89119 
    90120 /* Plays back data as long as there is data in the buffer. Else, sleep until it can. */ 
    91121 /* Two loops! :3 Beware! */ 
    92122 while(1) { 
     123  ROAR_DBG("roar_plugin_thread(thread_data=%p): got woken up.", thread_data); 
    93124  while(1) { 
    94125   _TEST_CANCEL(); 
     
    96127   // We only bother to check after 1 sec of audio has been played, as it might be quite inaccurate in the start of the stream. 
    97128 
     129   // do wee need to lock before this? 
     130   pthread_mutex_lock(&(self->lock)); 
     131   ROAR_DBG("roar_plugin_thread(thread_data=%p): Start iterate", thread_data); 
     132   rc = roar_vs_iterate(self->vss, ROAR_VS_WAIT, &err); 
     133   ROAR_DBG("roar_plugin_thread(thread_data=%p): Ended iterate", thread_data); 
     134   pthread_mutex_unlock(&(self->lock)); 
     135 
     136   if ( rc == 0 ) { 
     137    break; 
     138   } else if ( rc < 0 ) { 
     139    _TEST_CANCEL(); 
     140    roar_plugin_reset(self); 
     141 
     142    /* Wakes up a potentially sleeping fill_buffer() */ 
     143    pthread_cond_signal(&(self->cond)); 
     144 
     145    /* This thread will not be joined, so detach. */ 
     146    pthread_detach(pthread_self()); 
     147    pthread_exit(NULL); 
     148   } 
     149 
     150   pthread_cond_signal(&(self->cond)); 
     151 
     152#if 0 
    98153   /* If the buffer is empty or we've stopped the stream. Jump out of this for loop */ 
    99154   pthread_mutex_lock(&(self->lock)); 
     
    139194   /* Buffer has decreased, signal fill_buffer() */ 
    140195   pthread_cond_signal(&(self->cond)); 
    141  
    142   } 
     196#endif 
     197 
     198  } 
     199  ROAR_DBG("roar_plugin_thread(thread_data=%p): going to sleep again.", thread_data); 
    143200 
    144201  /* If we're still good to go, sleep. We are waiting for fill_buffer() to fill up some data. */ 
    145202test_quit: 
    146203  if ( self->thread_active ) { 
    147    pthread_cond_signal(&(self->cond)); 
     204   //struct timespec abstime = {0, 1000*1000*100}; 
     205   pthread_cond_signal(&(self->cond)); 
     206   ROAR_DBG("roar_plugin_thread(thread_data=%p): Wating for wakeup", thread_data); 
    148207   pthread_mutex_lock(&(self->cond_lock)); 
    149208   pthread_cond_wait(&(self->cond), &(self->cond_lock)); 
     
    159218 
    160219void roar_plugin_drain(struct roar_alsa_pcm *self) { 
     220#if 0 
    161221 struct timespec now_tv; 
    162222 int64_t temp, temp2; 
     
    183243  self->bytes_in_buffer = self->bufptr; 
    184244 } 
     245#endif 
     246 ROAR_DBG("roar_plugin_drain(self=%p) = (void)", self); 
    185247} 
    186248 
Note: See TracChangeset for help on using the changeset viewer.