【发布时间】:2014-11-19 09:37:05
【问题描述】:
语言:C / ENV:Linux
我正在开发一个流媒体引擎,目前我可以启动、停止和暂停流媒体,但是 seeking 是让我很头疼的操作,我已经在这里问了一个问题之前并从答案中修复了代码中的一些问题。
使用 lseek() 函数,我将打开的流文件描述符作为第一个参数传递,另外我使用 UDP 进行传输,类似于以下代码:
transport_fd = open(tsfile, O_RDONLY);
int offset = 1024;
off_t offsetIndicator;
if ((offsetIndicator=lseek(transport_fd, offset, SEEK_CUR))<0) printf("Error seeking\n");
每当我在流式传输时尝试搜索,流式传输就会停止并且图片挂起。
有什么我应该注意的吗?,即:像在寻找文件后尝试 sleep() 或 nanosleep() 以便更改生效。
我找不到此类引擎中最佳实践的示例、论文或相关文章。
编辑:
经过测试,似乎文件继续流,但网络上的接收设备不再捕获流连接,并计算减去搜索时间后完成的时间,流似乎正常完成。
代码片段:
while (!completed)
{
while (/* Comparing conditions */ && !completed)
{
if (seekLck == 1) // seekLck is a semaphore to test seek signal from father process initiated by 0
{
int offset = 1024;
off_t offsetIndicator;
if ((offsetIndicator=lseek(transport_fd, offset, SEEK_CUR))<0)
printf("Error seeking\n");
nanosleep(&nano_sleep_packet, 0); //Try to sleep to see if it is still hanging, didn't work
seekLck = 0;
}
len = read(transport_fd, send_buf, packet_size);
if(len < 0) {
fprintf(stderr, "File read error \n");
completed = 1;
}
else if (len == 0)
{
fprintf(stderr, "Sent done\n");
completed = 1;
}
else
{
sent = sendto(sockfdstr, send_buf, len, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
if(sent <= 0)
{
perror("send(): error ");
completed = 1;
}
}
}
nanosleep(&nano_sleep_packet, 0);
}
close(transport_fd);
close(sockfdstr);
free(send_buf);
printf("cleaning up\n");
return 0;
}
【问题讨论】:
-
(不是专家,但是...)阅读 lseek 的手册页,如果失败,它应该填充
errno。请参阅 lseek#errors man page 和 errno manpage。这可能会产生更多信息。 -
搜索没有错误,从我的代码中应该会打印错误信息,搜索后我检查了指针位置,它是准确的,但是流挂了,所以我正在寻找意见关于那里可能出了什么问题!
-
你的问题的性质肯定是特定于你的服务器实现的,也可能是你的客户端实现的。
lseek()本身不太可能成为问题。你没有给我们任何我们可以使用的东西。 -
没错,我用代码sn-ps编辑了帖子,我想这不会有帮助,因为我猜问题出在接收端,另一个问题是接收端是不可编辑,即:接收盒和加扰器,所以我必须在寻找之前从服务器端进行一些调整。