示例#1
0
void * FileThread(void * v_arg)
{
	ringbuffer_data_t vec[2];
	size_t readsize;
	unsigned int filecount = 0;
	const unsigned long long splitsize = (limit / TS_SIZE) * TS_SIZE;
	unsigned long long remfile=0;
	int fd2 = -1;
	ringbuffer_t * ringbuf = ((struct filenames_t *)v_arg)->ringbuffer;

	while (1)
	{
		ringbuffer_get_read_vector(ringbuf, &(vec[0]));
		readsize = vec[0].len + vec[1].len;
		if (readsize)
		{
			// Do Splitting if necessary
			if (remfile == 0)
			{
				char filename[FILENAMEBUFFERSIZE];

				sprintf(filename, "%s.%3.3d.%s", myfilename, ++filecount, ((struct filenames_t *)v_arg)->extension);
				if (fd2 != -1)
					close(fd2);
				if ((fd2 = open(filename, use_o_sync ? O_WRONLY | O_CREAT | O_SYNC | O_TRUNC | O_LARGEFILE : O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
				{
					perror("[stream2file]: error opening outfile");
					exit_flag = STREAM2FILE_STATUS_WRITE_OPEN_FAILURE;
					pthread_exit(NULL);
				}
				remfile = splitsize;
			}

			/* make sure file contains complete TS-packets and is <= splitsize */
			if ((unsigned long long)readsize > remfile)
			{ 
				readsize = remfile;

				if (vec[0].len > readsize)
					vec[0].len = readsize;

				vec[1].len = readsize - vec[0].len;
			}

			ssize_t written;

			while (1)
			{
				if ((written = write(fd2, vec[0].buf, vec[0].len)) < 0)
				{
					if (errno != EAGAIN)
					{
						exit_flag = STREAM2FILE_STATUS_WRITE_FAILURE;
						perror("[stream2file]: error in write");
						goto terminate_thread;
					}
				}
				else
				{
					ringbuffer_read_advance(ringbuf, written);
					
					if (vec[0].len == (size_t)written)
					{
						if (vec[1].len == 0)
						{
							goto all_bytes_written;
						}
						
						vec[0] = vec[1];
						vec[1].len = 0;
					}
					else
					{
						vec[0].len -= written;
						vec[0].buf += written;
					}
				}
			}

		all_bytes_written:
			if (use_fdatasync)
				fdatasync(fd2);
			
			remfile -= (unsigned long long)readsize;
		}
		else
		{
			if (exit_flag != STREAM2FILE_STATUS_RUNNING)
				goto terminate_thread;
			usleep(1000);
		}
	}
 terminate_thread:
	if (fd2 != -1)
		close (fd2);

	pthread_exit(NULL);
}
示例#2
0
void cRecord::FileThread()
{
	ringbuffer_data_t vec[2];
	size_t readsize;

fprintf(stderr, "%s:%s >\n", __FILE__, __FUNCTION__);
	ringbuffer_t * ringbuf = ringbuffer;
	while (1)
	{
		ringbuffer_get_read_vector(ringbuf, &(vec[0]));
		readsize = vec[0].len + vec[1].len;
		if (readsize)
		{
			ssize_t written;

			while (1)
			{
				if ((written = write(file_fd, vec[0].buf, vec[0].len)) < 0)
				{
					if (errno != EAGAIN)
					{
						exit_flag = STREAM2FILE_STATUS_WRITE_FAILURE;
						perror("[stream2file]: error in write");
						goto terminate_thread;
					}
				}
				else
				{
					ringbuffer_read_advance(ringbuf, written);
					
					if (vec[0].len == (size_t)written)
					{
						if (vec[1].len == 0)
						{
							goto all_bytes_written;
						}
						
						vec[0] = vec[1];
						vec[1].len = 0;
					}
					else
					{
						vec[0].len -= written;
						vec[0].buf += written;
					}
				}
			}

all_bytes_written:
;
//fixme: do we need this			if (use_fdatasync)
//				fdatasync(fd2);
			
		}
		else
		{
			if (exit_flag != STREAM2FILE_STATUS_RUNNING)
				goto terminate_thread;
			usleep(1000);
		}
	}
 terminate_thread:

fprintf(stderr, "%s:%s <\n", __FILE__, __FUNCTION__);
	pthread_exit(NULL);
}
示例#3
0
void *FileThread (void *v_arg)
{
	ringbuffer_data_t vec[2];
	size_t readsize, maxreadsize=0;
	unsigned int filecount = 0;
	const unsigned long long splitsize=((1024*1024*1024)/TS_SIZE)*TS_SIZE * limit; // 1GB%188
	unsigned long long remfile=0;
	char filename[512];
	time_t timer1 = 0;
	unsigned long long filesize2 = 0;
	unsigned int bitrate = 0;

	while (1)
	{
		ringbuffer_get_read_vector(ringbuf, &(vec[0]));
		readsize = vec[0].len + vec[1].len;
		if (readsize)
		{
			if ((!silent)&&(readsize > maxreadsize))
			{
				maxreadsize = readsize;
			}

			// Do Splitting if necessary
			if (remfile == 0)
			{
				sprintf(filename, "%s.%3.3d.ts", (char *)v_arg, ++filecount);
				if (fd2 != -1)
					close(fd2);
				if ((fd2 = open(filename, O_WRONLY | O_CREAT | O_SYNC | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
				{
					perror("[streamfile]: opening outfile");
					exit_flag = 1;
					pthread_exit(NULL);
				}
				remfile = splitsize;
				timer1 = time(NULL);
			}

			/* make sure file contains complete TS-packets and is <= splitsize */
			if ((unsigned long long)readsize > remfile)
			{ 
				readsize = remfile;

				if (vec[0].len > readsize)
					vec[0].len = readsize;

				vec[1].len = readsize - vec[0].len;
			}

			ssize_t written;

			while (1)
			{
				if ((written = write(fd2, vec[0].buf, vec[0].len)) < 0)
				{
					if (errno != EAGAIN)
					{
						exit_flag = 1;
						perror("[streamfile]: write");
						goto terminate_thread;
					}
				}
				else
				{
					ringbuffer_read_advance(ringbuf, written);
					
					if (vec[0].len == written)
					{
						if (vec[1].len == 0)
						{
							goto all_bytes_written;
						}
						
						vec[0] = vec[1];
						vec[1].len = 0;
					}
					else
					{
						vec[0].len -= written;
						vec[0].buf += written;
					}
				}
			}

		all_bytes_written:
			fdatasync(fd2);
			
			remfile -= (unsigned long long)readsize;
			if (!silent)
			{
				filesize2 += (unsigned long long)readsize;
			
				if ((time(NULL) - timer1) > 10)
				{
					bitrate = (filesize2 / (time(NULL) - timer1) * 8);
					printf("Datarate %d bits/sec, %d Kbits/sec, max. rb used %d bytes\n"
					       , (int)bitrate, (int)bitrate/1024, maxreadsize);
					filesize2 = 0;
					timer1 = time(NULL);
				}
			}
		}
		else
		{
			if (exit_flag)
				goto terminate_thread;
			usleep(1000);
		}
	}
 terminate_thread:
	if (fd2 != -1)
		close (fd2);

	pthread_exit(NULL);
}
示例#4
0
void * FileThread(void * v_arg)
{
	ringbuffer_data_t vec[2];
	size_t readsize;
	unsigned int filecount = 0;
	char radio_extension[5];
	const unsigned long long splitsize = (limit / TS_SIZE) * TS_SIZE;
	unsigned long long remfile=0;
	int fd2 = -1;
	ringbuffer_t * ringbuf = ((struct filenames_t *)v_arg)->ringbuffer;
	while (1)
	{
		ringbuffer_get_read_vector(ringbuf, &(vec[0]));
		readsize = vec[0].len + vec[1].len;
		if (readsize)
		{
			// Do Splitting if necessary
			if (remfile == 0)
			{
				char filename[FILENAMEBUFFERSIZE];
				int flags = O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE;
				if (use_o_sync)
					flags |= O_SYNC;

 retry:
				if (mymode == 2)
					// without extension .mp2 the Neutrino Audioplayer can not access the recording
					sprintf(radio_extension, "%s", ".mp2");
				else
					sprintf(radio_extension, "%s", "");
				sprintf(filename, "%s.%3.3d.%s%s", myfilename, ++filecount, ((struct filenames_t *)v_arg)->extension, radio_extension);
				printf("[stream2file] filename: '%s'\n"
				       "            myfilename: '%s'\n", filename, myfilename);
				if (fd2 != -1)
					close(fd2);

				if ((fd2 = open(filename, flags, REC_FILE_PERMISSIONS)) < 0)
				{
					if (errno == EEXIST) {
						printf("[stream2file] %s exists, retrying...\n", filename);
						goto retry;
					}
					perror("[stream2file]: error opening outfile");
					exit_flag = STREAM2FILE_STATUS_WRITE_OPEN_FAILURE;
					pthread_exit(NULL);
				}
				if( strstr(filename, ".ts") != NULL && gen_psi )
				{
					genpsi(fd2);
				}
				remfile = splitsize;
			}

			/* make sure file contains complete TS-packets and is <= splitsize */
			if ((unsigned long long)readsize > remfile)
			{ 
				readsize = remfile;

				if (vec[0].len > readsize)
					vec[0].len = readsize;

				vec[1].len = readsize - vec[0].len;
			}

			ssize_t written;

			while (1)
			{
				if ((written = write(fd2, vec[0].buf, vec[0].len)) < 0)
				{
					if (errno != EAGAIN)
					{
						exit_flag = STREAM2FILE_STATUS_WRITE_FAILURE;
						perror("[stream2file]: error in write");
						goto terminate_thread;
					}
				}
				else
				{
					ringbuffer_read_advance(ringbuf, written);
					
					if (vec[0].len == (size_t)written)
					{
						if (vec[1].len == 0)
						{
							goto all_bytes_written;
						}
						
						vec[0] = vec[1];
						vec[1].len = 0;
					}
					else
					{
						vec[0].len -= written;
						vec[0].buf += written;
					}
				}
			}

		all_bytes_written:
			if (use_fdatasync)
				fdatasync(fd2);
			
			remfile -= (unsigned long long)readsize;
		}
		else
		{
			if (exit_flag != STREAM2FILE_STATUS_RUNNING)
				goto terminate_thread;
			usleep(1000);
		}
	}
 terminate_thread:
	if (fd2 != -1)
		close (fd2);

	pthread_exit(NULL);
}