From 63ac729b32331438a607ec5b8be046143c7592e6 Mon Sep 17 00:00:00 2001 From: deva Date: Sat, 26 Mar 2005 10:32:50 +0000 Subject: Added an error object for genereic error message handling. --- src/Makefile.am | 4 ++- src/camera.cc | 36 ++++++++++++++++++++++----- src/camera.h | 23 ++++++++++++++++-- src/decoder.cc | 21 +++++++++------- src/decoder.h | 9 +++++-- src/encoder.cc | 7 ++++-- src/encoder.h | 14 ++++++++--- src/error.cc | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/error.h | 53 ++++++++++++++++++++++++++++++++++++++++ src/mainwindow.cc | 26 +++++++++++++++++--- src/player.cc | 16 ++++++++---- src/player.h | 11 ++++++--- 12 files changed, 255 insertions(+), 38 deletions(-) create mode 100644 src/error.cc create mode 100644 src/error.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 75e6f85..9d574e4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,4 @@ +#frekin' wierd ## TODO: Move ffmpeg, libxml and libsdl into configure.in AM_CXXFLAGS := $(CXXFLAGS) $(EXTRA_CXXFLAGS) -I../include $(QT_CXXFLAGS) \ @@ -26,7 +27,8 @@ miav_SOURCES = $(shell if [ $QT_CXXFLAGS ] ; then ../tools/MocList cc; fi ) \ miav_config.cc \ mov_encoder.cc \ img_encoder.cc \ - server.cc + server.cc \ + error.cc miav_LDADD := $(shell if [ $QT_CXXFLAGS ] ; then ../tools/MocList o; fi ) \ -lavcodec -lavformat diff --git a/src/camera.cc b/src/camera.cc index 574caf3..ac23fc9 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -27,8 +27,14 @@ #include "camera.h" -Camera::Camera(const char *ip, const int port) +Camera::Camera() { +} + +void Camera::connect(const char *ip, const int port) +{ + errorstatus = new Error(); + pthread_mutex_init (&mutex, NULL); //mutex = PTHREAD_MUTEX_INITIALIZER; @@ -48,7 +54,8 @@ Camera::Camera(const char *ip, const int port) sem_init(&encode_sem, 0, 0); sem_init(&player_sem, 0, 0); - decoder = new Decoder(device, + decoder = new Decoder(errorstatus, + device, channel, &encode_sem, &player_sem, @@ -59,14 +66,16 @@ Camera::Camera(const char *ip, const int port) // ifmtctx = decoder->fc; if(!decoder->fc) return; - encoder = new Encoder(ip, port, + encoder = new Encoder(errorstatus, + ip, port, &encode_sem, encode_queue, &mutex, &running); ofmtctx = encoder->fc; - player = new Player(&running, + player = new Player(errorstatus, + &running, &player_sem, player_queue, &mutex); @@ -94,26 +103,31 @@ Camera::~Camera() delete player_queue; delete encode_queue; + + delete errorstatus; } void Camera::setCpr(char *newcpr) { + encoder->setCpr(newcpr); } void Camera::start() { - encoder->stop(); + encoder->start(); } void Camera::stop() { - encoder->start(); + encoder->stop(); } void Camera::freeze() { + // FIXME: Ensure they freeze the same frame, i.e. the player + // shows the same frame that is actually fronzen on the server. player->stop(); encoder->freeze(); } @@ -128,4 +142,14 @@ void Camera::snapshot() encoder->shoot(); } +bool Camera::hasError() +{ + return errorstatus->hasError(); +} + +string Camera::getErrorString() +{ + return errorstatus->getErrorString(); +} + #endif/* USE_GUI */ diff --git a/src/camera.h b/src/camera.h index 5190954..44901aa 100644 --- a/src/camera.h +++ b/src/camera.h @@ -28,6 +28,10 @@ #ifndef __CAMERA_H__ #define __CAMERA_H__ +#include +using namespace std; +#include "error.h" + #include #include #include @@ -46,19 +50,34 @@ #include +/** + * This class represents the symbolic representation of the camera and + * the network functionality. + */ class Camera { public: - Camera(const char *ip, - const int port); + Camera(); ~Camera(); + void connect(const char *ip, + const int port); + void setCpr(char *newcpr); + + // Camera actions void start(); void stop(); void freeze(); void unfreeze(); void snapshot(); + // Status methods + bool hasError(); + string getErrorString(); + private: + // Error object passed to all sub objects. + Error *errorstatus; + pthread_t playertid; pthread_t decodetid; pthread_t encodetid; diff --git a/src/decoder.cc b/src/decoder.cc index 3c5a4e8..eae0f76 100644 --- a/src/decoder.cc +++ b/src/decoder.cc @@ -24,7 +24,8 @@ #include "decoder.h" -Decoder::Decoder(char *device, +Decoder::Decoder(Error* err, + char *device, int channel, sem_t *gencode_sem, sem_t *gplayer_sem, @@ -33,6 +34,8 @@ Decoder::Decoder(char *device, pthread_mutex_t *gmutex, volatile int *grunning) { + errobj = err; + encode_sem = gencode_sem; player_sem = gplayer_sem; encode_queue = gencode_queue; @@ -48,7 +51,7 @@ Decoder::Decoder(char *device, memset(&dvpars, 0, sizeof(dvpars)); if(!(iformat = av_find_input_format("dv1394"))) { - fprintf(stderr, "Failed to get input format dv1394\n"); + errobj->setError("Failed to get input format dv1394"); exit(1); } @@ -57,23 +60,23 @@ Decoder::Decoder(char *device, dvpars.standard = "pal"; if(av_open_input_file(&ifc, "", iformat, 0, &dvpars) < 0) { - fprintf(stderr, "Device is in use\n"); + errobj->setError("Device is in use."); fc = NULL; return; } if(av_find_stream_info(ifc) < 0) { - fprintf (stderr, "Could not find enough parameters\n"); + errobj->setError("Could not find enough parameters."); fc = NULL; return; } dump_format(ifc, 1, "", 0); if(!(dec_codec = avcodec_find_decoder(ifc->streams[0]->codec.codec_id))) { - printf("Unsupported codec for input stream \n"); + errobj->setError("Unsupported codec for input stream "); fc = NULL; return; } if(avcodec_open(&ifc->streams[0]->codec, dec_codec) < 0) { - printf("Error while opening codec for input stream\n"); + errobj->setError("Error while opening codec for input stream"); fc = NULL; return; } @@ -89,7 +92,7 @@ Decoder::~Decoder() void Decoder::decode() { if(fc == NULL) { - printf("Decoder not initialized.\n"); + errobj->setError("Decoder not initialized."); return; } @@ -116,13 +119,13 @@ void Decoder::decode() memcpy(dvf->frame, ptr, len); - // fprintf(stderr, "DVBufferSize: [%d]bytes\n", len); + //printf("DVBufferSize: [%d]bytes\n", len); ret = avcodec_decode_video(&fc->streams[0]->codec, fff->frame, &got_picture, ptr, len); if(ret < 0) { - fprintf(stderr, "Error while decoding stream\n"); + errobj->setError("Error while decoding stream"); exit(1); } diff --git a/src/decoder.h b/src/decoder.h index ad323b1..846ac26 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -23,6 +23,8 @@ #ifndef __RTVIDEOREC_DECODER_H #define __RTVIDEOREC_DECODER_H +#include "error.h" + #include #include #include @@ -38,8 +40,9 @@ #include class Decoder : public Thread { - public: - Decoder(char *device, +public: + Decoder(Error* err, + char *device, int channel, sem_t *gencode_sem, sem_t *gplayer_sem, @@ -59,6 +62,8 @@ class Decoder : public Thread { volatile int *running; private: + Error *errobj; + void decode(); }; diff --git a/src/encoder.cc b/src/encoder.cc index 3da7443..ee60a8f 100644 --- a/src/encoder.cc +++ b/src/encoder.cc @@ -24,13 +24,16 @@ #include #include "encoder.h" -Encoder::Encoder(const char *gip, +Encoder::Encoder(Error* err, + const char *gip, const int gport, sem_t *gsem, Queue *gqueue, pthread_mutex_t *gmutex, volatile int *grunning) { + errobj = err; + sprintf(ip, gip); port = gport; memset(cpr, 0, sizeof(cpr)); @@ -54,9 +57,9 @@ Encoder::Encoder(const char *gip, shoot_value = 0; freeze_request = 0; freeze_value = 0; - } + Encoder::~Encoder() { if(n) delete n; diff --git a/src/encoder.h b/src/encoder.h index d517ff5..1b459b9 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -23,6 +23,8 @@ #ifndef __RTVIDEOREC_ENCODER_H #define __RTVIDEOREC_ENCODER_H +#include "error.h" + #include #include #include @@ -36,13 +38,15 @@ #include "thread.h" #include -#define VIDEO_BUFFER_SIZE (1024*1024) /* FIXME: One size fits all... */ +// FIXME: One size fits all... +#define VIDEO_BUFFER_SIZE (1024*1024) class Encoder : public Thread { - public: - Encoder(const char *gip, +public: + Encoder(Error* err, + const char *gip, const int gport, sem_t *gsem, Queue *gqueue, @@ -66,7 +70,9 @@ class Encoder : public Thread { pthread_mutex_t *mutex; volatile int *running; - private: +private: + Error *errobj; + int port; char ip[32]; char cpr[32]; diff --git a/src/error.cc b/src/error.cc new file mode 100644 index 0000000..4056aa9 --- /dev/null +++ b/src/error.cc @@ -0,0 +1,73 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * error.cc + * + * Sat Mar 26 10:57:07 CET 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 +#include "error.h" + +Error::Error() +{ + // Initialize + removeError(); +} + +Error::~Error() +{ +} + +bool Error::hasError() +{ + return error; +} + +string Error::getErrorString() +{ + string le = lastError; + removeError(); + return le; +} + +void Error::setError(char* errstr) +{ + printf("New Error: [%s]\n", errstr); + + error = true; + lastError.append(errstr); + lastError.append("\n"); + + printf("All Errors: [%s]\n", lastError.c_str()); +} + +void Error::setError(string &errstr) +{ + error = true; + lastError.append(errstr); + lastError.append("\n"); +} + +void Error::removeError() +{ + error = false; + lastError = string(""); +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..09cfbb9 --- /dev/null +++ b/src/error.h @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * error.h + * + * Sat Mar 26 10:57:07 CET 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 +#ifndef __MIAV_ERROR_H__ +#define __MIAV_ERROR_H__ + +#include +using namespace std; + +class Error { +public: + Error(); + ~Error(); + + // Status methods + bool hasError(); + string getErrorString(); + + // Set methods + void setError(char* errstr); + void setError(string &errstr); + void removeError(); + +private: + // Used to save the state of the network and camera connections. + bool error; + string lastError; +}; + +#endif/*__MIAV_ERROR_H__*/ diff --git a/src/mainwindow.cc b/src/mainwindow.cc index b26d6ce..869d5c3 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -90,7 +90,13 @@ MainWindow::MainWindow( QWidget* parent, const char* name ) createGui(); show(); - camera = new Camera(cfg.readString("server_addr")->c_str(), cfg.readInt("server_port")); + camera = new Camera(); + camera->connect(cfg.readString("server_addr")->c_str(), + cfg.readInt("server_port")); + + if(camera->hasError()) { + MessageBox(this, "", camera->getErrorString().c_str(), TYPE_OK, ICON_ERROR).exec(); + } recording = false; frozen = false; @@ -242,8 +248,20 @@ void MainWindow::logo_clicked() void MainWindow::cpr_clicked() { + { + // TODO: Save CPR and name, from the labels. + } + + // Create and call the CPRQueryDialog. CPRQueryDialog dlg(lbl_cpr, lbl_name, this, "CPRQueryDialog"); - printf("Cpr Dialog returned: %d\n", dlg.exec()); + + if(dlg.exec() == 0) { + printf("Cancel!\n"); + // TODO: Restore old CPR and name, in the labels. + } else { + printf("CPR changed\n"); + // TODO: Change CPR camera. + } } void MainWindow::rec_clicked() @@ -252,11 +270,11 @@ void MainWindow::rec_clicked() if(recording) { img_recedge->setBackgroundColor(red); btn_rec->setPixmap(*pix_stop); - camera->stop(); + camera->start(); } else { img_recedge->setBackgroundColor(QColor(160,160,160)); btn_rec->setPixmap(*pix_record); - camera->start(); + camera->stop(); } } diff --git a/src/player.cc b/src/player.cc index a860afa..9a697f9 100644 --- a/src/player.cc +++ b/src/player.cc @@ -25,11 +25,15 @@ #include "player.h" -Player::Player(volatile int *grunning, +Player::Player(Error *err, + volatile int *grunning, sem_t *gsem, Queue *gqueue, pthread_mutex_t *gmutex) { + char errbuf[256]; + errobj = err; + running = grunning; sem = gsem; queue = gqueue; @@ -38,17 +42,19 @@ Player::Player(volatile int *grunning, sem_init(&play_sem, 0, 1); if(SDL_Init(SDL_INIT_VIDEO) < 0) { - fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); - exit(1); + sprintf(errbuf, "Unable to init SDL: %s\n", SDL_GetError()); + errobj->setError(errbuf); + return; } screen = SDL_SetVideoMode(DISPLAYWIDTH, DISPLAYHEIGHT, 16, SDL_HWSURFACE|SDL_ANYFORMAT|SDL_HWACCEL); if(!screen) { - fprintf(stderr, "Unable to set %dx%d video: %s\n", + sprintf(errbuf, "Unable to set %dx%d video: %s\n", DISPLAYWIDTH, DISPLAYHEIGHT, SDL_GetError()); - exit(1); + errobj->setError(errbuf); + return; } overlay = SDL_CreateYUVOverlay(DISPLAYWIDTH, DISPLAYHEIGHT, SDL_IYUV_OVERLAY, screen); diff --git a/src/player.h b/src/player.h index e33c4e7..3e7efb6 100644 --- a/src/player.h +++ b/src/player.h @@ -26,6 +26,8 @@ #ifndef __RTVIDEOREC_PLAYER_H #define __RTVIDEOREC_PLAYER_H +#include "error.h" + #include #include #include @@ -46,8 +48,9 @@ #define DISPLAYHEIGHT 576 class Player : public Thread { - public: - Player(volatile int *grunning, +public: + Player(Error* err, + volatile int *grunning, sem_t *gsem, Queue *gqueue, pthread_mutex_t *gmutex); @@ -58,7 +61,9 @@ class Player : public Thread { void run(); - private: +private: + Error *errobj; + void player(); volatile int *running; -- cgit v1.2.3