My third Side Project was my BCompSc Thesis: Remote Operating Theater
Tags: sideprojects, software, engineering, thesis
Published: 2020-05-20 02:45:07

Or, in spanish, "Sistema Quirófano Remoto".

Yes, my BCompSC thesis was my side project because I started a full-time remote job by the end of college and I only could be able to work on my thesis at night and on weekends. I had a tough yet very creative tutor and this was his idea:

"So, we have these analog video recording cards that we can attach to PCI ports and record the video signal of cameras and devices like laparoscopes during a surgical practice. I wonder if we can transmit it somewhere else with high quality so another surgeon could give advice or even teach remotely".

Yes, PCI, not even PCI Express. It was 2008 and there was no money for advanced hardware like that. By that year, commercial internet was also really slow in this corner of the world. 1 Mbps was considered extremely fast and there was no such thing as simultaneous HQ video with that speed.

"But, do you know what Internet 2 is? We have that type of Internet here and so other buildings in the city including the Urology Institute. With <a href="https://www.internet2.edu/">Internet 2</a> you can connect Valencia (Venezuela) and Valencia (Spain) with 56 Mbps and imperceptible delay".

Woohoo! I imagined a new world with that speed! But soon a realized that it isn't commercial so I wouldn't find YouTube on it. 😞

