winlin

refine http/dvr/hls to use file reader/writer. 0.9.146

@@ -447,7 +447,7 @@ MODULE_ID="KERNEL" @@ -447,7 +447,7 @@ MODULE_ID="KERNEL"
447 MODULE_DEPENDS=("CORE") 447 MODULE_DEPENDS=("CORE")
448 ModuleLibIncs=(${SRS_OBJS}) 448 ModuleLibIncs=(${SRS_OBJS})
449 MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer" 449 MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer"
450 - "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec") 450 + "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec" "srs_kernel_file")
451 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh 451 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh
452 KERNEL_OBJS="${MODULE_OBJS[@]}" 452 KERNEL_OBJS="${MODULE_OBJS[@]}"
453 # 453 #
@@ -40,6 +40,7 @@ using namespace std; @@ -40,6 +40,7 @@ using namespace std;
40 #include <srs_app_http_hooks.hpp> 40 #include <srs_app_http_hooks.hpp>
41 #include <srs_kernel_codec.hpp> 41 #include <srs_kernel_codec.hpp>
42 #include <srs_kernel_flv.hpp> 42 #include <srs_kernel_flv.hpp>
  43 +#include <srs_kernel_file.hpp>
43 44
44 SrsFlvSegment::SrsFlvSegment() 45 SrsFlvSegment::SrsFlvSegment()
45 { 46 {
@@ -45,6 +45,7 @@ using namespace std; @@ -45,6 +45,7 @@ using namespace std;
45 #include <srs_app_pithy_print.hpp> 45 #include <srs_app_pithy_print.hpp>
46 #include <srs_kernel_utility.hpp> 46 #include <srs_kernel_utility.hpp>
47 #include <srs_app_avc_aac.hpp> 47 #include <srs_app_avc_aac.hpp>
  48 +#include <srs_kernel_file.hpp>
48 49
49 // max PES packets size to flush the video. 50 // max PES packets size to flush the video.
50 #define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024 51 #define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024
@@ -150,12 +151,11 @@ public: @@ -150,12 +151,11 @@ public:
150 class SrsMpegtsWriter 151 class SrsMpegtsWriter
151 { 152 {
152 public: 153 public:
153 - static int write_header(int fd) 154 + static int write_header(SrsFileWriter* writer)
154 { 155 {
155 int ret = ERROR_SUCCESS; 156 int ret = ERROR_SUCCESS;
156 157
157 - // TODO: FIXME: maybe should use st_write.  
158 - if (::write(fd, mpegts_header, sizeof(mpegts_header)) != sizeof(mpegts_header)) { 158 + if ((ret = writer->write(mpegts_header, sizeof(mpegts_header), NULL)) != ERROR_SUCCESS) {
159 ret = ERROR_HLS_WRITE_FAILED; 159 ret = ERROR_HLS_WRITE_FAILED;
160 srs_error("write ts file header failed. ret=%d", ret); 160 srs_error("write ts file header failed. ret=%d", ret);
161 return ret; 161 return ret;
@@ -163,7 +163,7 @@ public: @@ -163,7 +163,7 @@ public:
163 163
164 return ret; 164 return ret;
165 } 165 }
166 - static int write_frame(int fd, SrsMpegtsFrame* frame, SrsCodecBuffer* buffer) 166 + static int write_frame(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsCodecBuffer* buffer)
167 { 167 {
168 int ret = ERROR_SUCCESS; 168 int ret = ERROR_SUCCESS;
169 169
@@ -279,8 +279,7 @@ public: @@ -279,8 +279,7 @@ public:
279 } 279 }
280 280
281 // write ts packet 281 // write ts packet
282 - // TODO: FIXME: maybe should use st_write.  
283 - if (::write(fd, packet, sizeof(packet)) != sizeof(packet)) { 282 + if ((ret = writer->write(packet, sizeof(packet), NULL)) != ERROR_SUCCESS) {
284 ret = ERROR_HLS_WRITE_FAILED; 283 ret = ERROR_HLS_WRITE_FAILED;
285 srs_error("write ts file failed. ret=%d", ret); 284 srs_error("write ts file failed. ret=%d", ret);
286 return ret; 285 return ret;
@@ -414,12 +413,13 @@ void SrsHlsAacJitter::on_buffer_continue() @@ -414,12 +413,13 @@ void SrsHlsAacJitter::on_buffer_continue()
414 413
415 SrsTSMuxer::SrsTSMuxer() 414 SrsTSMuxer::SrsTSMuxer()
416 { 415 {
417 - fd = -1; 416 + writer = new SrsFileWriter();
418 } 417 }
419 418
420 SrsTSMuxer::~SrsTSMuxer() 419 SrsTSMuxer::~SrsTSMuxer()
421 { 420 {
422 close(); 421 close();
  422 + srs_freep(writer);
423 } 423 }
424 424
425 int SrsTSMuxer::open(string _path) 425 int SrsTSMuxer::open(string _path)
@@ -430,17 +430,12 @@ int SrsTSMuxer::open(string _path) @@ -430,17 +430,12 @@ int SrsTSMuxer::open(string _path)
430 430
431 close(); 431 close();
432 432
433 - int flags = O_CREAT|O_WRONLY|O_TRUNC;  
434 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;  
435 - // TODO: FIXME: refine the file stream.  
436 - if ((fd = ::open(path.c_str(), flags, mode)) < 0) {  
437 - ret = ERROR_HLS_OPEN_FAILED;  
438 - srs_error("open ts file %s failed. ret=%d", path.c_str(), ret); 433 + if ((ret = writer->open(path)) != ERROR_SUCCESS) {
439 return ret; 434 return ret;
440 } 435 }
441 436
442 // write mpegts header 437 // write mpegts header
443 - if ((ret = SrsMpegtsWriter::write_header(fd)) != ERROR_SUCCESS) { 438 + if ((ret = SrsMpegtsWriter::write_header(writer)) != ERROR_SUCCESS) {
444 return ret; 439 return ret;
445 } 440 }
446 441
@@ -451,7 +446,7 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab) @@ -451,7 +446,7 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab)
451 { 446 {
452 int ret = ERROR_SUCCESS; 447 int ret = ERROR_SUCCESS;
453 448
454 - if ((ret = SrsMpegtsWriter::write_frame(fd, af, ab)) != ERROR_SUCCESS) { 449 + if ((ret = SrsMpegtsWriter::write_frame(writer, af, ab)) != ERROR_SUCCESS) {
455 return ret; 450 return ret;
456 } 451 }
457 452
@@ -462,7 +457,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb) @@ -462,7 +457,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb)
462 { 457 {
463 int ret = ERROR_SUCCESS; 458 int ret = ERROR_SUCCESS;
464 459
465 - if ((ret = SrsMpegtsWriter::write_frame(fd, vf, vb)) != ERROR_SUCCESS) { 460 + if ((ret = SrsMpegtsWriter::write_frame(writer, vf, vb)) != ERROR_SUCCESS) {
466 return ret; 461 return ret;
467 } 462 }
468 463
@@ -471,10 +466,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb) @@ -471,10 +466,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb)
471 466
472 void SrsTSMuxer::close() 467 void SrsTSMuxer::close()
473 { 468 {
474 - if (fd > 0) {  
475 - ::close(fd);  
476 - fd = -1;  
477 - } 469 + writer->close();
478 } 470 }
479 471
480 SrsHlsSegment::SrsHlsSegment() 472 SrsHlsSegment::SrsHlsSegment()
@@ -45,6 +45,7 @@ class SrsAvcAacCodec; @@ -45,6 +45,7 @@ class SrsAvcAacCodec;
45 class SrsRequest; 45 class SrsRequest;
46 class SrsPithyPrint; 46 class SrsPithyPrint;
47 class SrsSource; 47 class SrsSource;
  48 +class SrsFileWriter;
48 49
49 /** 50 /**
50 * jitter correct for audio, 51 * jitter correct for audio,
@@ -83,7 +84,7 @@ public: @@ -83,7 +84,7 @@ public:
83 class SrsTSMuxer 84 class SrsTSMuxer
84 { 85 {
85 private: 86 private:
86 - int fd; 87 + SrsFileWriter* writer;
87 std::string path; 88 std::string path;
88 public: 89 public:
89 SrsTSMuxer(); 90 SrsTSMuxer();
@@ -42,6 +42,7 @@ using namespace std; @@ -42,6 +42,7 @@ using namespace std;
42 #include <srs_app_config.hpp> 42 #include <srs_app_config.hpp>
43 #include <srs_kernel_flv.hpp> 43 #include <srs_kernel_flv.hpp>
44 #include <srs_kernel_utility.hpp> 44 #include <srs_kernel_utility.hpp>
  45 +#include <srs_kernel_file.hpp>
45 46
46 #define SRS_HTTP_DEFAULT_PAGE "index.html" 47 #define SRS_HTTP_DEFAULT_PAGE "index.html"
47 48
@@ -191,28 +192,22 @@ int SrsHttpVhost::response_regular_file(SrsSocket* skt, SrsHttpMessage* req, str @@ -191,28 +192,22 @@ int SrsHttpVhost::response_regular_file(SrsSocket* skt, SrsHttpMessage* req, str
191 { 192 {
192 int ret = ERROR_SUCCESS; 193 int ret = ERROR_SUCCESS;
193 194
194 - // TODO: FIXME: refine the file stream.  
195 - int fd = ::open(fullpath.c_str(), O_RDONLY);  
196 - if (fd < 0) {  
197 - ret = ERROR_HTTP_OPEN_FILE; 195 + SrsFileReader fs;
  196 +
  197 + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
198 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); 198 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
199 return ret; 199 return ret;
200 } 200 }
201 201
202 - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END);  
203 - ::lseek(fd, 0, SEEK_SET); 202 + int64_t length = fs.filesize();
204 203
205 char* buf = new char[length]; 204 char* buf = new char[length];
206 SrsAutoFree(char, buf); 205 SrsAutoFree(char, buf);
207 206
208 - // TODO: FIXME: use st_read.  
209 - if (::read(fd, buf, length) < 0) {  
210 - ::close(fd);  
211 - ret = ERROR_HTTP_READ_FILE; 207 + if ((ret = fs.read(buf, length, NULL)) != ERROR_SUCCESS) {
212 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); 208 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
213 return ret; 209 return ret;
214 } 210 }
215 - ::close(fd);  
216 211
217 std::string str; 212 std::string str;
218 str.append(buf, length); 213 str.append(buf, length);
@@ -244,17 +239,15 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string @@ -244,17 +239,15 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string
244 { 239 {
245 int ret = ERROR_SUCCESS; 240 int ret = ERROR_SUCCESS;
246 241
  242 + SrsFileReader fs;
  243 +
247 // TODO: FIXME: use more advance cache. 244 // TODO: FIXME: use more advance cache.
248 - // for ts video large file, use bytes to write it.  
249 - int fd = ::open(fullpath.c_str(), O_RDONLY);  
250 - if (fd < 0) {  
251 - ret = ERROR_HTTP_OPEN_FILE; 245 + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
252 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); 246 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
253 return ret; 247 return ret;
254 } 248 }
255 249
256 - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END);  
257 - ::lseek(fd, 0, SEEK_SET); 250 + int64_t length = fs.filesize();
258 251
259 // write http header for ts. 252 // write http header for ts.
260 std::stringstream ss; 253 std::stringstream ss;
@@ -279,9 +272,7 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string @@ -279,9 +272,7 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string
279 272
280 while (left > 0) { 273 while (left > 0) {
281 ssize_t nread = -1; 274 ssize_t nread = -1;
282 - // TODO: FIXME: use st_read.  
283 - if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) {  
284 - ret = ERROR_HTTP_READ_FILE; 275 + if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) {
285 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); 276 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
286 break; 277 break;
287 } 278 }
@@ -291,7 +282,6 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string @@ -291,7 +282,6 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string
291 break; 282 break;
292 } 283 }
293 } 284 }
294 - ::close(fd);  
295 285
296 return ret; 286 return ret;
297 } 287 }
@@ -406,17 +396,15 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f @@ -406,17 +396,15 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
406 { 396 {
407 int ret = ERROR_SUCCESS; 397 int ret = ERROR_SUCCESS;
408 398
  399 + SrsFileReader fs;
  400 +
409 // TODO: FIXME: use more advance cache. 401 // TODO: FIXME: use more advance cache.
410 - // for ts video large file, use bytes to write it.  
411 - int fd = ::open(fullpath.c_str(), O_RDONLY);  
412 - if (fd < 0) {  
413 - ret = ERROR_HTTP_OPEN_FILE; 402 + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
414 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); 403 srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
415 return ret; 404 return ret;
416 } 405 }
417 406
418 - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END);  
419 - ::lseek(fd, 0, SEEK_SET); 407 + int64_t length = fs.filesize();
420 408
421 // write http header for ts. 409 // write http header for ts.
422 std::stringstream ss; 410 std::stringstream ss;
@@ -441,9 +429,7 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f @@ -441,9 +429,7 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
441 429
442 while (left > 0) { 430 while (left > 0) {
443 ssize_t nread = -1; 431 ssize_t nread = -1;
444 - // TODO: FIXME: use st_read.  
445 - if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) {  
446 - ret = ERROR_HTTP_READ_FILE; 432 + if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) {
447 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); 433 srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
448 break; 434 break;
449 } 435 }
@@ -453,7 +439,6 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f @@ -453,7 +439,6 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
453 break; 439 break;
454 } 440 }
455 } 441 }
456 - ::close(fd);  
457 442
458 return ret; 443 return ret;
459 } 444 }
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "145" 34 +#define VERSION_REVISION "146"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"
@@ -185,11 +185,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -185,11 +185,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
185 #define ERROR_HTTP_PARSE_HEADER 802 185 #define ERROR_HTTP_PARSE_HEADER 802
186 #define ERROR_HTTP_HANDLER_MATCH_URL 803 186 #define ERROR_HTTP_HANDLER_MATCH_URL 803
187 #define ERROR_HTTP_HANDLER_INVALID 804 187 #define ERROR_HTTP_HANDLER_INVALID 804
188 -#define ERROR_HTTP_OPEN_FILE 805  
189 -#define ERROR_HTTP_READ_FILE 806  
190 -#define ERROR_HTTP_API_LOGS 807  
191 -#define ERROR_HTTP_FLV_SEQUENCE_HEADER 808  
192 -#define ERROR_HTTP_FLV_OFFSET_OVERFLOW 809 188 +#define ERROR_HTTP_API_LOGS 805
  189 +#define ERROR_HTTP_FLV_SEQUENCE_HEADER 806
  190 +#define ERROR_HTTP_FLV_OFFSET_OVERFLOW 807
193 191
194 // system control message, 192 // system control message,
195 // not an error, but special control logic. 193 // not an error, but special control logic.
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_kernel_file.hpp>
  25 +
  26 +#include <fcntl.h>
  27 +#include <unistd.h>
  28 +#include <sstream>
  29 +using namespace std;
  30 +
  31 +#include <srs_kernel_log.hpp>
  32 +#include <srs_kernel_error.hpp>
  33 +
  34 +SrsFileWriter::SrsFileWriter()
  35 +{
  36 +}
  37 +
  38 +SrsFileWriter::~SrsFileWriter()
  39 +{
  40 + close();
  41 +}
  42 +
  43 +int SrsFileWriter::open(string file)
  44 +{
  45 + int ret = ERROR_SUCCESS;
  46 +
  47 + if (fd > 0) {
  48 + ret = ERROR_SYSTEM_FILE_ALREADY_OPENED;
  49 + srs_error("file %s already opened. ret=%d", _file.c_str(), ret);
  50 + return ret;
  51 + }
  52 +
  53 + int flags = O_CREAT|O_WRONLY|O_TRUNC;
  54 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
  55 +
  56 + if ((fd = ::open(file.c_str(), flags, mode)) < 0) {
  57 + ret = ERROR_SYSTEM_FILE_OPENE;
  58 + srs_error("open file %s failed. ret=%d", file.c_str(), ret);
  59 + return ret;
  60 + }
  61 +
  62 + _file = file;
  63 +
  64 + return ret;
  65 +}
  66 +
  67 +void SrsFileWriter::close()
  68 +{
  69 + int ret = ERROR_SUCCESS;
  70 +
  71 + if (fd < 0) {
  72 + return;
  73 + }
  74 +
  75 + if (::close(fd) < 0) {
  76 + ret = ERROR_SYSTEM_FILE_CLOSE;
  77 + srs_error("close file %s failed. ret=%d", _file.c_str(), ret);
  78 + return;
  79 + }
  80 + fd = -1;
  81 +
  82 + return;
  83 +}
  84 +
  85 +bool SrsFileWriter::is_open()
  86 +{
  87 + return fd > 0;
  88 +}
  89 +
  90 +int64_t SrsFileWriter::tellg()
  91 +{
  92 + return (int64_t)::lseek(fd, 0, SEEK_CUR);
  93 +}
  94 +
  95 +int SrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)
  96 +{
  97 + int ret = ERROR_SUCCESS;
  98 +
  99 + ssize_t nwrite;
  100 + // TODO: FIXME: use st_write.
  101 + if ((nwrite = ::write(fd, buf, count)) < 0) {
  102 + ret = ERROR_SYSTEM_FILE_WRITE;
  103 + srs_error("write to file %s failed. ret=%d", _file.c_str(), ret);
  104 + return ret;
  105 + }
  106 +
  107 + if (pnwrite != NULL) {
  108 + *pnwrite = nwrite;
  109 + }
  110 +
  111 + return ret;
  112 +}
  113 +
  114 +SrsFileReader::SrsFileReader()
  115 +{
  116 + fd = -1;
  117 +}
  118 +
  119 +SrsFileReader::~SrsFileReader()
  120 +{
  121 + close();
  122 +}
  123 +
  124 +int SrsFileReader::open(string file)
  125 +{
  126 + int ret = ERROR_SUCCESS;
  127 +
  128 + if (fd > 0) {
  129 + ret = ERROR_SYSTEM_FILE_ALREADY_OPENED;
  130 + srs_error("file %s already opened. ret=%d", _file.c_str(), ret);
  131 + return ret;
  132 + }
  133 +
  134 + if ((fd = ::open(file.c_str(), O_RDONLY)) < 0) {
  135 + ret = ERROR_SYSTEM_FILE_OPENE;
  136 + srs_error("open file %s failed. ret=%d", file.c_str(), ret);
  137 + return ret;
  138 + }
  139 +
  140 + _file = file;
  141 +
  142 + return ret;
  143 +}
  144 +
  145 +void SrsFileReader::close()
  146 +{
  147 + int ret = ERROR_SUCCESS;
  148 +
  149 + if (fd < 0) {
  150 + return;
  151 + }
  152 +
  153 + if (::close(fd) < 0) {
  154 + ret = ERROR_SYSTEM_FILE_CLOSE;
  155 + srs_error("close file %s failed. ret=%d", _file.c_str(), ret);
  156 + return;
  157 + }
  158 + fd = -1;
  159 +
  160 + return;
  161 +}
  162 +
  163 +bool SrsFileReader::is_open()
  164 +{
  165 + return fd > 0;
  166 +}
  167 +
  168 +int64_t SrsFileReader::tellg()
  169 +{
  170 + return (int64_t)::lseek(fd, 0, SEEK_CUR);
  171 +}
  172 +
  173 +void SrsFileReader::skip(int64_t size)
  174 +{
  175 + ::lseek(fd, size, SEEK_CUR);
  176 +}
  177 +
  178 +int64_t SrsFileReader::lseek(int64_t offset)
  179 +{
  180 + return (int64_t)::lseek(fd, offset, SEEK_SET);
  181 +}
  182 +
  183 +int64_t SrsFileReader::filesize()
  184 +{
  185 + int64_t cur = tellg();
  186 + int64_t size = (int64_t)::lseek(fd, 0, SEEK_END);
  187 + ::lseek(fd, cur, SEEK_SET);
  188 + return size;
  189 +}
  190 +
  191 +int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)
  192 +{
  193 + int ret = ERROR_SUCCESS;
  194 +
  195 + ssize_t nread;
  196 + // TODO: FIXME: use st_read.
  197 + if ((nread = ::read(fd, buf, count)) < 0) {
  198 + ret = ERROR_SYSTEM_FILE_READ;
  199 + srs_error("read from file %s failed. ret=%d", _file.c_str(), ret);
  200 + return ret;
  201 + }
  202 +
  203 + if (nread == 0) {
  204 + ret = ERROR_SYSTEM_FILE_EOF;
  205 + return ret;
  206 + }
  207 +
  208 + if (pnread != NULL) {
  209 + *pnread = nread;
  210 + }
  211 +
  212 + return ret;
  213 +}
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_KERNEL_FILE_HPP
  25 +#define SRS_KERNEL_FILE_HPP
  26 +
  27 +/*
  28 +#include <srs_kernel_file.hpp>
  29 +*/
  30 +#include <srs_core.hpp>
  31 +
  32 +#include <string>
  33 +
  34 +/**
  35 +* file writer, to write to file.
  36 +*/
  37 +class SrsFileWriter
  38 +{
  39 +private:
  40 + std::string _file;
  41 + int fd;
  42 +public:
  43 + SrsFileWriter();
  44 + virtual ~SrsFileWriter();
  45 +public:
  46 + /**
  47 + * open file writer, can open then close then open...
  48 + */
  49 + virtual int open(std::string file);
  50 + virtual void close();
  51 +public:
  52 + virtual bool is_open();
  53 + virtual int64_t tellg();
  54 +public:
  55 + /**
  56 + * write to file.
  57 + * @param pnwrite the output nb_write, NULL to ignore.
  58 + */
  59 + virtual int write(void* buf, size_t count, ssize_t* pnwrite);
  60 +};
  61 +
  62 +/**
  63 +* file reader, to read from file.
  64 +*/
  65 +class SrsFileReader
  66 +{
  67 +private:
  68 + std::string _file;
  69 + int fd;
  70 +public:
  71 + SrsFileReader();
  72 + virtual ~SrsFileReader();
  73 +public:
  74 + /**
  75 + * open file reader, can open then close then open...
  76 + */
  77 + virtual int open(std::string file);
  78 + virtual void close();
  79 +public:
  80 + virtual bool is_open();
  81 + virtual int64_t tellg();
  82 + virtual void skip(int64_t size);
  83 + virtual int64_t lseek(int64_t offset);
  84 + virtual int64_t filesize();
  85 +public:
  86 + /**
  87 + * read from file.
  88 + * @param pnread the output nb_read, NULL to ignore.
  89 + */
  90 + virtual int read(void* buf, size_t count, ssize_t* pnread);
  91 +};
  92 +
  93 +#endif
@@ -33,189 +33,11 @@ using namespace std; @@ -33,189 +33,11 @@ using namespace std;
33 #include <srs_core_autofree.hpp> 33 #include <srs_core_autofree.hpp>
34 #include <srs_kernel_stream.hpp> 34 #include <srs_kernel_stream.hpp>
35 #include <srs_kernel_utility.hpp> 35 #include <srs_kernel_utility.hpp>
  36 +#include <srs_kernel_file.hpp>
36 37
37 #define SRS_FLV_TAG_HEADER_SIZE 11 38 #define SRS_FLV_TAG_HEADER_SIZE 11
38 #define SRS_FLV_PREVIOUS_TAG_SIZE 4 39 #define SRS_FLV_PREVIOUS_TAG_SIZE 4
39 40
40 -SrsFileWriter::SrsFileWriter()  
41 -{  
42 -}  
43 -  
44 -SrsFileWriter::~SrsFileWriter()  
45 -{  
46 - close();  
47 -}  
48 -  
49 -int SrsFileWriter::open(string file)  
50 -{  
51 - int ret = ERROR_SUCCESS;  
52 -  
53 - if (fd > 0) {  
54 - ret = ERROR_SYSTEM_FILE_ALREADY_OPENED;  
55 - srs_error("file %s already opened. ret=%d", _file.c_str(), ret);  
56 - return ret;  
57 - }  
58 -  
59 - int flags = O_CREAT|O_WRONLY|O_TRUNC;  
60 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;  
61 -  
62 - if ((fd = ::open(file.c_str(), flags, mode)) < 0) {  
63 - ret = ERROR_SYSTEM_FILE_OPENE;  
64 - srs_error("open file %s failed. ret=%d", file.c_str(), ret);  
65 - return ret;  
66 - }  
67 -  
68 - _file = file;  
69 -  
70 - return ret;  
71 -}  
72 -  
73 -void SrsFileWriter::close()  
74 -{  
75 - int ret = ERROR_SUCCESS;  
76 -  
77 - if (fd < 0) {  
78 - return;  
79 - }  
80 -  
81 - if (::close(fd) < 0) {  
82 - ret = ERROR_SYSTEM_FILE_CLOSE;  
83 - srs_error("close file %s failed. ret=%d", _file.c_str(), ret);  
84 - return;  
85 - }  
86 - fd = -1;  
87 -  
88 - return;  
89 -}  
90 -  
91 -bool SrsFileWriter::is_open()  
92 -{  
93 - return fd > 0;  
94 -}  
95 -  
96 -int64_t SrsFileWriter::tellg()  
97 -{  
98 - return (int64_t)::lseek(fd, 0, SEEK_CUR);  
99 -}  
100 -  
101 -int SrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)  
102 -{  
103 - int ret = ERROR_SUCCESS;  
104 -  
105 - ssize_t nwrite;  
106 - if ((nwrite = ::write(fd, buf, count)) < 0) {  
107 - ret = ERROR_SYSTEM_FILE_WRITE;  
108 - srs_error("write to file %s failed. ret=%d", _file.c_str(), ret);  
109 - return ret;  
110 - }  
111 -  
112 - if (pnwrite != NULL) {  
113 - *pnwrite = nwrite;  
114 - }  
115 -  
116 - return ret;  
117 -}  
118 -  
119 -SrsFileReader::SrsFileReader()  
120 -{  
121 - fd = -1;  
122 -}  
123 -  
124 -SrsFileReader::~SrsFileReader()  
125 -{  
126 - close();  
127 -}  
128 -  
129 -int SrsFileReader::open(string file)  
130 -{  
131 - int ret = ERROR_SUCCESS;  
132 -  
133 - if (fd > 0) {  
134 - ret = ERROR_SYSTEM_FILE_ALREADY_OPENED;  
135 - srs_error("file %s already opened. ret=%d", _file.c_str(), ret);  
136 - return ret;  
137 - }  
138 -  
139 - if ((fd = ::open(file.c_str(), O_RDONLY)) < 0) {  
140 - ret = ERROR_SYSTEM_FILE_OPENE;  
141 - srs_error("open file %s failed. ret=%d", file.c_str(), ret);  
142 - return ret;  
143 - }  
144 -  
145 - _file = file;  
146 -  
147 - return ret;  
148 -}  
149 -  
150 -void SrsFileReader::close()  
151 -{  
152 - int ret = ERROR_SUCCESS;  
153 -  
154 - if (fd < 0) {  
155 - return;  
156 - }  
157 -  
158 - if (::close(fd) < 0) {  
159 - ret = ERROR_SYSTEM_FILE_CLOSE;  
160 - srs_error("close file %s failed. ret=%d", _file.c_str(), ret);  
161 - return;  
162 - }  
163 - fd = -1;  
164 -  
165 - return;  
166 -}  
167 -  
168 -bool SrsFileReader::is_open()  
169 -{  
170 - return fd > 0;  
171 -}  
172 -  
173 -int64_t SrsFileReader::tellg()  
174 -{  
175 - return (int64_t)::lseek(fd, 0, SEEK_CUR);  
176 -}  
177 -  
178 -void SrsFileReader::skip(int64_t size)  
179 -{  
180 - ::lseek(fd, size, SEEK_CUR);  
181 -}  
182 -  
183 -int64_t SrsFileReader::lseek(int64_t offset)  
184 -{  
185 - return (int64_t)::lseek(fd, offset, SEEK_SET);  
186 -}  
187 -  
188 -int64_t SrsFileReader::filesize()  
189 -{  
190 - int64_t cur = tellg();  
191 - int64_t size = (int64_t)::lseek(fd, 0, SEEK_END);  
192 - ::lseek(fd, cur, SEEK_SET);  
193 - return size;  
194 -}  
195 -  
196 -int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)  
197 -{  
198 - int ret = ERROR_SUCCESS;  
199 -  
200 - ssize_t nread;  
201 - if ((nread = ::read(fd, buf, count)) < 0) {  
202 - ret = ERROR_SYSTEM_FILE_READ;  
203 - srs_error("read from file %s failed. ret=%d", _file.c_str(), ret);  
204 - return ret;  
205 - }  
206 -  
207 - if (nread == 0) {  
208 - ret = ERROR_SYSTEM_FILE_EOF;  
209 - return ret;  
210 - }  
211 -  
212 - if (pnread != NULL) {  
213 - *pnread = nread;  
214 - }  
215 -  
216 - return ret;  
217 -}  
218 -  
219 SrsFlvEncoder::SrsFlvEncoder() 41 SrsFlvEncoder::SrsFlvEncoder()
220 { 42 {
221 _fs = NULL; 43 _fs = NULL;
@@ -32,51 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -32,51 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include <string> 32 #include <string>
33 33
34 class SrsStream; 34 class SrsStream;
35 -  
36 -/**  
37 -* file writer, to write to file.  
38 -*/  
39 -class SrsFileWriter  
40 -{  
41 -private:  
42 - std::string _file;  
43 - int fd;  
44 -public:  
45 - SrsFileWriter();  
46 - virtual ~SrsFileWriter();  
47 -public:  
48 - virtual int open(std::string file);  
49 - virtual void close();  
50 -public:  
51 - virtual bool is_open();  
52 - virtual int64_t tellg();  
53 -public:  
54 - virtual int write(void* buf, size_t count, ssize_t* pnwrite);  
55 -};  
56 -  
57 -/**  
58 -* file reader, to read from file.  
59 -*/  
60 -class SrsFileReader  
61 -{  
62 -private:  
63 - std::string _file;  
64 - int fd;  
65 -public:  
66 - SrsFileReader();  
67 - virtual ~SrsFileReader();  
68 -public:  
69 - virtual int open(std::string file);  
70 - virtual void close();  
71 -public:  
72 - virtual bool is_open();  
73 - virtual int64_t tellg();  
74 - virtual void skip(int64_t size);  
75 - virtual int64_t lseek(int64_t offset);  
76 - virtual int64_t filesize();  
77 -public:  
78 - virtual int read(void* buf, size_t count, ssize_t* pnread);  
79 -}; 35 +class SrsFileWriter;
  36 +class SrsFileReader;
80 37
81 /** 38 /**
82 * encode data to flv file. 39 * encode data to flv file.
@@ -41,6 +41,7 @@ using namespace std; @@ -41,6 +41,7 @@ using namespace std;
41 #include <srs_protocol_amf0.hpp> 41 #include <srs_protocol_amf0.hpp>
42 #include <srs_kernel_flv.hpp> 42 #include <srs_kernel_flv.hpp>
43 #include <srs_kernel_codec.hpp> 43 #include <srs_kernel_codec.hpp>
  44 +#include <srs_kernel_file.hpp>
44 45
45 // if user want to define log, define the folowing macro. 46 // if user want to define log, define the folowing macro.
46 #ifndef SRS_RTMP_USER_DEFINED_LOG 47 #ifndef SRS_RTMP_USER_DEFINED_LOG
@@ -21,6 +21,8 @@ file @@ -21,6 +21,8 @@ file
21 ..\kernel\srs_kernel_codec.cpp, 21 ..\kernel\srs_kernel_codec.cpp,
22 ..\kernel\srs_kernel_error.hpp, 22 ..\kernel\srs_kernel_error.hpp,
23 ..\kernel\srs_kernel_error.cpp, 23 ..\kernel\srs_kernel_error.cpp,
  24 + ..\kernel\srs_kernel_file.hpp,
  25 + ..\kernel\srs_kernel_file.cpp,
24 ..\kernel\srs_kernel_flv.hpp, 26 ..\kernel\srs_kernel_flv.hpp,
25 ..\kernel\srs_kernel_flv.cpp, 27 ..\kernel\srs_kernel_flv.cpp,
26 ..\kernel\srs_kernel_log.hpp, 28 ..\kernel\srs_kernel_log.hpp,
@@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <srs_utest.hpp> 30 #include <srs_utest.hpp>
31 31
32 #include <string> 32 #include <string>
33 -#include <srs_kernel_flv.hpp> 33 +#include <srs_kernel_file.hpp>
34 34
35 class MockSrsFileWriter : public SrsFileWriter 35 class MockSrsFileWriter : public SrsFileWriter
36 { 36 {