winlin

Merge branch 'srs.master'

@@ -65,6 +65,27 @@ int main(int argc, char** argv) @@ -65,6 +65,27 @@ int main(int argc, char** argv)
65 goto rtmp_destroy; 65 goto rtmp_destroy;
66 } 66 }
67 67
  68 + off_t file_size = lseek(raw_fd, 0, SEEK_END);
  69 + if (file_size <= 0) {
  70 + srs_trace("h264 raw file %s empty.", raw_file);
  71 + goto rtmp_destroy;
  72 + }
  73 + srs_trace("read entirely h264 raw file, size=%dKB", (int)(file_size / 1024));
  74 +
  75 + char* h264_raw = (char*)malloc(file_size);
  76 + if (!h264_raw) {
  77 + srs_trace("alloc raw buffer failed for file %s.", raw_file);
  78 + goto rtmp_destroy;
  79 + }
  80 +
  81 + lseek(raw_fd, 0, SEEK_SET);
  82 + ssize_t nb_read = 0;
  83 + if ((nb_read = read(raw_fd, h264_raw, file_size)) != file_size) {
  84 + srs_trace("buffer %s failed, expect=%dKB, actual=%dKB.",
  85 + raw_file, (int)(file_size / 1024), (int)(nb_read / 1024));
  86 + goto rtmp_destroy;
  87 + }
  88 +
68 // connect rtmp context 89 // connect rtmp context
69 srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url); 90 srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);
70 91
@@ -86,10 +107,13 @@ int main(int argc, char** argv) @@ -86,10 +107,13 @@ int main(int argc, char** argv)
86 } 107 }
87 srs_trace("publish stream success"); 108 srs_trace("publish stream success");
88 109
  110 + // @remark, the dts and pts if read from device, for instance, the encode lib,
  111 + // so we assume the fps is 25, and each h264 frame is 1000ms/25fps=40ms/f.
  112 + u_int32_t fps = 25;
89 u_int32_t dts = 0; 113 u_int32_t dts = 0;
90 u_int32_t pts = 0; 114 u_int32_t pts = 0;
91 for (;;) { 115 for (;;) {
92 - // read from file, or get h264 raw data from device, whatever. 116 + // get h264 raw data from device, whatever.
93 int size = 4096; 117 int size = 4096;
94 char* data = (char*)malloc(4096); 118 char* data = (char*)malloc(4096);
95 if ((size = read(raw_fd, data, size)) < 0) { 119 if ((size = read(raw_fd, data, size)) < 0) {
@@ -101,26 +125,37 @@ int main(int argc, char** argv) @@ -101,26 +125,37 @@ int main(int argc, char** argv)
101 goto rtmp_destroy; 125 goto rtmp_destroy;
102 } 126 }
103 127
  128 + // convert the h264 packet to rtmp packet.
104 char* rtmp_data = NULL; 129 char* rtmp_data = NULL;
105 int rtmp_size = 0; 130 int rtmp_size = 0;
106 u_int32_t timestamp = 0; 131 u_int32_t timestamp = 0;
107 - if (srs_h264_to_rtmp(data, size, dts, pts, &rtmp_data, &rtmp_size, &timestamp) < 0) { 132 + if (srs_h264_to_rtmp(data, size, dts, pts, &rtmp_data, &rtmp_size, &timestamp) != 0) {
108 srs_trace("h264 raw data to rtmp data failed."); 133 srs_trace("h264 raw data to rtmp data failed.");
109 goto rtmp_destroy; 134 goto rtmp_destroy;
110 } 135 }
111 136
  137 + // send out the rtmp packet.
112 int type = SRS_RTMP_TYPE_VIDEO; 138 int type = SRS_RTMP_TYPE_VIDEO;
113 if (srs_write_packet(rtmp, type, timestamp, rtmp_data, rtmp_size) != 0) { 139 if (srs_write_packet(rtmp, type, timestamp, rtmp_data, rtmp_size) != 0) {
114 goto rtmp_destroy; 140 goto rtmp_destroy;
115 } 141 }
116 - srs_trace("sent packet: type=%s, time=%d, size=%d", srs_type2string(type), timestamp, rtmp_size); 142 + srs_trace("sent packet: type=%s, time=%d, size=%d, fps=%d",
  143 + srs_type2string(type), timestamp, rtmp_size, fps);
  144 +
  145 + // @remark, please get the dts and pts from device,
  146 + // we assume there is no B frame, and the fps can guess the fps and dts,
  147 + // while the dts and pts must read from encode lib or device.
  148 + dts += 1000 / fps;
  149 + pts = dts;
117 150
118 - usleep(40 * 1000); 151 + // @remark, when use encode device, it not need to sleep.
  152 + usleep(1000 / fps * 1000);
119 } 153 }
120 154
121 rtmp_destroy: 155 rtmp_destroy:
122 srs_rtmp_destroy(rtmp); 156 srs_rtmp_destroy(rtmp);
123 close(raw_fd); 157 close(raw_fd);
  158 + free(h264_raw);
124 159
125 return 0; 160 return 0;
126 } 161 }
@@ -54,6 +54,7 @@ typedef void* srs_rtmp_t; @@ -54,6 +54,7 @@ typedef void* srs_rtmp_t;
54 * create/destroy a rtmp protocol stack. 54 * create/destroy a rtmp protocol stack.
55 * @url rtmp url, for example: 55 * @url rtmp url, for example:
56 * rtmp://localhost/live/livestream 56 * rtmp://localhost/live/livestream
  57 +*
