1 /**
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <ImsMediaDataQueue.h>
18 #include <string.h>
19
ImsMediaDataQueue()20 ImsMediaDataQueue::ImsMediaDataQueue() {}
21
~ImsMediaDataQueue()22 ImsMediaDataQueue::~ImsMediaDataQueue()
23 {
24 Clear();
25 }
26
Add(DataEntry * pEntry)27 void ImsMediaDataQueue::Add(DataEntry* pEntry)
28 {
29 if (pEntry != nullptr)
30 {
31 std::lock_guard<std::mutex> guard(mMutex);
32 DataEntry* pbData = new DataEntry(*pEntry);
33 mList.push_back(pbData);
34 }
35 }
36
InsertAt(uint32_t index,DataEntry * pEntry)37 void ImsMediaDataQueue::InsertAt(uint32_t index, DataEntry* pEntry)
38 {
39 if (pEntry != nullptr)
40 {
41 std::lock_guard<std::mutex> guard(mMutex);
42 DataEntry* pbData = new DataEntry(*pEntry);
43
44 if (mList.empty() || index == 0)
45 {
46 mList.push_front(pbData);
47 }
48 else if (index >= mList.size())
49 {
50 mList.push_back(pbData);
51 }
52 else
53 {
54 std::list<DataEntry*>::iterator iter = mList.begin();
55 advance(iter, index);
56 mList.insert(iter, pbData);
57 }
58 }
59 }
60
Delete()61 void ImsMediaDataQueue::Delete()
62 {
63 std::lock_guard<std::mutex> guard(mMutex);
64
65 if (!mList.empty())
66 {
67 DataEntry* pbData = mList.front();
68 pbData->deleteBuffer();
69 delete pbData;
70 mList.pop_front();
71 }
72 }
73
Clear()74 void ImsMediaDataQueue::Clear()
75 {
76 while (!mList.empty())
77 {
78 Delete();
79 }
80 }
81
Get(DataEntry ** ppEntry)82 bool ImsMediaDataQueue::Get(DataEntry** ppEntry)
83 {
84 if (ppEntry == nullptr)
85 {
86 return false;
87 }
88
89 std::lock_guard<std::mutex> guard(mMutex);
90
91 if (!mList.empty())
92 {
93 // get first data in the queue
94 *ppEntry = mList.front();
95 return true;
96 }
97 else
98 {
99 *ppEntry = nullptr;
100 return false;
101 }
102 }
103
GetLast(DataEntry ** ppEntry)104 bool ImsMediaDataQueue::GetLast(DataEntry** ppEntry)
105 {
106 if (ppEntry == nullptr)
107 {
108 return false;
109 }
110
111 std::lock_guard<std::mutex> guard(mMutex);
112 // get last data in the queue
113 if (!mList.empty())
114 {
115 *ppEntry = mList.back();
116 return true;
117 }
118 else
119 {
120 *ppEntry = nullptr;
121 return false;
122 }
123 }
124
GetAt(uint32_t index,DataEntry ** ppEntry)125 bool ImsMediaDataQueue::GetAt(uint32_t index, DataEntry** ppEntry)
126 {
127 if (ppEntry == nullptr)
128 {
129 return false;
130 }
131
132 std::lock_guard<std::mutex> guard(mMutex);
133
134 if (mList.size() > index)
135 {
136 std::list<DataEntry*>::iterator iter = mList.begin();
137 advance(iter, index);
138 *ppEntry = *(iter);
139 return true;
140 }
141 else
142 {
143 *ppEntry = nullptr;
144 return false;
145 }
146 }
147
GetCount()148 uint32_t ImsMediaDataQueue::GetCount()
149 {
150 std::lock_guard<std::mutex> guard(mMutex);
151 return mList.size();
152 }
153
SetReadPosFirst()154 void ImsMediaDataQueue::SetReadPosFirst()
155 {
156 std::lock_guard<std::mutex> guard(mMutex);
157 mListIter = mList.begin();
158 }
159
GetNext(DataEntry ** ppEntry)160 bool ImsMediaDataQueue::GetNext(DataEntry** ppEntry)
161 {
162 if (ppEntry == nullptr)
163 {
164 return false;
165 }
166
167 std::lock_guard<std::mutex> guard(mMutex);
168
169 if (mListIter != mList.end())
170 {
171 *ppEntry = *mListIter++;
172 return true;
173 }
174 else
175 {
176 *ppEntry = nullptr;
177 return false;
178 }
179 }
180