![Alt Text](http://www.quickmeme.com/img/f3/f3ac0751c0b20cd618ddcbe2425f8b0caf7ad057d44a230fbca5253924116921.jpg)

"So your duty, if you accept it, is to build some kind of software where you could transmit and let doctors assist, and review, remote surgeries letting them capture video signal from at least 4 cameras or equipment at the same time, hold video conferencing, and accessible through a web browser"

![Alt text](https://i0.wp.com/gecon.es/wp-content/uploads/2016/11/Challenge-Accepted-Meme-3.jpg)

It took me exactly 12 months to complete what was proposed. Mainly because the lack of time, and also testing phase took longer than what was expected because surgeries can't be scheduled based on my free time so I had to quit my job by the end of the project if I was willing to get my BCompSc degree by 2010.

I also wrote a paper, with my tutor, about this awesome project and you can get it <a href="http://www.wseas.org/multimedia/journals/biology/2012/035702-187.pdf">here</a>.

<h1>Deciding The Stack</h1>

<h2>Backend</h2>
<ul>
<li><b>PostgreSQL:</b> I wanted so bad to use MySQL, but public university encourage the use of PostgreSQL for reasons that nobody knew and nobody knows. NoSQL was also an option but I had 0 expertise on that field so I played it safe. A database was needed to store user and surgery information so can be acceded later to review a past procedure for teaching or reviewing purposes.
</li>
<li>
<b>PHP:</b> Also, playing safe. My job was about building Drupal apps. So, PHP was the smart choice.
</li>
</ul>

<h2>Middleware</h2>
<ul>
<li>
<b>Media Server:</b> Well, you just can't stream video without something in the middle that could let you watch live or resume a video stream. You need a media server and there were a few options.
</li>
</ul>

The following benchmark exposes an assessment we made in order to select which media server will be used. The were some open source solutions like <a href="https://www.red5pro.com/red5-media-server/">Red5</a> and Wowza but Wowza documentation was so poor we couldn't test it without failing on every attempt. 

<table>
 <tr><td></td><td width=131 style='width:98.5pt;border:solid black 1.0pt;mso-border-themecolor:text1;border-left:none;mso-border-left-alt:solid black .5pt;mso-border-left-themecolor:text1;mso-border-alt:solid black .5pt;mso-border-themecolor:text1;background:#767171;mso-background-themecolor:background2;mso-background-themeshade:128;'><p class=MsoNormal align=center style='text-align:center;mso-layout-grid-align:none;text-autospace:none'><span class=SpellE><b style='mso-bidi-font-weight:normal'><span style='line-height:115%;font-family:"Arial",sans-serif;color:white;mso-themecolor:background1'>Wowza</span></b></span><bstyle='mso-bidi-font-weight:normal'><span style='line-height:115%;font-family:"Arial",sans-serif;color:white;mso-themecolor:background1'></td><td><p class=MsoNormal align=center style='text-align:center;mso-layout-grid-align:none;text-autospace:none'><b style='mso-bidi-font-weight:normal'><spanstyle='line-height:115%;font-family:"Arial",sans-serif;color:white;mso-themecolor:background1'>Red5</td><td><p class=MsoNormal align=center style='text-align:center;mso-layout-grid-align:none;text-autospace:none'><b style='mso-bidi-font-weight:normal'><spanstyle='line-height:115%;font-family:"Arial",sans-serif;color:white;mso-themecolor:background1'>FMS</td>
 </tr>
 <tr><td>Multiplatform</td><td>Yes</td><td>Yes</td><td>Yes</td>
 </tr>
 <tr><td>Open Source</td><td>No</td><td>Yes</td><td>No</td>
 </tr>
 <tr><td>Performance</td><td>Limited</td><td>Limited</td><td>High</td>
 </tr>
 <tr><td>Codebase</td><td>Java</td><td>Java</td><td>C</span><span lang=EN-USstyle='line-height:150%;font-family:"Arial",sans-serif;mso-ansi-language:EN-US'>++</td>
 </tr>
 <t><td>Documentation</td><td>Not available</td><td>Poor</td><td>Good</td>
 </tr>
 <tr><td>Has RTMP</td><td>Partial</td><td>Yes</td><td>Yes</td>
 </tr>
 <tr><td>Supported Video Codecs</td><td>FLV, H.264</td><td>FLV</td><td>FLV, H.264</td>
 </tr>
 <tr><td>Supported Audio Codecs</td><td>FLV, MP3, AAC</td><td>FLV, MP3</td><td>FLV, MP3, AAC</td>
 </tr>
 <tr><td>AMF0 and AMF3 Support</td><td>Partial</td><td>Partial</td><td>Yes</td>
 </tr>
 <tr><td>Clustering</td><td>Yes</td><td>Yes</td><td>Yes</td>
 </tr>
 <tr><td>Server Side Programming</td><td>JS and limited AS3</td><td>JS</td><td>AS3</td>
 </tr>
 <tr><td>Streaming</td><td>Yes</td><td>Yes</td><td>Yes</td>
 </tr>
 <tr><td>Shared Objects</td><td>Partial</td><td>Partial</td><td>Yes</td>
 </tr>
</table>

Red5 worked pretty fine with low res videos but it couldn't handle 480p using OnVP6. On the other hand, we had Flash Media Server. Written in C++, it was fast and reliable even with resolutions like 720p (remember, 2008 and PIV computers). The free license of Flash Media Server only was able to handle up to 10 streams at the time and it was enough! 

Also, streaming isn't the only thing it was able to handle, also Shared Objects so apps like realtime chat and real time whiteboards were possible to implement with such middleware.


<ul>
<li><strong><a href="https://www.silexlabs.org/amfphp/">AMFPHP</a>:</strong> this middleware library was responsible for serializing AS3 objects so they could be transferred from and to the backend. It still exists on <a href="https://github.com/silexlabs/amfphp-2.0">GitHub</a>. But why am I mentioning Action Script here? Well, you'll find why in the frontend section.
</li>
</ul>

<h2>Frontend</h2>

Analogous to the media server, we benchmarked 4 scenarios or technologies HTML & JS, Laszlo, Silverlight, and Flex. I won't show that table here, it is bigger than the other one and I don't want to annoy you (more...) 🙈

Before Angular, React or VueJS, there was something called Rich Internet Applications in a world where HTML5 & ECMAScript were not able to deliver desktop like solutions in your browser. Java Applets were already obsolete and Adobe and Microsoft decided to implement runtimes to fill this gap.

Adobe was saying that they Open Sourced an Object-Oriented framework (Adobe Flex) that would let you write amazing apps, using a language very similar to Java (Yes, Action Script 3) and style them using its API or CSS. Also, it would integrate flawlessly with Flash Media Server so we got a winner!

The following code shows how we use AMFPHP inside one of the classes of the project written in AS3. Follow the comments to understand it better.


```java
package assets {
  import flash.events.*;
  import flash.net.NetConnection;
  import flash.net.Responder;
  
  public class PGDBConexion {
    //The file gateway.php has a class with a set of public methods prepared to receive an object
    private var gateway:String = "http://127.0.0.1/amfphp/gateway.php";
    private var conexion:NetConnection = new NetConnection();
		
    public function PGDBConexion() {		 
      this.conexion.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
      this.conexion.connect(this.gateway);
    }
	
    private function netStatusHandler(event:NetStatusEvent):void {
      ...
    }
        
     public function call(funcion:String, responder:Responder):void {
       //The variable 'funcion' is the name of the method inside the php class in gateway.php
       //and responder is the callback function that will receive the response
       this.conexion.call(funcion, responder);
     }
  }
}
```
And when we need to call a particular endpoint or method we can do something like this
```java
package assets {
  ...
  import flash.net.Responder;

  public class SelectorStream extends Panel {		
    ...		
    private var conexion:PGDBConexion = new PGDBConexion();
    private var responder:Responder;
    ...
		 
    public function SelectorStream() {
      super();	
      ...		
      this.responder = new Responder(onResult, onFault);
      //This is were we invoke the getStreams method from gateway.php
      this.conexion.call("pgDB.getStreams", responder);
    }
		
    private function onResult(data:Object):void {
      //The object 'data' will contain the response already serialized to be used in AS3
      ...	
    }		

    private function onFault(fault:Object):void {
      ...
    }
  }
}
```

<h2>Formal Software Engineering</h2>
Let's remember this was a formal software engineering challenge so we had to documentate professionally everything so I'm sharing a few diagrams that describe the overall architecture of the solution. I used UML, of course. 🤓

<h3>Use Case Diagram</h3>
This is the Use Case diagram of one of the three possible roles that a user could have inside the system.
![Use case diagram](https://dev-to-uploads.s3.amazonaws.com/i/0alhip5avs29027y5i6q.png)

<h3>Entity-Relationship Diagram</h3>
![Entity-Relationship diagram](https://dev-to-uploads.s3.amazonaws.com/i/mtn6ea0gnzsmr6pm0ajr.png)

<h3>Class Diagram</h3>
This is a small subset showing only two types of relationships and methods
![Class diagram](https://dev-to-uploads.s3.amazonaws.com/i/p81ebzxfl00xwn3btdsk.png)

<h3>Deployment Diagram</h3>
![Deployment Diagram](https://dev-to-uploads.s3.amazonaws.com/i/bejj6yc198jjt46qdpgw.png)

<h1>Final words</h1>
This Side Project was software engineering as its finest for me!

I did love this project, I researched, I tested, I delivered small increments using the MVP approach without knowing it was called that way.
I did something nobody has done before and that system is still running on a Sun Server at the Urology Institute here, in Valencia, Venezuela.

This was my last side project until 2020, where I started two new side projects. Now I have 10 additional years of experience and technology has changed. I'm willing to share more side projects with you guys!

Unlike the other ones, I do have the source code and binaries of this gem. 💎

Thanks for reading! And if you like it, then like it, clap it or share it! 😜