2019-09-05 07:16:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-03-30 10:22:57 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <cstring>
|
2019-08-12 06:25:17 +00:00
|
|
|
#include <iostream>
|
2019-09-05 07:16:48 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <queue>
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-05-17 06:25:11 +00:00
|
|
|
#include "debug.hh"
|
2019-07-15 07:16:31 +00:00
|
|
|
#include "queue.hh"
|
2019-05-22 09:43:35 +00:00
|
|
|
#include "send.hh"
|
2020-02-07 10:22:58 +00:00
|
|
|
#include "sender.hh"
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-09-11 08:12:10 +00:00
|
|
|
#include "formats/hevc.hh"
|
|
|
|
|
|
2019-09-11 07:06:52 +00:00
|
|
|
#define PTR_DIFF(a, b) ((ptrdiff_t)((char *)(a) - (char *)(b)))
|
|
|
|
|
|
|
|
|
|
#define haszero64_le(v) (((v) - 0x0101010101010101) & ~(v) & 0x8080808080808080UL)
|
|
|
|
|
#define haszero32_le(v) (((v) - 0x01010101) & ~(v) & 0x80808080UL)
|
|
|
|
|
|
|
|
|
|
#define haszero64_be(v) (((v) - 0x1010101010101010) & ~(v) & 0x0808080808080808UL)
|
|
|
|
|
#define haszero32_be(v) (((v) - 0x10101010) & ~(v) & 0x08080808UL)
|
|
|
|
|
|
2020-01-09 08:10:58 +00:00
|
|
|
#ifndef __LITTLE_ENDIAN
|
|
|
|
|
#define __LITTLE_ENDIAN 1337
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef __BYTE_ORDER
|
2020-02-14 12:24:42 +00:00
|
|
|
//#warning "setting byte order to little endian"
|
2020-01-09 08:10:58 +00:00
|
|
|
#define __BYTE_ORDER __LITTLE_ENDIAN
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
extern rtp_error_t __hevc_receiver_optimistic(kvz_rtp::receiver *receiver);
|
|
|
|
|
extern rtp_error_t __hevc_receiver(kvz_rtp::receiver *receiver);
|
2019-09-12 05:57:44 +00:00
|
|
|
|
2019-09-11 07:06:52 +00:00
|
|
|
static inline unsigned __find_hevc_start(uint32_t value)
|
|
|
|
|
{
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
uint16_t u = (value >> 16) & 0xffff;
|
|
|
|
|
uint16_t l = (value >> 0) & 0xffff;
|
|
|
|
|
|
|
|
|
|
bool t1 = (l == 0);
|
|
|
|
|
bool t2 = ((u & 0xff) == 0x01);
|
|
|
|
|
bool t3 = (u == 0x0100);
|
|
|
|
|
bool t4 = (((l >> 8) & 0xff) == 0);
|
|
|
|
|
#else
|
|
|
|
|
uint16_t u = (value >> 0) & 0xffff;
|
|
|
|
|
uint16_t l = (value >> 16) & 0xffff;
|
|
|
|
|
|
|
|
|
|
bool t1 = (l == 0);
|
|
|
|
|
bool t2 = (((u >> 8) & 0xff) == 0x01);
|
|
|
|
|
bool t3 = (u == 0x0001);
|
|
|
|
|
bool t4 = ((l & 0xff) == 0);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (t1) {
|
|
|
|
|
/* 0x00000001 */
|
|
|
|
|
if (t3)
|
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
|
|
/* "value" definitely has a start code (0x000001XX), but at this
|
|
|
|
|
* point we can't know for sure whether it's 3 or 4 bytes long.
|
|
|
|
|
*
|
|
|
|
|
* Return 5 to indicate that start length could not be determined
|
|
|
|
|
* and that caller must check previous dword's last byte for 0x00 */
|
|
|
|
|
if (t2)
|
|
|
|
|
return 5;
|
|
|
|
|
} else if (t4 && t3) {
|
|
|
|
|
/* 0xXX000001 */
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 07:54:33 +00:00
|
|
|
/* NOTE: the area 0 - len (ie data[0] - data[len - 1]) must be addressable
|
2019-09-11 07:06:52 +00:00
|
|
|
* Do not add offset to "data" ptr before passing it to __get_hevc_start()! */
|
|
|
|
|
static ssize_t __get_hevc_start(uint8_t *data, size_t len, size_t offset, uint8_t& start_len)
|
2019-03-30 10:22:57 +00:00
|
|
|
{
|
2019-09-11 07:06:52 +00:00
|
|
|
bool prev_z = false;
|
|
|
|
|
bool cur_z = false;
|
|
|
|
|
size_t pos = offset;
|
2020-02-04 10:07:05 +00:00
|
|
|
size_t rpos = len - (len % 8) - 1;
|
2019-09-11 07:06:52 +00:00
|
|
|
uint8_t *ptr = data + offset;
|
|
|
|
|
uint8_t *tmp = nullptr;
|
|
|
|
|
uint8_t lb = 0;
|
|
|
|
|
uint32_t prev = UINT32_MAX;
|
|
|
|
|
|
|
|
|
|
uint64_t prefetch = UINT64_MAX;
|
|
|
|
|
uint32_t value = UINT32_MAX;
|
|
|
|
|
unsigned ret = 0;
|
|
|
|
|
|
|
|
|
|
/* We can get rid of the bounds check when looping through
|
|
|
|
|
* non-zero 8 byte chunks by setting the last byte to zero.
|
|
|
|
|
*
|
|
|
|
|
* This added zero will make the last 8 byte zero check to fail
|
2020-02-04 10:07:05 +00:00
|
|
|
* and when we get out of the loop we can check if we've reached the end */
|
|
|
|
|
lb = data[rpos];
|
|
|
|
|
data[rpos] = 0;
|
2019-09-11 07:06:52 +00:00
|
|
|
|
2020-02-04 10:07:05 +00:00
|
|
|
while (pos + 8 < len) {
|
2019-09-11 07:06:52 +00:00
|
|
|
prefetch = *(uint64_t *)ptr;
|
|
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
if (!prev_z && !(cur_z = haszero64_le(prefetch))) {
|
|
|
|
|
#else
|
|
|
|
|
if (!prev_z && !(cur_z = haszero64_be(prefetch))) {
|
|
|
|
|
#endif
|
|
|
|
|
/* pos is not used in the following loop so it makes little sense to
|
|
|
|
|
* update it on every iteration. Faster way to do the loop is to save
|
|
|
|
|
* ptr's current value before loop, update only ptr in the loop and when
|
|
|
|
|
* the loop is exited, calculate the difference between tmp and ptr to get
|
|
|
|
|
* the number of iterations done * 8 */
|
|
|
|
|
tmp = ptr;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
ptr += 8;
|
|
|
|
|
prefetch = *(uint64_t *)ptr;
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
cur_z = haszero64_le(prefetch);
|
|
|
|
|
#else
|
|
|
|
|
cur_z = haszero64_be(prefetch);
|
|
|
|
|
#endif
|
|
|
|
|
} while (!cur_z);
|
|
|
|
|
|
|
|
|
|
pos += PTR_DIFF(ptr, tmp);
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2020-02-04 10:07:05 +00:00
|
|
|
if (pos + 8 >= len)
|
2019-09-11 07:06:52 +00:00
|
|
|
break;
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-11 07:06:52 +00:00
|
|
|
value = *(uint32_t *)ptr;
|
|
|
|
|
|
|
|
|
|
if (cur_z)
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
cur_z = haszero32_le(value);
|
|
|
|
|
#else
|
|
|
|
|
cur_z = haszero32_be(value);
|
|
|
|
|
#endif
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-09-11 07:06:52 +00:00
|
|
|
if (!prev_z && !cur_z)
|
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
|
|
/* Previous dword had zeros but this doesn't. The only way there might be a start code
|
|
|
|
|
* is if the most significant byte of current dword is 0x01 */
|
|
|
|
|
if (prev_z && !cur_z) {
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
/* previous dword: 0xXX000000 or 0xXXXX0000 and current dword 0x01XXXXXX */
|
|
|
|
|
if (((value >> 0) & 0xff) == 0x01 && ((prev >> 16) & 0xffff) == 0) {
|
|
|
|
|
start_len = (((prev >> 8) & 0xffffff) == 0) ? 4 : 3;
|
|
|
|
|
#else
|
|
|
|
|
if (((value >> 24) & 0xff) == 0x01 && ((prev >> 0) & 0xffff) == 0) {
|
|
|
|
|
start_len = (((prev >> 0) & 0xffffff) == 0) ? 4 : 3;
|
|
|
|
|
#endif
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-09-11 07:06:52 +00:00
|
|
|
return pos + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
if ((ret = start_len = __find_hevc_start(value)) > 0) {
|
|
|
|
|
if (ret == 5) {
|
|
|
|
|
ret = 3;
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
start_len = (((prev >> 24) & 0xff) == 0) ? 4 : 3;
|
|
|
|
|
#else
|
|
|
|
|
start_len = (((prev >> 0) & 0xff) == 0) ? 4 : 3;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-09-11 07:06:52 +00:00
|
|
|
return pos + ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
uint16_t u = (value >> 16) & 0xffff;
|
|
|
|
|
uint16_t l = (value >> 0) & 0xffff;
|
|
|
|
|
uint16_t p = (prev >> 16) & 0xffff;
|
|
|
|
|
|
|
|
|
|
bool t1 = ((p & 0xffff) == 0);
|
|
|
|
|
bool t2 = (((p >> 8) & 0xff) == 0);
|
|
|
|
|
bool t4 = (l == 0x0100);
|
|
|
|
|
bool t5 = (l == 0x0000 && u == 0x01);
|
|
|
|
|
#else
|
|
|
|
|
uint16_t u = (value >> 0) & 0xffff;
|
|
|
|
|
uint16_t l = (value >> 16) & 0xffff;
|
|
|
|
|
uint16_t p = (prev >> 0) & 0xffff;
|
|
|
|
|
|
|
|
|
|
bool t1 = ((p & 0xffff) == 0);
|
|
|
|
|
bool t2 = ((p & 0xff) == 0);
|
|
|
|
|
bool t4 = (l == 0x0001);
|
|
|
|
|
bool t5 = (l == 0x0000 && u == 0x01);
|
|
|
|
|
#endif
|
|
|
|
|
if (t1 && t4) {
|
|
|
|
|
/* previous dword 0xxxxx0000 and current dword is 0x0001XXXX */
|
|
|
|
|
if (t4) {
|
|
|
|
|
start_len = 4;
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-09-11 07:06:52 +00:00
|
|
|
return pos + 2;
|
|
|
|
|
}
|
|
|
|
|
/* Previous dwod was 0xXXXXXX00 */
|
|
|
|
|
} else if (t2) {
|
|
|
|
|
/* Current dword is 0x000001XX */
|
|
|
|
|
if (t5) {
|
|
|
|
|
start_len = 4;
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-09-11 07:06:52 +00:00
|
|
|
return pos + 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Current dword is 0x0001XXXX */
|
|
|
|
|
else if (t4) {
|
|
|
|
|
start_len = 3;
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-09-11 07:06:52 +00:00
|
|
|
return pos + 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
end:
|
|
|
|
|
prev_z = cur_z;
|
|
|
|
|
pos += 4;
|
|
|
|
|
ptr += 4;
|
|
|
|
|
prev = value;
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 10:07:05 +00:00
|
|
|
data[rpos] = lb;
|
2019-03-30 10:22:57 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:11:21 +00:00
|
|
|
static rtp_error_t __push_hevc_nal(
|
2020-02-07 10:22:58 +00:00
|
|
|
kvz_rtp::sender *sender,
|
2019-10-08 05:20:29 +00:00
|
|
|
kvz_rtp::frame_queue *fqueue,
|
2019-08-26 05:49:22 +00:00
|
|
|
uint8_t *data, size_t data_len,
|
|
|
|
|
bool more
|
|
|
|
|
)
|
2019-03-30 10:22:57 +00:00
|
|
|
{
|
2019-06-03 08:58:53 +00:00
|
|
|
uint8_t nalType = (data[0] >> 1) & 0x3F;
|
|
|
|
|
rtp_error_t ret = RTP_OK;
|
|
|
|
|
size_t data_left = data_len;
|
|
|
|
|
size_t data_pos = 0;
|
2019-05-22 09:43:35 +00:00
|
|
|
|
2019-08-26 05:49:22 +00:00
|
|
|
#ifdef __linux__
|
2019-05-22 09:43:35 +00:00
|
|
|
if (data_len <= MAX_PAYLOAD) {
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = fqueue->enqueue_message(sender, data, data_len)) != RTP_OK) {
|
2019-10-08 05:20:29 +00:00
|
|
|
LOG_ERROR("enqeueu failed for small packet");
|
2019-08-26 05:49:22 +00:00
|
|
|
return ret;
|
2019-10-08 05:20:29 +00:00
|
|
|
}
|
2019-10-22 09:27:43 +00:00
|
|
|
|
|
|
|
|
if (more)
|
|
|
|
|
return RTP_NOT_READY;
|
2020-02-07 10:22:58 +00:00
|
|
|
return fqueue->flush_queue(sender);
|
2019-05-22 09:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:20:29 +00:00
|
|
|
/* The payload is larger than MTU (1500 bytes) so we must split it into smaller RTP frames
|
|
|
|
|
* Because we don't if the SCD is enabled and thus cannot make any assumptions about the life time
|
|
|
|
|
* of current stack, we need to store NAL and FU headers to the frame queue transaction.
|
|
|
|
|
*
|
|
|
|
|
* This can be done by asking a handle to current transaction's buffer vectors.
|
|
|
|
|
*
|
|
|
|
|
* During Connection initialization, the frame queue was given HEVC as the payload format so the
|
|
|
|
|
* transaction also contains our media-specifi headers [get_media_headers()]. */
|
|
|
|
|
auto buffers = fqueue->get_buffer_vector();
|
|
|
|
|
auto headers = (kvz_rtp::hevc::media_headers_t *)fqueue->get_media_headers();
|
2019-06-12 08:21:40 +00:00
|
|
|
|
2019-10-08 05:20:29 +00:00
|
|
|
headers->nal_header[0] = 49 << 1; /* fragmentation unit */
|
|
|
|
|
headers->nal_header[1] = 1; /* temporal id */
|
2019-06-12 08:21:40 +00:00
|
|
|
|
2019-10-08 05:20:29 +00:00
|
|
|
headers->fu_headers[0] = (uint8_t)((1 << 7) | nalType);
|
|
|
|
|
headers->fu_headers[1] = nalType;
|
|
|
|
|
headers->fu_headers[2] = (uint8_t)((1 << 6) | nalType);
|
2019-06-12 08:21:40 +00:00
|
|
|
|
2019-10-08 05:20:29 +00:00
|
|
|
buffers.push_back(std::make_pair(sizeof(headers->nal_header), headers->nal_header));
|
|
|
|
|
buffers.push_back(std::make_pair(sizeof(uint8_t), &headers->fu_headers[0]));
|
|
|
|
|
buffers.push_back(std::make_pair(MAX_PAYLOAD, nullptr));
|
2019-06-12 08:21:40 +00:00
|
|
|
|
|
|
|
|
data_pos = kvz_rtp::frame::HEADER_SIZE_HEVC_NAL;
|
|
|
|
|
data_left -= kvz_rtp::frame::HEADER_SIZE_HEVC_NAL;
|
|
|
|
|
|
|
|
|
|
while (data_left > MAX_PAYLOAD) {
|
2019-07-15 07:16:31 +00:00
|
|
|
buffers.at(2).first = MAX_PAYLOAD;
|
2019-06-12 08:21:40 +00:00
|
|
|
buffers.at(2).second = &data[data_pos];
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = fqueue->enqueue_message(sender, buffers)) != RTP_OK) {
|
2019-10-08 05:20:29 +00:00
|
|
|
LOG_ERROR("enqueue failed");
|
2019-10-09 07:27:28 +00:00
|
|
|
fqueue->deinit_transaction();
|
2019-06-12 08:21:40 +00:00
|
|
|
return ret;
|
2019-10-08 05:20:29 +00:00
|
|
|
}
|
2019-06-12 08:21:40 +00:00
|
|
|
|
|
|
|
|
data_pos += MAX_PAYLOAD;
|
|
|
|
|
data_left -= MAX_PAYLOAD;
|
|
|
|
|
|
2019-08-09 09:13:38 +00:00
|
|
|
/* from now on, use the FU header meant for middle fragments */
|
2019-10-08 05:20:29 +00:00
|
|
|
buffers.at(1).second = &headers->fu_headers[1];
|
2019-06-12 08:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-09 09:13:38 +00:00
|
|
|
/* use the FU header meant for the last fragment */
|
2019-10-08 05:20:29 +00:00
|
|
|
buffers.at(1).second = &headers->fu_headers[2];
|
2019-06-12 08:21:40 +00:00
|
|
|
|
|
|
|
|
buffers.at(2).first = data_left;
|
|
|
|
|
buffers.at(2).second = &data[data_pos];
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = fqueue->enqueue_message(sender, buffers)) != RTP_OK) {
|
2019-07-15 07:16:31 +00:00
|
|
|
LOG_ERROR("Failed to send HEVC frame!");
|
2019-10-08 05:20:29 +00:00
|
|
|
fqueue->deinit_transaction();
|
2019-07-15 07:16:31 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 09:27:43 +00:00
|
|
|
if (more)
|
|
|
|
|
return RTP_NOT_READY;
|
2020-02-07 10:22:58 +00:00
|
|
|
return fqueue->flush_queue(sender);
|
2019-06-12 08:21:40 +00:00
|
|
|
#else
|
2019-08-26 05:49:22 +00:00
|
|
|
if (data_len <= MAX_PAYLOAD) {
|
|
|
|
|
LOG_DEBUG("send unfrag size %zu, type %u", data_len, nalType);
|
2019-11-27 06:39:56 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = kvz_rtp::generic::push_frame(sender, data, data_len, 0)) != RTP_OK) {
|
|
|
|
|
LOG_ERROR("failed to send small packet! %s", strerror(errno));
|
2019-11-27 06:39:56 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (more)
|
|
|
|
|
return RTP_NOT_READY;
|
|
|
|
|
return RTP_OK;
|
2019-08-26 05:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-12 08:21:40 +00:00
|
|
|
const size_t HEADER_SIZE =
|
|
|
|
|
kvz_rtp::frame::HEADER_SIZE_RTP +
|
|
|
|
|
kvz_rtp::frame::HEADER_SIZE_HEVC_NAL +
|
|
|
|
|
kvz_rtp::frame::HEADER_SIZE_HEVC_FU;
|
|
|
|
|
|
2020-03-10 11:55:19 +00:00
|
|
|
uint8_t buffer[HEADER_SIZE + MAX_PAYLOAD] = { 0 };
|
2019-06-12 08:21:40 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
sender->get_rtp_ctx()->fill_header(buffer);
|
2019-06-12 08:21:40 +00:00
|
|
|
|
|
|
|
|
buffer[kvz_rtp::frame::HEADER_SIZE_RTP + 0] = 49 << 1; /* fragmentation unit */
|
|
|
|
|
buffer[kvz_rtp::frame::HEADER_SIZE_RTP + 1] = 1; /* TID */
|
|
|
|
|
buffer[kvz_rtp::frame::HEADER_SIZE_RTP +
|
2019-06-04 10:57:09 +00:00
|
|
|
kvz_rtp::frame::HEADER_SIZE_HEVC_NAL] = (1 << 7) | nalType; /* Start bit + NAL type */
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-06-04 10:57:09 +00:00
|
|
|
data_pos = kvz_rtp::frame::HEADER_SIZE_HEVC_NAL;
|
|
|
|
|
data_left -= kvz_rtp::frame::HEADER_SIZE_HEVC_NAL;
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-05-28 07:12:08 +00:00
|
|
|
while (data_left > MAX_PAYLOAD) {
|
2019-06-12 08:21:40 +00:00
|
|
|
memcpy(&buffer[HEADER_SIZE], &data[data_pos], MAX_PAYLOAD);
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = kvz_rtp::send::send_frame(sender, buffer, sizeof(buffer))) != RTP_OK)
|
2019-06-12 08:21:40 +00:00
|
|
|
return RTP_GENERIC_ERROR;
|
2019-05-22 09:43:35 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
sender->get_rtp_ctx()->update_sequence(buffer);
|
2019-07-26 07:53:13 +00:00
|
|
|
|
2019-05-28 07:12:08 +00:00
|
|
|
data_pos += MAX_PAYLOAD;
|
|
|
|
|
data_left -= MAX_PAYLOAD;
|
2019-03-30 10:22:57 +00:00
|
|
|
|
|
|
|
|
/* Clear extra bits */
|
2019-06-12 08:21:40 +00:00
|
|
|
buffer[kvz_rtp::frame::HEADER_SIZE_RTP +
|
2019-07-24 07:17:58 +00:00
|
|
|
kvz_rtp::frame::HEADER_SIZE_HEVC_NAL] = nalType;
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-12 08:21:40 +00:00
|
|
|
buffer[kvz_rtp::frame::HEADER_SIZE_RTP +
|
2020-03-10 11:55:19 +00:00
|
|
|
kvz_rtp::frame::HEADER_SIZE_HEVC_NAL] = nalType | (1 << 6); /* set E bit to signal end of data */
|
2019-06-12 08:21:40 +00:00
|
|
|
|
|
|
|
|
memcpy(&buffer[HEADER_SIZE], &data[data_pos], data_left);
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = kvz_rtp::send::send_frame(sender, buffer, HEADER_SIZE + data_left)) != RTP_OK) {
|
2019-11-27 06:39:56 +00:00
|
|
|
LOG_ERROR("Failed to send frame");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (more)
|
|
|
|
|
return RTP_NOT_READY;
|
|
|
|
|
return RTP_OK;
|
2019-06-12 08:21:40 +00:00
|
|
|
#endif
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:11:21 +00:00
|
|
|
static rtp_error_t __push_hevc_slice(
|
2020-02-07 10:22:58 +00:00
|
|
|
kvz_rtp::sender *sender,
|
2019-10-11 06:11:21 +00:00
|
|
|
kvz_rtp::frame_queue *fqueue,
|
|
|
|
|
uint8_t *data, size_t data_len,
|
|
|
|
|
int flags
|
|
|
|
|
)
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
{
|
|
|
|
|
rtp_error_t ret;
|
|
|
|
|
|
|
|
|
|
if ((flags & RTP_SLICE) == 0) {
|
|
|
|
|
LOG_DEBUG("not a slice!");
|
|
|
|
|
return RTP_INVALID_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_len >= MAX_PAYLOAD) {
|
|
|
|
|
LOG_ERROR("slice is too big!");
|
2019-10-08 05:20:29 +00:00
|
|
|
(void)fqueue->deinit_transaction();
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
return RTP_INVALID_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = fqueue->enqueue_message(sender, data, data_len)) != RTP_OK) {
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
LOG_ERROR("Failed to enqueue HEVC slice!");
|
2019-10-08 05:20:29 +00:00
|
|
|
(void)fqueue->deinit_transaction();
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((flags & RTP_MORE) == 0)
|
2020-02-07 10:22:58 +00:00
|
|
|
ret = fqueue->flush_queue(sender);
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:11:21 +00:00
|
|
|
static rtp_error_t __push_hevc_frame(
|
2020-02-07 10:22:58 +00:00
|
|
|
kvz_rtp::sender *sender,
|
2019-10-11 06:11:21 +00:00
|
|
|
kvz_rtp::frame_queue *fqueue,
|
|
|
|
|
uint8_t *data, size_t data_len,
|
|
|
|
|
int flags
|
|
|
|
|
)
|
2019-03-30 10:22:57 +00:00
|
|
|
{
|
Add support for HEVC slices
The API didn't change much, if user wishes to use HEVC slices
(and thus preserve the state between push_frame() calls), he must
call the push_frame() with RTP_SLICE and RTP_MORE flags, like this:
push_frame(conn, data, 123, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 456, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 789, RTP_SLICE | RTP_MORE);
push_frame(conn, data, 100, RTP_SLICE);
RTP_MORE preserves the state between push_frame() calls and when the
last slice is given to kvzRTP, the RTP_MORE flags must be removed.
This flushes the frame queue and deinitializes it.
2019-09-24 05:54:09 +00:00
|
|
|
if (flags & RTP_SLICE)
|
2020-02-07 10:22:58 +00:00
|
|
|
return __push_hevc_slice(sender, fqueue, data, data_len, flags);
|
2019-08-30 06:03:35 +00:00
|
|
|
|
2019-08-26 05:49:22 +00:00
|
|
|
#ifdef __linux__
|
2019-09-11 07:06:52 +00:00
|
|
|
/* find first start code */
|
2019-08-30 06:03:35 +00:00
|
|
|
uint8_t start_len = 0;
|
2019-09-11 07:06:52 +00:00
|
|
|
int offset = __get_hevc_start(data, data_len, 0, start_len);
|
2019-08-30 06:03:35 +00:00
|
|
|
int prev_offset = offset;
|
2019-09-11 07:06:52 +00:00
|
|
|
size_t r_off = 0;
|
2019-08-30 06:03:35 +00:00
|
|
|
rtp_error_t ret = RTP_GENERIC_ERROR;
|
|
|
|
|
|
|
|
|
|
if (data_len < MAX_PAYLOAD) {
|
2019-09-11 07:06:52 +00:00
|
|
|
r_off = (offset < 0) ? 0 : offset; /* TODO: this looks ugly */
|
2019-10-22 09:27:43 +00:00
|
|
|
fqueue->deinit_transaction();
|
2020-02-07 10:22:58 +00:00
|
|
|
return kvz_rtp::generic::push_frame(sender, data + r_off, data_len - r_off, flags);
|
2019-08-30 06:03:35 +00:00
|
|
|
}
|
2019-08-26 05:49:22 +00:00
|
|
|
|
|
|
|
|
while (offset != -1) {
|
2019-09-11 07:06:52 +00:00
|
|
|
offset = __get_hevc_start(data, data_len, offset, start_len);
|
2019-08-26 05:49:22 +00:00
|
|
|
|
2019-09-11 07:06:52 +00:00
|
|
|
if (offset != -1) {
|
2020-02-07 10:22:58 +00:00
|
|
|
ret = __push_hevc_nal(sender, fqueue, &data[prev_offset], offset - prev_offset - start_len, true);
|
2019-08-26 05:49:22 +00:00
|
|
|
|
|
|
|
|
if (ret != RTP_NOT_READY)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
prev_offset = offset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if ((ret = __push_hevc_nal(sender, fqueue, &data[prev_offset], data_len - prev_offset, false)) == RTP_OK)
|
2019-08-26 05:49:22 +00:00
|
|
|
return RTP_OK;
|
|
|
|
|
|
|
|
|
|
error:
|
2019-10-08 05:20:29 +00:00
|
|
|
fqueue->deinit_transaction();
|
2019-08-26 05:49:22 +00:00
|
|
|
return ret;
|
|
|
|
|
#else
|
2019-05-22 09:43:35 +00:00
|
|
|
uint8_t start_len;
|
|
|
|
|
int32_t prev_offset = 0;
|
2019-11-13 06:08:52 +00:00
|
|
|
int offset = __get_hevc_start(data, data_len, 0, start_len);
|
2019-05-22 09:43:35 +00:00
|
|
|
prev_offset = offset;
|
2019-03-30 10:22:57 +00:00
|
|
|
|
|
|
|
|
while (offset != -1) {
|
2019-09-11 07:06:52 +00:00
|
|
|
offset = __get_hevc_start(data, data_len, offset, start_len);
|
2019-03-30 10:22:57 +00:00
|
|
|
|
|
|
|
|
if (offset > 4 && offset != -1) {
|
2020-02-07 10:22:58 +00:00
|
|
|
if (__push_hevc_nal(sender, nullptr, &data[prev_offset], offset - prev_offset - start_len, false) == -1)
|
2019-05-22 09:43:35 +00:00
|
|
|
return RTP_GENERIC_ERROR;
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2019-05-22 09:43:35 +00:00
|
|
|
prev_offset = offset;
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 09:43:35 +00:00
|
|
|
if (prev_offset == -1)
|
|
|
|
|
prev_offset = 0;
|
2019-03-30 10:22:57 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
return __push_hevc_nal(sender, nullptr, &data[prev_offset], data_len - prev_offset, false);
|
2019-08-26 05:49:22 +00:00
|
|
|
#endif
|
2019-06-03 08:58:53 +00:00
|
|
|
}
|
2019-07-30 05:33:21 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
rtp_error_t kvz_rtp::hevc::push_frame(kvz_rtp::sender *sender, uint8_t *data, size_t data_len, int flags)
|
2019-10-11 06:11:21 +00:00
|
|
|
{
|
2020-02-07 10:22:58 +00:00
|
|
|
if (!sender || !data || data_len == 0)
|
2019-10-11 06:11:21 +00:00
|
|
|
return RTP_INVALID_VALUE;
|
|
|
|
|
|
|
|
|
|
rtp_error_t ret = RTP_GENERIC_ERROR;
|
2020-02-07 10:22:58 +00:00
|
|
|
kvz_rtp::frame_queue *fqueue = sender->get_frame_queue();
|
2019-10-11 06:11:21 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if (!fqueue) {
|
|
|
|
|
LOG_ERROR("invalid frame queue");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = fqueue->init_transaction(sender, data)) != RTP_OK) {
|
2019-10-11 06:11:21 +00:00
|
|
|
LOG_ERROR("Invalid frame queue or failed to initialize transaction!");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags & RTP_SLICE)
|
2020-02-07 10:22:58 +00:00
|
|
|
return __push_hevc_slice(sender, fqueue, data, data_len, flags);
|
|
|
|
|
return __push_hevc_frame(sender, fqueue, data, data_len, flags);
|
2019-10-11 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
rtp_error_t kvz_rtp::hevc::push_frame(kvz_rtp::sender *sender, std::unique_ptr<uint8_t[]> data, size_t data_len, int flags)
|
2019-10-11 06:11:21 +00:00
|
|
|
{
|
2020-02-07 10:22:58 +00:00
|
|
|
if (!sender || !data || data_len == 0)
|
2019-10-11 06:11:21 +00:00
|
|
|
return RTP_INVALID_VALUE;
|
|
|
|
|
|
|
|
|
|
uint8_t *data_ptr = nullptr;
|
|
|
|
|
rtp_error_t ret = RTP_GENERIC_ERROR;
|
2020-02-07 10:22:58 +00:00
|
|
|
kvz_rtp::frame_queue *fqueue = sender->get_frame_queue();
|
2019-10-11 06:11:21 +00:00
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
if (!fqueue || (ret = fqueue->init_transaction(sender, std::move(data))) != RTP_OK) {
|
2019-10-11 06:11:21 +00:00
|
|
|
LOG_ERROR("Invalid frame queue or failed to initialize transaction!");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((data_ptr = fqueue->get_active_dataptr()) == nullptr) {
|
|
|
|
|
LOG_ERROR("Invalid data pointer, cannot continue!");
|
|
|
|
|
return RTP_INVALID_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags & RTP_SLICE)
|
2020-02-07 10:22:58 +00:00
|
|
|
return __push_hevc_slice(sender, fqueue, data_ptr, data_len, flags);
|
|
|
|
|
return __push_hevc_frame(sender, fqueue, data_ptr, data_len, flags);
|
2019-10-11 06:11:21 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
rtp_error_t kvz_rtp::hevc::frame_receiver(kvz_rtp::receiver *receiver, bool optimistic)
|
2019-07-30 05:33:21 +00:00
|
|
|
{
|
2020-01-09 08:10:58 +00:00
|
|
|
#ifdef __linux__
|
2020-02-07 10:22:58 +00:00
|
|
|
if (optimistic)
|
|
|
|
|
return __hevc_receiver_optimistic(receiver);
|
2020-01-09 08:10:58 +00:00
|
|
|
#endif
|
2020-02-07 10:22:58 +00:00
|
|
|
return __hevc_receiver(receiver);
|
2019-03-30 10:22:57 +00:00
|
|
|
}
|