@@ -270,18 +270,31 @@ void CAgoraCrossChannelDlg::OnBnClickedButtonAddCrossChannel()
270270 AfxMessageBox (_T (" The channel and user ID cannot be empty" ));
271271 return ;
272272 }
273- ChannelMediaInfo mediaInfo;
273+
274274 std::string szChannel = cs2utf8 (strChannel);
275275 std::string szToken = cs2utf8 (strToken);
276- mediaInfo.channelName = new char [strChannel.GetLength () + 1 ];
277- mediaInfo.token = new char [strToken.GetLength () + 1 ];
278- mediaInfo.uid = _ttol (strUID);
279- strcpy_s (const_cast <char *>(mediaInfo.channelName ), strChannel.GetLength () + 1 , szChannel.data ());
280- strcpy_s (const_cast <char *>(mediaInfo.token ), strToken.GetLength () + 1 , szToken.data ());
281- // add mediaInfo to vector.
282- m_vecChannelMedias.push_back (mediaInfo);
283- m_cmbCrossChannelList.AddString (strChannel);
284- m_cmbCrossChannelList.SetCurSel (m_cmbCrossChannelList.GetCount () - 1 );
276+ auto it = std::find_if (m_vecChannelMedias.begin (), m_vecChannelMedias.end (),
277+ [&](const ChannelMediaInfo& info) {
278+ return info.channelName == szChannel && info.token == szToken;
279+ });
280+
281+ if (it == m_vecChannelMedias.end ()) {
282+ ChannelMediaInfo mediaInfo;
283+ mediaInfo.channelName = new char [strChannel.GetLength () + 1 ];
284+ mediaInfo.token = new char [strToken.GetLength () + 1 ];
285+ mediaInfo.uid = _ttol (strUID);
286+ strcpy_s (const_cast <char *>(mediaInfo.channelName ), strChannel.GetLength () + 1 , szChannel.data ());
287+ strcpy_s (const_cast <char *>(mediaInfo.token ), strToken.GetLength () + 1 , szToken.data ());
288+ // add mediaInfo to vector.
289+ m_vecChannelMedias.push_back (mediaInfo);
290+ m_cmbCrossChannelList.AddString (strChannel);
291+ m_cmbCrossChannelList.SetCurSel (m_cmbCrossChannelList.GetCount () - 1 );
292+ }
293+ else
294+ {
295+ AfxMessageBox (_T (" already added for same channel and token" ));
296+ }
297+
285298}
286299
287300// remove combobox item
@@ -293,20 +306,19 @@ void CAgoraCrossChannelDlg::OnBnClickedButtonRemoveCrossChannel2()
293306 m_cmbCrossChannelList.GetWindowText (strChannelName);
294307 std::string szChannelName = cs2utf8 (strChannelName);
295308
296- int offset = 0 ;
297- // erase media info from m_vecChannelMedias
298- for (auto & mediaInfo : m_vecChannelMedias)
299- {
300- if (szChannelName.compare (mediaInfo.channelName ) == 0 )
301- {
302- delete mediaInfo.channelName ;
303- delete mediaInfo.token ;
304- m_vecChannelMedias.erase (m_vecChannelMedias.begin () + offset);
305- }
306- offset++;
309+ auto it = std::find_if (m_vecChannelMedias.begin (), m_vecChannelMedias.end (),
310+ [&szChannelName](const ChannelMediaInfo& info) {
311+ return info.channelName == szChannelName;
312+ });
313+
314+ if (it != m_vecChannelMedias.end ()) {
315+ delete it->channelName ;
316+ delete it->token ;
317+ m_vecChannelMedias.erase (it);
307318 }
308319 m_cmbCrossChannelList.DeleteString (nSel);
309320 m_cmbCrossChannelList.SetCurSel (m_cmbCrossChannelList.GetCount () - 1 );
321+
310322}
311323
312324// start media relay or stop media relay
@@ -315,6 +327,11 @@ void CAgoraCrossChannelDlg::OnBnClickedButtonStartMediaRelay()
315327 if (!m_startMediaRelay)
316328 {
317329 int nDestCount = m_vecChannelMedias.size ();
330+ if (nDestCount<=0 )
331+ {
332+ AfxMessageBox (_T (" please config target channel config first" ));
333+ return ;
334+ }
318335 ChannelMediaInfo *lpDestInfos = new ChannelMediaInfo[nDestCount];
319336 for (int nIndex = 0 ; nIndex < nDestCount; nIndex++) {
320337 lpDestInfos[nIndex].channelName = m_vecChannelMedias[nIndex].channelName ;
0 commit comments