winlin

fix bug of extended-timestamp, support librtmp/ffmpeg publish

@@ -25,6 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,6 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <srs_core_error.hpp> 26 #include <srs_core_error.hpp>
27 #include <srs_core_socket.hpp> 27 #include <srs_core_socket.hpp>
  28 +#include <srs_core_log.hpp>
28 29
29 #define SOCKET_READ_SIZE 4096 30 #define SOCKET_READ_SIZE 4096
30 31
@@ -62,7 +63,11 @@ int SrsBuffer::ensure_buffer_bytes(SrsSocket* skt, int required_size) @@ -62,7 +63,11 @@ int SrsBuffer::ensure_buffer_bytes(SrsSocket* skt, int required_size)
62 { 63 {
63 int ret = ERROR_SUCCESS; 64 int ret = ERROR_SUCCESS;
64 65
65 - srs_assert(required_size >= 0); 66 + if (required_size < 0) {
  67 + ret = ERROR_SYSTEM_SIZE_NEGATIVE;
  68 + srs_error("size is negative. size=%d, ret=%d", required_size, ret);
  69 + return ret;
  70 + }
66 71
67 while (size() < required_size) { 72 while (size() < required_size) {
68 char buffer[SOCKET_READ_SIZE]; 73 char buffer[SOCKET_READ_SIZE];
@@ -64,11 +64,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -64,11 +64,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64 #define ERROR_RTMP_CHUNK_SIZE 310 64 #define ERROR_RTMP_CHUNK_SIZE 310
65 #define ERROR_RTMP_TRY_SIMPLE_HS 311 65 #define ERROR_RTMP_TRY_SIMPLE_HS 311
66 #define ERROR_RTMP_CH_SCHEMA 312 66 #define ERROR_RTMP_CH_SCHEMA 312
  67 +#define ERROR_RTMP_PACKET_SIZE 313
67 68
68 #define ERROR_SYSTEM_STREAM_INIT 400 69 #define ERROR_SYSTEM_STREAM_INIT 400
69 #define ERROR_SYSTEM_PACKET_INVALID 401 70 #define ERROR_SYSTEM_PACKET_INVALID 401
70 #define ERROR_SYSTEM_CLIENT_INVALID 402 71 #define ERROR_SYSTEM_CLIENT_INVALID 402
71 #define ERROR_SYSTEM_ASSERT_FAILED 403 72 #define ERROR_SYSTEM_ASSERT_FAILED 403
  73 +#define ERROR_SYSTEM_SIZE_NEGATIVE 404
72 74
73 // see librtmp. 75 // see librtmp.
74 // failed when open ssl create the dh 76 // failed when open ssl create the dh
@@ -827,6 +827,15 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz @@ -827,6 +827,15 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz
827 pp[0] = *p++; 827 pp[0] = *p++;
828 pp[3] = 0; 828 pp[3] = 0;
829 829
  830 + // if msg exists in cache, the size must not changed.
  831 + if (chunk->msg->size > 0 && chunk->msg->size != chunk->header.payload_length) {
  832 + ret = ERROR_RTMP_PACKET_SIZE;
  833 + srs_error("msg exists in chunk cache, "
  834 + "size=%d cannot change to %d, ret=%d",
  835 + chunk->msg->size, chunk->header.payload_length, ret);
  836 + return ret;
  837 + }
  838 +
830 chunk->header.message_type = *p++; 839 chunk->header.message_type = *p++;
831 840
832 if (fmt == 0) { 841 if (fmt == 0) {
@@ -867,11 +876,21 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz @@ -867,11 +876,21 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz
867 return ret; 876 return ret;
868 } 877 }
869 878
870 - char* pp = (char*)&chunk->header.timestamp; 879 + // ffmpeg/librtmp may donot send this filed, need to detect the value.
  880 + // @see also: http://blog.csdn.net/win_lin/article/details/13363699
  881 + int32_t timestamp = 0x00;
  882 + char* pp = (char*)&timestamp;
871 pp[3] = *p++; 883 pp[3] = *p++;
872 pp[2] = *p++; 884 pp[2] = *p++;
873 pp[1] = *p++; 885 pp[1] = *p++;
874 pp[0] = *p++; 886 pp[0] = *p++;
  887 +
  888 + if (chunk->header.timestamp > RTMP_EXTENDED_TIMESTAMP && chunk->header.timestamp != timestamp) {
  889 + mh_size -= 4;
  890 + srs_verbose("ignore the 4bytes extended timestamp. mh_size=%d", mh_size);
  891 + } else {
  892 + chunk->header.timestamp = timestamp;
  893 + }
875 srs_verbose("header read ext_time completed. time=%d", chunk->header.timestamp); 894 srs_verbose("header read ext_time completed. time=%d", chunk->header.timestamp);
876 } 895 }
877 896
@@ -897,7 +916,7 @@ int SrsProtocol::read_message_payload(SrsChunkStream* chunk, int bh_size, int mh @@ -897,7 +916,7 @@ int SrsProtocol::read_message_payload(SrsChunkStream* chunk, int bh_size, int mh
897 int ret = ERROR_SUCCESS; 916 int ret = ERROR_SUCCESS;
898 917
899 // empty message 918 // empty message
900 - if (chunk->header.payload_length == 0) { 919 + if (chunk->header.payload_length <= 0) {
901 // need erase the header in buffer. 920 // need erase the header in buffer.
902 buffer->erase(bh_size + mh_size); 921 buffer->erase(bh_size + mh_size);
903 922