Changeset 5174:c24a2089056a in roaraudio for plugins/alsavs/thread.c
- Timestamp:
- 10/22/11 12:11:33 (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/alsavs/thread.c
r5157 r5174 42 42 43 43 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 44 51 // Writes to the FIFO buffer. Waits until there is room to write. 45 52 size_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 46 58 /* Wait until we have a ready buffer */ 47 59 while (1) { … … 52 64 //fprintf(stderr, "%d + %d : %d\n", (int)self->bufptr, (int)size, (int)self->bufsize); 53 65 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) { 55 67 pthread_mutex_unlock(&(self->lock)); 56 68 break; … … 58 70 pthread_mutex_unlock(&(self->lock)); 59 71 72 ROAR_DBG("roar_plugin_write(self=%p, buf=%p, size=%llu): Send signal", self, buf, (long long unsigned int)size); 60 73 pthread_cond_signal(&(self->cond)); 61 74 /* 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); 62 76 pthread_mutex_lock(&(self->cond_lock)); 63 77 pthread_cond_wait(&(self->cond), &(self->cond_lock)); 64 78 pthread_mutex_unlock(&(self->cond_lock)); 65 79 } 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 } 74 100 75 101 return size; … … 83 109 // Attemps to drain the buffer at all times and write to libroar. 84 110 // 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) {111 void * roar_plugin_thread (void * thread_data) { 86 112 /* We share data between thread and callable functions */ 87 113 struct roar_alsa_pcm *self = thread_data; 88 114 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. 89 119 90 120 /* Plays back data as long as there is data in the buffer. Else, sleep until it can. */ 91 121 /* Two loops! :3 Beware! */ 92 122 while(1) { 123 ROAR_DBG("roar_plugin_thread(thread_data=%p): got woken up.", thread_data); 93 124 while(1) { 94 125 _TEST_CANCEL(); … … 96 127 // 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. 97 128 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 98 153 /* If the buffer is empty or we've stopped the stream. Jump out of this for loop */ 99 154 pthread_mutex_lock(&(self->lock)); … … 139 194 /* Buffer has decreased, signal fill_buffer() */ 140 195 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); 143 200 144 201 /* If we're still good to go, sleep. We are waiting for fill_buffer() to fill up some data. */ 145 202 test_quit: 146 203 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); 148 207 pthread_mutex_lock(&(self->cond_lock)); 149 208 pthread_cond_wait(&(self->cond), &(self->cond_lock)); … … 159 218 160 219 void roar_plugin_drain(struct roar_alsa_pcm *self) { 220 #if 0 161 221 struct timespec now_tv; 162 222 int64_t temp, temp2; … … 183 243 self->bytes_in_buffer = self->bufptr; 184 244 } 245 #endif 246 ROAR_DBG("roar_plugin_drain(self=%p) = (void)", self); 185 247 } 186 248
Note: See TracChangeset
for help on using the changeset viewer.