bool InFileRTMPStream::AVCBuilder::BuildFrame(MediaFile* pFile, MediaFrame& mediaFrame, IOBuffer& buffer) { if (mediaFrame.isBinaryHeader) { buffer.ReadFromBuffer(_videoCodecHeaderInit, sizeof (_videoCodecHeaderInit)); } else { if (mediaFrame.isKeyFrame) { // video key frame buffer.ReadFromBuffer(_videoCodecHeaderKeyFrame, sizeof (_videoCodecHeaderKeyFrame)); } else { //video normal frame buffer.ReadFromBuffer(_videoCodecHeader, sizeof (_videoCodecHeader)); } uint32_t cts = (EHTONL(((uint32_t) mediaFrame.cts) & 0x00ffffff)) >> 8; buffer.ReadFromBuffer((uint8_t *) & cts, 3); } if (!pFile->SeekTo(mediaFrame.start)) { FATAL("Unable to seek to position %"PRIu64, mediaFrame.start); return false; } if (!buffer.ReadFromFs(*pFile, (uint32_t) mediaFrame.length)) { FATAL("Unable to read %"PRIu64" bytes from offset %"PRIu64, mediaFrame.length, mediaFrame.start); return false; } return true; }
bool AMF0Serializer::WriteLongString(IOBuffer &buffer, string &value, bool writeType) { if (writeType) buffer.ReadFromRepeat(AMF0_LONG_STRING, 1); uint32_t length = EHTONL((uint32_t) value.length()); buffer.ReadFromBuffer((uint8_t *) & length, 4); buffer.ReadFromString(value); return true; }
bool InboundJSONCLIProtocol::SendMessage(Variant &message) { string json; if (!message.SerializeToJSON(json)) { FATAL("Unable to serialize to JSON"); return false; } json += "\r\n"; if (_useLengthPadding) { uint32_t size = EHTONL((uint32_t) json.length()); _outputBuffer.ReadFromBuffer((uint8_t *) & size, 4); } _outputBuffer.ReadFromString(json); return EnqueueForOutbound(); }
bool Header::Write(IOBuffer &buffer) { if (ci < 64) { buffer.ReadFromByte((ht << 6) | ((uint8_t) ci)); } else if (ci < 319) { buffer.ReadFromByte(ht << 6); buffer.ReadFromByte((uint8_t) (ci - 64)); } else if (ci < 65599) { uint16_t temp = EHTONS((uint16_t) (ci - 64)); buffer.ReadFromByte((ht << 6) | 0x01); buffer.ReadFromBuffer((uint8_t *) & temp, 2); } else { FATAL("Invalid channel index"); return false; } switch (ht) { case HT_FULL: { if (hf.s.ts < 0x00ffffff) { hf.s.ts = EHTONL(hf.s.ts); //----MARKED-LONG--- hf.s.ml = EHTONL(hf.s.ml << 8); //----MARKED-LONG--- buffer.ReadFromBuffer(&hf.datac[1], 11); hf.s.ts = ENTOHL(hf.s.ts); //----MARKED-LONG--- hf.s.ml = ENTOHL(hf.s.ml) >> 8; //----MARKED-LONG--- return true; } else { uint32_t temp = EHTONL(hf.s.ts); //----MARKED-LONG--- hf.s.ts = EHTONL(0x00ffffff); //----MARKED-LONG--- hf.s.ml = EHTONL(hf.s.ml << 8); //----MARKED-LONG--- buffer.ReadFromBuffer(&hf.datac[1], 11); hf.s.ts = ENTOHL(temp); //----MARKED-LONG--- hf.s.ml = ENTOHL(hf.s.ml) >> 8; //----MARKED-LONG--- buffer.ReadFromBuffer((uint8_t *) & temp, 4); return true; } }
bool InboundXMLCLIProtocol::SendMessage(Variant &message) { string xml; #if (defined(HAS_TINYXML)) if (!message.SerializeToXml(xml, true)) { FATAL("Unable to serialize to XML"); return false; } xml += "\r\n\r\n"; if (_useLengthPadding) { uint32_t size = EHTONL((uint32_t) xml.length()); _outputBuffer.ReadFromBuffer((uint8_t *) & size, 4); } _outputBuffer.ReadFromString(xml); return EnqueueForOutbound(); #else FATAL("Unable to serialize to XML, must enable tinyXML"); return false; #endif }
UDPCarrier* UDPCarrier::Create(string bindIp, uint16_t bindPort, uint16_t ttl, uint16_t tos) { //1. Create the socket int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { int err = errno; FATAL("Unable to create socket: (%d) %s", err, strerror(err)); return NULL; } //2. fd options if (!setFdOptions(sock, true)) { FATAL("Unable to set fd options"); CLOSE_SOCKET(sock); return NULL; } if (tos <= 255) { if (!setFdTOS(sock, (uint8_t) tos)) { FATAL("Unable to set tos"); CLOSE_SOCKET(sock); return NULL; } } //3. bind if necessary sockaddr_in bindAddress; memset(&bindAddress, 0, sizeof (bindAddress)); if (bindIp != "") { bindAddress.sin_family = PF_INET; bindAddress.sin_addr.s_addr = inet_addr(STR(bindIp)); bindAddress.sin_port = EHTONS(bindPort); //----MARKED-SHORT---- if (bindAddress.sin_addr.s_addr == INADDR_NONE) { FATAL("Unable to bind on address %s:%hu", STR(bindIp), bindPort); CLOSE_SOCKET(sock); return NULL; } uint32_t testVal = EHTONL(bindAddress.sin_addr.s_addr); if ((testVal > 0xe0000000) && (testVal < 0xefffffff)) { INFO("Subscribe to multicast %s:%"PRIu16, STR(bindIp), bindPort); int activateBroadcast = 1; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &activateBroadcast, sizeof (activateBroadcast)) != 0) { int err = errno; FATAL("Unable to activate SO_BROADCAST on the socket: (%d) %s", err, strerror(err)); return NULL; } if (ttl <= 255) { if (!setFdMulticastTTL(sock, (uint8_t) ttl)) { FATAL("Unable to set ttl"); CLOSE_SOCKET(sock); return NULL; } } } else { if (ttl <= 255) { if (!setFdTTL(sock, (uint8_t) ttl)) { FATAL("Unable to set ttl"); CLOSE_SOCKET(sock); return NULL; } } } if (bind(sock, (sockaddr *) & bindAddress, sizeof (sockaddr)) != 0) { int err = errno; FATAL("Unable to bind on address: udp://%s:%"PRIu16"; Error was: (%d) %s", STR(bindIp), bindPort, err, strerror(err)); CLOSE_SOCKET(sock); return NULL; } if ((testVal > 0xe0000000) && (testVal < 0xefffffff)) { struct ip_mreq group; group.imr_multiaddr.s_addr = inet_addr(STR(bindIp)); group.imr_interface.s_addr = INADDR_ANY; if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &group, sizeof (group)) < 0) { FATAL("Adding multicast group error"); CLOSE_SOCKET(sock); return NULL; } } } //4. Create the carrier UDPCarrier *pResult = new UDPCarrier(sock); pResult->_nearAddress = bindAddress; return pResult; }
void CommonTestsSuite::test_Endianess() { uint16_t ui16 = 0x0102; uint32_t ui32 = 0x01020304; uint64_t ui64 = 0x0102030405060708LL; double d = 123.456; //host to network uint8_t *pBuffer = NULL; ui16 = EHTONS(ui16); pBuffer = (uint8_t *) & ui16; TS_ASSERT(pBuffer[0] == 0x01); TS_ASSERT(pBuffer[1] == 0x02); pBuffer = NULL; ui32 = EHTONL(ui32); pBuffer = (uint8_t *) & ui32; TS_ASSERT(pBuffer[0] == 0x01); TS_ASSERT(pBuffer[1] == 0x02); TS_ASSERT(pBuffer[2] == 0x03); TS_ASSERT(pBuffer[3] == 0x04); pBuffer = NULL; ui32 = 0x01020304; ui32 = EHTONA(ui32); pBuffer = (uint8_t *) & ui32; TS_ASSERT(pBuffer[0] == 0x02); TS_ASSERT(pBuffer[1] == 0x03); TS_ASSERT(pBuffer[2] == 0x04); TS_ASSERT(pBuffer[3] == 0x01); pBuffer = NULL; ui64 = EHTONLL(ui64); pBuffer = (uint8_t *) & ui64; TS_ASSERT(pBuffer[0] == 0x01); TS_ASSERT(pBuffer[1] == 0x02); TS_ASSERT(pBuffer[2] == 0x03); TS_ASSERT(pBuffer[3] == 0x04); TS_ASSERT(pBuffer[4] == 0x05); TS_ASSERT(pBuffer[5] == 0x06); TS_ASSERT(pBuffer[6] == 0x07); TS_ASSERT(pBuffer[7] == 0x08); pBuffer = NULL; EHTOND(d, ui64); pBuffer = (uint8_t *) & ui64; TS_ASSERT(pBuffer[0] == 0x40); TS_ASSERT(pBuffer[1] == 0x5e); TS_ASSERT(pBuffer[2] == 0xdd); TS_ASSERT(pBuffer[3] == 0x2f); TS_ASSERT(pBuffer[4] == 0x1a); TS_ASSERT(pBuffer[5] == 0x9f); TS_ASSERT(pBuffer[6] == 0xbe); TS_ASSERT(pBuffer[7] == 0x77); //network to host pointer char buffer[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; ui16 = ENTOHSP(buffer); TS_ASSERT(ui16 == 0x0001); ui16 = ENTOHSP(buffer + 1); TS_ASSERT(ui16 == 0x0102); ui16 = ENTOHSP(buffer + 2); TS_ASSERT(ui16 == 0x0203); ui16 = ENTOHSP(buffer + 3); TS_ASSERT(ui16 == 0x0304); ui16 = ENTOHSP(buffer + 4); TS_ASSERT(ui16 == 0x0405); ui16 = ENTOHSP(buffer + 5); TS_ASSERT(ui16 == 0x0506); ui16 = ENTOHSP(buffer + 6); TS_ASSERT(ui16 == 0x0607); ui16 = ENTOHSP(buffer + 7); TS_ASSERT(ui16 == 0x0708); ui16 = ENTOHSP(buffer + 8); TS_ASSERT(ui16 == 0x0809); ui16 = ENTOHSP(buffer + 9); TS_ASSERT(ui16 == 0x090a); ui16 = ENTOHSP(buffer + 10); TS_ASSERT(ui16 == 0x0a0b); ui16 = ENTOHSP(buffer + 11); TS_ASSERT(ui16 == 0x0b0c); ui16 = ENTOHSP(buffer + 12); TS_ASSERT(ui16 == 0x0c0d); ui16 = ENTOHSP(buffer + 13); TS_ASSERT(ui16 == 0x0d0e); ui16 = ENTOHSP(buffer + 14); TS_ASSERT(ui16 == 0x0e0f); ui32 = ENTOHLP(buffer); TS_ASSERT(ui32 == 0x00010203); ui32 = ENTOHLP(buffer + 1); TS_ASSERT(ui32 == 0x01020304); ui32 = ENTOHLP(buffer + 2); TS_ASSERT(ui32 == 0x02030405); ui32 = ENTOHLP(buffer + 3); TS_ASSERT(ui32 == 0x03040506); ui32 = ENTOHLP(buffer + 4); TS_ASSERT(ui32 == 0x04050607); ui32 = ENTOHLP(buffer + 5); TS_ASSERT(ui32 == 0x05060708); ui32 = ENTOHLP(buffer + 6); TS_ASSERT(ui32 == 0x06070809); ui32 = ENTOHLP(buffer + 7); TS_ASSERT(ui32 == 0x0708090a); ui32 = ENTOHLP(buffer + 8); TS_ASSERT(ui32 == 0x08090a0b); ui32 = ENTOHLP(buffer + 9); TS_ASSERT(ui32 == 0x090a0b0c); ui32 = ENTOHLP(buffer + 10); TS_ASSERT(ui32 == 0x0a0b0c0d); ui32 = ENTOHLP(buffer + 11); TS_ASSERT(ui32 == 0x0b0c0d0e); ui32 = ENTOHLP(buffer + 12); TS_ASSERT(ui32 == 0x0c0d0e0f); ui32 = ENTOHAP(buffer); TS_ASSERT(ui32 == 0x03000102); ui32 = ENTOHAP(buffer + 1); TS_ASSERT(ui32 == 0x04010203); ui32 = ENTOHAP(buffer + 2); TS_ASSERT(ui32 == 0x05020304); ui32 = ENTOHAP(buffer + 3); TS_ASSERT(ui32 == 0x06030405); ui32 = ENTOHAP(buffer + 4); TS_ASSERT(ui32 == 0x07040506); ui32 = ENTOHAP(buffer + 5); TS_ASSERT(ui32 == 0x08050607); ui32 = ENTOHAP(buffer + 6); TS_ASSERT(ui32 == 0x09060708); ui32 = ENTOHAP(buffer + 7); TS_ASSERT(ui32 == 0x0a070809); ui32 = ENTOHAP(buffer + 8); TS_ASSERT(ui32 == 0x0b08090a); ui32 = ENTOHAP(buffer + 9); TS_ASSERT(ui32 == 0x0c090a0b); ui32 = ENTOHAP(buffer + 10); TS_ASSERT(ui32 == 0x0d0a0b0c); ui32 = ENTOHAP(buffer + 11); TS_ASSERT(ui32 == 0x0e0b0c0d); ui32 = ENTOHAP(buffer + 12); TS_ASSERT(ui32 == 0x0f0c0d0e); ui64 = ENTOHLLP(buffer); TS_ASSERT(ui64 == 0x0001020304050607LL); ui64 = ENTOHLLP(buffer + 1); TS_ASSERT(ui64 == 0x0102030405060708LL); ui64 = ENTOHLLP(buffer + 2); TS_ASSERT(ui64 == 0x0203040506070809LL); ui64 = ENTOHLLP(buffer + 3); TS_ASSERT(ui64 == 0x030405060708090aLL); ui64 = ENTOHLLP(buffer + 4); TS_ASSERT(ui64 == 0x0405060708090a0bLL); ui64 = ENTOHLLP(buffer + 5); TS_ASSERT(ui64 == 0x05060708090a0b0cLL); ui64 = ENTOHLLP(buffer + 6); TS_ASSERT(ui64 == 0x060708090a0b0c0dLL); ui64 = ENTOHLLP(buffer + 7); TS_ASSERT(ui64 == 0x0708090a0b0c0d0eLL); ui64 = ENTOHLLP(buffer + 8); TS_ASSERT(ui64 == 0x08090a0b0c0d0e0fLL); char *pTempBuffer = new char[64 + 8]; unsigned char rawDouble[] = {0x40, 0x5E, 0xDD, 0x2F, 0x1A, 0x9F, 0xBE, 0x77}; double tempDoubleVal = 0; for (int i = 0; i <= 64; i++) { memset(pTempBuffer, 0, i); memcpy(pTempBuffer + i, rawDouble, 8); memset(pTempBuffer + i + 8, 0, 64 + 8 - i - 8); ENTOHDP((pTempBuffer + i), tempDoubleVal); TS_ASSERT(d == tempDoubleVal); } delete[] pTempBuffer; //network to host #ifdef LITTLE_ENDIAN_BYTE_ALIGNED TS_ASSERT(ENTOHA(0x01040302) == 0x01020304); TS_ASSERT(ENTOHLL(0x0807060504030201LL) == 0x0102030405060708LL); ENTOHD(0x77BE9F1A2FDD5E40LL, tempDoubleVal); TS_ASSERT(d == tempDoubleVal); #endif /* LITTLE_ENDIAN_BYTE_ALIGNED */ #ifdef LITTLE_ENDIAN_SHORT_ALIGNED TS_ASSERT(ENTOHA(0x01040302) == 0x01020304); TS_ASSERT(ENTOHLL(0x0807060504030201LL) == 0x0102030405060708LL); ENTOHD(0x77BE9F1A2FDD5E40LL, tempDoubleVal); TS_ASSERT(d == tempDoubleVal); #endif /* LITTLE_ENDIAN_SHORT_ALIGNED */ #ifdef BIG_ENDIAN_BYTE_ALIGNED TS_ASSERT(ENTOHA(0x02030401) == 0x01020304); TS_ASSERT(ENTOHLL(0x0102030405060708LL) == 0x0102030405060708LL); #error ENTOHD not tested #endif /* BIG_ENDIAN_BYTE_ALIGNED */ #ifdef BIG_ENDIAN_SHORT_ALIGNED #error BIG_ENDIAN_SHORT_ALIGNED set of tests not yet implemented!!! Please take care of this first!!! #endif /* BIG_ENDIAN_SHORT_ALIGNED */ //double mirror TS_ASSERT(ENTOHS(EHTONS(0x0102)) == 0x0102); TS_ASSERT(EHTONS(ENTOHS(0x0102)) == 0x0102); TS_ASSERT(ENTOHL(EHTONL(0x01020304)) == 0x01020304); TS_ASSERT(EHTONL(ENTOHL(0x01020304)) == 0x01020304); TS_ASSERT(ENTOHLL(EHTONLL(0x0102030405060708LL)) == 0x0102030405060708LL); TS_ASSERT(EHTONLL(ENTOHLL(0x0102030405060708LL)) == 0x0102030405060708LL); //EHTOND/ENTOHD are different. Requires 2 parameters. So, no double mirror TS_ASSERT(ENTOHA(EHTONA(0x01020304)) == 0x01020304); TS_ASSERT(EHTONA(ENTOHA(0x01020304)) == 0x01020304); // Buffer Put routines for (int i = 0; i < 16; i++) { EHTONSP(buffer + i, 0x0102); TS_ASSERT(ENTOHSP(buffer + i) == 0x0102); EHTONLP(buffer + i, 0x01020304); TS_ASSERT(ENTOHLP(buffer + i) == 0x01020304); EHTONLLP(buffer + i, 0x0102030405060708LL); TS_ASSERT(ENTOHLLP(buffer + i) == 0x0102030405060708LL); EHTONDP(d, (buffer + i)); ENTOHDP(buffer + i, tempDoubleVal); TS_ASSERT(d == tempDoubleVal); } }
UDPCarrier* UDPCarrier::Create(string bindIp, uint16_t bindPort, uint16_t ttl, uint16_t tos, string ssmIp) { //1. Create the socket int sock = socket(AF_INET, SOCK_DGRAM, 0); if ((sock < 0) || (!setFdCloseOnExec(sock))) { int err = LASTSOCKETERROR; FATAL("Unable to create socket: %d", err); return NULL; } //2. fd options if (!setFdOptions(sock, true)) { FATAL("Unable to set fd options"); CLOSE_SOCKET(sock); return NULL; } if (tos <= 255) { if (!setFdTOS(sock, (uint8_t) tos)) { FATAL("Unable to set tos"); CLOSE_SOCKET(sock); return NULL; } } //3. bind if necessary sockaddr_in bindAddress; memset(&bindAddress, 0, sizeof (bindAddress)); if (bindIp != "") { bindAddress.sin_family = PF_INET; bindAddress.sin_addr.s_addr = inet_addr(STR(bindIp)); bindAddress.sin_port = EHTONS(bindPort); //----MARKED-SHORT---- if (bindAddress.sin_addr.s_addr == INADDR_NONE) { FATAL("Unable to bind on address %s:%hu", STR(bindIp), bindPort); CLOSE_SOCKET(sock); return NULL; } uint32_t testVal = EHTONL(bindAddress.sin_addr.s_addr); if ((testVal > 0xe0000000) && (testVal < 0xefffffff)) { INFO("Subscribe to multicast %s:%"PRIu16, STR(bindIp), bindPort); BOOL activateBroadcast = FALSE; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *) &activateBroadcast, sizeof (activateBroadcast)) != 0) { int err = LASTSOCKETERROR; FATAL("Unable to activate SO_BROADCAST on the socket: %d", err); return NULL; } if (ttl <= 255) { if (!setFdMulticastTTL(sock, (uint8_t) ttl)) { FATAL("Unable to set ttl"); CLOSE_SOCKET(sock); return NULL; } } } else { if (ttl <= 255) { if (!setFdTTL(sock, (uint8_t) ttl)) { FATAL("Unable to set ttl"); CLOSE_SOCKET(sock); return NULL; } } } if (bind(sock, (sockaddr *) & bindAddress, sizeof (sockaddr)) != 0) { int err = LASTSOCKETERROR; FATAL("Unable to bind on address: udp://%s:%"PRIu16"; Error was: %d", STR(bindIp), bindPort, err); CLOSE_SOCKET(sock); return NULL; } if ((testVal > 0xe0000000) && (testVal < 0xefffffff)) { if (!setFdJoinMulticast(sock, bindIp, bindPort, ssmIp)) { FATAL("Adding multicast failed"); CLOSE_SOCKET(sock); return NULL; } } } //4. Create the carrier UDPCarrier *pResult = new UDPCarrier(sock); pResult->_nearAddress = bindAddress; return pResult; }