diff --git a/omxplayer.cpp b/omxplayer.cpp
index 197216d..8f123c2 100644
--- a/omxplayer.cpp
+++ b/omxplayer.cpp
@@ -33,6 +33,10 @@
 extern "C" {
 #include <libavformat/avformat.h>
 #include <libavutil/avutil.h>
+int playerInit(char *filename);
+void playerPlay();
+int playerLoop();
+void playerClose();
 };
 
 #include "OMXStreamInfo.h"
@@ -100,7 +104,24 @@ bool              m_has_audio           = false;
 bool              m_has_subtitle        = false;
 float             m_display_aspect      = 0.0f;
 bool              m_boost_on_downmix    = false;
-bool              m_gen_log             = false;
+
+std::string            m_filename;
+  double                m_incr                = 0;
+  CRBP                  g_RBP;
+  COMXCore              g_OMX;
+  bool                  m_stats               = false;
+  bool                  m_dump_format         = false;
+  FORMAT_3D_T           m_3d                  = CONF_FLAGS_FORMAT_NONE;
+  bool                  m_refresh             = false;
+  double                startpts              = 0;
+  CRect                 DestRect              = {0,0,0,0};
+  float audio_fifo_size = 0.0; // zero means use default
+  float video_fifo_size = 0.0;
+  float audio_queue_size = 0.0;
+  float video_queue_size = 0.0;
+  bool has_buffered = false;
+  TV_DISPLAY_STATE_T   tv_state;
+
 
 enum{ERROR=-1,SUCCESS,ONEBYTE};
 
@@ -133,92 +154,6 @@ void sig_handler(int s)
   abort();
 }
 
