正在显示
2 个修改的文件
包含
65 行增加
和
47 行删除
| @@ -164,58 +164,18 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req) | @@ -164,58 +164,18 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req) | ||
| 164 | std::string fullpath = get_request_file(req); | 164 | std::string fullpath = get_request_file(req); |
| 165 | 165 | ||
| 166 | if (srs_string_ends_with(fullpath, ".ts")) { | 166 | if (srs_string_ends_with(fullpath, ".ts")) { |
| 167 | - // TODO: FIXME: use more advance cache. | ||
| 168 | - // for ts video large file, use bytes to write it. | ||
| 169 | - int fd = ::open(fullpath.c_str(), O_RDONLY); | ||
| 170 | - if (fd < 0) { | ||
| 171 | - ret = ERROR_HTTP_OPEN_FILE; | ||
| 172 | - srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); | ||
| 173 | - return ret; | ||
| 174 | - } | ||
| 175 | - | ||
| 176 | - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); | ||
| 177 | - ::lseek(fd, 0, SEEK_SET); | ||
| 178 | - | ||
| 179 | - // write http header for ts. | ||
| 180 | - std::stringstream ss; | ||
| 181 | - | ||
| 182 | - res_status_line(ss)->res_content_type_mpegts(ss) | ||
| 183 | - ->res_content_length(ss, (int)length); | ||
| 184 | - | ||
| 185 | - if (req->requires_crossdomain()) { | ||
| 186 | - res_enable_crossdomain(ss); | ||
| 187 | - } | ||
| 188 | - | ||
| 189 | - res_header_eof(ss); | ||
| 190 | - | ||
| 191 | - // flush http header to peer | ||
| 192 | - if ((ret = res_flush(skt, ss)) != ERROR_SUCCESS) { | ||
| 193 | - return ret; | 167 | + return response_ts_file(skt, req, fullpath); |
| 168 | + } else { | ||
| 169 | + return response_regular_file(skt, req, fullpath); | ||
| 194 | } | 170 | } |
| 195 | 171 | ||
| 196 | - // write body. | ||
| 197 | - int64_t left = length; | ||
| 198 | - char* buf = req->http_ts_send_buffer(); | ||
| 199 | - | ||
| 200 | - while (left > 0) { | ||
| 201 | - ssize_t nread = -1; | ||
| 202 | - // TODO: FIXME: use st_read. | ||
| 203 | - if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) { | ||
| 204 | - ::close(fd); | ||
| 205 | - ret = ERROR_HTTP_READ_FILE; | ||
| 206 | - srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); | ||
| 207 | return ret; | 172 | return ret; |
| 208 | - } | 173 | +} |
| 209 | 174 | ||
| 210 | - left -= nread; | ||
| 211 | - if ((ret = skt->write(buf, nread, NULL)) != ERROR_SUCCESS) { | ||
| 212 | - break; | ||
| 213 | - } | ||
| 214 | - } | ||
| 215 | - ::close(fd); | 175 | +int SrsHttpVhost::response_regular_file(SrsSocket* skt, SrsHttpMessage* req, string fullpath) |
| 176 | +{ | ||
| 177 | + int ret = ERROR_SUCCESS; | ||
| 216 | 178 | ||
| 217 | - return ret; | ||
| 218 | - } else { | ||
| 219 | // TODO: FIXME: refine the file stream. | 179 | // TODO: FIXME: refine the file stream. |
| 220 | int fd = ::open(fullpath.c_str(), O_RDONLY); | 180 | int fd = ::open(fullpath.c_str(), O_RDONLY); |
| 221 | if (fd < 0) { | 181 | if (fd < 0) { |
| @@ -261,8 +221,64 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req) | @@ -261,8 +221,64 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req) | ||
| 261 | } else { | 221 | } else { |
| 262 | return res_text(skt, req, str); | 222 | return res_text(skt, req, str); |
| 263 | } | 223 | } |
| 224 | + | ||
| 225 | + return ret; | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string fullpath) | ||
| 229 | +{ | ||
| 230 | + int ret = ERROR_SUCCESS; | ||
| 231 | + | ||
| 232 | + // TODO: FIXME: use more advance cache. | ||
| 233 | + // for ts video large file, use bytes to write it. | ||
| 234 | + int fd = ::open(fullpath.c_str(), O_RDONLY); | ||
| 235 | + if (fd < 0) { | ||
| 236 | + ret = ERROR_HTTP_OPEN_FILE; | ||
| 237 | + srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); | ||
| 238 | + return ret; | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); | ||
| 242 | + ::lseek(fd, 0, SEEK_SET); | ||
| 243 | + | ||
| 244 | + // write http header for ts. | ||
| 245 | + std::stringstream ss; | ||
| 246 | + | ||
| 247 | + res_status_line(ss)->res_content_type_mpegts(ss) | ||
| 248 | + ->res_content_length(ss, (int)length); | ||
| 249 | + | ||
| 250 | + if (req->requires_crossdomain()) { | ||
| 251 | + res_enable_crossdomain(ss); | ||
| 264 | } | 252 | } |
| 265 | 253 | ||
| 254 | + res_header_eof(ss); | ||
| 255 | + | ||
| 256 | + // flush http header to peer | ||
| 257 | + if ((ret = res_flush(skt, ss)) != ERROR_SUCCESS) { | ||
| 258 | + return ret; | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + // write body. | ||
| 262 | + int64_t left = length; | ||
| 263 | + char* buf = req->http_ts_send_buffer(); | ||
| 264 | + | ||
| 265 | + while (left > 0) { | ||
| 266 | + ssize_t nread = -1; | ||
| 267 | + // TODO: FIXME: use st_read. | ||
| 268 | + if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) { | ||
| 269 | + ::close(fd); | ||
| 270 | + ret = ERROR_HTTP_READ_FILE; | ||
| 271 | + srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); | ||
| 272 | + return ret; | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + left -= nread; | ||
| 276 | + if ((ret = skt->write(buf, nread, NULL)) != ERROR_SUCCESS) { | ||
| 277 | + break; | ||
| 278 | + } | ||
| 279 | + } | ||
| 280 | + ::close(fd); | ||
| 281 | + | ||
| 266 | return ret; | 282 | return ret; |
| 267 | } | 283 | } |
| 268 | 284 |
| @@ -70,6 +70,8 @@ protected: | @@ -70,6 +70,8 @@ protected: | ||
| 70 | virtual bool is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase); | 70 | virtual bool is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase); |
| 71 | virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req); | 71 | virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req); |
| 72 | private: | 72 | private: |
| 73 | + virtual int response_regular_file(SrsSocket* skt, SrsHttpMessage* req, std::string fullpath); | ||
| 74 | + virtual int response_ts_file(SrsSocket* skt, SrsHttpMessage* req, std::string fullpath); | ||
| 73 | virtual std::string get_request_file(SrsHttpMessage* req); | 75 | virtual std::string get_request_file(SrsHttpMessage* req); |
| 74 | public: | 76 | public: |
| 75 | virtual std::string vhost(); | 77 | virtual std::string vhost(); |
-
请 注册 或 登录 后发表评论