winlin

Merge branch 'srs.master'

@@ -37,26 +37,30 @@ gcc srs_h264_raw_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_h @@ -37,26 +37,30 @@ gcc srs_h264_raw_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_h
37 37
38 #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") 38 #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n")
39 39
40 -int read_h264_frame(char* data, int size, char** p, int fps, 40 +int read_h264_frame(char* data, int size, char** pp, int fps,
41 char** frame, int* frame_size, int* dts, int* pts) 41 char** frame, int* frame_size, int* dts, int* pts)
42 { 42 {
  43 + char* p = *pp;
  44 +
43 // @remark, for this demo, to publish h264 raw file to SRS, 45 // @remark, for this demo, to publish h264 raw file to SRS,
44 // we search the h264 frame from the buffer which cached the h264 data. 46 // we search the h264 frame from the buffer which cached the h264 data.
45 // please get h264 raw data from device, it always a encoded frame. 47 // please get h264 raw data from device, it always a encoded frame.
46 int pnb_start_code = 0; 48 int pnb_start_code = 0;
47 - if (!srs_h264_startswith_annexb(*p, size - (*p - data), &pnb_start_code)) { 49 + if (!srs_h264_startswith_annexb(p, size - (p - data), &pnb_start_code)) {
48 srs_trace("h264 raw data invalid."); 50 srs_trace("h264 raw data invalid.");
49 return -1; 51 return -1;
50 } 52 }
51 - *p += pnb_start_code; 53 + p += pnb_start_code;
52 54
53 - *frame = *p;  
54 - for (;*p < data + size; *p = *p + 1) {  
55 - if (srs_h264_startswith_annexb(*p, size - (*p - data), &pnb_start_code)) { 55 + *frame = p;
  56 + for (;p < data + size; p++) {
  57 + if (srs_h264_startswith_annexb(p, size - (p - data), &pnb_start_code)) {
56 break; 58 break;
57 } 59 }
58 } 60 }
59 - *frame_size = *p - *frame; 61 +
  62 + *pp = p;
  63 + *frame_size = p - *frame;
60 if (*frame_size <= 0) { 64 if (*frame_size <= 0) {
61 srs_trace("h264 raw data invalid."); 65 srs_trace("h264 raw data invalid.");
62 return -1; 66 return -1;
@@ -157,18 +161,9 @@ int main(int argc, char** argv) @@ -157,18 +161,9 @@ int main(int argc, char** argv)
157 goto rtmp_destroy; 161 goto rtmp_destroy;
158 } 162 }
159 163
160 - // convert the h264 packet to rtmp packet.  
161 - char* rtmp_data = NULL;  
162 - int rtmp_size = 0;  
163 - u_int32_t timestamp = 0;  
164 - if (srs_h264_to_rtmp(data, size, dts, pts, &rtmp_data, &rtmp_size, &timestamp) != 0) {  
165 - srs_trace("h264 raw data to rtmp data failed.");  
166 - goto rtmp_destroy;  
167 - }  
168 -  
169 - // send out the rtmp packet.  
170 - int type = SRS_RTMP_TYPE_VIDEO;  
171 - if (srs_write_packet(rtmp, type, timestamp, rtmp_data, rtmp_size) != 0) { 164 + // send out the h264 packet over RTMP
  165 + if (srs_write_h264_raw_frame(rtmp, data, size, dts, pts) != 0) {
  166 + srs_trace("send h264 raw data failed.");
172 goto rtmp_destroy; 167 goto rtmp_destroy;
173 } 168 }
174 169
@@ -176,7 +171,7 @@ int main(int argc, char** argv) @@ -176,7 +171,7 @@ int main(int argc, char** argv)
176 // H.264-AVC-ISO_IEC_14496-10.pdf, page 44. 171 // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
177 u_int8_t nut = (char)data[0] & 0x1f; 172 u_int8_t nut = (char)data[0] & 0x1f;
178 srs_trace("sent packet: type=%s, time=%d, size=%d, fps=%d, b[0]=%#x(%s)", 173 srs_trace("sent packet: type=%s, time=%d, size=%d, fps=%d, b[0]=%#x(%s)",
179 - srs_type2string(type), timestamp, rtmp_size, fps, nut, 174 + srs_type2string(SRS_RTMP_TYPE_VIDEO), dts, size, fps, (char)data[0],
180 (nut == 7? "SPS":(nut == 8? "PPS":(nut == 5? "I":(nut == 1? "P":"Unknown"))))); 175 (nut == 7? "SPS":(nut == 8? "PPS":(nut == 5? "I":(nut == 1? "P":"Unknown")))));
181 176
182 // @remark, when use encode device, it not need to sleep. 177 // @remark, when use encode device, it not need to sleep.
@@ -996,27 +996,31 @@ char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize) @@ -996,27 +996,31 @@ char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize)
996 return any->human_print(pdata, psize); 996 return any->human_print(pdata, psize);
997 } 997 }
998 998
999 -int srs_h264_to_rtmp(char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_int32_t pts, char** prtmp_data, int* prtmp_size, u_int32_t* ptimestamp) 999 +int srs_write_h264_raw_frame(srs_rtmp_t rtmp, char* frame, int frame_size, u_int32_t dts, u_int32_t pts)
1000 { 1000 {
1001 - srs_assert(h264_raw_size > 0); 1001 + int ret = ERROR_SUCCESS;
  1002 +
  1003 + srs_assert(frame_size > 1);
  1004 +
  1005 + srs_assert(rtmp != NULL);
  1006 + Context* context = (Context*)rtmp;
  1007 +
  1008 + // 5bits, 7.3.1 NAL unit syntax,
  1009 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  1010 + // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame
  1011 + u_int8_t nal_unit_type = (char)frame[0] & 0x1f;
1002 1012
1003 - // the timestamp in rtmp message header is dts.  
1004 - *ptimestamp = dts; 1013 + /*// the timestamp in rtmp message header is dts.
  1014 + u_int32_t timestamp = dts;
1005 1015
1006 // for h264 in RTMP video payload, there is 5bytes header: 1016 // for h264 in RTMP video payload, there is 5bytes header:
1007 // 1bytes, FrameType | CodecID 1017 // 1bytes, FrameType | CodecID
1008 // 1bytes, AVCPacketType 1018 // 1bytes, AVCPacketType
1009 // 3bytes, CompositionTime, the cts. 1019 // 3bytes, CompositionTime, the cts.
1010 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78 1020 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
1011 - *prtmp_size = h264_raw_size + 5;  
1012 - char* p = new char[*prtmp_size];  
1013 - memcpy(p + 5, h264_raw_data, h264_raw_size);  
1014 - *prtmp_data = p;  
1015 -  
1016 - // 5bits, 7.3.1 NAL unit syntax,  
1017 - // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.  
1018 - // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame  
1019 - u_int8_t nal_unit_type = (char)h264_raw_data[0] & 0x1f; 1021 + int size = h264_raw_size + 5;
  1022 + char* data = new char[size];
  1023 + memcpy(data + 5, h264_raw_data, h264_raw_size);
1020 1024
1021 // Frame Type, Type of video frame. 1025 // Frame Type, Type of video frame.
1022 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78 1026 // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
@@ -1044,9 +1048,9 @@ int srs_h264_to_rtmp(char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_in @@ -1044,9 +1048,9 @@ int srs_h264_to_rtmp(char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_in
1044 char* pp = (char*)&cts; 1048 char* pp = (char*)&cts;
1045 *p++ = pp[2]; 1049 *p++ = pp[2];
1046 *p++ = pp[1]; 1050 *p++ = pp[1];
1047 - *p++ = pp[0]; 1051 + *p++ = pp[0];*/
1048 1052
1049 - return 0; 1053 + return ret;
1050 } 1054 }
1051 1055
1052 int srs_h264_startswith_annexb(char* h264_raw_data, int h264_raw_size, int* pnb_start_code) 1056 int srs_h264_startswith_annexb(char* h264_raw_data, int h264_raw_size, int* pnb_start_code)
@@ -337,24 +337,22 @@ extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize); @@ -337,24 +337,22 @@ extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize);
337 ************************************************************** 337 **************************************************************
338 *************************************************************/ 338 *************************************************************/
339 /** 339 /**
340 -* convert h264 stream data to rtmp packet.  
341 -* @param h264_raw_data the input h264 raw data, a encoded h.264 I/P/B frame data.  
342 -* @paam h264_raw_size the size of h264 raw data. assert > 0. 340 +* write h.264 raw frame over RTMP to rtmp server.
  341 +* @param frame the input h264 raw data, an encoded h.264 I/P/B frame data.
  342 +* the frame without h.264 annexb header, by N[00] 00 00 01, where N>=0,
  343 +* for instance, header(00 00 00 01) + frame(67 42 80 29 95 A0 14 01 6E 40)
  344 +* @paam frame_size the size of h264 raw data.
  345 +* assert frame_size > 1, at least has 1 bytes header.
343 * @param dts the dts of h.264 raw data. 346 * @param dts the dts of h.264 raw data.
344 * @param pts the pts of h.264 raw data. 347 * @param pts the pts of h.264 raw data.
345 -* @param prtmp_data the output rtmp format packet, which can be send by srs_write_packet.  
346 -* @param prtmp_size the size of rtmp packet, for srs_write_packet.  
347 -* @param ptimestamp the timestamp of rtmp packet, for srs_write_packet.  
348 * 348 *
349 -* @remark, user should free the h264_raw_data.  
350 -* @remark, user should free the prtmp_data if success. 349 +* @remark, user should free the frame.
351 * @remark, the tbn of dts/pts is 1/1000 for RTMP, that is, in ms. 350 * @remark, the tbn of dts/pts is 1/1000 for RTMP, that is, in ms.
352 * 351 *
353 * @return 0, success; otherswise, failed. 352 * @return 0, success; otherswise, failed.
354 */ 353 */
355 -extern int srs_h264_to_rtmp(  
356 - char* h264_raw_data, int h264_raw_size, u_int32_t dts, u_int32_t pts,  
357 - char** prtmp_data, int* prtmp_size, u_int32_t* ptimestamp 354 +extern int srs_write_h264_raw_frame(srs_rtmp_t rtmp,
  355 + char* frame, int frame_size, u_int32_t dts, u_int32_t pts
358 ); 356 );
359 /** 357 /**
360 * whether h264 raw data starts with the annexb, 358 * whether h264 raw data starts with the annexb,