-void print_usage()
-{
-  printf("Usage: omxplayer [OPTIONS] [FILE]\n");
-  printf("Options :\n");
-  printf("         -h / --help                    print this help\n");
-  printf("         -k / --keys                    print key bindings\n");
-//  printf("         -a / --alang language          audio language        : e.g. ger\n");
-  printf("         -n / --aidx  index             audio stream index    : e.g. 1\n");
-  printf("         -o / --adev  device            audio out device      : e.g. hdmi/local\n");
-  printf("         -i / --info                    dump stream format and exit\n");
-  printf("         -s / --stats                   pts and buffer stats\n");
-  printf("         -p / --passthrough             audio passthrough\n");
-  printf("         -d / --deinterlace             deinterlacing\n");
-  printf("         -w / --hw                      hw audio decoding\n");
-  printf("         -3 / --3d mode                 switch tv into 3d mode (e.g. SBS/TB)\n");
-  printf("         -y / --hdmiclocksync           adjust display refresh rate to match video (default)\n");
-  printf("         -z / --nohdmiclocksync         do not adjust display refresh rate to match video\n");
-  printf("         -t / --sid index               show subtitle with index\n");
-  printf("         -r / --refresh                 adjust framerate/resolution to video\n");
-  printf("         -g / --genlog                  generate log file\n");
-  printf("         -l / --pos n                   start position (in seconds)\n");
-  printf("              --boost-on-downmix        boost volume when downmixing\n");
-  printf("              --vol n                   Set initial volume in millibels (default 0)\n");
-  printf("              --subtitles path          external subtitles in UTF-8 srt format\n");
-  printf("              --font path               subtitle font\n");
-  printf("                                        (default: /usr/share/fonts/truetype/freefont/FreeSans.ttf)\n");
-  printf("              --font-size size          font size as thousandths of screen height\n");
-  printf("                                        (default: 55)\n");
-  printf("              --align left/center       subtitle alignment (default: left)\n");
-  printf("              --lines n                 number of lines to accommodate in the subtitle buffer\n");
-  printf("                                        (default: 3)\n");
-  printf("              --win \"x1 y1 x2 y2\"     Set position of video window\n");
-  printf("              --audio_fifo  n           Size of audio output fifo in seconds\n");
-  printf("              --video_fifo  n           Size of video output fifo in MB\n");
-  printf("              --audio_queue n           Size of audio input queue in MB\n");
-  printf("              --video_queue n           Size of video input queue in MB\n");
-}
-
-void print_keybindings()
-{
-  printf("Key bindings :\n");
-  printf("        1                  decrease speed\n");
-  printf("        2                  increase speed\n");
-  printf("        z                  show info\n");
-  printf("        j                  previous audio stream\n");
-  printf("        k                  next audio stream\n");
-  printf("        i                  previous chapter\n");
-  printf("        o                  next chapter\n");
-  printf("        n                  previous subtitle stream\n");
-  printf("        m                  next subtitle stream\n");
-  printf("        s                  toggle subtitles\n");
-  printf("        d                  decrease subtitle delay (- 250 ms)\n");
-  printf("        f                  increase subtitle delay (+ 250 ms)\n");
-  printf("        q                  exit omxplayer\n");
-  printf("        p / space          pause/resume\n");
-  printf("        -                  decrease volume\n");
-  printf("        + / =              increase volume\n");
-  printf("        left arrow         seek -30 seconds\n");
-  printf("        right arrow        seek +30 seconds\n");
-  printf("        down arrow         seek -600 seconds\n");
-  printf("        up arrow           seek +600 seconds\n");
-}
-
-void PrintSubtitleInfo()
-{
-  auto count = m_omx_reader.SubtitleStreamCount();
-  size_t index = 0;
-
-  if(m_has_external_subtitles)
-  {
-    ++count;
-    if(!m_player_subtitles.GetUseExternalSubtitles())
-      index = m_player_subtitles.GetActiveStream() + 1;
-  }
-  else if(m_has_subtitle)
-  {
-      index = m_player_subtitles.GetActiveStream();
-  }
-
-  printf("Subtitle count: %d, state: %s, index: %d, delay: %d\n",
-         count,
-         m_has_subtitle && m_player_subtitles.GetVisible() ? " on" : "off",
-         index+1,
-         m_has_subtitle ? m_player_subtitles.GetDelay() : 0);
-}
-
 void SetSpeed(int iSpeed)
 {
   if(!m_av_clock)
@@ -319,11 +254,6 @@ void SetVideoMode(int width, int height, int fpsrate, int fpsscale, FORMAT_3D_T
   {
     num_modes = m_BcmHost.vc_tv_hdmi_get_supported_modes_new(group,
         supported_modes, max_supported_modes, &prefer_group, &prefer_mode);
-
-    if(m_gen_log) {
-    CLog::Log(LOGDEBUG, "EGL get supported modes (%d) = %d, prefer_group=%x, prefer_mode=%x\n",
-        group, num_modes, prefer_group, prefer_mode);
-    }
   }
 
   TV_SUPPORTED_MODE_NEW_T *tv_found = NULL;
@@ -441,12 +371,9 @@ bool IsURL(const std::string& str)
   return true;
 }
 
-int main(int argc, char *argv[])
-{
-  signal(SIGSEGV, sig_handler);
-  signal(SIGABRT, sig_handler);
-  signal(SIGFPE, sig_handler);
-  signal(SIGINT, sig_handler);
+
+int playerInit(char *filename) {
+  m_filename = filename;
 
   if (isatty(STDIN_FILENO))
   {
@@ -468,204 +395,6 @@ int main(int argc, char *argv[])
   }
   atexit(restore_term);
 
-  std::string            m_filename;
-  double                m_incr                = 0;
-  CRBP                  g_RBP;
-  COMXCore              g_OMX;
-  bool                  m_stats               = false;
-  bool                  m_dump_format         = false;
-  FORMAT_3D_T           m_3d                  = CONF_FLAGS_FORMAT_NONE;
-  bool                  m_refresh             = false;
-  double                startpts              = 0;
-  CRect                 DestRect              = {0,0,0,0};
-  float audio_fifo_size = 0.0; // zero means use default
-  float video_fifo_size = 0.0;
-  float audio_queue_size = 0.0;
-  float video_queue_size = 0.0;
-  bool has_buffered = false;
-  TV_DISPLAY_STATE_T   tv_state;
-
-  const int font_opt        = 0x100;
-  const int font_size_opt   = 0x101;
-  const int align_opt       = 0x102;
-  const int subtitles_opt   = 0x103;
-  const int lines_opt       = 0x104;
-  const int pos_opt         = 0x105;
-  const int vol_opt         = 0x106;
-  const int audio_fifo_opt  = 0x107;
-  const int video_fifo_opt  = 0x108;
-  const int audio_queue_opt = 0x109;
-  const int video_queue_opt = 0x10a;
-  const int boost_on_downmix_opt = 0x200;
-
-  struct option longopts[] = {
-    { "info",         no_argument,        NULL,          'i' },
-    { "help",         no_argument,        NULL,          'h' },
-    { "keys",         no_argument,        NULL,          'k' },
-    { "aidx",         required_argument,  NULL,          'n' },
-    { "adev",         required_argument,  NULL,          'o' },
-    { "stats",        no_argument,        NULL,          's' },
-    { "passthrough",  no_argument,        NULL,          'p' },
-    { "vol",          required_argument,  NULL,          vol_opt },
-    { "deinterlace",  no_argument,        NULL,          'd' },
-    { "hw",           no_argument,        NULL,          'w' },
-    { "3d",           required_argument,  NULL,          '3' },
-    { "hdmiclocksync", no_argument,       NULL,          'y' },
-    { "nohdmiclocksync", no_argument,     NULL,          'z' },
-    { "refresh",      no_argument,        NULL,          'r' },
-    { "genlog",       no_argument,        NULL,          'g' },
-    { "sid",          required_argument,  NULL,          't' },
-    { "pos",          required_argument,  NULL,          'l' },    
-    { "font",         required_argument,  NULL,          font_opt },
-    { "font-size",    required_argument,  NULL,          font_size_opt },
-    { "align",        required_argument,  NULL,          align_opt },
-    { "subtitles",    required_argument,  NULL,          subtitles_opt },
-    { "lines",        required_argument,  NULL,          lines_opt },
-    { "win",          required_argument,  NULL,          pos_opt },
-    { "audio_fifo",   required_argument,  NULL,          audio_fifo_opt },
-    { "video_fifo",   required_argument,  NULL,          video_fifo_opt },
-    { "audio_queue",  required_argument,  NULL,          audio_queue_opt },
-    { "video_queue",  required_argument,  NULL,          video_queue_opt },
-    { "boost-on-downmix", no_argument,    NULL,          boost_on_downmix_opt },
-    { 0, 0, 0, 0 }
-  };
-
-  int c;
-  std::string mode;
-  while ((c = getopt_long(argc, argv, "wihkn:l:o:cslpd3:yzt:rg", longopts, NULL)) != -1)
-  {
-    switch (c) 
-    {
-      case 'r':
-        m_refresh = true;
-        break;
-      case 'g':
-        m_gen_log = true;
-        break;
-      case 'y':
-        m_hdmi_clock_sync = true;
-        break;
-      case 'z':
-        m_no_hdmi_clock_sync = true;
-        break;
-      case '3':
-        mode = optarg;
-        if(mode != "SBS" && mode != "TB")
-        {
-          print_usage();
-          return 0;
-        }
-        if(mode == "TB")
-          m_3d = CONF_FLAGS_FORMAT_TB;
-        else
-          m_3d = CONF_FLAGS_FORMAT_SBS;
-        break;
-      case 'd':
-        m_Deinterlace = true;
-        break;
-      case 'w':
-        m_use_hw_audio = true;
-        break;
-      case 'p':
-        m_passthrough = true;
-        break;
-      case 's':
-        m_stats = true;
-        break;
-      case 'o':
-        deviceString = optarg;
-        if(deviceString != "local" && deviceString != "hdmi")
-        {
-          print_usage();
-          return 0;
-        }
-        deviceString = "omx:" + deviceString;
-        break;
-      case 'i':
-        m_dump_format = true;
-        break;
-      case 't':
-        m_subtitle_index = atoi(optarg) - 1;
-        if(m_subtitle_index < 0)
-          m_subtitle_index = 0;
-        break;
-      case 'n':
-        m_audio_index_use = atoi(optarg) - 1;
-        if(m_audio_index_use < 0)
-          m_audio_index_use = 0;
-        break;
-      case 'l':
-        m_seek_pos = atoi(optarg) ;
-        if (m_seek_pos < 0)
-            m_seek_pos = 0;
-        break;
-      case font_opt:
-        m_font_path = optarg;
-        m_has_font = true;
-        break;
-      case font_size_opt:
-        {
-          const int thousands = atoi(optarg);
-          if (thousands > 0)
-            m_font_size = thousands*0.001f;
-        }
-        break;
-      case align_opt:
-        m_centered = !strcmp(optarg, "center");
-        break;
-      case subtitles_opt:
-        m_external_subtitles_path = optarg;
-        m_has_external_subtitles = true;
-        break;
-      case lines_opt:
-        m_subtitle_lines = std::max(atoi(optarg), 1);
-        break;
-      case pos_opt:
-	sscanf(optarg, "%f %f %f %f", &DestRect.x1, &DestRect.y1, &DestRect.x2, &DestRect.y2);
-        break;
-      case vol_opt:
-	m_initialVolume = atoi(optarg);
-        break;
-      case boost_on_downmix_opt:
-        m_boost_on_downmix = true;
-        break;
-      case audio_fifo_opt:
-	audio_fifo_size = atof(optarg);
-        break;
-      case video_fifo_opt:
-	video_fifo_size = atof(optarg);
-        break;
-      case audio_queue_opt:
-	audio_queue_size = atof(optarg);
-        break;
-      case video_queue_opt:
-	video_queue_size = atof(optarg);
-        break;
-      case 0:
-        break;
-      case 'h':
-        print_usage();
-        return 0;
-        break;
-      case 'k':
-        print_keybindings();
-        return 0;
-        break;
-      case ':':
-        return 0;
-        break;
-      default:
-        return 0;
-        break;
-    }
-  }
-
-  if (optind >= argc) {
-    print_usage();
-    return 0;
-  }
-
-  m_filename = argv[optind];
 
   auto PrintFileNotFound = [](const std::string& path)
   {
@@ -704,13 +433,6 @@ int main(int argc, char *argv[])
     }
   }
     
-  if(m_gen_log) {
-    CLog::SetLogLevel(LOG_LEVEL_DEBUG);
-    CLog::Init("./");
-  } else {
-    CLog::SetLogLevel(LOG_LEVEL_NONE);
-  }
-
   g_RBP.Initialize();
   g_OMX.Initialize();
 
@@ -719,10 +441,10 @@ int main(int argc, char *argv[])
   m_thread_player = true;
 
   if(!m_omx_reader.Open(m_filename.c_str(), m_dump_format))
-    goto do_exit;
+    return -1;
 
   if(m_dump_format)
-    goto do_exit;
+    return -1;
 
   m_bMpeg         = m_omx_reader.IsMpegVideo();
   m_has_video     = m_omx_reader.VideoStreamCount();
@@ -744,10 +466,10 @@ int main(int argc, char *argv[])
     m_hdmi_clock_sync = true;
 
   if(!m_av_clock->OMXInitialize(m_has_video, m_has_audio))
-    goto do_exit;
+    return -1;
 
   if(m_hdmi_clock_sync && !m_av_clock->HDMIClockSync())
-      goto do_exit;
+      return -1;
 
   m_omx_reader.GetHints(OMXSTREAM_AUDIO, m_hints_audio);
   m_omx_reader.GetHints(OMXSTREAM_VIDEO, m_hints_video);
@@ -783,7 +505,7 @@ int main(int argc, char *argv[])
   
   if(m_has_video && !m_player_video.Open(m_hints_video, m_av_clock, DestRect, m_Deinterlace,  m_bMpeg,
                                          m_hdmi_clock_sync, m_thread_player, m_display_aspect, video_queue_size, video_fifo_size))
-    goto do_exit;
+    return -1;
 
   {
     std::vector<Subtitle> external_subtitles;
@@ -791,7 +513,7 @@ int main(int argc, char *argv[])
        !ReadSrt(m_external_subtitles_path, external_subtitles))
     {
        puts("Unable to read the subtitle file.");
-       goto do_exit;
+       return -1;
     }
 
     if(m_has_subtitle &&
@@ -802,7 +524,7 @@ int main(int argc, char *argv[])
                                 m_centered,
                                 m_subtitle_lines,
                                 m_av_clock))
-      goto do_exit;
+      return -1;
   }
 
   if(m_has_subtitle)
