rtmp: Check return codes of net IO operations
authorSamuel Pitoiset <samuel.pitoiset@gmail.com>
Thu, 24 May 2012 11:48:25 +0000 (13:48 +0200)
committerMartin Storsjö <martin@martin.st>
Thu, 24 May 2012 19:16:46 +0000 (22:16 +0300)
Signed-off-by: Martin Storsjö <martin@martin.st>
libavformat/rtmppkt.c
libavformat/rtmpproto.c

index 474f637..ed8e6b2 100644 (file)
@@ -151,7 +151,10 @@ int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
         offset    += chunk_size;
         size      += chunk_size;
         if (data_size > 0) {
-            ffurl_read_complete(h, &t, 1); //marker
+            if ((ret = ffurl_read_complete(h, &t, 1)) < 0) { // marker
+                ff_rtmp_packet_destroy(p);
+                return ret;
+            }
             size++;
             if (t != (0xC0 + channel_id))
                 return -1;
@@ -167,6 +170,7 @@ int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
     int mode = RTMP_PS_TWELVEBYTES;
     int off = 0;
     int size = 0;
+    int ret;
 
     pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
 
@@ -218,15 +222,18 @@ int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
     }
     prev_pkt[pkt->channel_id].extra      = pkt->extra;
 
-    ffurl_write(h, pkt_hdr, p-pkt_hdr);
+    if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
+        return ret;
     size = p - pkt_hdr + pkt->data_size;
     while (off < pkt->data_size) {
         int towrite = FFMIN(chunk_size, pkt->data_size - off);
-        ffurl_write(h, pkt->data + off, towrite);
+        if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
+            return ret;
         off += towrite;
         if (off < pkt->data_size) {
             uint8_t marker = 0xC0 | pkt->channel_id;
-            ffurl_write(h, &marker, 1);
+            if ((ret = ffurl_write(h, &marker, 1)) < 0)
+                return ret;
             size++;
         }
     }
index 31cc194..1f78d1c 100644 (file)
@@ -167,10 +167,11 @@ static int gen_connect(URLContext *s, RTMPContext *rt)
 
     pkt.data_size = p - pkt.data;
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -194,10 +195,11 @@ static int gen_release_stream(URLContext *s, RTMPContext *rt)
     ff_amf_write_null(&p);
     ff_amf_write_string(&p, rt->playpath);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -221,10 +223,11 @@ static int gen_fcpublish_stream(URLContext *s, RTMPContext *rt)
     ff_amf_write_null(&p);
     ff_amf_write_string(&p, rt->playpath);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -248,10 +251,11 @@ static int gen_fcunpublish_stream(URLContext *s, RTMPContext *rt)
     ff_amf_write_null(&p);
     ff_amf_write_string(&p, rt->playpath);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -276,10 +280,11 @@ static int gen_create_stream(URLContext *s, RTMPContext *rt)
     ff_amf_write_null(&p);
     rt->create_stream_invoke = rt->nb_invokes;
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 
@@ -305,10 +310,11 @@ static int gen_delete_stream(URLContext *s, RTMPContext *rt)
     ff_amf_write_null(&p);
     ff_amf_write_number(&p, rt->main_channel_id);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -336,9 +342,13 @@ static int gen_play(URLContext *s, RTMPContext *rt)
     ff_amf_write_string(&p, rt->playpath);
     ff_amf_write_number(&p, rt->live);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
+    if (ret < 0)
+        return ret;
+
     // set client buffer time disguised in ping packet
     if ((ret = ff_rtmp_packet_create(&pkt, RTMP_NETWORK_CHANNEL, RTMP_PT_PING,
                                      1, 10)) < 0)
@@ -349,10 +359,11 @@ static int gen_play(URLContext *s, RTMPContext *rt)
     bytestream_put_be32(&p, 1);
     bytestream_put_be32(&p, 256); //TODO: what is a good value here?
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -379,7 +390,8 @@ static int gen_publish(URLContext *s, RTMPContext *rt)
     ff_amf_write_string(&p, rt->playpath);
     ff_amf_write_string(&p, "live");
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
     return ret;
