aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/DecodeManager.h
blob: 146bf8ae0ec998aec7d21b7cd2240ce1c4ae25b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright 2015 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifndef __RFB_DECODEMANAGER_H__
#define __RFB_DECODEMANAGER_H__

#include <condition_variable>
#include <exception>
#include <list>
#include <mutex>
#include <thread>

#include <core/Region.h>

#include <rfb/encodings.h>

namespace core {
  struct Rect;
}

namespace rdr {
  class MemOutStream;
}

namespace rfb {

  class CConnection;
  class Decoder;
  class ModifiablePixelBuffer;

  class DecodeManager {
  public:
    DecodeManager(CConnection *conn);
    ~DecodeManager();

    bool decodeRect(const core::Rect& r, int encoding,
                    ModifiablePixelBuffer* pb);

    void flush();

  private:
    void logStats();

    void setThreadException();
    void throwThreadException();

  private:
    CConnection *conn;
    Decoder *decoders[encodingMax+1];

    struct DecoderStats {
      unsigned rects;
      unsigned long long bytes;
      unsigned long long pixels;
      unsigned long long equivalent;
    };

    DecoderStats stats[encodingMax+1];

    struct QueueEntry {
      bool active;
      core::Rect rect;
      int encoding;
      Decoder* decoder;
      const ServerParams* server;
      ModifiablePixelBuffer* pb;
      rdr::MemOutStream* bufferStream;
      core::Region affectedRegion;
    };

    std::list<rdr::MemOutStream*> freeBuffers;
    std::list<QueueEntry*> workQueue;

    std::mutex queueMutex;
    std::condition_variable producerCond;
    std::condition_variable consumerCond;

  private:
    class DecodeThread {
    public:
      DecodeThread(DecodeManager* manager);
      ~DecodeThread();

      void start();
      void stop();

    protected:
      void worker();
      DecodeManager::QueueEntry* findEntry();

    private:
      DecodeManager* manager;

      std::thread* thread;
      bool stopRequested;
    };

    std::list<DecodeThread*> threads;
    std::exception_ptr threadException;
  };

}

#endif