@@ -841,186 +563,55 @@ int main(int argc, char *argv[])
   if(m_has_audio && !m_player_audio.Open(m_hints_audio, m_av_clock, &m_omx_reader, deviceString, 
                                          m_passthrough, m_initialVolume, m_use_hw_audio,
                                          m_boost_on_downmix, m_thread_player, audio_queue_size, audio_fifo_size))
-    goto do_exit;
+    return -1;
 
-  m_av_clock->SetSpeed(DVD_PLAYSPEED_NORMAL);
-  m_av_clock->OMXStart(0.0);
-  m_av_clock->OMXPause();
-  m_av_clock->OMXStateExecute();
+  return 0;
+}
 
-  PrintSubtitleInfo();
+void playerClose() {
+  if(!m_stop && !g_abort)
+  {
+    if(m_has_audio)
+      m_player_audio.WaitCompletion();
+    else if(m_has_video)
+      m_player_video.WaitCompletion();
+  }
 
-  while(!m_stop)
+  if(m_has_video && m_refresh && tv_state.display.hdmi.group && tv_state.display.hdmi.mode)
   {
-    int ch[8];
-    int chnum = 0;
+    m_BcmHost.vc_tv_hdmi_power_on_explicit_new(HDMI_MODE_HDMI, (HDMI_RES_GROUP_T)tv_state.display.hdmi.group, tv_state.display.hdmi.mode);
+  }
 
-    if(g_abort)
-      goto do_exit;
-    
-    while((ch[chnum] = getchar()) != EOF) chnum++;
-    if (chnum > 1) ch[0] = ch[chnum - 1] | (ch[chnum - 2] << 8);
+  m_av_clock->OMXStop();
+  m_av_clock->OMXStateIdle();
 
-    switch(ch[0])
-    {
-      case 'z':
-        m_tv_show_info = !m_tv_show_info;
-        vc_tv_show_info(m_tv_show_info);
-        break;
-      case '1':
-        SetSpeed(m_av_clock->OMXPlaySpeed() - 1);
-        break;
-      case '2':
-        SetSpeed(m_av_clock->OMXPlaySpeed() + 1);
-        break;
-      case 'j':
-        if(m_has_audio)
-        {
-          int new_index = m_omx_reader.GetAudioIndex() - 1;
-          if (new_index >= 0)
-            m_omx_reader.SetActiveStream(OMXSTREAM_AUDIO, new_index);
-        }
-        break;
-      case 'k':
-        if(m_has_audio)
-          m_omx_reader.SetActiveStream(OMXSTREAM_AUDIO, m_omx_reader.GetAudioIndex() + 1);
-        break;
-      case 'i':
-        if(m_omx_reader.GetChapterCount() > 0)
-        {
-          m_omx_reader.SeekChapter(m_omx_reader.GetChapter() - 1, &startpts);
-          FlushStreams(startpts);
-        }
-        else
-        {
-          m_incr = -600.0;
-        }
-        break;
-      case 'o':
-        if(m_omx_reader.GetChapterCount() > 0)
-        {
-          m_omx_reader.SeekChapter(m_omx_reader.GetChapter() + 1, &startpts);
-          FlushStreams(startpts);
-        }
-        else
-        {
-          m_incr = 600.0;
-        }
-        break;
-      case 'n':
-        if(m_has_subtitle)
-        {
-          if(!m_player_subtitles.GetUseExternalSubtitles())
-          {
-            if (m_player_subtitles.GetActiveStream() == 0)
-            {
-              if(m_has_external_subtitles)
-                m_player_subtitles.SetUseExternalSubtitles(true);
-            }
-            else
-            {
-              m_player_subtitles.SetActiveStream(
-                m_player_subtitles.GetActiveStream()-1);
-            }
-          }
-
-          m_player_subtitles.SetVisible(true);
-          PrintSubtitleInfo();
-        }
-        break;
-      case 'm':
-        if(m_has_subtitle)
-        {
-          if(m_player_subtitles.GetUseExternalSubtitles())
-          {
-            if(m_omx_reader.SubtitleStreamCount())
-            {
-              assert(m_player_subtitles.GetActiveStream() == 0);
-              m_player_subtitles.SetUseExternalSubtitles(false);
-            }
-          }
-          else
-          {
-            auto new_index = m_player_subtitles.GetActiveStream()+1;
-            if(new_index < (size_t) m_omx_reader.SubtitleStreamCount())
-              m_player_subtitles.SetActiveStream(new_index);
-          }
-
-          m_player_subtitles.SetVisible(true);
-          PrintSubtitleInfo();
-        }
-        break;
-      case 's':
-        if(m_has_subtitle)
-        {
-          m_player_subtitles.SetVisible(!m_player_subtitles.GetVisible());
-          PrintSubtitleInfo();
-        }
-        break;
-      case 'd':
-        if(m_has_subtitle && m_player_subtitles.GetVisible())
-        {
-          m_player_subtitles.SetDelay(m_player_subtitles.GetDelay() - 250);
-          PrintSubtitleInfo();
-        }
-        break;
-      case 'f':
-        if(m_has_subtitle && m_player_subtitles.GetVisible())
-        {
-          m_player_subtitles.SetDelay(m_player_subtitles.GetDelay() + 250);
-          PrintSubtitleInfo();
-        }
-        break;
-      case 'q': case 27:
-        m_stop = true;
-        goto do_exit;
-        break;
-      case 0x5b44: // key left
-        if(m_omx_reader.CanSeek()) m_incr = -30.0;
-        break;
-      case 0x5b43: // key right
-        if(m_omx_reader.CanSeek()) m_incr = 30.0;
-        break;
-      case 0x5b41: // key up
-        if(m_omx_reader.CanSeek()) m_incr = 600.0;
-        break;
-      case 0x5b42: // key down
-        if(m_omx_reader.CanSeek()) m_incr = -600.0;
-        break;
-      case ' ':
-      case 'p':
-        m_Pause = !m_Pause;
-        if(m_Pause)
-        {
-          SetSpeed(OMX_PLAYSPEED_PAUSE);
-          m_av_clock->OMXPause();
-          if(m_has_subtitle)
-            m_player_subtitles.Pause();
-        }
-        else
-        {
-          if(m_has_subtitle)
-            m_player_subtitles.Resume();
-          SetSpeed(OMX_PLAYSPEED_NORMAL);
-          m_av_clock->OMXResume();
-        }
-        break;
-      case '-':
-        m_player_audio.SetCurrentVolume(m_player_audio.GetCurrentVolume() - 300);
-        printf("Current Volume: %.2fdB\n", m_player_audio.GetCurrentVolume() / 100.0f);
-        break;
-      case '+': case '=':
-        m_player_audio.SetCurrentVolume(m_player_audio.GetCurrentVolume() + 300);
-        printf("Current Volume: %.2fdB\n", m_player_audio.GetCurrentVolume() / 100.0f);
-        break;
-      default:
-        break;
-    }
+  m_player_subtitles.Close();
+  m_player_video.Close();
+  m_player_audio.Close();
+
+  if(m_omx_pkt)
+  {
+    m_omx_reader.FreePacket(m_omx_pkt);
+    m_omx_pkt = NULL;
+  }
+
+  m_omx_reader.Close();
+
+  m_av_clock->Deinitialize();
+  if (m_av_clock)
+    delete m_av_clock;
+
+  vc_tv_show_info(0);
+
+  g_OMX.Deinitialize();
+  g_RBP.Deinitialize();
+}
 
