Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

8 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. #include <map>
  2. #include <memory>
  3. #include <string>
  4. #include "sockutil.h"
  5. #include "sys/atomic.h"
  6. #include "sys/thread.h"
  7. #include "sys/system.h"
  8. #include "sys/pollfd.h"
  9. #include "port/network.h"
  10. #include "port/ip-route.h"
  11. #include "sip-uac.h"
  12. #include "sip-uas.h"
  13. #include "sip-message.h"
  14. #include "sip-transport.h"
  15. #include "sip-timer.h"
  16. #include "port/ip-route.h"
  17. #include "http-parser.h"
  18. #include "http-header-auth.h"
  19. #include "rtp.h"
  20. #include "rtp-profile.h"
  21. #include "rtp-payload.h"
  22. #include "mov-format.h"
  23. #include "mpeg4-aac.h"
  24. #include "mpeg4-avc.h"
  25. #include "mpeg4-hevc.h"
  26. #include "stun-proto.h"
  27. #include "ice-agent.h"
  28. #include "stun-agent.h"
  29. #include "uri-parse.h"
  30. #include "cstringext.h"
  31. #include "base64.h"
  32. #include "time64.h"
  33. #include "sdp.h"
  34. #include "md5.h"
  35. #include "aio-timeout.h"
  36. #include <inttypes.h>
  37. #include <stdint.h>
  38. #include <stdio.h>
  39. #include <errno.h>
  40. #include "rtsp-media.h"
  41. #include "rtp-sender.h"
  42. #include "../test/ice-transport.h"
  43. #include "../test/media/mp4-file-reader.h"
  44. #define SIP_PWD "1234"
  45. #define SIP_HOST "119.23.15.234"
  46. #define SIP_FROM "sip:1002@127.0.0.1:5061"
  47. #define SIP_PEER "sip:1001@192.168.1.100"
  48. #define SIP_EXPIRED 60
  49. #define TURN_SERVER NULL
  50. #define TURN_USR "test"
  51. #define TURN_PWD "123456"
  52. extern "C" void rtp_receiver_test(socket_t rtp[2], const char* peer, int peerport[2], int payload, const char* encoding);
  53. static int rtp_packet_send(void* param, const void *packet, int bytes, uint32_t timestamp, int flags);
  54. struct sip_uac_transport_address_t
  55. {
  56. socket_t udp;
  57. socket_t tcp;
  58. socklen_t addrlen;
  59. struct sockaddr_storage addr;
  60. };
  61. struct sip_uac_test2_t;
  62. struct sip_uac_test2_session_t : public VodFileSource::IListener
  63. {
  64. struct sip_uac_test2_t *ctx;
  65. char buffer[2 * 1024 * 1024];
  66. std::string user;
  67. std::string from;
  68. struct sip_uas_transaction_t* t;
  69. std::shared_ptr<struct sip_uac_transaction_t> tuac;
  70. struct ice_transport_t* avt;
  71. struct sip_uac_transport_address_t transport;
  72. struct rtsp_media_t medias[3]; // peer sdp media
  73. int nmedia;
  74. struct av_media_t
  75. {
  76. int stream; // stream index, base 0
  77. int connected;
  78. struct sip_uac_test2_session_t* s;
  79. char sdp[1204];
  80. struct rtsp_media_t* m;
  81. int fmt;
  82. enum AVPACKET_CODEC_ID codec;
  83. void* decoder;
  84. struct rtp_sender_t sender;
  85. time64_t clock;
  86. int track;
  87. FILE* fp;
  88. union
  89. {
  90. struct mpeg4_aac_t aac;
  91. struct mpeg4_avc_t avc;
  92. struct mpeg4_hevc_t hevc;
  93. } u;
  94. } audio, video;
  95. bool running;
  96. pthread_t th;
  97. std::shared_ptr<AVPacketQueue> pkts;
  98. std::shared_ptr<VodFileSource> source;
  99. virtual int OnPacket(struct avpacket_t* pkt) {
  100. return pkts->Push(pkt);
  101. }
  102. };
  103. struct sip_uac_test2_t
  104. {
  105. bool running;
  106. char usr[64];
  107. char local[65];
  108. socket_t udp;
  109. socket_t tcp;
  110. struct sip_agent_t* sip;
  111. struct sip_uac_transport_address_t transport; // for register
  112. struct http_header_www_authenticate_t auth;
  113. int nonce_count;
  114. char callid[64];
  115. int cseq;
  116. ThreadLocker locker;
  117. typedef std::map<std::string, std::shared_ptr<sip_uac_test2_session_t> > TSessions;
  118. TSessions sessions;
  119. };
  120. static int sip_uac_transport_via(void* transport, const char* destination, char protocol[16], char local[128], char dns[128])
  121. {
  122. int r;
  123. char ip[65];
  124. u_short port;
  125. struct uri_t* uri;
  126. struct sip_uac_transport_address_t *t = (struct sip_uac_transport_address_t *)transport;
  127. // rfc3263 4.1 Selecting a Transport Protocol
  128. // 1. If the URI specifies a transport protocol in the transport parameter,
  129. // that transport protocol SHOULD be used. Otherwise, if no transport
  130. // protocol is specified, but the TARGET(maddr) is a numeric IP address,
  131. // the client SHOULD use UDP for a SIP URI, and TCP for a SIPS URI.
  132. // 2. if no transport protocol is specified, and the TARGET is not numeric,
  133. // but an explicit port is provided, the client SHOULD use UDP for a SIP URI,
  134. // and TCP for a SIPS URI
  135. // 3. Otherwise, if no transport protocol or port is specified, and the target
  136. // is not a numeric IP address, the client SHOULD perform a NAPTR query for
  137. // the domain in the URI.
  138. // The client SHOULD try the first record. If an attempt should fail, based on
  139. // the definition of failure in Section 4.3, the next SHOULD be tried, and if
  140. // that should fail, the next SHOULD be tried, and so on.
  141. t->addrlen = sizeof(t->addr);
  142. memset(&t->addr, 0, sizeof(t->addr));
  143. strcpy(protocol, socket_invalid == t->tcp ? "UDP" : "TCP");
  144. uri = uri_parse(destination, strlen(destination));
  145. if (!uri)
  146. return -1; // invalid uri
  147. // rfc3263 4-Client Usage (p5)
  148. // once a SIP server has successfully been contacted (success is defined below),
  149. // all retransmissions of the SIP request and the ACK for non-2xx SIP responses
  150. // to INVITE MUST be sent to the same host.
  151. // Furthermore, a CANCEL for a particular SIP request MUST be sent to the same
  152. // SIP server that the SIP request was delivered to.
  153. // TODO: sips port
  154. r = socket_addr_from(&t->addr, &t->addrlen, uri->host, uri->port ? uri->port : SIP_PORT);
  155. if (0 == r)
  156. {
  157. socket_addr_to((struct sockaddr*)&t->addr, t->addrlen, ip, &port);
  158. socket_getname(socket_invalid == t->tcp ? t->udp : t->tcp, local, &port);
  159. r = ip_route_get(ip, local);
  160. if (0 == r)
  161. {
  162. dns[0] = 0;
  163. struct sockaddr_storage ss;
  164. socklen_t len = sizeof(ss);
  165. if (0 == socket_addr_from(&ss, &len, local, port))
  166. socket_addr_name((struct sockaddr*)&ss, len, dns, 128);
  167. if (SIP_PORT != port)
  168. snprintf(local + strlen(local), 128 - strlen(local), ":%hu", port);
  169. if (NULL == strchr(dns, '.'))
  170. snprintf(dns, 128, "%s", local); // don't have valid dns
  171. }
  172. }
  173. uri_free(uri);
  174. return r;
  175. }
  176. static int sip_uac_transport_send(void* transport, const void* data, size_t bytes)
  177. {
  178. struct sip_uac_transport_address_t *t = (struct sip_uac_transport_address_t *)transport;
  179. //char p1[1024];
  180. //char p2[1024];
  181. ((char*)data)[bytes] = 0;
  182. //printf("%s\n", (const char*)data);
  183. int r = socket_sendto(socket_invalid == t->tcp ? t->udp : t->tcp, data, bytes, 0, (struct sockaddr*)&t->addr, t->addrlen);
  184. return r == bytes ? 0 : -1;
  185. }
  186. static int sip_uas_transport_send(void* param, const struct cstring_t* /*protocol*/, const struct cstring_t* peer, const struct cstring_t* received, int rport, const void* data, int bytes)
  187. {
  188. struct sip_uac_test2_t *test = (struct sip_uac_test2_t *)param;
  189. std::shared_ptr<uri_t> uri(uri_parse(peer->p, peer->n), uri_free);
  190. if (!uri.get())
  191. return -1; // invalid uri
  192. socklen_t addrlen;
  193. struct sockaddr_storage addr;
  194. int r = socket_addr_from(&addr, &addrlen, cstrvalid(received) ? received->p : uri->host, rport > 0 ? rport : (uri->port ? uri->port : SIP_PORT));
  195. if (0 != r)
  196. return -1; // invalid
  197. //char p1[1024];
  198. //char p2[1024];
  199. ((char*)data)[bytes] = 0;
  200. //printf("%s\n\n", (const char*)data);
  201. r = socket_sendto(socket_invalid == test->tcp ? test->udp : test->tcp, data, bytes, 0, (struct sockaddr*)&addr, addrlen);
  202. return r == bytes ? 0 : -1;
  203. }
  204. static int sdp_media_negotiate(const struct rtsp_media_t* m)
  205. {
  206. int i, j;
  207. const int payloads[] = { RTP_PAYLOAD_PCMA, RTP_PAYLOAD_PCMU };
  208. const char* payloads2[] = { "H264", "H265", "MP4V-ES", "MP4A-LATM", "MPEG4-GENERIC", "opus" };
  209. assert(0 == strcasecmp("IP4", m->addrtype) || 0 == strcasecmp("IP6", m->addrtype));
  210. for (i = 0; i < m->avformat_count; i++)
  211. {
  212. if(m->avformats[i].fmt < RTP_PAYLOAD_DYNAMIC)
  213. {
  214. //for(j = 0; j < sizeof(payloads)/sizeof(payloads[0]); j++)
  215. //{
  216. // if(payloads[j] == m->avformats[i].fmt)
  217. // return i;
  218. //}
  219. }
  220. else
  221. {
  222. for(j = 0; j < sizeof(payloads2)/sizeof(payloads2[0]); j++)
  223. {
  224. if(0 == strcmp(m->avformats[i].encoding, payloads2[j]))
  225. return i;
  226. }
  227. }
  228. }
  229. return -1;
  230. }
  231. //static uint8_t sdp_media_find(const struct rtsp_media_t* medias, int count, const char* media, uint8_t payload, const char* encoding)
  232. //{
  233. // int i, j;
  234. // const struct rtsp_media_t* m;
  235. // for (i = 0; i < count; i++)
  236. // {
  237. // m = medias + i;
  238. // if(0 != strcmp(m->media, media))
  239. // continue;
  240. //
  241. // if (payload < RTP_PAYLOAD_DYNAMIC)
  242. // {
  243. // for (j = 0; j < m->avformat_count; j++)
  244. // {
  245. // if (payload == m->avformats[j].fmt)
  246. // return payload;
  247. // }
  248. // }
  249. // else
  250. // {
  251. // for (j = 0; j < m->avformat_count; j++)
  252. // {
  253. // if (0 == strcmp(m->avformats[j].encoding, encoding))
  254. // return m->avformats[j].fmt;
  255. // }
  256. // }
  257. // }
  258. // return payload; // invalid
  259. //}
  260. static void mp4_onvideo(void* param, uint32_t track, uint8_t object, int width, int height, const void* extra, size_t bytes)
  261. {
  262. sip_uac_test2_session_t* s = (sip_uac_test2_session_t*)param;
  263. s->video.track = track;
  264. s->video.fp = fopen("sipvideo.h264", "wb");
  265. s->video.s = s;
  266. s->video.sender.onpacket = rtp_packet_send;
  267. s->video.sender.param = &s->video;
  268. char ip[SOCKET_ADDRLEN];
  269. u_short port;
  270. struct sockaddr_storage addr[2];
  271. assert(0 == ice_transport_getaddr(s->avt, s->video.stream, 1, &addr[0]));
  272. assert(0 == ice_transport_getaddr(s->avt, s->video.stream, 2, &addr[1]));
  273. socket_addr_to((struct sockaddr*)&addr[0], socket_addr_len((struct sockaddr*)&addr[0]), ip, &port);
  274. if (MOV_OBJECT_H264 == object)
  275. {
  276. s->video.codec = AVCODEC_VIDEO_H264;
  277. mpeg4_avc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &s->video.u.avc);
  278. rtp_sender_init_video(&s->video.sender, "RTP/AVP", port, RTP_PAYLOAD_H264, "H264", 90000, extra, bytes);
  279. }
  280. else if (MOV_OBJECT_HEVC == object)
  281. {
  282. s->video.codec = AVCODEC_VIDEO_H265;
  283. mpeg4_hevc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &s->video.u.hevc);
  284. rtp_sender_init_video(&s->video.sender, "RTP/AVP", port, RTP_PAYLOAD_H265, "H265", 90000, extra, bytes);
  285. }
  286. else if (MOV_OBJECT_MP4V == object)
  287. {
  288. s->video.codec = AVCODEC_VIDEO_MPEG4;
  289. rtp_sender_init_video(&s->video.sender, "RTP/AVP", port, RTP_PAYLOAD_MP4V, "MP4V-ES", 90000, extra, bytes);
  290. }
  291. else
  292. {
  293. assert(0);
  294. return;
  295. }
  296. int n = snprintf(s->video.sdp, sizeof(s->video.sdp), "%s", (char*)s->video.sender.buffer);
  297. n += ice_transport_getsdp(s->avt, s->video.stream, (char*)s->video.sender.buffer + n, sizeof(s->video.sender.buffer) - n);
  298. }
  299. static void mp4_onaudio(void* param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes)
  300. {
  301. sip_uac_test2_session_t* s = (sip_uac_test2_session_t*)param;
  302. s->audio.track = track;
  303. s->audio.fp = fopen("sipaudio.pcm", "wb");
  304. s->audio.s = s;
  305. s->audio.sender.onpacket = rtp_packet_send;
  306. s->audio.sender.param = &s->audio;
  307. char ip[SOCKET_ADDRLEN];
  308. u_short port;
  309. struct sockaddr_storage addr[2];
  310. assert(0 == ice_transport_getaddr(s->avt, s->audio.stream, 1, &addr[0]));
  311. assert(0 == ice_transport_getaddr(s->avt, s->audio.stream, 2, &addr[1]));
  312. socket_addr_to((struct sockaddr*)&addr[0], socket_addr_len((struct sockaddr*)&addr[0]), ip, &port);
  313. if (MOV_OBJECT_AAC == object || MOV_OBJECT_AAC_LOW == object)
  314. {
  315. s->audio.codec = AVCODEC_AUDIO_AAC;
  316. mpeg4_aac_audio_specific_config_load((const uint8_t*)extra, bytes, &s->audio.u.aac);
  317. rtp_sender_init_audio(&s->audio.sender, "RTP/AVP", port, RTP_PAYLOAD_LATM, "MP4A-LATM", sample_rate, channel_count, extra, bytes);
  318. }
  319. else if (MOV_OBJECT_OPUS == object)
  320. {
  321. s->audio.codec = AVCODEC_AUDIO_OPUS;
  322. rtp_sender_init_audio(&s->audio.sender, "RTP/AVP", port, RTP_PAYLOAD_OPUS, "opus", sample_rate, channel_count, extra, bytes);
  323. }
  324. else if (MOV_OBJECT_G711u == object)
  325. {
  326. s->audio.codec = AVCODEC_AUDIO_PCM;
  327. rtp_sender_init_audio(&s->audio.sender, "RTP/AVP", port, RTP_PAYLOAD_PCMU, "", sample_rate, channel_count, extra, bytes);
  328. }
  329. else if (MOV_OBJECT_G711a == object)
  330. {
  331. s->audio.codec = AVCODEC_AUDIO_PCM;
  332. rtp_sender_init_audio(&s->audio.sender, "RTP/AVP", port, RTP_PAYLOAD_PCMA, "", sample_rate, channel_count, extra, bytes);
  333. }
  334. else
  335. {
  336. assert(0);
  337. return;
  338. }
  339. int n = snprintf(s->audio.sdp, sizeof(s->audio.sdp), "%s", (char*)s->audio.sender.buffer);
  340. n += ice_transport_getsdp(s->avt, s->audio.stream, (char*)s->audio.sender.buffer + n, sizeof(s->audio.sender.buffer) - n);
  341. }
  342. static void ice_transport_onconnected(void* param, uint64_t flags, uint64_t mask)
  343. {
  344. struct sip_uac_test2_session_t* s = (struct sip_uac_test2_session_t*)param;
  345. for (int stream = 0; stream < 2; stream++)
  346. {
  347. sip_uac_test2_session_t::av_media_t* av = s->video.stream == stream ? &s->video : &s->audio;
  348. av->connected = (flags & ((int64_t)1 << stream)) ? 1 : 0;
  349. //for (int component = 0; component < 2; component++)
  350. //{
  351. // assert(0 == ice_transport_get_candidate(s->ice, av->stream, component + 1, &av->local[component]));
  352. //}
  353. }
  354. printf("ice_transport_onconnected 0x%x\n", (unsigned int)flags);
  355. s->source->Play();
  356. // TODO: reinvite
  357. }
  358. static void ice_transport_onbind(void* param, int code)
  359. {
  360. const char* pattern = "v=0\n"
  361. "o=%s 0 0 IN IP4 %s\n"
  362. "s=Talk\n"
  363. "c=IN IP4 %s\n"
  364. "t=0 0\n"
  365. "%s%s"; // audio/video
  366. char reply[4*1024];
  367. struct sip_uac_test2_session_t* s = (struct sip_uac_test2_session_t*)param;
  368. if (0 == code)
  369. {
  370. std::shared_ptr<MP4FileReader> reader(new MP4FileReader("e:\\video\\mp4\\name.opus.mp4"));
  371. struct mov_reader_trackinfo_t info = { mp4_onvideo, mp4_onaudio };
  372. reader->GetInfo(&info, s);
  373. std::shared_ptr<sip_uac_test2_session_t> listener(s); // fixme: s->addref()
  374. std::shared_ptr<VodFileSource> source(new VodFileSource(reader, listener));
  375. s->source = source;
  376. // default connect address
  377. u_short port;
  378. char host[SOCKET_ADDRLEN];
  379. struct sockaddr_storage addr;
  380. ice_transport_getaddr(s->avt, 0, 1, &addr);
  381. socket_addr_to((struct sockaddr*)&addr, socket_addr_len((struct sockaddr*)&addr), host, &port);
  382. // TODO: PRACK add 100rel/precondiation
  383. //sip_uac_add_header(s->tuac, "Supported", "100ref");
  384. //sip_uac_add_header(s->tuac, "Supported", "precondition");
  385. // TODO: add Recv-Info
  386. //sip_uac_add_header(s->tuac, "Recv-Info", "");
  387. // TODO: add Allow-Events
  388. //sip_uac_add_header(s->tuac, "Allow-Events", "");
  389. sip_uas_add_header(s->t, "Content-Type", "application/sdp");
  390. //sip_uas_add_header(s->t, "Contact", "sip:" SIP_USR "@" LOCAL_HOST);
  391. snprintf(reply, sizeof(reply), pattern, s->user.c_str(), host, host, s->audio.m ? (char*)s->audio.sender.buffer : "", s->video.m ? (char*)s->video.sender.buffer : "");
  392. assert(0 == sip_uas_reply(s->t, 200, reply, strlen(reply), s->ctx));
  393. ice_transport_connect(s->avt, s->medias, s->nmedia);
  394. }
  395. else
  396. {
  397. assert(0 == sip_uas_reply(s->t, 501, "Internal Server Error", 21, s->ctx));
  398. }
  399. }
  400. static void ice_transport_ondata(void* param, int stream, int component, const void* data, int bytes)
  401. {
  402. struct sip_uac_test2_session_t* s = (struct sip_uac_test2_session_t*)param;
  403. sip_uac_test2_session_t::av_media_t* av = s->audio.stream == stream ? &s->audio : &s->video;
  404. if (1 == component)
  405. {
  406. assert(rtp_payload_decode_input(av->decoder, data, bytes) >= 0);
  407. //assert(0 == rtp_onreceived(av->sender.rtp, data, bytes));
  408. }
  409. else
  410. {
  411. //assert(0 == rtp_onreceived_rtcp(av->decoder, data, bytes));
  412. }
  413. }
  414. static int rtp_packet_onrecv(void* param, const void *packet, int bytes, uint32_t timestamp, int flags)
  415. {
  416. sip_uac_test2_session_t::av_media_t* av = (sip_uac_test2_session_t::av_media_t*)param;
  417. return bytes == fwrite(packet, 1, bytes, av->fp) ? 0 : ferror(av->fp);
  418. }
  419. static int rtp_packet_send(void* param, const void *packet, int bytes, uint32_t timestamp, int flags)
  420. {
  421. sip_uac_test2_session_t::av_media_t* av = (sip_uac_test2_session_t::av_media_t*)param;
  422. return ice_transport_send(av->s->avt, av->stream, 1, packet, bytes);
  423. }
  424. static int rtcp_report(struct sip_uac_test2_session_t::av_media_t* av, time64_t clock)
  425. {
  426. int r;
  427. int interval = rtp_rtcp_interval(av->sender.rtp);
  428. if (av->clock + interval < clock)
  429. {
  430. r = rtp_rtcp_report(av->sender.rtp, av->sender.buffer, sizeof(av->sender.buffer));
  431. r = ice_transport_send(av->s->avt, av->stream, 2, av->sender.buffer, r);
  432. //printf("rtcp_report: %d\n", r);
  433. av->clock = clock;
  434. }
  435. return 0;
  436. }
  437. static int STDCALL sip_work_thread(void* param)
  438. {
  439. time64_t clock;
  440. struct sip_uac_test2_session_t* s = (struct sip_uac_test2_session_t*)param;
  441. while (s->running)
  442. {
  443. clock = time64_now();
  444. // RTCP report
  445. if (s->audio.sender.rtp && s->audio.connected)
  446. rtcp_report(&s->audio, clock);
  447. if (s->video.sender.rtp && s->video.connected)
  448. rtcp_report(&s->video, clock);
  449. for (std::shared_ptr<avpacket_t> pkt(s->pkts->FrontWait(0), avpacket_release); pkt.get(); pkt.reset(s->pkts->FrontWait(0), avpacket_release))
  450. {
  451. if (pkt->stream->stream == s->audio.track && s->audio.m)
  452. {
  453. uint32_t timestamp = s->audio.sender.timestamp + (uint32_t)(pkt->pts * (s->audio.sender.frequency / 1000) /*kHz*/);
  454. rtp_payload_encode_input(s->audio.sender.encoder, pkt->data, pkt->size, timestamp);
  455. //printf("send audio[%d] packet pts: %" PRId64 ", timestamp: %u\n", s->audio.sender.frequency, pkt->pts, timestamp);
  456. }
  457. else if (pkt->stream->stream == s->video.track && s->video.m)
  458. {
  459. int n = h264_mp4toannexb(&s->video.u.avc, pkt->data, pkt->size, s->buffer, sizeof(s->buffer));
  460. uint32_t timestamp = s->video.sender.timestamp + (uint32_t)(pkt->pts * (s->video.sender.frequency / 1000) /*kHz*/);
  461. rtp_payload_encode_input(s->video.sender.encoder, s->buffer, n, timestamp);
  462. //printf("send video[%d] packet pts: %" PRId64 ", timestamp: %u\n", s->video.sender.frequency, pkt->pts, timestamp);
  463. }
  464. else
  465. {
  466. //assert(0);
  467. // ignore
  468. }
  469. s->pkts->Pop();
  470. }
  471. }
  472. //size_t n = rtp_rtcp_bye(m->rtp, rtcp, sizeof(rtcp));
  473. s->source->Pause();
  474. return 0;
  475. }
  476. static int sip_uas_oninvite_indialog(sip_uac_test2_t* ctx, sip_uac_test2_session_t* s, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* dialog, const void* data, int bytes, void** session)
  477. {
  478. const char* pattern = "v=0\n"
  479. "o=%s 0 0 IN IP4 %s\n"
  480. "s=Talk\n"
  481. "c=IN IP4 %s\n"
  482. "t=0 0\n"
  483. "%s%s"; // audio/video
  484. // default connect address
  485. u_short port;
  486. char host[SOCKET_ADDRLEN];
  487. struct sockaddr_storage addr;
  488. ice_transport_getaddr(s->avt, 0, 1, &addr);
  489. socket_addr_to((struct sockaddr*)&addr, socket_addr_len((struct sockaddr*)&addr), host, &port);
  490. char reply[4 * 1024];
  491. int n = snprintf(reply, sizeof(reply), pattern, s->user.c_str(), host, host, "", "");
  492. if (s->audio.m)
  493. {
  494. n += snprintf(reply + n, sizeof(reply) - n, "%s", s->audio.sdp);
  495. n += ice_transport_getsdp(s->avt, s->audio.stream, reply + n, sizeof(reply) - n);
  496. }
  497. if (s->video.m)
  498. {
  499. n += snprintf(reply + n, sizeof(reply) - n, "%s", s->video.sdp);
  500. n += ice_transport_getsdp(s->avt, s->video.stream, reply + n, sizeof(reply) - n);
  501. }
  502. *session = s;
  503. s->t = t;
  504. sip_uas_add_header(t, "Content-Type", "application/sdp");
  505. //sip_uas_add_header(s->t, "Contact", "sip:" SIP_USR "@" LOCAL_HOST);
  506. assert(0 == sip_uas_reply(t, 200, reply, strlen(reply), ctx));
  507. return 0;
  508. }
  509. static int sip_uas_oninvite(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* dialog, const void* data, int bytes, void** session)
  510. {
  511. sip_uac_test2_t* ctx = (sip_uac_test2_t*)param;
  512. const cstring_t* h = sip_message_get_header_by_name(req, "Content-Type");
  513. if (0 != cstrcasecmp(h, "application/sdp"))
  514. {
  515. assert(0);
  516. return 0; // discard
  517. }
  518. std::shared_ptr<sip_uac_test2_session_t> s(new sip_uac_test2_session_t());
  519. s->from = std::string(req->from.uri.host.p, req->from.uri.host.n);
  520. {
  521. sip_uac_test2_t::TSessions::iterator it;
  522. AutoThreadLocker locker(ctx->locker);
  523. it = ctx->sessions.find(s->from);
  524. if (ctx->sessions.end() != it)
  525. {
  526. if(!dialog)
  527. return 0; // ignore
  528. // in dialog
  529. return sip_uas_oninvite_indialog(ctx, it->second.get(), req, t, dialog, data, bytes, session);
  530. }
  531. ctx->sessions.insert(std::make_pair(s->from, s));
  532. }
  533. struct ice_transport_handler_t handler = {
  534. ice_transport_ondata,
  535. ice_transport_onbind,
  536. ice_transport_onconnected,
  537. };
  538. s->pkts = std::shared_ptr<AVPacketQueue>(new AVPacketQueue(200));
  539. s->avt = ice_transport_create(0, &handler, s.get());
  540. memset(&s->audio, 0, sizeof(s->audio));
  541. memset(&s->video, 0, sizeof(s->video));
  542. s->user = ctx->usr;
  543. s->ctx = ctx;
  544. s->t = t;
  545. s->nmedia = rtsp_media_sdp((const char*)data, bytes, s->medias, sizeof(s->medias) / sizeof(s->medias[0]));
  546. for (int i = 0; i < s->nmedia; i++)
  547. {
  548. struct rtsp_media_t* m = s->medias + i;
  549. if (0 == strcmp("audio", m->media))
  550. {
  551. int j = sdp_media_negotiate(m);
  552. if(-1 == j)
  553. continue;
  554. struct rtp_payload_t handler;
  555. handler.alloc = NULL;
  556. handler.free = NULL;
  557. handler.packet = rtp_packet_onrecv;
  558. s->audio.decoder = rtp_payload_decode_create(m->avformats[j].fmt, m->avformats[j].encoding, &handler, &s->audio);
  559. if (NULL == s->audio.decoder)
  560. {
  561. assert(0);
  562. continue; // ignore
  563. }
  564. s->audio.m = m;
  565. s->audio.fmt = j;
  566. s->audio.stream = i;
  567. //socket_addr_from(&s->audio.remote[0], &addrlen, m->address, m->port[0]);
  568. //socket_addr_from(&s->audio.remote[1], &addrlen, m->address, m->port[1]);
  569. }
  570. else if (0 == strcmp("video", m->media))
  571. {
  572. int j = sdp_media_negotiate(m);
  573. if (-1 == j)
  574. continue;
  575. struct rtp_payload_t handler;
  576. handler.alloc = NULL;
  577. handler.free = NULL;
  578. handler.packet = rtp_packet_onrecv;
  579. s->video.decoder = rtp_payload_decode_create(m->avformats[j].fmt, m->avformats[j].encoding, &handler, &s->video);
  580. if (NULL == s->video.decoder)
  581. {
  582. assert(0);
  583. continue; // ignore
  584. }
  585. s->video.m = m;
  586. s->video.fmt = j;
  587. s->video.stream = i;
  588. //socket_addr_from(&s->video.remote[0], &addrlen, m->address, m->port[0]);
  589. //socket_addr_from(&s->video.remote[1], &addrlen, m->address, m->port[1]);
  590. }
  591. }
  592. struct sockaddr_storage stun;
  593. memset(&stun, 0, sizeof(stun));
  594. socket_addr_from_ipv4((struct sockaddr_in*)&stun, TURN_SERVER, STUN_PORT);
  595. ice_transport_bind(s->avt, s->nmedia, 2, TURN_SERVER ? (struct sockaddr*)&stun : NULL, 0, TURN_USR, TURN_PWD);
  596. // std::shared_ptr<AVPacketQueue> pkts(new AVPacketQueue(200));
  597. // std::shared_ptr<MP4FileReader> reader(new MP4FileReader("/Users/ireader/video/opus.mp4"));
  598. // struct mov_reader_trackinfo_t info = { mp4_onvideo, mp4_onaudio};
  599. // reader->GetInfo(&info, s.get());
  600. // std::shared_ptr<VodFileSource> source(new VodFileSource(reader, pkts));
  601. //
  602. // s->pkts = pkts;
  603. //s->source = source;
  604. *session = s.get();
  605. //sip_uas_add_header(t, "Content-Type", "application/sdp");
  606. //sip_uas_add_header(t, "Contact", "sip:" SIP_USR "@" LOCAL_HOST);
  607. // snprintf(reply, sizeof(reply), pattern, SIP_USR, LOCAL_HOST, LOCAL_HOST, s->audio.decoder?(char*)s->audio.sender.buffer:"", s->video.decoder?(char*)s->video.sender.buffer:"");
  608. //assert(0 == sip_uas_reply(t, 200, reply, strlen(reply)));
  609. s->running = true;
  610. thread_create(&s->th, sip_work_thread, s.get());
  611. return 0;
  612. }
  613. /// @param[in] code 0-ok, other-sip status code
  614. /// @return 0-ok, other-error
  615. static int sip_uas_onack(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, int code, const void* data, int bytes)
  616. {
  617. struct sip_uac_test2_t* ctx = (struct sip_uac_test2_t*)param; assert(ctx);
  618. sip_uac_test2_session_t* s = (sip_uac_test2_session_t*)session; assert(s);
  619. printf("sip_uas_onack[%p]: %d\n", s, code);
  620. if (200 <= code && code < 300)
  621. {
  622. //if(s->source.get())
  623. // s->source->Play();
  624. }
  625. else
  626. {
  627. s->running = false;
  628. if (s->source.get())
  629. s->source->Pause();
  630. thread_destroy(s->th);
  631. AutoThreadLocker locker(ctx->locker);
  632. ctx->sessions.erase(s->from);
  633. }
  634. return 0;
  635. }
  636. /// on terminating a session(dialog)
  637. static int sip_uas_onbye(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session)
  638. {
  639. struct sip_uac_test2_t* ctx = (struct sip_uac_test2_t*)param;
  640. sip_uac_test2_session_t* p = (sip_uac_test2_session_t*)session;
  641. std::shared_ptr<sip_uac_test2_session_t> s;
  642. sip_uac_test2_t::TSessions::iterator it;
  643. {
  644. AutoThreadLocker locker(ctx->locker);
  645. it = ctx->sessions.find(p->from);
  646. if (it == ctx->sessions.end())
  647. return sip_uas_reply(t, 481, NULL, 0, param);
  648. s = it->second;
  649. ctx->sessions.erase(it);
  650. }
  651. s->running = false;
  652. s->source->Pause();
  653. thread_destroy(s->th);
  654. return sip_uas_reply(t, 200, NULL, 0, param);
  655. }
  656. /// cancel a transaction(should be an invite transaction)
  657. static int sip_uas_oncancel(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session)
  658. {
  659. return sip_uas_reply(t, 200, NULL, 0, param);
  660. }
  661. /// @param[in] expires in seconds
  662. static int sip_uas_onregister(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const char* user, const char* location, int expires)
  663. {
  664. return sip_uas_reply(t, 200, NULL, 0, param);
  665. }
  666. static int sip_uas_onmessage(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, const void* payload, int bytes)
  667. {
  668. return sip_uas_reply(t, 200, NULL, 0, param);
  669. }
  670. static void sip_uac_ice_transport_onbind(void* param, int code)
  671. {
  672. const char* pattern = "v=0\n"
  673. "o=%s 0 0 IN IP4 %s\n"
  674. "s=Talk\n"
  675. "c=IN IP4 %s\n"
  676. "t=0 0\n"
  677. "%s%s"; // audio/video
  678. char buffer[4*1024];
  679. struct sip_uac_test2_session_t* s = (struct sip_uac_test2_session_t*)param;
  680. if (0 == code)
  681. {
  682. std::shared_ptr<MP4FileReader> reader(new MP4FileReader("e:\\video\\mp4\\name.opus.mp4"));
  683. struct mov_reader_trackinfo_t info = { mp4_onvideo, mp4_onaudio };
  684. reader->GetInfo(&info, s);
  685. std::shared_ptr<sip_uac_test2_session_t> listener(s); // fixme: s->addref()
  686. std::shared_ptr<VodFileSource> source(new VodFileSource(reader, listener));
  687. s->source = source;
  688. // default connect address
  689. u_short port;
  690. char host[SOCKET_ADDRLEN];
  691. struct sockaddr_storage addr;
  692. ice_transport_getaddr(s->avt, 0, 1, &addr);
  693. socket_addr_to((struct sockaddr*)&addr, socket_addr_len((struct sockaddr*)&addr), host, &port);
  694. // TODO: PRACK add 100rel/precondiation
  695. //sip_uac_add_header(s->tuac, "Supported", "100ref");
  696. //sip_uac_add_header(s->tuac, "Supported", "precondition");
  697. // TODO: add Recv-Info
  698. //sip_uac_add_header(s->tuac, "Recv-Info", "");
  699. // TODO: add Allow-Events
  700. //sip_uac_add_header(s->tuac, "Allow-Events", "");
  701. sip_uac_add_header(s->tuac.get(), "Content-Type", "application/sdp");
  702. int n = snprintf(buffer, sizeof(buffer), pattern, s->user.c_str(), host, host, (char*)s->audio.sender.buffer, (char*)s->video.sender.buffer);
  703. struct sip_transport_t t = {
  704. sip_uac_transport_via,
  705. sip_uac_transport_send,
  706. };
  707. assert(0 == sip_uac_send(s->tuac.get(), buffer, n, &t, &s->transport));
  708. }
  709. else
  710. {
  711. assert(0);
  712. }
  713. }
  714. static int sip_uac_oninvited(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, struct sip_dialog_t* dialog, int code, void** session)
  715. {
  716. const cstring_t* h;
  717. std::shared_ptr<struct sip_uac_test2_session_t> s;
  718. struct sip_uac_test2_t *ctx = (struct sip_uac_test2_t*)param;
  719. if (reply)
  720. {
  721. sip_uac_test2_t::TSessions::iterator it;
  722. AutoThreadLocker locker(ctx->locker);
  723. it = ctx->sessions.find(SIP_PEER);
  724. if (it == ctx->sessions.end())
  725. return 0; // ignore
  726. s = it->second;
  727. }
  728. // TODO: handle reply->recv_info
  729. if (200 <= code && code < 300)
  730. {
  731. h = sip_message_get_header_by_name(reply, "Content-Type");
  732. if (!h || 0 != cstrcasecmp(h, "application/sdp"))
  733. {
  734. assert(0);
  735. return 0;
  736. }
  737. s->nmedia = rtsp_media_sdp((const char*)reply->payload, reply->size, s->medias, sizeof(s->medias) / sizeof(s->medias[0]));
  738. assert(0 == ice_transport_connect(s->avt, s->medias, s->nmedia));
  739. for (int i = 0; i < s->nmedia; i++)
  740. {
  741. struct rtsp_media_t* m = s->medias + i;
  742. if (0 == strcmp("audio", m->media))
  743. {
  744. int j = sdp_media_negotiate(m);
  745. if (-1 == j)
  746. continue;
  747. struct rtp_payload_t handler;
  748. handler.alloc = NULL;
  749. handler.free = NULL;
  750. handler.packet = rtp_packet_onrecv;
  751. s->audio.decoder = rtp_payload_decode_create(m->avformats[j].fmt, m->avformats[j].encoding, &handler, &s->audio);
  752. if (NULL == s->audio.decoder)
  753. {
  754. assert(0);
  755. continue; // ignore
  756. }
  757. assert(i == s->audio.stream);
  758. s->audio.m = m;
  759. s->audio.fmt = j;
  760. }
  761. else if (0 == strcmp("video", m->media))
  762. {
  763. int j = sdp_media_negotiate(m);
  764. if (-1 == j)
  765. continue;
  766. struct rtp_payload_t handler;
  767. handler.alloc = NULL;
  768. handler.free = NULL;
  769. handler.packet = rtp_packet_onrecv;
  770. s->video.decoder = rtp_payload_decode_create(m->avformats[j].fmt, m->avformats[j].encoding, &handler, &s->video);
  771. if (NULL == s->video.decoder)
  772. {
  773. assert(0);
  774. continue; // ignore
  775. }
  776. assert(i == s->video.stream);
  777. s->video.m = m;
  778. s->video.fmt = j;
  779. }
  780. }
  781. s->from = SIP_PEER;
  782. s->running = true;
  783. thread_create(&s->th, sip_work_thread, s.get());
  784. *session = s.get();
  785. sip_uac_ack(t, NULL, 0);
  786. return 0;
  787. }
  788. else if (code == 183)
  789. {
  790. h = sip_message_get_header_by_name(reply, "Require");
  791. if (!h || (0 != cstrcasecmp(h, "100rel") && 0 != cstrcasecmp(h, "precondition")))
  792. {
  793. assert(0);
  794. return 0;
  795. }
  796. assert(dialog);
  797. //struct sip_uac_transaction_t* prack = sip_uac_prack(ctx->sip, dialog, sip_uac_onprack, ctx);
  798. //sip_uac_add_header(prack, "Supported", "precondition");
  799. //sip_uac_send(prack, sdp, 0, transport, param);
  800. return 0;
  801. }
  802. else if (code >= 300)
  803. {
  804. // TODO: delete session
  805. printf("sip_uac_oninvited code: %d\n", code);
  806. return 0;
  807. }
  808. else
  809. {
  810. // trying
  811. return 0;
  812. }
  813. }
  814. static void sip_uac_invite_test(struct sip_uac_test2_t *ctx)
  815. {
  816. char buffer[1024];
  817. std::shared_ptr<sip_uac_transaction_t> t(sip_uac_invite(ctx->sip, SIP_FROM, SIP_PEER, sip_uac_oninvited, ctx), sip_uac_transaction_release);
  818. if (HTTP_AUTHENTICATION_DIGEST == ctx->auth.scheme)
  819. {
  820. ++ctx->auth.nc;
  821. snprintf(ctx->auth.uri, sizeof(ctx->auth.uri), "%s", SIP_PEER);
  822. snprintf(ctx->auth.username, sizeof(ctx->auth.username), "%s", ctx->usr);
  823. http_header_auth(&ctx->auth, SIP_PWD, "INVITE", NULL, 0, buffer, sizeof(buffer));
  824. sip_uac_add_header(t.get(), "Proxy-Authorization", buffer);
  825. }
  826. struct ice_transport_handler_t handler = {
  827. ice_transport_ondata,
  828. sip_uac_ice_transport_onbind,
  829. ice_transport_onconnected,
  830. };
  831. std::shared_ptr<sip_uac_test2_session_t> s(new sip_uac_test2_session_t());
  832. s->pkts = std::shared_ptr<AVPacketQueue>(new AVPacketQueue(200));
  833. s->avt = ice_transport_create(0, &handler, s.get());
  834. memset(&s->audio, 0, sizeof(s->audio));
  835. memset(&s->video, 0, sizeof(s->video));
  836. s->user = ctx->usr;
  837. s->tuac = t;
  838. {
  839. AutoThreadLocker locker(ctx->locker);
  840. if (ctx->sessions.end() != ctx->sessions.find(SIP_PEER))
  841. return; // ignore
  842. ctx->sessions.insert(std::make_pair(SIP_PEER, s));
  843. }
  844. s->audio.stream = 0;
  845. s->video.stream = 1;
  846. s->transport.udp = ctx->udp;
  847. s->transport.tcp = ctx->tcp;
  848. struct sockaddr_storage stun;
  849. memset(&stun, 0, sizeof(stun));
  850. assert(0 == socket_addr_from_ipv4((struct sockaddr_in*)&stun, TURN_SERVER, STUN_PORT));
  851. ice_transport_bind(s->avt, 2, 2, TURN_SERVER ? (struct sockaddr*)&stun : NULL, 0, TURN_USR, TURN_PWD);
  852. }
  853. static void sip_uac_register_test(struct sip_uac_test2_t *test);
  854. static int sip_uac_onregister(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, int code)
  855. {
  856. struct sip_uac_test2_t *test = (struct sip_uac_test2_t *)param;
  857. const cstring_t* h;
  858. if (0 == test->callid[0] && reply)
  859. {
  860. h = sip_message_get_header_by_name(reply, "Call-ID");
  861. if (h)
  862. {
  863. snprintf(test->callid, sizeof(test->callid), "%.*s", (int)h->n, h->p);
  864. h = sip_message_get_header_by_name(reply, "CSeq");
  865. test->cseq = atoi(h->p);
  866. }
  867. }
  868. if (200 <= code && code < 300)
  869. {
  870. printf("Register OK\n");
  871. //sip_uac_invite_test(test);
  872. }
  873. else if (401 == code)
  874. {
  875. // Unauthorized
  876. memset(&test->auth, 0, sizeof(test->auth));
  877. h = sip_message_get_header_by_name(reply, "WWW-Authenticate");
  878. assert(0 == http_header_www_authenticate(h->p, &test->auth));
  879. test->nonce_count = 0;
  880. switch (test->auth.scheme)
  881. {
  882. case HTTP_AUTHENTICATION_DIGEST:
  883. sip_uac_register_test(test);
  884. break;
  885. case HTTP_AUTHENTICATION_BASIC:
  886. assert(0);
  887. break;
  888. default:
  889. assert(0);
  890. }
  891. }
  892. else
  893. {
  894. }
  895. return 0;
  896. }
  897. static void sip_uac_register_test(struct sip_uac_test2_t *test)
  898. {
  899. char buffer[256];
  900. //t = sip_uac_register(uac, "Bob <sip:bob@biloxi.com>", "sip:registrar.biloxi.com", 7200, sip_uac_message_onregister, test);
  901. std::shared_ptr<sip_uac_transaction_t> t(sip_uac_register(test->sip, SIP_FROM, "sip:" SIP_HOST, SIP_EXPIRED, sip_uac_onregister, test), sip_uac_transaction_release);
  902. if (test->callid[0])
  903. {
  904. // All registrations from a UAC SHOULD use the same Call-ID
  905. sip_uac_add_header(t.get(), "Call-ID", test->callid);
  906. snprintf(buffer, sizeof(buffer), "%u REGISTER", ++test->cseq);
  907. sip_uac_add_header(t.get(), "CSeq", buffer);
  908. }
  909. if (HTTP_AUTHENTICATION_DIGEST == test->auth.scheme)
  910. {
  911. // https://blog.csdn.net/yunlianglinfeng/article/details/81109380
  912. // http://www.voidcn.com/article/p-oqqbqgvd-bgn.html
  913. ++test->auth.nc;
  914. snprintf(test->auth.uri, sizeof(test->auth.uri), "sip:%s", SIP_HOST);
  915. snprintf(test->auth.username, sizeof(test->auth.username), "%s", test->usr);
  916. http_header_auth(&test->auth, SIP_PWD, "REGISTER", NULL, 0, buffer, sizeof(buffer));
  917. sip_uac_add_header(t.get(), "Authorization", buffer);
  918. }
  919. struct sip_transport_t transport = {
  920. sip_uac_transport_via,
  921. sip_uac_transport_send,
  922. };
  923. assert(0 == sip_uac_send(t.get(), NULL, 0, &transport, &test->transport));
  924. }
  925. static int STDCALL TimerThread(void* param)
  926. {
  927. uint64_t clock = 0;
  928. struct sip_uac_test2_t* ctx = (struct sip_uac_test2_t*)param;
  929. while (ctx->running)
  930. {
  931. uint64_t now = time64_now();
  932. if (clock + SIP_EXPIRED * 1000 < now)
  933. {
  934. clock = now;
  935. sip_uac_register_test(ctx);
  936. }
  937. system_sleep(5);
  938. }
  939. return 0;
  940. }
  941. static int sip_uac_test_process(struct sip_uac_test2_t* test)
  942. {
  943. uint8_t buffer[4 * 1024];
  944. http_parser_t* parser;
  945. http_parser_t* request;
  946. http_parser_t* response;
  947. request = http_parser_create(HTTP_PARSER_RESPONSE, NULL, NULL);
  948. response = http_parser_create(HTTP_PARSER_REQUEST, NULL, NULL);
  949. do
  950. {
  951. socklen_t addrlen;
  952. struct sockaddr_storage addr;
  953. addrlen = sizeof(addr);
  954. memset(buffer, 0, sizeof(buffer));
  955. int r = socket_recvfrom(socket_invalid==test->tcp ? test->udp : test->tcp, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addrlen);
  956. if (-1 == r && EINTR == errno)
  957. continue;
  958. printf("\n%s\n", buffer);
  959. parser = 0 == strncasecmp("SIP", (char*)buffer, 3) ? request : response;
  960. size_t n = r;
  961. assert(0 == http_parser_input(parser, buffer, &n));
  962. struct sip_message_t* msg = sip_message_create(parser == response ? SIP_MESSAGE_REQUEST : SIP_MESSAGE_REPLY);
  963. assert(0 == sip_message_load(msg, parser));
  964. assert(0 == sip_agent_input(test->sip, msg, test));
  965. sip_message_destroy(msg);
  966. http_parser_clear(parser);
  967. } while (1);
  968. http_parser_destroy(request);
  969. http_parser_destroy(response);
  970. return 0;
  971. }
  972. static int sip_uas_onsubscribe(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, sip_subscribe_t* subscribe, void** sub)
  973. {
  974. return 0;
  975. }
  976. static int sip_uas_onnotify(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, const struct cstring_t* event)
  977. {
  978. return 0;
  979. }
  980. static int sip_uas_onpublish(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* event)
  981. {
  982. return 0;
  983. }
  984. static int sip_uas_onrefer(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session)
  985. {
  986. return 0;
  987. }
  988. static int sip_uas_onprack(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const void* data, int bytes)
  989. {
  990. return 0;
  991. }
  992. static int sip_uas_onupdate(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const void* data, int bytes)
  993. {
  994. return 0;
  995. }
  996. static int sip_uas_oninfo(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const struct cstring_t* package, const void* data, int bytes)
  997. {
  998. return 0;
  999. }
  1000. void sip_uac_test2(void)
  1001. {
  1002. socket_init();
  1003. sip_timer_init();
  1004. struct sip_uac_test2_t test;
  1005. test.running = true;
  1006. test.callid[0] = 0;
  1007. struct sip_uas_handler_t handler = {
  1008. sip_uas_transport_send,
  1009. sip_uas_onregister,
  1010. sip_uas_oninvite,
  1011. sip_uas_onack,
  1012. sip_uas_onprack,
  1013. sip_uas_onupdate,
  1014. sip_uas_oninfo,
  1015. sip_uas_onbye,
  1016. sip_uas_oncancel,
  1017. sip_uas_onsubscribe,
  1018. sip_uas_onnotify,
  1019. sip_uas_onpublish,
  1020. sip_uas_onmessage,
  1021. sip_uas_onrefer,
  1022. };
  1023. assert(1 == sscanf(SIP_FROM, "sip:%[^@]", test.usr));
  1024. ip_route_get(SIP_HOST, test.local);
  1025. test.udp = socket_udp();
  1026. //test.tcp = socket_connect_host(SIP_HOST, SIP_PORT, 2000);
  1027. //if(socket_invalid != test.tcp)
  1028. // socket_setnonblock(test.tcp, 0);
  1029. test.tcp = socket_invalid;
  1030. test.sip = sip_agent_create(&handler);
  1031. socket_bind_any(test.udp, SIP_PORT);
  1032. test.transport.udp = test.udp;
  1033. test.transport.tcp = test.tcp;
  1034. pthread_t th;
  1035. thread_create(&th, TimerThread, &test);
  1036. //sip_uac_register_test(&test);
  1037. sip_uac_test_process(&test);
  1038. thread_destroy(th);
  1039. sip_agent_destroy(test.sip);
  1040. socket_close(test.udp);
  1041. socket_cleanup();
  1042. sip_timer_cleanup();
  1043. }