aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Pitoiset <samuel.pitoiset@gmail.com>2012-06-14 15:28:40 +0200
committerMartin Storsjö <martin@martin.st>2012-06-14 22:22:58 +0300
commit7dc747f50b0adeaf2bcf6413e291dc4bffa54f9a (patch)
treeccdcab4a4c61542a0f84e7a504ee42435a60d375
parentd2d193c9b6963f3041ee0037c791c44453b845a0 (diff)
downloadffmpeg-7dc747f50b0adeaf2bcf6413e291dc4bffa54f9a.tar.gz
rtmp: Read and handle incoming packets while writing data
This makes sure all incoming packets are read and handled (and reacted to) while sending an FLV stream over RTMP to a server. If there were enough incoming data to fill the TCP buffers, this could potentially make things block at unexpected places. For the upcoming RTMPT support, we need to consume all incoming data before we can send the next request. Signed-off-by: Martin Storsjö <martin@martin.st>
-rw-r--r--libavformat/rtmppkt.c16
-rw-r--r--libavformat/rtmppkt.h13
-rw-r--r--libavformat/rtmpproto.c30
3 files changed, 56 insertions, 3 deletions
diff --git a/libavformat/rtmppkt.c b/libavformat/rtmppkt.c
index ed8e6b203d..4ce238d5d0 100644
--- a/libavformat/rtmppkt.c
+++ b/libavformat/rtmppkt.c
@@ -74,15 +74,25 @@ void ff_amf_write_object_end(uint8_t **dst)
int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
int chunk_size, RTMPPacket *prev_pkt)
{
- uint8_t hdr, t, buf[16];
+ uint8_t hdr;
+
+ if (ffurl_read(h, &hdr, 1) != 1)
+ return AVERROR(EIO);
+
+ return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt, hdr);
+}
+
+int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
+ RTMPPacket *prev_pkt, uint8_t hdr)
+{
+
+ uint8_t t, buf[16];
int channel_id, timestamp, data_size, offset = 0;
uint32_t extra = 0;
enum RTMPPacketType type;
int size = 0;
int ret;
- if (ffurl_read(h, &hdr, 1) != 1)
- return AVERROR(EIO);
size++;
channel_id = hdr & 0x3F;
diff --git a/libavformat/rtmppkt.h b/libavformat/rtmppkt.h
index 8372484fbd..a83d0feb8f 100644
--- a/libavformat/rtmppkt.h
+++ b/libavformat/rtmppkt.h
@@ -115,6 +115,19 @@ void ff_rtmp_packet_destroy(RTMPPacket *pkt);
*/
int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
int chunk_size, RTMPPacket *prev_pkt);
+/**
+ * Read internal RTMP packet sent by the server.
+ *
+ * @param h reader context
+ * @param p packet
+ * @param chunk_size current chunk size
+ * @param prev_pkt previously read packet headers for all channels
+ * (may be needed for restoring incomplete packet header)
+ * @param c the first byte already read
+ * @return number of bytes read on success, negative value otherwise
+ */
+int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
+ RTMPPacket *prev_pkt, uint8_t c);
/**
* Send RTMP packet to the server.
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index e64e2a322b..b3ae5a21e6 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -1287,6 +1287,7 @@ static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
int pktsize, pkttype;
uint32_t ts;
const uint8_t *buf_temp = buf;
+ uint8_t c;
int ret;
do {
@@ -1356,6 +1357,35 @@ static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
rt->flv_header_bytes = 0;
}
} while (buf_temp - buf < size);
+
+ /* set stream into nonblocking mode */
+ rt->stream->flags |= AVIO_FLAG_NONBLOCK;
+
+ /* try to read one byte from the stream */
+ ret = ffurl_read(rt->stream, &c, 1);
+
+ /* switch the stream back into blocking mode */
+ rt->stream->flags &= ~AVIO_FLAG_NONBLOCK;
+
+ if (ret == AVERROR(EAGAIN)) {
+ /* no incoming data to handle */
+ return size;
+ } else if (ret < 0) {
+ return ret;
+ } else if (ret == 1) {
+ RTMPPacket rpkt = { 0 };
+
+ if ((ret = ff_rtmp_packet_read_internal(rt->stream, &rpkt,
+ rt->chunk_size,
+ rt->prev_pkt[0], c)) <= 0)
+ return ret;
+
+ if ((ret = rtmp_parse_result(s, rt, &rpkt)) < 0)
+ return ret;
+
+ ff_rtmp_packet_destroy(&rpkt);
+ }
+
return size;
}