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 CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/threading/thread_checker.h"
15 #include "content/common/media/video_capture.h"
16 #include "content/public/renderer/media_stream_video_sink.h"
17 #include "media/base/video_frame.h"
21 // VideoFrameDeliverer is a helper class used for registering
22 // VideoCaptureDeliverFrameCB on the main render thread to receive video frames
24 // Its used by MediaStreamVideoTrack.
25 class VideoFrameDeliverer
26 : public base::RefCountedThreadSafe<VideoFrameDeliverer> {
28 explicit VideoFrameDeliverer(
29 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
31 // Add |callback| to receive video frames on the IO-thread.
32 // Must be called on the main render thread.
33 void AddCallback(void* id, const VideoCaptureDeliverFrameCB& callback);
35 // Removes |callback| associated with |id| from receiving video frames if |id|
36 // has been added. It is ok to call RemoveCallback even if the |id| has not
37 // been added. Note that the added callback will be reset on the main thread.
38 // Must be called on the main render thread.
39 void RemoveCallback(void* id);
41 // Triggers all registered callbacks with |frame|, |format| and
42 // |estimated_capture_time| as parameters. Must be called on the IO-thread.
43 virtual void DeliverFrameOnIO(
44 const scoped_refptr<media::VideoFrame>& frame,
45 const media::VideoCaptureFormat& format,
46 const base::TimeTicks& estimated_capture_time);
48 const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const {
49 return io_message_loop_;
53 void AddCallbackOnIO(void* id, const VideoCaptureDeliverFrameCB& callback);
55 // Callback will be removed and then reset on the designated message loop.
56 // It is ok to call RemoveCallback even if |id| has not been added.
57 void RemoveCallbackOnIO(
58 void* id, const scoped_refptr<base::MessageLoopProxy>& message_loop);
61 virtual ~VideoFrameDeliverer();
62 const base::ThreadChecker& thread_checker() const {
63 return thread_checker_;
67 friend class base::RefCountedThreadSafe<VideoFrameDeliverer>;
69 // Used to DCHECK that AddCallback and RemoveCallback are called on the main
71 base::ThreadChecker thread_checker_;
72 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
74 typedef std::pair<void*, VideoCaptureDeliverFrameCB> VideoIdCallbackPair;
75 std::vector<VideoIdCallbackPair> callbacks_;
77 DISALLOW_COPY_AND_ASSIGN(VideoFrameDeliverer);
80 } // namespace content
82 #endif // CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_