@@ -401,10 +413,11 @@ static int gen_pong(URLContext *s, RTMPContext *rt, RTMPPacket *ppkt)
     p = pkt.data;
     bytestream_put_be16(&p, 7);
     bytestream_put_be32(&p, AV_RB32(ppkt->data+2));
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -422,10 +435,11 @@ static int gen_server_bw(URLContext *s, RTMPContext *rt)
 
     p = pkt.data;
     bytestream_put_be32(&p, 2500000);
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 /**
@@ -446,7 +460,8 @@ static int gen_check_bw(URLContext *s, RTMPContext *rt)
     ff_amf_write_number(&p, ++rt->nb_invokes);
     ff_amf_write_null(&p);
 
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
     return ret;
@@ -467,10 +482,11 @@ static int gen_bytes_read(URLContext *s, RTMPContext *rt, uint32_t ts)
 
     p = pkt.data;
     bytestream_put_be32(&p, rt->bytes_read);
-    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
+    ret = ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size,
+                               rt->prev_pkt[1]);
     ff_rtmp_packet_destroy(&pkt);
 
-    return 0;
+    return ret;
 }
 
 //TODO: Move HMAC code somewhere. Eventually.
@@ -617,7 +633,12 @@ static int rtmp_handshake(URLContext *s, RTMPContext *rt)
     if (client_pos < 0)
         return client_pos;
 
-    ffurl_write(rt->stream, tosend, RTMP_HANDSHAKE_PACKET_SIZE + 1);
+    if ((ret = ffurl_write(rt->stream, tosend,
+                           RTMP_HANDSHAKE_PACKET_SIZE + 1)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Cannot write RTMP handshake request\n");
+        return ret;
+    }
+
     i = ffurl_read_complete(rt->stream, serverdata, RTMP_HANDSHAKE_PACKET_SIZE + 1);
     if (i != RTMP_HANDSHAKE_PACKET_SIZE + 1) {
         av_log(s, AV_LOG_ERROR, "Cannot read RTMP handshake response\n");
@@ -678,9 +699,13 @@ static int rtmp_handshake(URLContext *s, RTMPContext *rt)
             return ret;
 
         // write reply back to the server
-        ffurl_write(rt->stream, tosend, RTMP_HANDSHAKE_PACKET_SIZE);
+        if ((ret = ffurl_write(rt->stream, tosend,
+                               RTMP_HANDSHAKE_PACKET_SIZE)) < 0)
+            return ret;
     } else {
-        ffurl_write(rt->stream, serverdata+1, RTMP_HANDSHAKE_PACKET_SIZE);
+        if ((ret = ffurl_write(rt->stream, serverdata + 1,
+                               RTMP_HANDSHAKE_PACKET_SIZE)) < 0)
+            return ret;
     }
 
     return 0;
@@ -710,7 +735,9 @@ static int rtmp_parse_result(URLContext *s, RTMPContext *rt, RTMPPacket *pkt)
             return -1;
         }
         if (!rt->is_input)
-            ff_rtmp_packet_write(rt->stream, pkt, rt->chunk_size, rt->prev_pkt[1]);
+            if ((ret = ff_rtmp_packet_write(rt->stream, pkt, rt->chunk_size,
+                                            rt->prev_pkt[1])) < 0)
+                return ret;
         rt->chunk_size = AV_RB32(pkt->data);
         if (rt->chunk_size <= 0) {
             av_log(s, AV_LOG_ERROR, "Incorrect chunk size %d\n", rt->chunk_size);
@@ -1201,7 +1228,9 @@ static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
         if (rt->flv_off == rt->flv_size) {
             rt->skip_bytes = 4;
 
-            ff_rtmp_packet_write(rt->stream, &rt->out_pkt, rt->chunk_size, rt->prev_pkt[1]);
+            if ((ret = ff_rtmp_packet_write(rt->stream, &rt->out_pkt,
+                                            rt->chunk_size, rt->prev_pkt[1])) < 0)
+                return ret;
             ff_rtmp_packet_destroy(&rt->out_pkt);
             rt->flv_size = 0;
             rt->flv_off = 0;