+int playerLoop() {
     if(m_Pause)
     {
       OMXClock::OMXSleep(2);
-      continue;
+      return 0;
     }
 
     if(m_incr != 0 && !m_bMpeg)
@@ -1049,7 +640,7 @@ int main(int argc, char *argv[])
       m_player_video.Close();
       if(m_has_video && !m_player_video.Open(m_hints_video, m_av_clock, DestRect, m_Deinterlace, m_bMpeg,
                                          m_hdmi_clock_sync, m_thread_player, m_display_aspect, video_queue_size, video_fifo_size))
-        goto do_exit;
+        return -1;
 
       m_av_clock->OMXStart(startpts);
       
@@ -1061,30 +652,20 @@ int main(int argc, char *argv[])
     if(m_player_audio.Error())
     {
       printf("audio player error. emergency exit!!!\n");
-      goto do_exit;
-    }
-
-    if(m_stats)
-    {
-      static int count;
-      if ((count++ & 15) == 0)
-         printf("V : %8.02f %8d %8d A : %8.02f %8.02f/%8.02f Cv : %8d Ca : %8d                            \r",
-             m_av_clock->OMXMediaTime(), m_player_video.GetDecoderBufferSize(), m_player_video.GetDecoderFreeSpace(),
-             m_player_audio.GetCurrentPTS() / DVD_TIME_BASE - m_av_clock->OMXMediaTime() * 1e-6, m_player_audio.GetDelay(), m_player_audio.GetCacheTotal(),
-             m_player_video.GetCached(), m_player_audio.GetCached());
+      return -1;
     }
 
     if(m_omx_reader.IsEof() && !m_omx_pkt)
     {
       if (!m_player_audio.GetCached() && !m_player_video.GetCached())
-        break;
+        return -1;
 
       // Abort audio buffering, now we're on our own
       if (m_av_clock->OMXIsPaused())
         m_av_clock->OMXResume();
 
       OMXClock::OMXSleep(10);
-      continue;
+      return 0;
     }
 
     /* when the audio buffer runs under 0.1 seconds we buffer up */
@@ -1123,24 +704,6 @@ int main(int argc, char *argv[])
         }
         OMXClock::OMXSleep(10);
       }
