| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2006 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE JPEG_VideoViewer. |
|---|
| 6 | |
|---|
| 7 | OSSIE JPEG_VideoViewer is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE JPEG_VideoViewer is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE JPEG_VideoViewer; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <string> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include "JPEG_VideoViewer.h" |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | #define VIDEOMON 1 |
|---|
| 30 | |
|---|
| 31 | unsigned int image_width = IMAGE_WIDTH; |
|---|
| 32 | unsigned int image_height = IMAGE_HEIGHT; |
|---|
| 33 | int owidth = 0; |
|---|
| 34 | int oheight = 0; |
|---|
| 35 | int testbpp=16; |
|---|
| 36 | int autobright = 0; |
|---|
| 37 | int bpp = 3; |
|---|
| 38 | SDL_Surface *pscreen; |
|---|
| 39 | int format = VIDEO_PALETTE_JPEG; |
|---|
| 40 | |
|---|
| 41 | JPEG_VideoViewer_i::JPEG_VideoViewer_i(const char *uuid, omni_condition *condition) |
|---|
| 42 | : Resource_impl(uuid), component_running(condition) |
|---|
| 43 | { |
|---|
| 44 | dataIn_0 = new standardInterfaces_i::realChar_p("JPEG_DataIn"); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | //Create the thread for the writer's processing function |
|---|
| 48 | processing_thread = new omni_thread(run, (void *) this); |
|---|
| 49 | |
|---|
| 50 | //Start the thread containing the writer's processing function |
|---|
| 51 | processing_thread->start(); |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // Destructor |
|---|
| 56 | JPEG_VideoViewer_i::~JPEG_VideoViewer_i(void) |
|---|
| 57 | { |
|---|
| 58 | delete dataIn_0; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // static function for omni thread |
|---|
| 62 | void JPEG_VideoViewer_i::run( void * data ) |
|---|
| 63 | { |
|---|
| 64 | ((JPEG_VideoViewer_i*)data)->run_loop(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | CORBA::Object_ptr JPEG_VideoViewer_i::getPort( const char* portName ) throw (CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 68 | { |
|---|
| 69 | DEBUG(3, JPEG_VideoViewer, "getPort invoked with port name: " << portName) |
|---|
| 70 | |
|---|
| 71 | CORBA::Object_var p; |
|---|
| 72 | |
|---|
| 73 | p = dataIn_0->getPort(portName); |
|---|
| 74 | |
|---|
| 75 | if (!CORBA::is_nil(p)) |
|---|
| 76 | return p._retn(); |
|---|
| 77 | |
|---|
| 78 | /*exception*/ |
|---|
| 79 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void JPEG_VideoViewer_i::start() throw (CORBA::SystemException, CF::Resource::StartError) |
|---|
| 83 | { |
|---|
| 84 | DEBUG(3, JPEG_VideoViewer, "start invoked") |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | isRunning = true; |
|---|
| 89 | |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void JPEG_VideoViewer_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 93 | { |
|---|
| 94 | DEBUG(3, JPEG_VideoViewer, "stop invoked") |
|---|
| 95 | //Uint32 frame_color = NULL; |
|---|
| 96 | //Uint8 r, g, b; |
|---|
| 97 | |
|---|
| 98 | isRunning = false; |
|---|
| 99 | |
|---|
| 100 | //r = g = b = 0xFF; |
|---|
| 101 | /* |
|---|
| 102 | frame_color = SDL_MapRGB(pscreen->format, r, g, b); |
|---|
| 103 | if(SDL_FillRect(pscreen, NULL, frame_color) < 0) |
|---|
| 104 | { |
|---|
| 105 | std::cout << "Error: Could not set frame to NULL" << std::endl; |
|---|
| 106 | } |
|---|
| 107 | SDL_UpdateRect (pscreen, 0, 0, 0, 0); //update the entire screen |
|---|
| 108 | */ |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void JPEG_VideoViewer_i::releaseObject() throw (CORBA::SystemException, CF::LifeCycle::ReleaseError) |
|---|
| 112 | { |
|---|
| 113 | DEBUG(3, JPEG_VideoViewer, "releaseObject invoked") |
|---|
| 114 | |
|---|
| 115 | if(VIDEOMON) |
|---|
| 116 | { |
|---|
| 117 | SDL_Quit(); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | component_running->signal(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void JPEG_VideoViewer_i::initialize() throw (CF::LifeCycle::InitializeError, CORBA::SystemException) |
|---|
| 124 | { |
|---|
| 125 | DEBUG(3, JPEG_VideoViewer, "initialize invoked") |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | if(VIDEOMON) |
|---|
| 129 | { |
|---|
| 130 | printf ("Initializing SDL.\n"); |
|---|
| 131 | |
|---|
| 132 | /* Initialize defaults, Video and Audio */ |
|---|
| 133 | if ((SDL_InitSubSystem (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)) |
|---|
| 134 | { |
|---|
| 135 | printf ("Could not initialize SDL: %s.\n", SDL_GetError ()); |
|---|
| 136 | exit (-1); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /* Clean up on exit */ |
|---|
| 140 | atexit (SDL_Quit); |
|---|
| 141 | if(!owidth || !oheight) |
|---|
| 142 | { |
|---|
| 143 | owidth = image_width; |
|---|
| 144 | oheight = image_height; |
|---|
| 145 | } |
|---|
| 146 | printf ("SDL initialized.\n"); |
|---|
| 147 | |
|---|
| 148 | printf("Creating Video Frame...\n"); |
|---|
| 149 | pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_SWSURFACE); |
|---|
| 150 | if (pscreen == NULL) |
|---|
| 151 | { |
|---|
| 152 | printf ("Couldn't set %d*%dx%d video mode: %s\n", owidth, oheight,3 * 8, SDL_GetError ()); |
|---|
| 153 | exit (1); |
|---|
| 154 | } |
|---|
| 155 | SDL_WM_SetCaption ("OSSIE Web Cam Viewer - Recvr", NULL); //videocap.name |
|---|
| 156 | printf("Done Creating Video Frame\n"); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void |
|---|
| 161 | JPEG_VideoViewer_i::query (CF::Properties & configProperties) |
|---|
| 162 | throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 163 | { |
|---|
| 164 | // for queries of zero length, return all id/value pairs in propertySet |
|---|
| 165 | if (configProperties.length () == 0) |
|---|
| 166 | { |
|---|
| 167 | configProperties.length (propertySet.length ()); |
|---|
| 168 | for (unsigned int i = 0; i < propertySet.length (); i++) |
|---|
| 169 | { |
|---|
| 170 | configProperties[i].id = CORBA::string_dup (propertySet[i].id); |
|---|
| 171 | configProperties[i].value = propertySet[i].value; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | return ; |
|---|
| 175 | } |
|---|
| 176 | else { |
|---|
| 177 | for (unsigned int i = 0; i < configProperties.length(); i++) { |
|---|
| 178 | for (unsigned int j=0; j < propertySet.length(); j++) { |
|---|
| 179 | if ( strcmp(configProperties[i].id, propertySet[i].id) == 0 ){ |
|---|
| 180 | configProperties[i].value = propertySet[i].value; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | void JPEG_VideoViewer_i::configure(const CF::Properties& props) throw (CORBA::SystemException, CF::PropertySet::InvalidConfiguration, CF::PropertySet::PartialConfiguration) |
|---|
| 191 | { |
|---|
| 192 | |
|---|
| 193 | DEBUG(3, JPEG_VideoViewer, "configure invoked") |
|---|
| 194 | |
|---|
| 195 | std::cout << "props length : " << props.length() << std::endl; |
|---|
| 196 | |
|---|
| 197 | for (unsigned int i = 0; i <props.length(); i++) |
|---|
| 198 | { |
|---|
| 199 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void JPEG_VideoViewer_i::run_loop() |
|---|
| 205 | { |
|---|
| 206 | DEBUG(3, JPEG_VideoViewer, "run loop invoked") |
|---|
| 207 | int i = 0; |
|---|
| 208 | short N = 0; |
|---|
| 209 | PortTypes::CharSequence *I_in_0(NULL); |
|---|
| 210 | unsigned char * image_data; |
|---|
| 211 | |
|---|
| 212 | while( true ) |
|---|
| 213 | { |
|---|
| 214 | dataIn_0->getData(I_in_0); |
|---|
| 215 | N = I_in_0->length(); |
|---|
| 216 | image_data = new unsigned char[N]; |
|---|
| 217 | for(i = 0; i < N; i++) |
|---|
| 218 | { |
|---|
| 219 | image_data[i] = (unsigned char) (*I_in_0)[i]; |
|---|
| 220 | } |
|---|
| 221 | if(VIDEOMON) |
|---|
| 222 | { |
|---|
| 223 | refresh_screen(image_data,(unsigned char*)(pscreen->pixels),format,image_width,image_height,owidth,oheight,image_width*image_height*bpp,autobright); |
|---|
| 224 | if (autobright) |
|---|
| 225 | { |
|---|
| 226 | //printf("AutoBright\n"); |
|---|
| 227 | //adjust_bright(&videopict, fd); |
|---|
| 228 | } |
|---|
| 229 | SDL_UpdateRect (pscreen, 0, 0, 0, 0); //update the entire screen |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | dataIn_0->bufferEmptied(); |
|---|
| 233 | delete [] image_data; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|