/* * This file part of gsopcast - A gtk front-end of p2p tv sopcast. * http://lianwei3.googlepages.com/home2 * Copyright (C) 2006 Wei Lian * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "header.h" #include "socket.h" #include "iorecord.h" #include "iochannel.h" Channel iorecord = { 0, 0 }; FILE *record_fp = 0; guint ptimer_record = 0; extern char outport[8]; char record_dir[64]; extern GtkWidget *entry_status; extern GtkWidget *label_record; extern guint title_len; extern char *title; //----------------------------------------------------- gboolean launch_record(void *) { /////////////////connect to server int socket_fd = connect_to_server("127.0.0.1", atoi(outport)); if (socket_fd < 0) { return TRUE; } write(socket_fd, "1", strlen("1") + 1); //////////////////parse stream head char buf[19]; if (recv(socket_fd, buf, sizeof(buf), 0) != sizeof(buf)) { ////quit gtk_entry_set_text(GTK_ENTRY(entry_status), "Error Parsing stream head"); fprintf(stderr, "err while parseing head\n"); ///close socket close(socket_fd); gtk_label_set_text(GTK_LABEL(label_record), "Record"); return FALSE; } ///////////////////testing data availibility int nread; ioctl(socket_fd, FIONREAD, &nread); if (nread > 0) ////data availible { ///close existing record_fp if (record_fp != NULL) { fprintf(stderr, "file remain unclosed!\n"); fclose(record_fp); record_fp = NULL; } ///open file for recording std::string data_path = record_dir; data_path += "/"; if (access(data_path.c_str(), F_OK)) mkdir(data_path.c_str(), S_IRWXU); ///replacing '/' by '#' std::string str = title; while (1) { std::string::size_type pos = str.find("/", 0); if (pos == std::string::npos) break; str.replace(pos, 1, "#"); } /// data_path += str; char t1str[11]; sprintf(t1str, "%lu", time(NULL)); data_path += t1str; data_path += ".asf"; record_fp = fopen(data_path.c_str(), "w"); if (record_fp == NULL) { perror("record path is invalid"); gtk_label_set_text(GTK_LABEL(label_record), "Record"); close(socket_fd); return FALSE; } ///put socket_fd to io channel if (iorecord.tag == 0) create_iochannel(iorecord, socket_fd, io_socket_record); else { fprintf(stderr, "attempt to create multiple io channels for record!\n"); if (record_fp != NULL) { fclose(record_fp); record_fp = NULL; } gtk_label_set_text(GTK_LABEL(label_record), "Record"); close(socket_fd); return FALSE; } GdkColor color; gdk_color_parse("blue", &color); gtk_widget_modify_fg(label_record, GTK_STATE_NORMAL, &color); } else ///no data availible yet { close(socket_fd); return TRUE; } ////////// return FALSE; } //----------------------------------------------------- gboolean io_socket_record(GIOChannel * source, GIOCondition cond, gpointer d) { GError *error = NULL; // for error handling GIOStatus status; // save the reading status gsize bytes_read; // save the number of chars read gchar buffer[1024]; ////data availible if ((status = g_io_channel_read_chars(source, buffer, sizeof(buffer), &bytes_read, &error)) == G_IO_STATUS_NORMAL && bytes_read > 0) { ///record if (record_fp != NULL) fwrite(buffer, bytes_read, 1, record_fp); } ////quit else if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) { ///close existing record_fp if (record_fp != NULL) { fclose(record_fp); record_fp = NULL; } ///close io channel delete_iochannel(iorecord); ///stop timer if (ptimer_record != 0) g_source_remove(ptimer_record); ///remove timer ////set record color to default GdkColor color; gdk_color_parse("black", &color); gtk_widget_modify_fg(label_record, GTK_STATE_NORMAL, &color); ///restart ptimer_record = g_timeout_add(1000, launch_record, NULL); return FALSE; } else { printf("rest options\n"); } //g_assert (status == G_IO_STATUS_AGAIN);//resource temporarily unavailable, try again return TRUE; /* keep the event source */ }