简体   繁体   English

如何通过c编写一个可以改变服务器上视频/音频进度的服务器?

[英]How to write a server by c that can change the progress of video/audio on the server?

How to write a server by c that can change the progress of video/audio on the server?如何通过c编写一个可以改变服务器上视频/音频进度的服务器?

I write a server program by c, but when I using javascript to change currentTime of <video> element which getting source from this c server, the video will always start at begin.我通过 c 编写了一个服务器程序,但是当我使用 javascript 来更改从该 c 服务器获取源的<video>元素的currentTime时,视频将始终在 begin.

The code of responding file request is as below:响应文件请求的代码如下:

void send_file()
{
  // count file length
  int fd = open(file, O_RDONLY);
  int file_len = lseek(fd, 0, SEEK_END);

  // send http header
  if (strcmp(type, ".mp4") == 0) {
    sprintf(type, "video/mp4");
  } else {
    // ...
  }
  sprintf(head, 
      "HTTP/1.1 200 OK\r\n"
      "Content-Type: %s\r\n"
      "Content-Length: %d\r\n"
      "\r\n"
      , type
      , file_len
      );
  send(c_sock, head, strlen(head), 0);

  // send file content
  lseek(fd, 0, SEEK_SET);
  memset(msg, 0, sizeof(msg));

  int delta = 0;
  while (delta < file_len) {// read by lines
    memset(msg, 0, 4096);
    int size = read(fd, msg, 1024);
    delta += size;
    // send(c_sock, msg, strlen(msg), 0);
    send_helper(msg, size);
  }

  close(fd);
}

void send_helper(char *content, int size)
{
  while (size > 0) {
    int delta = send(c_sock, content, size, 0);
    if (delta <= 0) return;
    size -= delta;
    content += delta;
  }
}

and I'm using我正在使用

  while (1) {
    c_sock = accept(s_sock, NULL, NULL);
    if (c_sock != -1) {
      int nread = recv(c_sock, buf, sizeof(buf), 0);
    }
  }

to receive the request from browser.接收来自浏览器的请求。

When requesting a mp4 file, the request and response header will be请求mp4文件时,请求和响应 header 将是

GET /source/1.mp4 HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
Accept-Encoding: identity;q=1, *;q=0
Accept: */*
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: video
Referer: http://127.0.0.1:8000/
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7,fr;q=0.6
Range: bytes=0-
HTTP/1.1 200 OK
Content-Type: video/mp4
Content-Length: 105270886

When using the apache server, the headers will be使用apache服务器时,headers 将是

Accept: */*
Accept-Encoding: identity;q=1, *;q=0
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7,fr;q=0.6
Connection: keep-alive
Host: 127.0.0.1
If-Modified-Since: Sat, 08 Aug 2020 11:04:55 GMT
If-None-Match: "6464e66-5ac5bb1097630"
Range: bytes=0-4653055
Referer: http://127.0.0.1/
Sec-Fetch-Dest: video
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
HTTP/1.1 206 Partial Content
Date: Sun, 09 Aug 2020 05:09:46 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Sat, 08 Aug 2020 11:04:55 GMT
Accept-Ranges: bytes
ETag: "6464e66-5ac5bb1097630"
Content-Type: video/mp4
Content-Range: bytes 58392576-105270885/105270886
Content-Length: 46878310

Is this problem (video always start at begin) only because the request and response head(for example Content-Range )?这个问题(视频总是从开头开始)仅仅是因为请求和响应头(例如Content-Range )吗? How to solve this problem?如何解决这个问题呢?

Thanks.谢谢。

Your server must declare that it accepts a range with Accept-Ranges: bytes and then it must process the Range header from the client, provide only the requested part and declare this in the Content-Range header - exactly like you see in the requests send to Apache.您的服务器必须声明它接受带有Accept-Ranges: bytes ,然后它必须处理来自客户端的Range header,仅提供请求的部分并在Content-Range header 中声明这一点 - 就像您在请求发送中看到的一样至 Apache。

For the exact but necessary details please follow the actual standard, ie RFC 7233 - Hypertext Transfer Protocol (HTTP/1.1): Range Requests .有关确切但必要的详细信息,请遵循实际标准,即RFC 7233 - 超文本传输协议 (HTTP/1.1):范围请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM