--- ../../../trunk/libavformat/mmst.c	2010-10-07 14:58:57.000000000 +0800
+++ libavformat/mmst.c	2010-10-08 10:14:13.000000000 +0800
@@ -35,6 +35,7 @@
 #include "libavcodec/bytestream.h"
 #include "network.h"
 #include "asf.h"
+#include <sys/time.h>
 
 #define LOCAL_ADDRESS 0xc0a80081    // FIXME get and use correct local ip address.
 #define LOCAL_PORT    1037          // as above.
@@ -95,6 +96,24 @@
     unsigned int header_packet_id;       ///< default is 2.
 } MMSTContext;
 
+static const int TCP_TIMEOUT_SEC = 10;
+static struct timeval interrupt_start_time, interrupt_current_time;
+
+static void reset_interrupt_timer(void)
+{
+    gettimeofday(&interrupt_start_time, NULL);
+}
+/** A TCP interrupt callback */
+static int mms_interrupt_cb(void)
+{
+    gettimeofday(&interrupt_current_time, NULL);
+    if(interrupt_current_time.tv_sec - interrupt_start_time.tv_sec > TCP_TIMEOUT_SEC) {
+        av_log(NULL, AV_LOG_ERROR, "MMS operation timed out.\n");
+        return 1;
+    }
+    return 0;
+}
+
 /** Create MMST command packet header */
 static void start_command_packet(MMSTContext *mmst, MMSCSPacketType packet_type)
 {
@@ -521,6 +540,10 @@
     if(port<0)
         port = 1755; // defaut mms protocol port
 
+    // set up customized interrupt callback
+    url_set_interrupt_cb(mms_interrupt_cb); /* invoke only once */
+    reset_interrupt_timer();
+
     // establish tcp connection.
     ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, mmst->host, port, NULL);
     err = url_open(&mms->mms_hd, tcpname, URL_RDWR);
@@ -583,7 +606,6 @@
 /** Read ASF data through the protocol. */
 static int mms_read(URLContext *h, uint8_t *buf, int size)
 {
-    /* TODO: see tcp.c:tcp_read() about a possible timeout scheme */
     MMSTContext *mmst = h->priv_data;
     MMSContext *mms   = &mmst->mms;
     int result = 0;
@@ -598,6 +620,7 @@
             result = ff_mms_read_data(mms, buf, size);
         } else {
             /* Read from network */
+            reset_interrupt_timer();
             int err = mms_safe_send_recv(mmst, NULL, SC_PKT_ASF_MEDIA);
             if (err == 0) {
                 if(mms->remaining_in_len>mms->asf_packet_len) {
