Skip to content

Commit 62d4beb

Browse files
sayanuragAnurag Krishnan
authored andcommitted
Merge branch 'dev_sprint_25_2' into feature/VPLAY-12164_http-404
2 parents 4d6be34 + e839071 commit 62d4beb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1974
-556
lines changed

.github/workflows/L1-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
- name: Run unit tests
2323
working-directory: ./test/utests
2424
run: |
25+
set -o pipefail
2526
./run.sh 2>&1 | tee utest_run.log
2627
2728
# Generate pretty test run output
@@ -62,6 +63,7 @@ jobs:
6263
- name: Run Middleware L1 unit tests
6364
working-directory: ./middleware/test/utests
6465
run: |
66+
set -o pipefail
6567
./run.sh 2>&1 | tee utest_run.log
6668
6769
# Publish Middleware test results

AampGrowableBuffer.cpp

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@
1818
*/
1919

2020
/**
21-
* @file AampGrowableBuffer.h
22-
* @brief Header file of helper functions for Growable Buffer class
21+
* @file AampGrowableBuffer.cpp
22+
* @brief Implementation file of helper functions for Growable Buffer class
2323
*/
2424

2525
#include "AampGrowableBuffer.h"
2626
#include "AampConfig.h"
27+
#include "AampLogManager.h"
2728
#include <assert.h>
28-
#include <glib.h>
2929

3030
bool AampGrowableBuffer::gbEnableLogging = false;
3131
int AampGrowableBuffer::gNetMemoryCount = 0;
3232
int AampGrowableBuffer::gNetMemoryHighWatermark = 0;
3333

34-
35-
void AampGrowableBuffer::EnableLogging( bool enable )
34+
void AampGrowableBuffer::EnableLogging(bool enable)
3635
{
37-
gbEnableLogging = enable;
36+
gbEnableLogging = enable;
3837
}
3938

4039
AampGrowableBuffer::~AampGrowableBuffer( void )
@@ -47,89 +46,87 @@ AampGrowableBuffer::~AampGrowableBuffer( void )
4746
*/
4847
void AampGrowableBuffer::Free( void )
4948
{
50-
if( ptr )
49+
if( buffer.capacity() > 0 )
5150
{
5251
NETMEMORY_MINUS();
53-
if( gbEnableLogging )
54-
{
55-
printf("AampGrowableBuffer::%s(%s:%d)\n", "Free",name,gNetMemoryCount);
56-
}
57-
g_free(ptr);
58-
ptr = NULL;
52+
if( gbEnableLogging )
53+
{
54+
printf("AampGrowableBuffer::%s(%s:%d)\n", "Free",name,gNetMemoryCount);
55+
}
56+
buffer.clear();
5957
}
60-
len = 0;
61-
avail = 0;
58+
buffer.shrink_to_fit(); // Release the allocated memory
6259
}
6360

6461
void AampGrowableBuffer::ReserveBytes( size_t numBytes )
6562
{
66-
assert( ptr==NULL && avail == 0 );
67-
ptr = (char *)g_malloc( numBytes );
68-
if( ptr )
63+
assert( buffer.empty() && buffer.capacity() == 0 );
64+
if( numBytes > 0 )
6965
{
70-
NETMEMORY_PLUS();
71-
if( gbEnableLogging )
66+
try {
67+
buffer.reserve(numBytes);
68+
NETMEMORY_PLUS();
69+
if( gbEnableLogging )
70+
{
71+
printf("AampGrowableBuffer::%s(%s:%d)\n", "ReserveBytes",name,gNetMemoryCount);
72+
}
73+
}
74+
catch (const std::bad_alloc&)
7275
{
73-
printf("AampGrowableBuffer::%s(%s:%d)\n", "ReserveBytes",name,gNetMemoryCount);
76+
AAMPLOG_ERR("Memory allocation failed!! Requested capacity: %zu", numBytes);
7477
}
75-
avail = numBytes;
7678
}
7779
}
7880

