api.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #include "pch.h"
  2. #include "../common/comm.h"
  3. #include "api.h"
  4. #include "callback.h"
  5. #include "api/create_peerconnection_factory.h"
  6. #include "p2p/client/basic_port_allocator.h"
  7. #include "video_frame_observer.h"
  8. #include "data_channel_observer.h"
  9. #include "audio_frame_observer.h"
  10. #include "peer_connection.h"
  11. #include "sdp_utils.h"
  12. #ifdef WEBRTC_LINUX
  13. //#include <opencv2/opencv.hpp>
  14. //#include <opencv2/core/core.hpp>
  15. //#include <opencv2/opencv.hpp>
  16. #include "capture_op.h"
  17. #include "gsml_capturer.h"
  18. #endif
  19. #ifdef WEBRTC_LINUX
  20. #include "sanitize_string.h"
  21. #include "capture_op.h"
  22. #include "gsml_capturer.h"
  23. #include "VideoScaler.h"
  24. #include "VideoFilter.h"
  25. #endif
  26. struct mrsEnumerator {
  27. virtual ~mrsEnumerator() = default;
  28. virtual void dispose() = 0;
  29. };
  30. rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
  31. g_peer_connection_factory;
  32. #ifdef WEBRTC_LINUX
  33. std::unique_ptr<webrtc::VideoDecoderFactory> g_video_decode_factory;
  34. #endif
  35. /// WebRTC worker thread.
  36. std::unique_ptr<rtc::Thread> g_worker_thread;
  37. // #ifdef WIN32
  38. // //rtc::Win32SocketServer w32_ss;
  39. // std::unique_ptr<rtc::Thread> g_network_thread;
  40. // #else
  41. // rtc::AutoSocketServerThread g_network_thread;
  42. // #endif
  43. /// WebRTC signaling thread.
  44. std::unique_ptr<rtc::Thread> g_signaling_thread;
  45. std::unordered_map<
  46. PeerConnectionHandle,
  47. rtc::scoped_refptr<PeerConnection>>
  48. g_peer_connection_map;
  49. /// Predefined name of the local video track.
  50. const std::string kLocalVideoLabel("local_video");
  51. /// Predefined name of the local audio track.
  52. const std::string kLocalAudioLabel("local_audio");
  53. const std::string kLocalDataChannel("local_channel");
  54. /// WebRTC worker thread.
  55. const std::unique_ptr<rtc::Thread>& GetWorkerThread() {
  56. return g_worker_thread;
  57. }
  58. /// WebRTC signaling thread.
  59. const std::unique_ptr<rtc::Thread>& GetSignalingThread() {
  60. return g_signaling_thread;
  61. }
  62. void mrsCloseEnum(mrsEnumHandle* handleRef) {
  63. if (handleRef) {
  64. if (auto& handle = *handleRef) {
  65. handle->dispose();
  66. delete handle;
  67. handle = nullptr;
  68. }
  69. }
  70. }
  71. void mrsEnumVideoCaptureDevicesAsync(
  72. mrsVideoCaptureDeviceEnumCallback callback,
  73. void* userData,
  74. mrsVideoCaptureDeviceEnumCompletedCallback completedCallback,
  75. void* completedCallbackUserData) {
  76. if (!callback) {
  77. return;
  78. }
  79. std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
  80. webrtc::VideoCaptureFactory::CreateDeviceInfo());
  81. if (!info) {
  82. if (completedCallback) {
  83. (*completedCallback)(completedCallbackUserData);
  84. }
  85. }
  86. int num_devices = info->NumberOfDevices();
  87. for (int i = 0; i < num_devices; ++i) {
  88. constexpr uint32_t kSize = 256;
  89. char name[kSize] = { 0 };
  90. char id[kSize] = { 0 };
  91. if (info->GetDeviceName(i, name, kSize, id, kSize) != -1) {
  92. (*callback)(id, name, userData);
  93. }
  94. }
  95. if (completedCallback) {
  96. (*completedCallback)(completedCallbackUserData);
  97. }
  98. }
  99. std::unique_ptr<webrtc::VideoEncoderFactory> CreateEncoderFactory(bool bNullCodec)
  100. {
  101. std::unique_ptr<webrtc::VideoEncoderFactory> factory;
  102. /*
  103. if (bNullCodec) {
  104. // factory=webrtc::CreateBuiltinVideoEncoderFactory();
  105. #ifdef WEBRTC_LINUX
  106. factory= std::make_unique<VideoEncoderFactory>();
  107. #endif
  108. }
  109. else {
  110. */
  111. factory=webrtc::CreateBuiltinVideoEncoderFactory();
  112. //}
  113. return factory;
  114. }
  115. std::unique_ptr<webrtc::VideoDecoderFactory> CreateDecoderFactory(bool nullCodec) {
  116. std::unique_ptr<webrtc::VideoDecoderFactory> factory;
  117. /*
  118. if (nullCodec) {
  119. #ifdef WEBRTC_LINUX
  120. //factory= webrtc::CreateBuiltinVideoDecoderFactory();
  121. factory = std::make_unique<VideoDecoderFactory>();
  122. #endif
  123. }
  124. else
  125. {
  126. factory= webrtc::CreateBuiltinVideoDecoderFactory();
  127. }
  128. */
  129. factory = webrtc::CreateBuiltinVideoDecoderFactory();
  130. return factory;
  131. }
  132. #include "../thirdparty/webrtc/include/api/task_queue/default_task_queue_factory.h"
  133. bool mrsWebrtcCreateFactory(bool nullCodec)
  134. {
  135. if (g_peer_connection_factory == nullptr) {
  136. g_worker_thread=(rtc::Thread::Create());
  137. g_worker_thread->Start();
  138. g_signaling_thread=(rtc::Thread::Create());
  139. g_signaling_thread->Start();
  140. /*
  141. rtc::scoped_refptr<webrtc::AudioDeviceModule> adm_ = webrtc::AudioDeviceModule::Create(
  142. webrtc::AudioDeviceModule::kPlatformDefaultAudio, webrtc::CreateDefaultTaskQueueFactory().get());
  143. if (adm_)
  144. int32_t ret = adm_->Init();
  145. int16_t numAudioOut = 0;
  146. numAudioOut = adm_->RecordingDevices();
  147. for (int i = 0; i < numAudioOut; i++)
  148. {
  149. char name[webrtc::kAdmMaxDeviceNameSize];
  150. char guid[webrtc::kAdmMaxGuidSize];
  151. int ret = adm_->RecordingDeviceName(i, name, guid);
  152. if(ret != -1)
  153. printf(" %d %s %s\n", i, name, guid);
  154. }*/
  155. /*
  156. std::unique_ptr<rtc::Thread> workerThread = rtc::Thread::Create();
  157. workerThread->SetName("worker_thread", nullptr);
  158. workerThread->Start();
  159. rtc::scoped_refptr<webrtc::AudioDeviceModule> adm= workerThread->Invoke<rtc::scoped_refptr<webrtc::AudioDeviceModule>>(
  160. RTC_FROM_HERE, [=] {
  161. rtc::scoped_refptr<webrtc::AudioDeviceModule> adm_ = webrtc::AudioDeviceModule::Create(
  162. webrtc::AudioDeviceModule::kPlatformDefaultAudio, webrtc::CreateDefaultTaskQueueFactory().get());
  163. //if (adm_)
  164. int32_t ret = adm_->Init();
  165. printf("----%d----\r\n",ret);
  166. int16_t numAudioOut = 0;
  167. return adm_;
  168. });*/
  169. #ifdef WEBRTC_LINUX
  170. g_video_decode_factory=CreateDecoderFactory(nullCodec);
  171. #endif
  172. g_peer_connection_factory = webrtc::CreatePeerConnectionFactory(
  173. nullptr, g_worker_thread.get(), g_signaling_thread.get(),
  174. nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
  175. webrtc::CreateBuiltinAudioDecoderFactory(),
  176. CreateEncoderFactory(nullCodec),
  177. /*
  178. std::unique_ptr<webrtc::VideoEncoderFactory>(
  179. new webrtc::MultiplexEncoderFactory(
  180. #ifdef WEBRTC_AARCH
  181. std::make_unique<webrtc::InternalEncoderFactory>()
  182. #else
  183. std::make_unique<webrtc::NvVideoEncoderFactory>()
  184. #endif
  185. )),
  186. */
  187. CreateDecoderFactory(nullCodec),
  188. /*
  189. std::unique_ptr<webrtc::VideoDecoderFactory>(
  190. new webrtc::MultiplexDecoderFactory(
  191. std::make_unique<webrtc::InternalDecoderFactory>())),*/
  192. nullptr, nullptr);
  193. }
  194. if (!g_peer_connection_factory.get()) {
  195. return false;
  196. }
  197. return true;
  198. }
  199. PeerConnectionHandle mrsPeerConnectionCreate(int32_t MinPort,int32_t MaxPort) {
  200. // Ensure the factory exists
  201. // Setup the connection configuration
  202. webrtc::PeerConnectionInterface::RTCConfiguration config;
  203. config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
  204. config.enable_dtls_srtp = false;//< TODO - Should be true/unset for security
  205. config.enable_rtp_data_channel = true;
  206. webrtc::PeerConnectionInterface::IceServer server;
  207. server.uri = "stun:stun1.l.google.com:19302";
  208. config.servers.push_back(server);
  209. server.uri = "turn:58.34.98.12:3478";
  210. server.username = "ubuntu12";
  211. server.password = "ubuntu12@123";
  212. config.servers.push_back(server);
  213. /*
  214. server.uri = "turn:10.0.0.6:3478";
  215. server.username = "zhouchao";
  216. server.password = "123456";
  217. config.servers.push_back(server);
  218. */
  219. std::unique_ptr<cricket::PortAllocator> port_allocator(new cricket::BasicPortAllocator(new rtc::BasicNetworkManager()));
  220. port_allocator->SetPortRange(MinPort, MaxPort);
  221. // Create the new peer connection
  222. rtc::scoped_refptr<PeerConnection> peer =
  223. new rtc::RefCountedObject<PeerConnection>();
  224. webrtc::PeerConnectionDependencies dependencies(peer);
  225. /*
  226. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection =
  227. g_peer_connection_factory->CreatePeerConnection(config, nullptr, nullptr,
  228. peer.get());*/
  229. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection =
  230. g_peer_connection_factory->CreatePeerConnection(config, std::move(port_allocator), nullptr,
  231. peer.get());
  232. if (peer_connection.get() == nullptr)
  233. return {};
  234. peer->SetPeerImpl(peer_connection);
  235. const PeerConnectionHandle handle{ peer.get() };
  236. g_peer_connection_map.insert({ handle, std::move(peer) });
  237. return handle;
  238. }
  239. void mrsPeerConnectionRegisterConnectedCallback(
  240. PeerConnectionHandle peerHandle,
  241. PeerConnectionConnectedCallback callback,
  242. void* user_data) {
  243. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  244. peer->RegisterConnectedCallback(Callback<>{callback, user_data});
  245. }
  246. }
  247. void mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(
  248. PeerConnectionHandle peerHandle,
  249. int32_t pid,
  250. int32_t index,
  251. PeerConnectionLocalSdpReadytoSendCallback callback,
  252. void* user_data) {
  253. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  254. peer->RegisterLocalSdpReadytoSendCallback(
  255. Callback<int32_t,int32_t,const char*, const char*>{callback,user_data,pid,index});
  256. }
  257. }
  258. void mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(
  259. PeerConnectionHandle peerHandle,
  260. int32_t pid,
  261. int32_t view,
  262. PeerConnectionIceCandidateReadytoSendCallback callback,
  263. void* user_data) {
  264. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  265. peer->RegisterIceCandidateReadytoSendCallback(
  266. Callback<int32_t,int32_t,const char*, int, const char*>{callback, user_data,pid,view});
  267. }
  268. }
  269. void mrsPeerConnectionRegisterARGBLocalVideoFrameCallback(
  270. PeerConnectionHandle peerHandle,
  271. PeerConnectionARGBVideoFrameCallback callback,
  272. void* user_data) {
  273. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  274. peer->RegisterLocalVideoFrameCallback(
  275. ARGBFrameReadyCallback{callback, user_data });
  276. }
  277. }
  278. void mrsPeerConnectionRegisterARGBRemoteVideoFrameCallback(
  279. PeerConnectionHandle peerHandle,
  280. PeerConnectionARGBVideoFrameCallback callback,
  281. void* user_data) {
  282. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  283. peer->RegisterRemoteVideoFrameCallback(
  284. ARGBFrameReadyCallback{callback, user_data });
  285. }
  286. }
  287. void mrsPeerConnectionRegisterChannelCallback(PeerConnectionHandle peerHandle,
  288. PeerConnectionDataChannelMessageCallback message_callback, void* message_user_data,
  289. PeerConnectionDataChannelBufferingCallback buffering_callback, void* buffering_user_data,
  290. PeerConnectionDataChannelStateCallback state_callback,
  291. void* state_user_data) {
  292. if (auto peer = static_cast<PeerConnection*>(peerHandle))
  293. {
  294. peer->RegisterDataChannelCallback(
  295. DataChannelMessageCallback{ message_callback, message_user_data },
  296. DataChannelBufferingCallback{ buffering_callback, buffering_user_data },
  297. DataChannelStateCallback{ state_callback, state_user_data });
  298. }
  299. }
  300. bool mrsPeerConnectionAddIceCandidate(PeerConnectionHandle peerHandle,
  301. const char* sdp,
  302. const int sdp_mline_index,
  303. const char* sdp_mid) {
  304. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  305. return peer->AddIceCandidate(sdp_mid, sdp_mline_index, sdp);
  306. }
  307. return false;
  308. }
  309. bool mrsPeerConnectionCreateOffer(PeerConnectionHandle peerHandle) {
  310. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  311. return peer->CreateOffer();
  312. }
  313. return false;
  314. }
  315. bool mrsPeerConnectionCreateAnswer(PeerConnectionHandle peerHandle) {
  316. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  317. return peer->CreateAnswer();
  318. }
  319. return false;
  320. }
  321. bool mrsPeerConnectionSetRemoteDescription(PeerConnectionHandle peerHandle,
  322. const char* type,
  323. const char* sdp) {
  324. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  325. return peer->SetRemoteDescription(type, sdp);
  326. }
  327. return false;
  328. }
  329. void mrsPeerConnectionClose(PeerConnectionHandle* peerHandlePtr) {
  330. if (peerHandlePtr) {
  331. if (auto peer = static_cast<PeerConnection*>(*peerHandlePtr)) {
  332. auto it = g_peer_connection_map.find(peer);
  333. if (it != g_peer_connection_map.end()) {
  334. // This generally removes the last reference to the PeerConnection and
  335. // leads to its destruction, unless some background running task is
  336. // still using the connection.
  337. g_peer_connection_map.erase(it);
  338. if (g_peer_connection_map.empty()) {
  339. // Release the factory so that the threads are stopped and the DLL can
  340. // be unloaded. This is mandatory to be able to unload/reload in the
  341. // Unity Editor and be able to Play/Stop multiple times per Editor
  342. // process run.
  343. g_peer_connection_factory = nullptr;
  344. g_signaling_thread.reset();
  345. g_worker_thread.reset();
  346. }
  347. }
  348. }
  349. *peerHandlePtr = nullptr;
  350. }
  351. }
  352. #ifdef WEBRTC_LINUX
  353. bool mrsPeerConnectionAddLocalVideoTrack( PeerConnectionHandle peerHandle,
  354. RenderPosition type,
  355. int32_t index,
  356. int32_t GSML)
  357. {
  358. if(auto peer=static_cast<PeerConnection *>(peerHandle))
  359. {
  360. if (!g_peer_connection_factory)
  361. return false;
  362. std::unique_ptr<CaptureOp > op=std::make_unique<CaptureOp>(type,index);
  363. auto ptr=op.get();
  364. peer->RegisterCaptureOp(op);
  365. rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source = OpenGSMLCapture(ptr,GSML);
  366. if(!video_source) return false;
  367. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track =
  368. g_peer_connection_factory->CreateVideoTrack(kLocalVideoLabel+std::to_string(type),
  369. video_source);
  370. if (!video_track) {
  371. return false;
  372. }
  373. return peer->AddLocalVideoTrack(std::move(video_track), kLocalVideoLabel+std::to_string(type));
  374. }
  375. return false;
  376. }
  377. bool mrsPeerConnectionSwitchCapture(
  378. PeerConnectionHandle peerHandle,
  379. bool front
  380. )
  381. {
  382. if(auto peer=static_cast<PeerConnection *>(peerHandle))
  383. {
  384. peer->SwitchCapture(front);
  385. return true;
  386. }
  387. return false;
  388. }
  389. void mrsPeerConnectionSetGsml(
  390. PeerConnectionHandle peerHandle,
  391. int64_t time,
  392. int64_t StartRecord,
  393. int64_t DeviceId
  394. )
  395. {
  396. if(auto peer=static_cast<PeerConnection *>(peerHandle))
  397. {
  398. peer->SetCaptureParm(time,StartRecord,DeviceId);
  399. }
  400. }
  401. bool mrsPeerConnectionSetCtx(PeerConnectionHandle peerHandle,void * data)
  402. {
  403. if(auto peer=static_cast<PeerConnection *>(peerHandle))
  404. {
  405. peer->SetOtherCtx(data);
  406. return true;
  407. }
  408. return false;
  409. }
  410. void * mrsPeerConnectionCurrentCtx(PeerConnectionHandle peerHandle)
  411. {
  412. if(auto peer=static_cast<PeerConnection *>(peerHandle))
  413. {
  414. return peer->GetCurrentCtx();
  415. }
  416. return nullptr;
  417. }
  418. #endif
  419. bool mrsPeerConnectionAddLocalAudioTrack(PeerConnectionHandle peerHandle) {
  420. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  421. if (!g_peer_connection_factory)
  422. return false;
  423. rtc::scoped_refptr<webrtc::AudioSourceInterface> audio_source =
  424. g_peer_connection_factory->CreateAudioSource(cricket::AudioOptions());
  425. if (!audio_source)
  426. return false;
  427. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track =
  428. g_peer_connection_factory->CreateAudioTrack(kLocalAudioLabel,
  429. audio_source);
  430. if (!audio_track)
  431. return false;
  432. return peer->AddLocalAudioTrack(std::move(audio_track));
  433. }
  434. return false;
  435. }
  436. mrsResult mrsPeerConnectionAddDataChannel(
  437. PeerConnectionHandle peerHandle,
  438. bool ordered,
  439. bool reliable//,
  440. // PeerConnectionDataChannelMessageCallback message_callback,
  441. // void* message_user_data,
  442. // PeerConnectionDataChannelBufferingCallback buffering_callback,
  443. // void* buffering_user_data,
  444. // PeerConnectionDataChannelStateCallback state_callback,
  445. // void* state_user_data
  446. )
  447. {
  448. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  449. return peer->AddDataChannel(kLocalDataChannel.c_str(), ordered, reliable);
  450. // DataChannelMessageCallback{ message_callback, message_user_data },
  451. // DataChannelBufferingCallback{ buffering_callback, buffering_user_data },
  452. // DataChannelStateCallback{ state_callback, state_user_data });
  453. }
  454. return MRS_E_INVALID_PEER_HANDLE;
  455. }
  456. void mrsPeerConnectionRemoveLocalVideoTrack(
  457. PeerConnectionHandle peerHandle) {
  458. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  459. peer->RemoveLocalVideoTrack();
  460. }
  461. }
  462. void mrsPeerConnectionRemoveLocalAudioTrack(
  463. PeerConnectionHandle peerHandle) {
  464. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  465. peer->RemoveLocalAudioTrack();
  466. }
  467. }
  468. bool mrsPeerConnectionRemoveDataChannel(PeerConnectionHandle peerHandle) {
  469. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  470. return peer->RemoveDataChannel();
  471. }
  472. return false;
  473. }
  474. bool mrsPeerConnectionSendDataChannelMessage(PeerConnectionHandle peerHandle,
  475. const void* data,
  476. uint64_t size) {
  477. if (auto peer = static_cast<PeerConnection*>(peerHandle)) {
  478. return peer->SendDataChannelMessage(data, size);
  479. }
  480. return false;
  481. }
  482. bool mrsSdpForceCodecs(const char* message,
  483. const char* audio_codec_name,
  484. const char* video_codec_name,
  485. char* buffer,
  486. size_t* buffer_size) {
  487. RTC_CHECK(message);
  488. RTC_CHECK(buffer);
  489. RTC_CHECK(buffer_size);
  490. std::string message_str(message);
  491. std::string audio_codec_name_str;
  492. std::string video_codec_name_str;
  493. if (audio_codec_name) {
  494. audio_codec_name_str.assign(audio_codec_name);
  495. }
  496. if (video_codec_name) {
  497. video_codec_name_str.assign(video_codec_name);
  498. }
  499. std::string out_message =
  500. SdpForceCodecs(message_str, audio_codec_name_str, video_codec_name_str);
  501. const size_t capacity = *buffer_size;
  502. const size_t size = out_message.size();
  503. *buffer_size = size + 1;
  504. if (capacity < size + 1) {
  505. return false;
  506. }
  507. memcpy(buffer, out_message.c_str(), size);
  508. buffer[size] = '\0';
  509. return true;
  510. }
  511. void mrsMemCpy(void* dst, const void* src, size_t size) {
  512. memcpy(dst, src, size);
  513. }
  514. void mrsMemCpyStride(void* dst,
  515. int dst_stride,
  516. const void* src,
  517. int src_stride,
  518. int elem_size,
  519. int elem_count) {
  520. RTC_CHECK(dst);
  521. RTC_CHECK(dst_stride >= elem_size);
  522. RTC_CHECK(src);
  523. RTC_CHECK(src_stride >= elem_size);
  524. if ((dst_stride == elem_size) && (src_stride == elem_size)) {
  525. // If tightly packed, do a single memcpy() for performance
  526. const size_t total_size = (size_t)elem_size * elem_count;
  527. memcpy(dst, src, total_size);
  528. }
  529. else {
  530. // Otherwise, copy row by row
  531. for (int i = 0; i < elem_count; ++i) {
  532. memcpy(dst, src, elem_size);
  533. dst = (char*)dst + dst_stride;
  534. src = (const char*)src + src_stride;
  535. }
  536. }
  537. }