-
-      if(m_tv_show_info)
-      {
-        static unsigned count;
-        if ((count++ & 15) == 0)
-        {
-          char response[80];
-          vc_gencmd(response, sizeof response, "render_bar 4 video_fifo %d %d %d %d",
-                m_player_video.GetDecoderBufferSize()-m_player_video.GetDecoderFreeSpace(),
-                0 , 0, m_player_video.GetDecoderBufferSize());
-          vc_gencmd(response, sizeof response, "render_bar 5 audio_fifo %d %d %d %d",
-                (int)(100.0*m_player_audio.GetDelay()), 0, 0, (int)(100.0f * m_player_audio.GetCacheTotal()));
-          vc_gencmd(response, sizeof response, "render_bar 6 video_queue %d %d %d %d",
-                m_player_video.GetLevel(), 0, 0, 100);
-          vc_gencmd(response, sizeof response, "render_bar 7 audio_queue %d %d %d %d",
-                m_player_audio.GetLevel(), 0, 0, 100);
-        }
-      }
     }
     else if(m_has_audio && m_omx_pkt && m_omx_pkt->codec_type == AVMEDIA_TYPE_AUDIO)
     {
@@ -1167,48 +730,33 @@ int main(int argc, char *argv[])
         m_omx_pkt = NULL;
       }
     }
