00001 /*************************************************************************** 00002 l1394mm_window.cpp - description 00003 ------------------- 00004 begin : Mon Jan 1 2001 00005 copyright : (C) 2001-2004 by Michael Repplinger 00006 email : repplinger@cs.uni-sb.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 #include "l1394_window.h" 00018 #include <iostream> 00019 00020 namespace L1394_MM{ 00021 00022 00023 void suspendThread(long millis) { 00024 struct timeval tv; 00025 if (millis > 0) { 00026 tv.tv_sec = millis / 1000; 00027 tv.tv_usec = (millis % 1000) * 1000; 00028 select(0, 0, 0, 0, &tv); 00029 } 00030 } 00031 00032 Window::Window(l1394_converter dec) { 00033 message = L1394::internal::SMessage::getInstance(); 00034 conv = dec; 00035 } 00036 00037 Window::~Window() { 00038 stop(); 00039 00040 window_thread.join(); 00041 if (device != NULL) 00042 device->removeEventHandle(this); 00043 } 00044 00045 void* Window::run_static(void *me) { 00046 static_cast<Window*>(me)->initWindow(); 00047 return NULL; 00048 } 00049 00050 void Window::start() { 00051 running = true; 00052 window_thread.start(run_static, static_cast<void*> (this)); 00053 } 00054 00055 void* Window::initWindow() { 00056 00057 L1394::Frame* buf = NULL; 00058 00059 if (device == NULL) { 00060 message->warningStream() << "Window > No device connected" << endl; 00061 return NULL; 00062 } 00063 00064 if (getBuffer() == NULL) { 00065 message->errorStream() << "Window > Window has no buffer" << endl; 00066 return NULL; 00067 } 00068 while(running) 00069 { 00070 // suspendThread(150); 00071 mutex.lock(); 00072 if (!device) { 00073 running = false; 00074 message->warningStream() << "No device connected" << endl; 00075 mutex.unlock(); 00076 return 0; 00077 } 00078 buf = device->getFrame(); 00079 if (buf == NULL) { 00080 message->warningStream()<< "Window > buffer is empty " << endl; 00081 } else { 00082 conv(reinterpret_cast<unsigned char*>(buf->getBuffer()), buf->getBufferSize(), getBuffer()); 00083 update(); 00084 if (! (device->getFrameMode()) ) { 00085 device->releaseFrame(buf); 00086 } 00087 } 00088 mutex.unlock(); 00089 } 00090 00091 return NULL; 00092 } 00093 00094 00095 void Window::connect(L1394::Device* device) 00096 { 00097 mutex.lock(); 00098 this->device = device; 00099 this->device->addEventHandle(this, L1394::NODE_DESTROY); 00100 mutex.unlock(); 00101 } 00102 00103 void Window::disconnect() 00104 { 00105 mutex.lock(); 00106 device = 0; 00107 mutex.unlock(); 00108 } 00109 00110 void Window::stop() 00111 { 00112 mutex.lock(); 00113 running = false; 00114 mutex.unlock(); 00115 } 00116 00117 }