Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Shm.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #include <string>
25 #include <sstream>
26 #include <iostream>
27 #include <sstream>
28 
29 #include <Image/Camera.hh>
30 #include <VideoSource/VideoSource_Shm.hh>
31 
32 #ifdef WIN32
33 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
34 # include <windows.h>
35 #else
36 # include <sys/types.h>
37 # include <sys/mman.h>
38 #include <fcntl.h>
39 #endif
40 
41 // for gcc 4.7
42 #ifndef WIN32
43 #include <unistd.h>
44 #endif
45 
46 
47 #include <Base/Common/BIASpragma.hh>
48 
49 
50 using namespace BIAS;
51 using namespace std;
52 
54 {
55  IsCreator_ = false;
56  bSync_ = true;
57  pImgBuffer_ = new std::stringstream;
58  LastTime_ = 0.0;
59  ColorChannels_ = 3; // fake
60  ColorMode_ = ImageBase::CM_RGB; // fake
61 #ifdef WIN32
62  Context_ = NULL;
63  Mapping_ = NULL;
64 #else
65  Context_ = 0;
66 #endif
67 }
68 
70 {
71  if (pImgBuffer_!=NULL) {
72  delete pImgBuffer_;
73  pImgBuffer_ = NULL;
74  }
75 }
76 
77 void VideoSource_Shm::SetSize(int w, int h, int bytesperpixel){
78  Width_ = w;
79  Height_ = h;
80  BytesPerPixel_ = bytesperpixel;
81  ColorChannels_ = bytesperpixel;
82 }
83 
84 
85 
87 {
88  return OpenDevice("/VideoSource_Shm.shm");
89 }
90 
91 int VideoSource_Shm::OpenDevice(const char *name)
92 {
93  if (Width_ ==0 || Height_ ==0) {
94  Width_ = 1024;
95  Height_ = 1024;
96  }
99  FileName_ = name;
100 #ifdef WIN32
101  SYSTEM_INFO sSysInfo;
102  GetSystemInfo( &sSysInfo );
103  Granularity_ = sSysInfo.dwAllocationGranularity;
104  while( long(TotalLength_) > Granularity_ )
105  Granularity_ += sSysInfo.dwAllocationGranularity;
106  TotalLength_ = Granularity_;
107 
108  //Try to connect to existing File-Mapping
109  while ( Mapping_ == NULL ) {
110  Mapping_ = OpenFileMapping( FILE_MAP_ALL_ACCESS,FALSE,FileName_.c_str());
111  cout <<"Wait for file mapping: "<<FileName_<<endl;
112  Sleep(1000);
113  }
114  Shm_= (unsigned char*)MapViewOfFile(Mapping_,FILE_MAP_ALL_ACCESS,0,0,0);
115  if (Shm_ == NULL) {
116  BIASERR("Failed to memory map file");
117  return -1;
118  };
119 
120  return 0;
121 
122 #else
123  // try to open existing file, if that failes, create new one
124  Context_ = shm_open(FileName_.c_str(),O_RDWR,
125  S_IRWXU| S_IRWXG| S_IRWXO);
126  if (Context_ <0) {
127  IsCreator_ = true;
128  cout <<"Creating new context"<<endl;
129  Context_ = shm_open(FileName_.c_str(),O_RDWR| O_CREAT,
130  S_IRWXU| S_IRWXG| S_IRWXO);
131  int res;
132  res = ftruncate(Context_,TotalLength_);
133  if (res <0) {
134  perror(FileName_.c_str());
135  return -1;
136  }
137 
138  }
139  else cout <<"Using old context"<<endl;
140  if (Context_ <0) {
141  perror(name);
142  return -1;
143  }
144 #endif
145  return 0;
146 }
147 
148 
150 {
151 #ifdef WIN32
152 
153 #else
154  Shm_ = mmap(0,TotalLength_, PROT_READ | PROT_WRITE ,MAP_SHARED,Context_,0 );
155 #endif
157  Data_ = (char*)Shm_ +sizeof(ShmImageHeader);
158  return 0;
159 } // PreGrab
160 
161 
163 {
164  bool gotit = false;
165  BIAS::RMatrix tmpR;
167  do {
168 
169  if ((Header_->Time_ >LastTime_)||(!bSync_)) {
170 // cout <<"VideoShmFeeder: Header is: "<<setw(20)<<setprecision(20)
171 // <<Header_->Time_<<" , "<<Header_->Size_<<endl;
173 
174  pImgBuffer_->str("");
175  if ((int)pImgBuffer_->rdbuf()->sputn(Data_,Header_->Size_) < (int) Header_->Size_) {
176  BIASERR("Could not write data to streambuffer!");
177  BIASABORT;
178  }
179  image.Release();
180  //copy images data
181  *pImgBuffer_>>image;
182  image.InvalidateUID();
183  //copy images time
184  image.SetTime(Header_->Time_);
185  //copy images RMatrix and CVector
187  Header_->AxisY_,
188  Header_->AxisZ_),
189  Header_->Angle_);
190  tmpC.Set(Header_->C1_, Header_->C2_, Header_->C3_);
191  image.SetR(tmpR);
192  image.SetC(tmpC);
193 
195  gotit = true;
196  //cout << "shm: cm: "<<boolalpha<< (image.GetColorModel()==ImageBase::CM_Bayer_RGGB)<<endl;
197  } else
198 #ifdef WIN32
199  Sleep(1);
200 #else
201  usleep(1000);
202 #endif
203  } while ((!gotit)&&(bSync_));
204  image.SetUID(UUID::GenerateUUID());
205 
206  return 0;
207 }
208 
209 
211 {
212 #ifdef WIN32
213 #else
214  munmap(Shm_, TotalLength_);
215 #endif
216  return 0;
217 } // PostGrab
218 
219 
221 {
222 #ifdef WIN32
223  UnmapViewOfFile(Shm_);
224  CloseHandle(Mapping_ );
225 #else
226  // shm_unlink(FileName_.c_str());
227 #endif
228  return 0;
229 } // CloseDevice
230 
231 
232 
233 
234 int VideoSource_Shm::Resize_(int newsize)
235 {
236  cout <<"Resizing from: "<<TotalLength_;
237  PostGrab();
238  DataLength_ = newsize;
239  TotalLength_ = DataLength_ + sizeof(ShmImageHeader);
240  PreGrab();
241  cout<<" to: "<<TotalLength_<<endl;
242  return newsize;
243 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
Defines a common interface to different devices.
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
void SetSize(int w, int h, int bytesperpixel)
Set image size and number of bytes per pixel (e.g.
float BytesPerPixel_
BytesPerPixel cannot be int (e.g. it is 1.5 for YUV420p)
int Width_
Image format.
3D rotation matrix
Definition: RMatrix.hh:49
int Resize_(int newsize)
void Set(const Vector3< ROTATION_MATRIX_TYPE > &w, const ROTATION_MATRIX_TYPE phi)
Set from rotation axis w and angle phi (in rad)
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
int GrabSingle(BIAS::Camera< unsigned char > &image)
ShmImageHeader * Header_
void InvalidateUID()
sets the image&#39;s uid to invalid
Definition: ImageBase.hh:597
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
int PostGrab()
Stop anything started in PreGrab()
int OpenDevice()
selects the first available device to open (e.g.
std::stringstream * pImgBuffer_
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235