-  }
-
-do_exit:
-  printf("\n");
 
-  if(!m_stop && !g_abort)
-  {
-    if(m_has_audio)
-      m_player_audio.WaitCompletion();
-    else if(m_has_video)
-      m_player_video.WaitCompletion();
-  }
+    return 0;
+}
 
-  if(m_has_video && m_refresh && tv_state.display.hdmi.group && tv_state.display.hdmi.mode)
-  {
-    m_BcmHost.vc_tv_hdmi_power_on_explicit_new(HDMI_MODE_HDMI, (HDMI_RES_GROUP_T)tv_state.display.hdmi.group, tv_state.display.hdmi.mode);
-  }
+void playerPlay() {
+  m_av_clock->SetSpeed(DVD_PLAYSPEED_NORMAL);
+  m_av_clock->OMXStart(0.0);
+  m_av_clock->OMXPause();
+  m_av_clock->OMXStateExecute();
+}
 
-  m_av_clock->OMXStop();
-  m_av_clock->OMXStateIdle();
+#if 0
+int main(int argc, char *argv[])
+{
+  if (playerInit(argv[1]) == -1)
+	goto do_exit;
 
-  m_player_subtitles.Close();
-  m_player_video.Close();
-  m_player_audio.Close();
+  playerPlay();
 
-  if(m_omx_pkt)
+  while(!m_stop)
   {
-    m_omx_reader.FreePacket(m_omx_pkt);
-    m_omx_pkt = NULL;
+	if (playerLoop() == -1)
+		break;
   }
 
-  m_omx_reader.Close();
-
-  m_av_clock->Deinitialize();
-  if (m_av_clock)
-    delete m_av_clock;
-
-  vc_tv_show_info(0);
-
-  g_OMX.Deinitialize();
-  g_RBP.Deinitialize();
-
-  printf("have a nice day ;)\n");
+do_exit:
+  playerClose();
   return 1;
 }
+#endif
