winlin

for bug #66, refine the api and demo.

@@ -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;
@@ -167,7 +171,7 @@ int main(int argc, char** argv) @@ -167,7 +171,7 @@ int main(int argc, char** argv)
167 // H.264-AVC-ISO_IEC_14496-10.pdf, page 44. 171 // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
168 u_int8_t nut = (char)data[0] & 0x1f; 172 u_int8_t nut = (char)data[0] & 0x1f;
169 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)",
170 - srs_type2string(SRS_RTMP_TYPE_VIDEO), dts, size, fps, nut, 174 + srs_type2string(SRS_RTMP_TYPE_VIDEO), dts, size, fps, (char)data[0],
171 (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")))));
172 176
173 // @remark, when use encode device, it not need to sleep. 177 // @remark, when use encode device, it not need to sleep.
@@ -70,9 +70,6 @@ struct Context @@ -70,9 +70,6 @@ struct Context
70 SimpleSocketStream* skt; 70 SimpleSocketStream* skt;
71 int stream_id; 71 int stream_id;
72 72
73 - // for h264 raw stream  
74 - SrsStream raw_stream;  
75 -  
76 Context() { 73 Context() {
77 rtmp = NULL; 74 rtmp = NULL;
78 skt = NULL; 75 skt = NULL;
@@ -1003,15 +1000,15 @@ int srs_write_h264_raw_frame(srs_rtmp_t rtmp, char* frame, int frame_size, u_int @@ -1003,15 +1000,15 @@ int srs_write_h264_raw_frame(srs_rtmp_t rtmp, char* frame, int frame_size, u_int
1003 { 1000 {
1004 int ret = ERROR_SUCCESS; 1001 int ret = ERROR_SUCCESS;
1005 1002
1006 - srs_assert(frame_size > 0); 1003 + srs_assert(frame_size > 1);
1007 1004
1008 srs_assert(rtmp != NULL); 1005 srs_assert(rtmp != NULL);
1009 Context* context = (Context*)rtmp; 1006 Context* context = (Context*)rtmp;
1010 1007
1011 - if ((ret = context->raw_stream.initialize(frame, frame_size)) != ERROR_SUCCESS) {  
1012 - return ret;  
1013 - }  
1014 - 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;
1015 1012
1016 /*// the timestamp in rtmp message header is dts. 1013 /*// the timestamp in rtmp message header is dts.
1017 u_int32_t timestamp = dts; 1014 u_int32_t timestamp = dts;
@@ -1025,11 +1022,6 @@ int srs_write_h264_raw_frame(srs_rtmp_t rtmp, char* frame, int frame_size, u_int @@ -1025,11 +1022,6 @@ int srs_write_h264_raw_frame(srs_rtmp_t rtmp, char* frame, int frame_size, u_int
1025 char* data = new char[size]; 1022 char* data = new char[size];
1026 memcpy(data + 5, h264_raw_data, h264_raw_size); 1023 memcpy(data + 5, h264_raw_data, h264_raw_size);
1027 1024
1028 - // 5bits, 7.3.1 NAL unit syntax,  
1029 - // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.  
1030 - // 7: SPS, 8: PPS, 5: I Frame, 1: P Frame  
1031 - u_int8_t nal_unit_type = (char)h264_raw_data[0] & 0x1f;  
1032 -  
1033 // Frame Type, Type of video frame. 1025 // Frame Type, Type of video frame.
1034 // @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
1035 int8_t frame_type = SrsCodecVideoAVCFrameInterFrame; 1027 int8_t frame_type = SrsCodecVideoAVCFrameInterFrame;
@@ -337,11 +337,12 @@ extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize); @@ -337,11 +337,12 @@ extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize);
337 ************************************************************** 337 **************************************************************
338 *************************************************************/ 338 *************************************************************/
339 /** 339 /**
340 -* write h.264 raw frame to rtmp server. 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. 341 * @param frame the input h264 raw data, an encoded h.264 I/P/B frame data.
342 -* it must be prefixed by h.264 annexb format, by N[00] 00 00 01, where N>=0,  
343 -* for instance, 00 00 00 01 67 42 80 29 95 A0 14 01 6E 40  
344 -* @paam frame_size the size of h264 raw data. assert frame_size > 0. 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.
345 * @param dts the dts of h.264 raw data. 346 * @param dts the dts of h.264 raw data.
346 * @param pts the pts of h.264 raw data. 347 * @param pts the pts of h.264 raw data.
347 * 348 *