common: Release the memory of copy with RTP_COPY flag

uvgRTP does not indeed take ownership of the data when used with non-
smart pointer API. Now the copy is deleted by making it a unique_ptr.
This commit is contained in:
Joni Räsänen 2022-09-15 11:56:22 +03:00
parent b0bc92b396
commit 1b2996fd25
4 changed files with 29 additions and 19 deletions

View File

@ -116,7 +116,7 @@ stream->push_frame(frame, frame_size, RTP_NO_H26X_SCL | RTP_COPY);
| Flag | Explanation | | Flag | Explanation |
| ---- |:----------:| | ---- |:----------:|
| RTP_NO_FLAGS | Use this if you don't need any RTP flags | | RTP_NO_FLAGS | Use this if you don't need any RTP flags |
| RTP_COPY | Copy the input buffer and operate on the copy. This means that uvgRTP does not take ownership of the frame. Does not work with unique_ptr for obvious reasons. | | RTP_COPY | Copy the input buffer and operate on the copy. Does not work with unique_ptr. |
| RTP_NO_H26X_SCL | By default, uvgRTP expect the need to search for NAL start codes from the frames using start code prefixes. Use this flag if your encoder provides ready NAL units without start code prefixes to disable Start Code Lookup (SCL). | | RTP_NO_H26X_SCL | By default, uvgRTP expect the need to search for NAL start codes from the frames using start code prefixes. Use this flag if your encoder provides ready NAL units without start code prefixes to disable Start Code Lookup (SCL). |
### Obsolete flags ### Obsolete flags

View File

@ -102,7 +102,7 @@ namespace uvgrtp {
* The frame is automatically reconstructed by the receiver if all fragments have been * The frame is automatically reconstructed by the receiver if all fragments have been
* received successfully. * received successfully.
* *
* \param data Pointer to data the that should be sent * \param data Pointer to data the that should be sent, uvgRTP does not take ownership of the memory
* \param data_len Length of data * \param data_len Length of data
* \param rtp_flags Optional flags, see ::RTP_FLAGS for more details * \param rtp_flags Optional flags, see ::RTP_FLAGS for more details
* *
@ -156,7 +156,7 @@ namespace uvgrtp {
* parameter. If RTCP has been enabled, uvgrtp::rtcp::set_ts_info() should have * parameter. If RTCP has been enabled, uvgrtp::rtcp::set_ts_info() should have
* been called. * been called.
* *
* \param data Pointer to data the that should be sent * \param data Pointer to data the that should be sent, uvgRTP does not take ownership of the memory
* \param data_len Length of data * \param data_len Length of data
* \param ts 32-bit timestamp value for the data * \param ts 32-bit timestamp value for the data
* \param rtp_flags Optional flags, see ::RTP_FLAGS for more details * \param rtp_flags Optional flags, see ::RTP_FLAGS for more details
@ -305,7 +305,7 @@ namespace uvgrtp {
bool check_pull_preconditions(); bool check_pull_preconditions();
rtp_error_t check_push_preconditions(int rtp_flags, bool smart_pointer); rtp_error_t check_push_preconditions(int rtp_flags, bool smart_pointer);
inline uint8_t* copy_frame(uint8_t* original, size_t data_len, int rtp_flags); inline uint8_t* copy_frame(uint8_t* original, size_t data_len);
uint32_t key_; uint32_t key_;

View File

@ -157,8 +157,7 @@ typedef enum RTP_FLAGS {
RTP_OBSOLETE = 1, RTP_OBSOLETE = 1,
RTP_SLICE = 1, // used to do what RTP_NO_H26X_SCL does, may do something different in the future RTP_SLICE = 1, // used to do what RTP_NO_H26X_SCL does, may do something different in the future
/** Make a copy of the frame and perform operation on the copy. This means /** Make a copy of the frame and perform operation on the copy. Cannot be used with unique_ptr. */
* that uvgRTP does not take ownership of the frame. Cannot be used with unique_ptr. */
RTP_COPY = 1 << 1, RTP_COPY = 1 << 1,
/** By default, uvgRTP searches for start code prefixes (0x000001 or 0x00000001) /** By default, uvgRTP searches for start code prefixes (0x000001 or 0x00000001)

View File

@ -413,8 +413,16 @@ rtp_error_t uvgrtp::media_stream::push_frame(uint8_t *data, size_t data_len, int
if (rce_flags_ & RCE_HOLEPUNCH_KEEPALIVE) if (rce_flags_ & RCE_HOLEPUNCH_KEEPALIVE)
holepuncher_->notify(); holepuncher_->notify();
data = copy_frame(data, data_len, rtp_flags); // makes a copy if RTP_COPY flag is given if (rtp_flags & RTP_COPY)
ret = media_->push_frame(data, data_len, rtp_flags); {
data = copy_frame(data, data_len);
std::unique_ptr<uint8_t[]> data_copy(data);
ret = media_->push_frame(std::move(data_copy), data_len, rtp_flags);
}
else
{
ret = media_->push_frame(data, data_len, rtp_flags);
}
} }
return ret; return ret;
@ -443,9 +451,17 @@ rtp_error_t uvgrtp::media_stream::push_frame(uint8_t *data, size_t data_len, uin
if (rce_flags_ & RCE_HOLEPUNCH_KEEPALIVE) if (rce_flags_ & RCE_HOLEPUNCH_KEEPALIVE)
holepuncher_->notify(); holepuncher_->notify();
data = copy_frame(data, data_len, rtp_flags); // makes a copy if RTP_COPY flag is given
rtp_->set_timestamp(ts); rtp_->set_timestamp(ts);
ret = media_->push_frame(data, data_len, rtp_flags); if (rtp_flags & RTP_COPY)
{
data = copy_frame(data, data_len);
std::unique_ptr<uint8_t[]> data_copy(data);
ret = media_->push_frame(std::move(data_copy), data_len, rtp_flags);
}
else
{
ret = media_->push_frame(data, data_len, rtp_flags);
}
rtp_->set_timestamp(INVALID_TS); rtp_->set_timestamp(INVALID_TS);
} }
@ -524,16 +540,11 @@ rtp_error_t uvgrtp::media_stream::check_push_preconditions(int rtp_flags, bool s
return RTP_OK; return RTP_OK;
} }
uint8_t* uvgrtp::media_stream::copy_frame(uint8_t* original, size_t data_len, int rtp_flags) uint8_t* uvgrtp::media_stream::copy_frame(uint8_t* original, size_t data_len)
{ {
if (rtp_flags & RTP_COPY) uint8_t* copy = new uint8_t[data_len];
{ memcpy(copy, original, data_len);
uint8_t* copy = new uint8_t[data_len]; return copy;
memcpy(copy, original, data_len);
return copy;
}
return original;
} }
rtp_error_t uvgrtp::media_stream::install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *)) rtp_error_t uvgrtp::media_stream::install_receive_hook(void *arg, void (*hook)(void *, uvgrtp::frame::rtp_frame *))