+ if (size < 2)
+ return AVERROR(EINVAL);
+
+ if (s->write_to_source) {
+ int fd;
+ struct sockaddr_storage *source, temp_source;
+ socklen_t *source_len, temp_len;
+ if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
+ av_log(h, AV_LOG_ERROR,
+ "Unable to send packet to source, no packets received yet\n");
+ // Intentionally not returning an error here
+ return size;
+ }
+
+ if (RTP_PT_IS_RTCP(buf[1])) {
+ fd = s->rtcp_fd;
+ source = &s->last_rtcp_source;
+ source_len = &s->last_rtcp_source_len;
+ } else {
+ fd = s->rtp_fd;
+ source = &s->last_rtp_source;
+ source_len = &s->last_rtp_source_len;
+ }
+ if (!source->ss_family) {
+ source = &temp_source;
+ source_len = &temp_len;
+ if (RTP_PT_IS_RTCP(buf[1])) {
+ temp_source = s->last_rtp_source;
+ temp_len = s->last_rtp_source_len;
+ set_port(source, get_port(source) + 1);
+ av_log(h, AV_LOG_INFO,
+ "Not received any RTCP packets yet, inferring peer port "
+ "from the RTP port\n");
+ } else {
+ temp_source = s->last_rtcp_source;
+ temp_len = s->last_rtcp_source_len;
+ set_port(source, get_port(source) - 1);
+ av_log(h, AV_LOG_INFO,
+ "Not received any RTP packets yet, inferring peer port "
+ "from the RTCP port\n");
+ }
+ }
+
+ if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
+ ret = ff_network_wait_fd(fd, 1);
+ if (ret < 0)
+ return ret;
+ }
+ ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
+ *source_len);
+
+ return ret < 0 ? ff_neterrno() : ret;
+ }
+
+ if (RTP_PT_IS_RTCP(buf[1])) {