OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef REMOTING_HOST_RECORD_SESSION_H_ |
| 6 #define REMOTING_HOST_RECORD_SESSION_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/ref_counted.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/time.h" |
| 15 #include "remoting/base/protocol/chromotocol.pb.h" |
| 16 #include "remoting/host/capturer.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 class DataBuffer; |
| 21 |
| 22 } // namespace media |
| 23 |
| 24 namespace remoting { |
| 25 |
| 26 class Encoder; |
| 27 class ClientConnection; |
| 28 |
| 29 // A class for controlling and coordinate Capturer, Encoder |
| 30 // and NetworkChannel in a record session. |
| 31 // |
| 32 // THREADING |
| 33 // |
| 34 // This class works on three threads, namely capture, encode and network |
| 35 // thread. The main function of this class is to coordinate and schedule |
| 36 // capture, encode and transmission of data on different threads. |
| 37 // |
| 38 // The following is an example of timeline for operations scheduled. |
| 39 // |
| 40 // | CAPTURE ENCODE NETWORK |
| 41 // | ............. |
| 42 // | . Capture . |
| 43 // | ............. |
| 44 // | ............ |
| 45 // | . . |
| 46 // | ............. . . |
| 47 // | . Capture . . Encode . |
| 48 // | ............. . . |
| 49 // | . . |
| 50 // | ............ |
| 51 // | ............. ............ .......... |
| 52 // | . Capture . . . . Send . |
| 53 // | ............. . . .......... |
| 54 // | . Encode . |
| 55 // | . . |
| 56 // | . . |
| 57 // | ............ |
| 58 // | Time |
| 59 // v |
| 60 // |
| 61 // SessionManager has the following responsibilities: |
| 62 // 1. Make sure capture and encode occurs no more frequently than |rate|. |
| 63 // 2. Make sure there is at most one outstanding capture not being encoded. |
| 64 // 3. Distribute tasks on three threads on a timely fashion to minimize latency. |
| 65 class SessionManager : public base::RefCountedThreadSafe<SessionManager> { |
| 66 public: |
| 67 |
| 68 // Construct a SessionManager. Message loops and threads are provided. |
| 69 // Ownership of Capturer and Encoder are given to this object. |
| 70 SessionManager(MessageLoop* capture_loop, |
| 71 MessageLoop* encode_loop, |
| 72 MessageLoop* network_loop, |
| 73 Capturer* capturer, |
| 74 Encoder* encoder); |
| 75 |
| 76 virtual ~SessionManager(); |
| 77 |
| 78 // Start recording. |
| 79 void Start(); |
| 80 |
| 81 // Pause the recording session. |
| 82 void Pause(); |
| 83 |
| 84 // Set the maximum capture rate. This is denoted by number of updates |
| 85 // in one second. The actual system may run in a slower rate than the maximum |
| 86 // rate due to various factors, e.g. capture speed, encode speed and network |
| 87 // conditions. |
| 88 // This method should be called before Start() is called. |
| 89 void SetMaxRate(double rate); |
| 90 |
| 91 // Add a client to this recording session. |
| 92 void AddClient(scoped_refptr<ClientConnection> client); |
| 93 |
| 94 // Remove a client from receiving screen updates. |
| 95 void RemoveClient(scoped_refptr<ClientConnection> client); |
| 96 |
| 97 private: |
| 98 void DoStart(); |
| 99 void DoPause(); |
| 100 void DoStartRateControl(); |
| 101 void DoPauseRateControl(); |
| 102 |
| 103 void DoCapture(); |
| 104 void DoFinishEncode(); |
| 105 void DoEncode(); |
| 106 void DoSendUpdate( |
| 107 chromotocol_pb::UpdateStreamPacketHeader* header, |
| 108 scoped_refptr<media::DataBuffer> encoded_data, |
| 109 bool begin_update, |
| 110 bool end_update); |
| 111 void DoSendInit(scoped_refptr<ClientConnection> client, |
| 112 int width, int height); |
| 113 void DoGetInitInfo(scoped_refptr<ClientConnection> client); |
| 114 void DoSetRate(double rate); |
| 115 void DoSetMaxRate(double max_rate); |
| 116 void DoAddClient(scoped_refptr<ClientConnection> client); |
| 117 void DoRemoveClient(scoped_refptr<ClientConnection> client); |
| 118 void DoRateControl(); |
| 119 |
| 120 // Hepler method to schedule next capture using the current rate. |
| 121 void ScheduleNextCapture(); |
| 122 |
| 123 // Helper method to schedule next rate regulation task. |
| 124 void ScheduleNextRateControl(); |
| 125 |
| 126 void CaptureDoneTask(); |
| 127 void EncodeDataAvailableTask(); |
| 128 |
| 129 // Message loops used by this class. |
| 130 MessageLoop* capture_loop_; |
| 131 MessageLoop* encode_loop_; |
| 132 MessageLoop* network_loop_; |
| 133 |
| 134 // Reference to the capturer. This member is always accessed on the capture |
| 135 // thread. |
| 136 scoped_ptr<Capturer> capturer_; |
| 137 |
| 138 // Reference to the encoder. This member is always accessed on the encode |
| 139 // thread. |
| 140 scoped_ptr<Encoder> encoder_; |
| 141 |
| 142 // A list of clients connected to this hosts. |
| 143 // This member is always accessed on the NETWORK thread. |
| 144 // TODO(hclam): Have to scoped_refptr the clients since they have a shorter |
| 145 // lifetime than this object. |
| 146 typedef std::vector<scoped_refptr<ClientConnection> > ClientConnectionList; |
| 147 ClientConnectionList clients_; |
| 148 |
| 149 // The following members are accessed on the capture thread. |
| 150 double rate_; // Number of captures to perform every second. |
| 151 bool started_; |
| 152 base::Time last_capture_time_; // Saves the time last capture started. |
| 153 int recordings_; // Count the number of recordings |
| 154 // (i.e. capture or encode) happening. |
| 155 |
| 156 // The maximum rate is written on the capture thread and read on the network |
| 157 // thread. |
| 158 double max_rate_; // Number of captures to perform every second. |
| 159 |
| 160 // The following member is accessed on the network thread. |
| 161 bool rate_control_started_; |
| 162 |
| 163 // Stores the data and information of the last capture done. |
| 164 // These members are written on capture thread and read on encode thread. |
| 165 // It is guranteed the read happens after the write. |
| 166 DirtyRects capture_dirty_rects_; |
| 167 const uint8* capture_data_[3]; |
| 168 int capture_data_strides_[3]; |
| 169 int capture_width_; |
| 170 int capture_height_; |
| 171 chromotocol_pb::PixelFormat capture_pixel_format_; |
| 172 |
| 173 // The following members are accessed on the encode thread. |
| 174 // Output parameter written by Encoder to carry encoded data. |
| 175 chromotocol_pb::UpdateStreamPacketHeader encoded_data_header_; |
| 176 scoped_refptr<media::DataBuffer> encoded_data_; |
| 177 |
| 178 // True if we have started receiving encoded data from the Encoder. |
| 179 bool encode_stream_started_; |
| 180 |
| 181 // Output parameter written by Encoder to notify the end of encoded data |
| 182 // stream. |
| 183 bool encode_done_; |
| 184 |
| 185 DISALLOW_COPY_AND_ASSIGN(SessionManager); |
| 186 }; |
| 187 |
| 188 } // namespace remoting |
| 189 |
| 190 #endif // REMOTING_HOST_RECORD_SESSION_H_ |
OLD | NEW |