1// Copyright (c) 2012 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_CLIENT_RECTANGLE_UPDATE_DECODER_H_
6#define REMOTING_CLIENT_RECTANGLE_UPDATE_DECODER_H_
7
8#include <list>
9
10#include "base/callback_forward.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "remoting/codec/video_decoder.h"
14#include "remoting/client/chromoting_stats.h"
15#include "remoting/client/frame_consumer_proxy.h"
16#include "remoting/client/frame_producer.h"
17#include "remoting/protocol/video_stub.h"
18
19namespace base {
20class SingleThreadTaskRunner;
21}  // namespace base
22
23namespace remoting {
24
25class ChromotingStats;
26
27namespace protocol {
28class SessionConfig;
29}  // namespace protocol
30
31// TODO(ajwong): Re-examine this API, especially with regards to how error
32// conditions on each step are reported.  Should they be CHECKs? Logs? Other?
33// TODO(sergeyu): Rename this class.
34class RectangleUpdateDecoder
35    : public base::RefCountedThreadSafe<RectangleUpdateDecoder>,
36      public FrameProducer,
37      public protocol::VideoStub {
38 public:
39  // Creates an update decoder on |main_task_runner_| and |decode_task_runner_|,
40  // outputting to |consumer|. The |main_task_runner_| is responsible for
41  // receiving and queueing packets. The |decode_task_runner_| is responsible
42  // for decoding the video packets.
43  // TODO(wez): Replace the ref-counted proxy with an owned FrameConsumer.
44  RectangleUpdateDecoder(
45      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
46      scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner,
47      scoped_refptr<FrameConsumerProxy> consumer);
48
49  // Initializes decoder with the information from the protocol config.
50  void Initialize(const protocol::SessionConfig& config);
51
52  // FrameProducer implementation.  These methods may be called before we are
53  // Initialize()d, or we know the source screen size.
54  virtual void DrawBuffer(webrtc::DesktopFrame* buffer) OVERRIDE;
55  virtual void InvalidateRegion(const SkRegion& region) OVERRIDE;
56  virtual void RequestReturnBuffers(const base::Closure& done) OVERRIDE;
57  virtual void SetOutputSizeAndClip(const SkISize& view_size,
58                                    const SkIRect& clip_area) OVERRIDE;
59  virtual const SkRegion* GetBufferShape() OVERRIDE;
60
61  // VideoStub implementation.
62  virtual void ProcessVideoPacket(scoped_ptr<VideoPacket> packet,
63                                  const base::Closure& done) OVERRIDE;
64
65  // Return the stats recorded by this client.
66  ChromotingStats* GetStats();
67
68 private:
69  friend class base::RefCountedThreadSafe<RectangleUpdateDecoder>;
70  virtual ~RectangleUpdateDecoder();
71
72  // Paints the invalidated region to the next available buffer and returns it
73  // to the consumer.
74  void SchedulePaint();
75  void DoPaint();
76
77  // Decodes the contents of |packet|. DecodePacket may keep a reference to
78  // |packet| so the |packet| must remain alive and valid until |done| is
79  // executed.
80  void DecodePacket(scoped_ptr<VideoPacket> packet, const base::Closure& done);
81
82  // Callback method when a VideoPacket is processed.
83  // If |last_packet| is true then |decode_start| contains the timestamp when
84  // the packet will start to be processed.
85  void OnPacketDone(bool last_packet,
86                    base::Time decode_start,
87                    const base::Closure& done);
88
89  scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
90  scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner_;
91  scoped_refptr<FrameConsumerProxy> consumer_;
92  scoped_ptr<VideoDecoder> decoder_;
93
94  // Remote screen size in pixels.
95  SkISize source_size_;
96
97  // Vertical and horizontal DPI of the remote screen.
98  SkIPoint source_dpi_;
99
100  // The current dimensions of the frame consumer view.
101  SkISize view_size_;
102  SkIRect clip_area_;
103
104  // The drawing buffers supplied by the frame consumer.
105  std::list<webrtc::DesktopFrame*> buffers_;
106
107  // Flag used to coalesce runs of SchedulePaint()s into a single DoPaint().
108  bool paint_scheduled_;
109
110  ChromotingStats stats_;
111
112  // Keep track of the most recent sequence number bounced back from the host.
113  int64 latest_sequence_number_;
114};
115
116}  // namespace remoting
117
118#endif  // REMOTING_CLIENT_RECTANGLE_UPDATE_DECODER_H_
119