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.
5 #ifndef REMOTING_HOST_VIDEO_FRAME_RECORDER_H_
6 #define REMOTING_HOST_VIDEO_FRAME_RECORDER_H_
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"
24 // Allows sequences of DesktopFrames passed to a VideoEncoder to be recorded.
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.
30 // On the control thread:
31 // 1. Create the VideoFrameRecorder on the controlling thread.
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 the returned wrapper VideoEncoder of 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.
40 // The wrapper VideoEncoder is designed to be handed off to the video encoding
41 // thread, and used and torn down there.
43 // The VideoFrameRecorder and VideoEncoder may be torn down in any order; frame
44 // recording will stop as soon as either is destroyed.
46 class VideoFrameRecorder
{
49 virtual ~VideoFrameRecorder();
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 // The caller must delete the previous recording VideoEncoder, or call
54 // DetachVideoEncoderWrapper() before calling WrapVideoEncoder() to create
56 scoped_ptr
<VideoEncoder
> WrapVideoEncoder(scoped_ptr
<VideoEncoder
> encoder
);
58 // Detaches the existing VideoEncoder wrapper, stopping it from recording.
59 void DetachVideoEncoderWrapper();
61 // Enables/disables frame recording. Frame recording is initially disabled.
62 void SetEnableRecording(bool enable_recording
);
64 // Sets the maximum number of bytes of pixel data that may be recorded.
65 // When this maximum is reached older frames will be discard to make space
67 void SetMaxContentBytes(int64_t max_content_bytes
);
69 // Pops the next recorded frame in the sequence, and returns it.
70 scoped_ptr
<webrtc::DesktopFrame
> NextFrame();
73 class RecordingVideoEncoder
;
74 friend class RecordingVideoEncoder
;
76 void SetEncoderTaskRunner(scoped_refptr
<base::TaskRunner
> task_runner
);
77 void RecordFrame(scoped_ptr
<webrtc::DesktopFrame
> frame
);
79 // The recorded frames, in sequence.
80 std::list
<webrtc::DesktopFrame
*> recorded_frames_
;
82 // Size of the recorded frames' content, in bytes.
83 int64_t content_bytes_
;
85 // Size that recorded frames' content must not exceed.
86 int64_t max_content_bytes_
;
88 // True if recording is started, false otherwise.
89 bool enable_recording_
;
91 // Task runner on which the wrapper VideoEncoder is being run.
92 scoped_refptr
<base::TaskRunner
> encoder_task_runner_
;
94 // Weak reference to the wrapper VideoEncoder, to use to control it.
95 base::WeakPtr
<RecordingVideoEncoder
> recording_encoder_
;
97 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner_
;
98 base::WeakPtrFactory
<VideoFrameRecorder
> weak_factory_
;
100 DISALLOW_COPY_AND_ASSIGN(VideoFrameRecorder
);
103 } // namespace remoting
105 #endif // REMOTING_HOST_VIDEO_FRAME_RECORDER_H_