Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / remoting / host / video_frame_recorder.h
1 // Copyright 2014 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_VIDEO_FRAME_RECORDER_H_
6 #define REMOTING_HOST_VIDEO_FRAME_RECORDER_H_
7
8 #include <stdint.h>
9 #include <list>
10
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/single_thread_task_runner.h"
15
16 namespace webrtc {
17 class DesktopFrame;
18 }
19
20 namespace remoting {
21
22 class VideoEncoder;
23
24 // Allows sequences of DesktopFrames passed to a VideoEncoder to be recorded.
25 //
26 // VideoFrameRecorder is designed to support applications which use a dedicated
27 // thread for video encoding, but need to manage that process from a "main"
28 // or "control" thread.
29 //
30 // On the control thread:
31 // 1. Create the VideoFrameRecorder.
32 // 2. Specify the amount of memory that may be used for recording.
33 // 3. Call WrapVideoEncoder(), passing the actual VideoEncoder that will be
34 //    used to encode frames.
35 // 4. Hand off the returned wrapper VideoEncoder to the video encoding thread,
36 //    to call in place of the actual VideoEncoder.
37 // 5. Start/stop frame recording as necessary.
38 // 6. Use NextFrame() to read each recorded frame in sequence.
39 //
40 // The wrapper VideoEncoder is designed to be handed off to the video encoding
41 // thread, and used and torn down there.
42 //
43 // The VideoFrameRecorder and VideoEncoder may be torn down in any order; frame
44 // recording will stop as soon as either is destroyed.
45
46 class VideoFrameRecorder {
47  public:
48   VideoFrameRecorder();
49   virtual ~VideoFrameRecorder();
50
51   // Wraps the supplied VideoEncoder, returning a replacement VideoEncoder that
52   // will route frames to the recorder, as well as passing them for encoding.
53   // Each VideoFrameRecorder supports at most one wrapper at a time; if a new
54   // wrapper is required then any existing one must be deleted, or detached via
55   // DetachVideoEncoderWrapper(), before WrapVideoEncoder() is called again.
56   scoped_ptr<VideoEncoder> WrapVideoEncoder(scoped_ptr<VideoEncoder> encoder);
57
58   // Detaches the existing VideoEncoder wrapper, stopping it from recording.
59   // The detached wrapper remains owned by the caller and will continue to
60   // pass-through frames to the wrapped encoder, without recording them.
61   void DetachVideoEncoderWrapper();
62
63   // Enables/disables frame recording. Frame recording is initially disabled.
64   void SetEnableRecording(bool enable_recording);
65
66   // Sets the maximum number of bytes of pixel data that may be recorded.
67   // When this maximum is reached older frames will be discarded to make space
68   // for new ones.
69   void SetMaxContentBytes(int64_t max_content_bytes);
70
71   // Pops the next recorded frame in the sequence, and returns it.
72   scoped_ptr<webrtc::DesktopFrame> NextFrame();
73
74  private:
75   class RecordingVideoEncoder;
76   friend class RecordingVideoEncoder;
77
78   void SetEncoderTaskRunner(scoped_refptr<base::TaskRunner> task_runner);
79   void RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame);
80
81   // The recorded frames, in sequence.
82   std::list<webrtc::DesktopFrame*> recorded_frames_;
83
84   // Size of the recorded frames' content, in bytes.
85   int64_t content_bytes_;
86
87   // Size that recorded frames' content must not exceed.
88   int64_t max_content_bytes_;
89
90   // True if recording is started, false otherwise.
91   bool enable_recording_;
92
93   // Task runner on which the wrapper VideoEncoder is being run.
94   scoped_refptr<base::TaskRunner> encoder_task_runner_;
95
96   // Weak reference to the wrapper VideoEncoder, to use to control it.
97   base::WeakPtr<RecordingVideoEncoder> recording_encoder_;
98
99   scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
100   base::WeakPtrFactory<VideoFrameRecorder> weak_factory_;
101
102   DISALLOW_COPY_AND_ASSIGN(VideoFrameRecorder);
103 };
104
105 }  // namespace remoting
106
107 #endif  // REMOTING_HOST_VIDEO_FRAME_RECORDER_H_