RDK Documentation (Open Sourced RDK Components)
audio_buffer.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #include <audio_buffer.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include "basic_types.h"
23 #include <pthread.h>
24 #include "safec_lib.h"
25 
26 audio_buffer::audio_buffer(const unsigned char *in_ptr, unsigned int in_size, unsigned int clip_length, unsigned int refcount) : m_size(in_size), m_clip_length(clip_length), m_refcount(refcount)
27 {
28  DEBUG("Creating new buffer.\n");
29  errno_t rc = -1;
30  m_start_ptr = (unsigned char *)malloc(in_size);
31  rc = memcpy_s(m_start_ptr, in_size, in_ptr, m_size);
32  if(rc != EOK)
33  {
34  ERR_CHK(rc);
35  }
36 }
37 
38 audio_buffer::~audio_buffer()
39 {
40  DEBUG("Deleting buffer.\n");
41  free(m_start_ptr);
42 }
43 
44 
45 audio_buffer * create_new_audio_buffer(const unsigned char *in_ptr, unsigned int in_size, unsigned int clip_length, unsigned int refcount)
46 {
47  return new audio_buffer(in_ptr, in_size, clip_length, refcount);
48 }
49 
50 static pthread_mutex_t g_audio_buffer_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
52 {
53  /* Current implementation will go for a simple plan, which is to check the refcount of the buffer while protected by a mutex. This means
54  * every unref operation involving any buffer at all will be serialized. If this turns out to be a performance bottleneck, plan B is to launch
55  * a garbage-cleaning thread of sorts that will actually check the refcount and free the buffers. In that implementation, this funciton
56  * will simply post a message (the address of the buffer to be unreffed) to the GC thread and be done with it.*/
57  REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&g_audio_buffer_mutex));
58  if(1 == ptr->m_refcount)
59  {
60  delete ptr;
61  }
62  else
63  {
64  ptr->m_refcount--;
65  }
66  REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&g_audio_buffer_mutex));
67 }
68 
70 {
71  delete ptr;
72 }
74 {
75  REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&g_audio_buffer_mutex));
76 }
78 {
79  REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&g_audio_buffer_mutex));
80 }
81 
audio_buffer
Definition: audio_buffer.h:25
free_audio_buffer
void free_audio_buffer(audio_buffer *ptr)
Deletes the audio buffer.
Definition: audio_buffer.cpp:69
audio_buffer_get_global_lock
void audio_buffer_get_global_lock()
Function to acquire lock.
Definition: audio_buffer.cpp:73
create_new_audio_buffer
audio_buffer * create_new_audio_buffer(const unsigned char *in_ptr, unsigned int in_size, unsigned int clip_length, unsigned int refcount)
This API creates new audio buffer.
Definition: audio_buffer.cpp:45
unref_audio_buffer
void unref_audio_buffer(audio_buffer *ptr)
This API is to release the audio buffer.
Definition: audio_buffer.cpp:51
audio_buffer_release_global_lock
void audio_buffer_release_global_lock()
Function to release lock.
Definition: audio_buffer.cpp:77