7981
void AampGrowableBuffer::AppendBytes( const void *srcPtr, size_t srcLen )
8082
{
81-
size_t required = len + srcLen;
82-
if( avail < required )
83-
{ // more memory needed - grow
84-
size_t numBytes = avail*2; // first try doubling size of existing reserved memory
85-
if( numBytes < required )
86-
{ // if still not enough, reallocate based on required
87-
numBytes = required*2;
83+
if( srcLen == 0 )
84+
{
85+
return;
86+
}
87+
88+
bool isFirstAllocation = buffer.empty() && (buffer.capacity() == 0);
89+
size_t required = buffer.size() + srcLen;
90+
91+
if( buffer.capacity() < required )
92+
{ // more memory needed - grow with same strategy as original implementation
93+
size_t newCapacity = buffer.capacity() * 2; // first try doubling
94+
if( newCapacity < required )
95+
{ // if still not enough, allocate double what's required
96+
newCapacity = required * 2;
8897
}
89-
gpointer mem = g_realloc(ptr, numBytes );
90-
if( mem )
98+
99+
try
91100
{
92-
if( !ptr )
93-
{ // first allocation
101+
buffer.reserve(newCapacity);
102+
103+
if( isFirstAllocation )
104+
{
94105
NETMEMORY_PLUS();
95106
if( gbEnableLogging )
96107
{
97108
printf("AampGrowableBuffer::%s(%s:%d)\n", "AppendBytes",name,gNetMemoryCount);
98109
}
99110
}
100-
ptr = mem;
101-
avail = numBytes;
102111
}
103-
else if (numBytes != 0)
112+
catch (const std::bad_alloc&)
104113
{
105-
AAMPLOG_ERR("Memory re-allocation failed!! Requested numBytes: %zu", numBytes);
114+
AAMPLOG_ERR("Memory re-allocation failed!! Requested capacity: %zu", newCapacity);
115+
return;
106116
}
107117
}
108-
if( ptr )
109-
{
110-
memcpy( len + (char *)ptr, srcPtr, srcLen);
111-
len = required;
112-
}
113-
}
114118

115-
/**
116-
* @brief replace contents of AampGrowableBuffer
117-
* @param srcPtr pointer to memory (may be subset of existing AampGrowableBuffer)
118-
* @param srcLen new logical size for AampGrowableBuffer reflecting memory being copied/moved
119-
*/
120-
void AampGrowableBuffer::MoveBytes( const void *srcPtr, size_t srcLen )
121-
{ // this API assumes AampGrowableBuffer is already big enough to fit
122-
assert( ptr && srcPtr && avail >= srcLen );
123-
memmove( ptr, srcPtr, srcLen );
124-
len = srcLen;
119+
// Append the data (reserve guarantees this won't throw or reallocate)
120+
const uint8_t* bytes = static_cast<const uint8_t*>(srcPtr);
121+
buffer.insert(buffer.end(), bytes, bytes + srcLen);
125122
}
126123

127124
/**
128125
* @brief reset AampGrowableBuffer logical length without releasing reserved memory
129126
*/
130127
void AampGrowableBuffer::Clear( void )
131128
{
132-
len = 0;
129+
buffer.clear();
133130
}
134131

135132
/**
@@ -138,31 +135,35 @@ void AampGrowableBuffer::Clear( void )
138135
*/
139136
void AampGrowableBuffer::Replace( AampGrowableBuffer *src )
140137
{
141-
assert( ptr == NULL ); // only replace if empty!
142-
ptr = src->GetPtr();
143-
len = src->GetLen();
144-
avail = src->GetAvail();
145-
146-
src->ptr = NULL;
147-
src->len = 0;
148-
src->avail = 0;
138+
buffer = std::move(src->buffer);
139+
src->buffer.clear();
140+
src->buffer.shrink_to_fit();
149141
}
150142

151143
/**
152-
* @brief called when internal memory is transferred (i.e. as part of GStreamer injection)
144+
* @brief Extract the internal vector for ownership transfer to external code (e.g., GStreamer)
145+
* @return vector object (moved) containing the buffer data
146+
* @note The internal buffer is moved out and the AampGrowableBuffer is reset to known empty state
153147
*/
154-
void AampGrowableBuffer::Transfer( void )
148+
std::vector<uint8_t> AampGrowableBuffer::ExtractVector( void )
155149
{
156-
assert( ptr );
157-
if( ptr )
150+
assert( !buffer.empty() );
151+
152+
if( buffer.capacity() > 0 )
158153
{
159154
NETMEMORY_MINUS();
160155
if( gbEnableLogging )
161156
{
162-
printf("AampGrowableBuffer::%s(%s:%d)\n", "Transfer",name,gNetMemoryCount);
157+
printf("AampGrowableBuffer::%s(%s:%d)\n", "ExtractVector",name,gNetMemoryCount);
163158
}
164159
}
165-
ptr = NULL;
166-
len = 0;
167-
avail = 0;
160+
161+
// Move our data into a temporary vector for return
162+
std::vector<uint8_t> extracted(std::move(buffer));
163+
164+
// Explicitly clear to ensure known empty state (not just moved-from state)
165+
buffer.clear();
166+
buffer.shrink_to_fit();
167+
168+
return extracted; // Move constructor will be used for efficient return
168169
}

AampGrowableBuffer.h

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,75 +30,67 @@
3030
#include <utility>
3131
#include <assert.h>
3232
#include <stdio.h>
33-
#include <mutex>
33+
#include <vector>
34+
#include <cstdint>
3435

3536
class AampGrowableBuffer
3637
{
3738
public:
38-
AampGrowableBuffer( const char *name="?" ):ptr(NULL),len(0),avail(0),name(name){}
39+
AampGrowableBuffer( const char *name="?" ):buffer(),name(name){}
3940
~AampGrowableBuffer();
40-
41+
4142
// Copy constructor
4243
AampGrowableBuffer(const AampGrowableBuffer & other)
43-
: ptr(nullptr),
44-
len{other.len},
45-
avail(0),name{other.name}
44+
: buffer(other.buffer),
45+
name{other.name}
4646
{ // never reached/used?
47-
ReserveBytes(len); // allocate the pointer and set avail
48-
std::memcpy(ptr, other.ptr, len); // populate
4947
}
48+
5049
// Copy assignment
5150
AampGrowableBuffer& operator=(const AampGrowableBuffer & other)
5251
{ // never reached/used?
53-
Free();
54-
len = other.len;
55-
ReserveBytes(len);
56-
std::memcpy(ptr, other.ptr, len);
52+
buffer = other.buffer;
5753
return *this;
5854
}
59-
55+
6056
// Move constructor
6157
AampGrowableBuffer(AampGrowableBuffer && other) noexcept
62-
: ptr {other.ptr},
63-
len {other.len},
64-
avail{other.avail},
65-
name{other.name}
58+
: buffer(std::move(other.buffer)),
59+
name{other.name}
6660
{ // never reached/used
67-
other.ptr = nullptr;
68-
other.len = 0;
69-
other.avail = 0;
7061
}
62+
7163
// Move assignment
7264
AampGrowableBuffer& operator=(AampGrowableBuffer && other) noexcept
7365
{ // never reached/used
74-
Free();
75-
std::swap(ptr, other.ptr);
76-
std::swap(len, other.len);
77-
std::swap(avail, other.avail);
66+
buffer = std::move(other.buffer);
7867
return *this;
7968
}
80-
69+
8170
void Free( void );
8271
void ReserveBytes( size_t len );
8372
void AppendBytes( const void *ptr, size_t len ); // append passed binary data to end of growable buffer, increasing underlying storage if required
84-
void MoveBytes( const void *ptr, size_t len );
8573
void Clear( void ); // sets logical buffer size back to zero, without releasing available pre-allocated memory; allows a growable buffer to be recycled
8674
void Replace( AampGrowableBuffer *src );
87-
void Transfer( void );
75+
76+
/**
77+
* @brief Extract the internal vector for ownership transfer
78+
* @return vector object (moved, not copied) containing the buffer data
79+
* @note The internal buffer is cleared after extraction. Uses move semantics for zero-copy transfer.
80+
*/
81+
std::vector<uint8_t> ExtractVector( void );
8882

89-
char *GetPtr( void ) { return (char *)ptr; } // accessor function for current growable buffer binary payload
90-
const char *GetPtr( void ) const { return static_cast<const char *>(ptr); } // accessor function for current growable buffer binary payload
91-
size_t GetLen( void ) const { return len; } // accessor function for current logical growable buffer size
92-
size_t GetAvail( void ) const { return avail; } // should be opaque, but used in logging
93-
void SetLen( size_t l ) { assert(l<=avail); len = l; }
83+
char *GetPtr( void ) { return buffer.empty() ? nullptr : reinterpret_cast<char*>(buffer.data()); }
84+
const char *GetPtr( void ) const { return buffer.empty() ? nullptr : reinterpret_cast<const char*>(buffer.data()); }
85+
size_t GetLen( void ) const { return buffer.size(); } // accessor function for current logical growable buffer size
86+
size_t GetAvail( void ) const { return buffer.capacity(); } // should be opaque, but used in logging
87+
void SetLen( size_t l ) { assert(l<=buffer.capacity()); buffer.resize(l); }
9488

9589
static void EnableLogging( bool enable );
9690

9791
private:
9892
const char *name;
99-
void *ptr; /**< Pointer to buffer's memory location (gpointer) */
100-
size_t len; /**< Subset of allocated buffer that is populated and in use */
101-
size_t avail; /**< Available buffer size */
93+
std::vector<uint8_t> buffer; /**< Vector holding buffer data */
10294

10395
static bool gbEnableLogging;
10496
static int gNetMemoryCount;

AampStreamSinkInactive.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ class AampStreamSinkInactive : public StreamSink
5555
* @fn SendCopy
5656
* @brief stub implementation for Inactive aamp instance
5757
*/
58-
virtual bool SendCopy( AampMediaType mediaType, const void *ptr, size_t len, double fpts, double fdts, double duration)
58+
virtual bool SendCopy( AampMediaType mediaType, std::vector<uint8_t>&& buffer, double fpts, double fdts, double duration)
5959
{
6060
AAMPLOG_WARN("Called AAMPGstPlayer()::%s stub", __FUNCTION__);
61+
// buffer will be automatically destroyed (RAII)
6162
return false;
6263
}
6364
/**
6465
* @fn SendTransfer
6566
* @brief stub implementation for Inactive aamp instance
6667
*/
67-
virtual bool SendTransfer( AampMediaType mediaType, void *ptr, size_t len, double fpts, double fdts, double duration, double fragmentPTSoffset, bool initFragment = false, bool discontinuity = false)
68+
virtual bool SendTransfer( AampMediaType mediaType, std::vector<uint8_t>&& buffer, double fpts, double fdts, double duration, double fragmentPTSoffset, bool initFragment = false, bool discontinuity = false)
6869
{
6970
AAMPLOG_WARN("Called AAMPGstPlayer()::%s stub", __FUNCTION__);
71+
// buffer will be automatically destroyed (RAII)
7072
return false;
7173
}
7274
/**

0 commit comments

Comments
 (0)