15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef REMOTING_CLIENT_SOFTWARE_VIDEO_RENDERER_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define REMOTING_CLIENT_SOFTWARE_VIDEO_RENDERER_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/ref_counted.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "remoting/client/chromoting_stats.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "remoting/client/frame_consumer_proxy.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "remoting/client/frame_producer.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "remoting/client/video_renderer.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SingleThreadTaskRunner;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace base
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace remoting {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ChromotingStats;
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Implementation of VideoRenderer interface that decodes frame on CPU (on a
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// decode thread) and then passes decoded frames to a FrameConsumer.
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// FrameProducer methods can be called on any thread. All other methods must be
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// called on the main thread. Owned must ensure that this class outlives
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// FrameConsumer (which calls FrameProducer interface).
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SoftwareVideoRenderer : public VideoRenderer,
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              public FrameProducer,
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              public base::NonThreadSafe {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Creates an update decoder on |main_task_runner_| and |decode_task_runner_|,
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // outputting to |consumer|. The |main_task_runner_| is responsible for
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // receiving and queueing packets. The |decode_task_runner_| is responsible
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for decoding the video packets.
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(wez): Replace the ref-counted proxy with an owned FrameConsumer.
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SoftwareVideoRenderer(
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner,
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<FrameConsumerProxy> consumer);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~SoftwareVideoRenderer();
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // VideoRenderer implementation.
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Initialize(const protocol::SessionConfig& config) OVERRIDE;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ChromotingStats* GetStats() OVERRIDE;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ProcessVideoPacket(scoped_ptr<VideoPacket> packet,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  const base::Closure& done) OVERRIDE;
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // FrameProducer implementation. These methods may be called before we are
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Initialize()d, or we know the source screen size. These methods may be
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // called on any thread.
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(sergeyu): On Android a separate display thread is used for drawing.
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // FrameConsumer calls FrameProducer on that thread. Can we avoid having a
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // separate display thread? E.g. can we do everything on the decode thread?
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void DrawBuffer(webrtc::DesktopFrame* buffer) OVERRIDE;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void InvalidateRegion(const webrtc::DesktopRegion& region) OVERRIDE;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void RequestReturnBuffers(const base::Closure& done) OVERRIDE;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetOutputSizeAndClip(
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const webrtc::DesktopSize& view_size,
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const webrtc::DesktopRect& clip_area) OVERRIDE;
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class Core;
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Callback method when a VideoPacket is processed. |decode_start| contains
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the timestamp when the packet will start to be processed.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnPacketDone(base::Time decode_start, const base::Closure& done);
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner_;
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<Core> core_;
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ChromotingStats stats_;
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Keep track of the most recent sequence number bounced back from the host.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int64 latest_sequence_number_;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::WeakPtrFactory<SoftwareVideoRenderer> weak_factory_;
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SoftwareVideoRenderer);
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace remoting
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // REMOTING_CLIENT_SOFTWARE_VIDEO_RENDERER_H_
87