57 * @return a rtmp handler, or NULL if error occured. 58 * @return a rtmp handler, or NULL if error occured.
58 */ 59 */
59 extern srs_rtmp_t srs_rtmp_create(const char* url); 60 extern srs_rtmp_t srs_rtmp_create(const char* url);
@@ -63,6 +64,8 @@ extern srs_rtmp_t srs_rtmp_create(const char* url); @@ -63,6 +64,8 @@ extern srs_rtmp_t srs_rtmp_create(const char* url);
63 * rtmp://localhost/live 64 * rtmp://localhost/live
64 * @remark this is used to create application connection-oriented, 65 * @remark this is used to create application connection-oriented,
65 * for example, the bandwidth client used this, no stream specified. 66 * for example, the bandwidth client used this, no stream specified.
  67 +*
  68 +* @return a rtmp handler, or NULL if error occured.
66 */ 69 */
67 extern srs_rtmp_t srs_rtmp_create2(const char* url); 70 extern srs_rtmp_t srs_rtmp_create2(const char* url);
68 /** 71 /**
@@ -81,7 +84,8 @@ extern void srs_rtmp_destroy(srs_rtmp_t rtmp); @@ -81,7 +84,8 @@ extern void srs_rtmp_destroy(srs_rtmp_t rtmp);
81 * category: publish/play 84 * category: publish/play
82 * previous: rtmp-create 85 * previous: rtmp-create
83 * next: connect-app 86 * next: connect-app
84 -* @return 0, success; otherwise, failed. 87 +*
  88 +* @return 0, success; otherswise, failed.
85 */ 89 */
86 /** 90 /**
87 * simple handshake specifies in rtmp 1.0, 91 * simple handshake specifies in rtmp 1.0,
@@ -107,7 +111,8 @@ extern int __srs_do_simple_handshake(srs_rtmp_t rtmp); @@ -107,7 +111,8 @@ extern int __srs_do_simple_handshake(srs_rtmp_t rtmp);
107 * category: publish/play 111 * category: publish/play
108 * previous: handshake 112 * previous: handshake
109 * next: publish or play 113 * next: publish or play
110 -* @return 0, success; otherwise, failed. 114 +*
  115 +* @return 0, success; otherswise, failed.
111 */ 116 */
112 extern int srs_connect_app(srs_rtmp_t rtmp); 117 extern int srs_connect_app(srs_rtmp_t rtmp);
113 118
@@ -121,6 +126,8 @@ extern int srs_connect_app(srs_rtmp_t rtmp); @@ -121,6 +126,8 @@ extern int srs_connect_app(srs_rtmp_t rtmp);
121 * @param srs_version, 32bytes, server version. 126 * @param srs_version, 32bytes, server version.
122 * @param srs_id, int, debug info, client id in server log. 127 * @param srs_id, int, debug info, client id in server log.
123 * @param srs_pid, int, debug info, server pid in log. 128 * @param srs_pid, int, debug info, server pid in log.
  129 +*
  130 +* @return 0, success; otherswise, failed.
124 */ 131 */
125 extern int srs_connect_app2(srs_rtmp_t rtmp, 132 extern int srs_connect_app2(srs_rtmp_t rtmp,
126 char srs_server_ip[128], char srs_server[128], char srs_primary_authors[128], 133 char srs_server_ip[128], char srs_server[128], char srs_primary_authors[128],
@@ -157,6 +164,8 @@ extern int srs_publish_stream(srs_rtmp_t rtmp); @@ -157,6 +164,8 @@ extern int srs_publish_stream(srs_rtmp_t rtmp);
157 * @param publish_bytes, output the publish/upload bytes. 164 * @param publish_bytes, output the publish/upload bytes.
158 * @param play_duration, output the play/download test duration, in ms. 165 * @param play_duration, output the play/download test duration, in ms.
159 * @param publish_duration, output the publish/upload test duration, in ms. 166 * @param publish_duration, output the publish/upload test duration, in ms.
  167 +*
  168 +* @return 0, success; otherswise, failed.
160 */ 169 */
161 extern int srs_bandwidth_check(srs_rtmp_t rtmp, 170 extern int srs_bandwidth_check(srs_rtmp_t rtmp,
162 int64_t* start_time, int64_t* end_time, 171 int64_t* start_time, int64_t* end_time,
@@ -200,6 +209,8 @@ extern const char* srs_type2string(int type); @@ -200,6 +209,8 @@ extern const char* srs_type2string(int type);
200 * 209 *
201 * @remark: for read, user must free the data. 210 * @remark: for read, user must free the data.
202 * @remark: for write, user should never free the data, even if error. 211 * @remark: for write, user should never free the data, even if error.
  212 +*
  213 +* @return 0, success; otherswise, failed.
203 */ 214 */
204 extern int srs_read_packet(srs_rtmp_t rtmp, 215 extern int srs_read_packet(srs_rtmp_t rtmp,
205 int* type, u_int32_t* timestamp, char** data, int* size 216 int* type, u_int32_t* timestamp, char** data, int* size
@@ -334,6 +345,9 @@ convert h264 stream data to rtmp packet. @@ -334,6 +345,9 @@ convert h264 stream data to rtmp packet.
334 @param prtmp_data the output rtmp format packet, which can be send by srs_write_packet. 345 @param prtmp_data the output rtmp format packet, which can be send by srs_write_packet.
335 @param prtmp_size the size of rtmp packet, for srs_write_packet. 346 @param prtmp_size the size of rtmp packet, for srs_write_packet.
336 @param ptimestamp the timestamp of rtmp packet, for srs_write_packet. 347 @param ptimestamp the timestamp of rtmp packet, for srs_write_packet.
  348 +@remark, user should never free the h264_raw_data.
  349 +@remark, user should free the prtmp_data if success.
  350 +@return 0, success; otherswise, failed.
337 */ 351 */
338 extern int srs_h264_to_rtmp( 352 extern int srs_h264_to_rtmp(
339 char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_int32_t pts, 353 char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_int32_t pts,