OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_HOST_SCREEN_RECORDER_H_ | 5 #ifndef REMOTING_HOST_SCREEN_RECORDER_H_ |
6 #define REMOTING_HOST_SCREEN_RECORDER_H_ | 6 #define REMOTING_HOST_SCREEN_RECORDER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 // | . . | 53 // | . . |
54 // | . . | 54 // | . . |
55 // | ............ | 55 // | ............ |
56 // | Time | 56 // | Time |
57 // v | 57 // v |
58 // | 58 // |
59 // ScreenRecorder has the following responsibilities: | 59 // ScreenRecorder has the following responsibilities: |
60 // 1. Make sure capture and encode occurs no more frequently than |rate|. | 60 // 1. Make sure capture and encode occurs no more frequently than |rate|. |
61 // 2. Make sure there is at most one outstanding capture not being encoded. | 61 // 2. Make sure there is at most one outstanding capture not being encoded. |
62 // 3. Distribute tasks on three threads on a timely fashion to minimize latency. | 62 // 3. Distribute tasks on three threads on a timely fashion to minimize latency. |
63 // | |
64 // This class has the following state variables: | |
65 // |started_| - If this is set to false there should be no activity on the | |
awong
2011/01/21 19:09:41
started_ and network_stopped_ are negations of eac
| |
66 // capture thread by this object. | |
67 // |network_stopped_| - This state is to prevent activity on the network thread | |
68 // if set to false. | |
63 class ScreenRecorder : public base::RefCountedThreadSafe<ScreenRecorder> { | 69 class ScreenRecorder : public base::RefCountedThreadSafe<ScreenRecorder> { |
64 public: | 70 public: |
65 | 71 |
66 // Construct a ScreenRecorder. Message loops and threads are provided. | 72 // Construct a ScreenRecorder. Message loops and threads are provided. |
67 // This object does not own capturer and encoder. | 73 // This object does not own capturer and encoder. |
68 ScreenRecorder(MessageLoop* capture_loop, | 74 ScreenRecorder(MessageLoop* capture_loop, |
69 MessageLoop* encode_loop, | 75 MessageLoop* encode_loop, |
70 MessageLoop* network_loop, | 76 MessageLoop* network_loop, |
71 Capturer* capturer, | 77 Capturer* capturer, |
72 Encoder* encoder); | 78 Encoder* encoder); |
73 | 79 |
74 virtual ~ScreenRecorder(); | 80 virtual ~ScreenRecorder(); |
75 | 81 |
76 // Start recording. | 82 // Start recording. |
77 void Start(); | 83 void Start(); |
78 | 84 |
79 // Pause the recording session. | 85 // Stop the recording session. |done_task| is executed when recording is fully |
80 void Pause(); | 86 // stopped. This object cannot be used again after |task| is executed. |
87 void Stop(Task* done_task); | |
81 | 88 |
82 // Set the maximum capture rate. This is denoted by number of updates | 89 // Set the maximum capture rate. This is denoted by number of updates |
83 // in one second. The actual system may run in a slower rate than the maximum | 90 // in one second. The actual system may run in a slower rate than the maximum |
84 // rate due to various factors, e.g. capture speed, encode speed and network | 91 // rate due to various factors, e.g. capture speed, encode speed and network |
85 // conditions. | 92 // conditions. |
86 // This method should be called before Start() is called. | 93 // This method should be called before Start() is called. |
87 void SetMaxRate(double rate); | 94 void SetMaxRate(double rate); |
88 | 95 |
89 // Add a connection to this recording session. | 96 // Add a connection to this recording session. |
90 void AddConnection(scoped_refptr<protocol::ConnectionToClient> connection); | 97 void AddConnection(scoped_refptr<protocol::ConnectionToClient> connection); |
91 | 98 |
92 // Remove a connection from receiving screen updates. | 99 // Remove a connection from receiving screen updates. |
93 void RemoveConnection(scoped_refptr<protocol::ConnectionToClient> connection); | 100 void RemoveConnection(scoped_refptr<protocol::ConnectionToClient> connection); |
94 | 101 |
95 // Remove all connections. | 102 // Remove all connections. |
96 void RemoveAllConnections(); | 103 void RemoveAllConnections(); |
97 | 104 |
98 private: | 105 private: |
99 // Getters for capturer and encoder. | 106 // Getters for capturer and encoder. |
100 Capturer* capturer(); | 107 Capturer* capturer(); |
101 Encoder* encoder(); | 108 Encoder* encoder(); |
102 | 109 |
103 // Capturer thread ---------------------------------------------------------- | 110 // Capturer thread ---------------------------------------------------------- |
104 | 111 |
105 void DoStart(); | 112 void DoStart(); |
106 void DoPause(); | 113 void DoStop(Task* done_task); |
114 void DoCompleteStop(Task* done_task); | |
107 | 115 |
108 void DoSetMaxRate(double max_rate); | 116 void DoSetMaxRate(double max_rate); |
109 | 117 |
110 // Hepler method to schedule next capture using the current rate. | 118 // Hepler method to schedule next capture using the current rate. |
111 void StartCaptureTimer(); | 119 void StartCaptureTimer(); |
112 | 120 |
113 void DoCapture(); | 121 void DoCapture(); |
114 void CaptureDoneCallback(scoped_refptr<CaptureData> capture_data); | 122 void CaptureDoneCallback(scoped_refptr<CaptureData> capture_data); |
115 void DoFinishSend(); | 123 void DoFinishOneRecording(); |
116 | 124 |
117 // Network thread ----------------------------------------------------------- | 125 // Network thread ----------------------------------------------------------- |
118 | 126 |
119 // DoSendVideoPacket takes ownership of the |packet| and is responsible | 127 // DoSendVideoPacket takes ownership of the |packet| and is responsible |
120 // for deleting it. | 128 // for deleting it. |
121 void DoSendVideoPacket(VideoPacket* packet); | 129 void DoSendVideoPacket(VideoPacket* packet); |
122 | 130 |
123 void DoSendInit(scoped_refptr<protocol::ConnectionToClient> connection, | 131 void DoSendInit(scoped_refptr<protocol::ConnectionToClient> connection, |
124 int width, int height); | 132 int width, int height); |
125 | 133 |
126 void DoAddConnection(scoped_refptr<protocol::ConnectionToClient> connection); | 134 void DoAddConnection(scoped_refptr<protocol::ConnectionToClient> connection); |
127 void DoRemoveClient(scoped_refptr<protocol::ConnectionToClient> connection); | 135 void DoRemoveClient(scoped_refptr<protocol::ConnectionToClient> connection); |
128 void DoRemoveAllClients(); | 136 void DoRemoveAllClients(); |
129 | 137 |
138 // Signal network thread to cease activities. | |
139 void DoStopOnNetworkThread(Task* done_task); | |
140 | |
130 // Callback for the last packet in one update. Deletes |packet| and | 141 // Callback for the last packet in one update. Deletes |packet| and |
131 // schedules next screen capture. | 142 // schedules next screen capture. |
132 void OnFrameSent(VideoPacket* packet); | 143 void FrameSentCallback(VideoPacket* packet); |
133 | 144 |
134 // Encoder thread ----------------------------------------------------------- | 145 // Encoder thread ----------------------------------------------------------- |
135 | 146 |
136 void DoEncode(scoped_refptr<CaptureData> capture_data); | 147 void DoEncode(scoped_refptr<CaptureData> capture_data); |
137 | 148 |
138 // EncodeDataAvailableTask takes ownership of |packet|. | 149 // Perform stop operations on encode thread. |
139 void EncodeDataAvailableTask(VideoPacket* packet); | 150 void DoStopOnEncodeThread(Task* done_task); |
151 | |
152 // EncodedDataAvailableCallback takes ownership of |packet|. | |
153 void EncodedDataAvailableCallback(VideoPacket* packet); | |
140 void SendVideoPacket(VideoPacket* packet); | 154 void SendVideoPacket(VideoPacket* packet); |
141 | 155 |
142 // Message loops used by this class. | 156 // Message loops used by this class. |
143 MessageLoop* capture_loop_; | 157 MessageLoop* capture_loop_; |
144 MessageLoop* encode_loop_; | 158 MessageLoop* encode_loop_; |
145 MessageLoop* network_loop_; | 159 MessageLoop* network_loop_; |
146 | 160 |
147 // Reference to the capturer. This member is always accessed on the capture | 161 // Reference to the capturer. This member is always accessed on the capture |
148 // thread. | 162 // thread. |
149 scoped_ptr<Capturer> capturer_; | 163 scoped_ptr<Capturer> capturer_; |
150 | 164 |
151 // Reference to the encoder. This member is always accessed on the encode | 165 // Reference to the encoder. This member is always accessed on the encode |
152 // thread. | 166 // thread. |
153 scoped_ptr<Encoder> encoder_; | 167 scoped_ptr<Encoder> encoder_; |
154 | 168 |
155 // A list of clients connected to this hosts. | 169 // A list of clients connected to this hosts. |
156 // This member is always accessed on the NETWORK thread. | 170 // This member is always accessed on the NETWORK thread. |
157 // TODO(hclam): Have to scoped_refptr the clients since they have a shorter | 171 // TODO(hclam): Have to scoped_refptr the clients since they have a shorter |
158 // lifetime than this object. | 172 // lifetime than this object. |
159 typedef std::vector<scoped_refptr<protocol::ConnectionToClient> > | 173 typedef std::vector<scoped_refptr<protocol::ConnectionToClient> > |
160 ConnectionToClientList; | 174 ConnectionToClientList; |
161 ConnectionToClientList connections_; | 175 ConnectionToClientList connections_; |
162 | 176 |
163 // The following members are accessed on the capture thread. | 177 // Flag that indicates recording has been started. |
164 bool started_; | 178 bool started_; |
165 | 179 |
180 // Flag that indicates network is being stopped. | |
181 bool network_stopped_; | |
182 | |
166 // Timer that calls DoCapture. | 183 // Timer that calls DoCapture. |
167 base::RepeatingTimer<ScreenRecorder> capture_timer_; | 184 base::RepeatingTimer<ScreenRecorder> capture_timer_; |
168 | 185 |
169 // Count the number of recordings (i.e. capture or encode) happening. | 186 // Count the number of recordings (i.e. capture or encode) happening. |
170 int recordings_; | 187 int recordings_; |
171 | 188 |
172 // Set to true if we've skipped last capture because there are too | 189 // Set to true if we've skipped last capture because there are too |
173 // many pending frames. | 190 // many pending frames. |
174 int frame_skipped_; | 191 int frame_skipped_; |
175 | 192 |
176 // Number of captures to perform every second. Written on the capture thread. | 193 // Number of captures to perform every second. Written on the capture thread. |
177 double max_rate_; | 194 double max_rate_; |
178 | 195 |
179 DISALLOW_COPY_AND_ASSIGN(ScreenRecorder); | 196 DISALLOW_COPY_AND_ASSIGN(ScreenRecorder); |
180 }; | 197 }; |
181 | 198 |
182 } // namespace remoting | 199 } // namespace remoting |
183 | 200 |
184 #endif // REMOTING_HOST_SCREEN_RECORDER_H_ | 201 #endif // REMOTING_HOST_SCREEN_RECORDER_H_ |
OLD | NEW |