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 "media/base/video_frame.h"
20 // VideoFrameDeliverer is a helper class used for registering
21 // VideoCaptureDeliverFrameCB on the main render thread to receive video frames
23 // Its used by MediaStreamVideoTrack and MediaStreamVideoSource.
24 class VideoFrameDeliverer
25 : public base::RefCountedThreadSafe<VideoFrameDeliverer> {
27 explicit VideoFrameDeliverer(
28 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
30 // Add |callback| to receive video frames on the IO-thread.
31 // Must be called on the main render thread.
32 void AddCallback(void* id, const VideoCaptureDeliverFrameCB& callback);
34 // Removes |callback| associated with |id| from receiving video frames if |id|
35 // has been added. It is ok to call RemoveCallback even if the |id| has not
36 // been added. Note that the added callback will be reset on the main thread.
37 // Must be called on the main render thread.
38 void RemoveCallback(void* id);
40 // Triggers all registered callbacks with |frame| and |format| as parameters.
41 // Must be called on the IO-thread.
42 virtual void DeliverFrameOnIO(
43 const scoped_refptr<media::VideoFrame>& frame,
44 const media::VideoCaptureFormat& format);
46 const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const {
47 return io_message_loop_;
51 void AddCallbackOnIO(void* id, const VideoCaptureDeliverFrameCB& callback);
53 // Callback will be removed and then reset on the designated message loop.
54 // It is ok to call RemoveCallback even if |id| has not been added.
55 void RemoveCallbackOnIO(
56 void* id, const scoped_refptr<base::MessageLoopProxy>& message_loop);
59 virtual ~VideoFrameDeliverer();
60 const base::ThreadChecker& thread_checker() const {
61 return thread_checker_;
65 friend class base::RefCountedThreadSafe<VideoFrameDeliverer>;
67 // Used to DCHECK that AddCallback and RemoveCallback are called on the main
69 base::ThreadChecker thread_checker_;
70 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
72 typedef std::pair<void*, VideoCaptureDeliverFrameCB> VideoIdCallbackPair;
73 std::vector<VideoIdCallbackPair> callbacks_;
75 DISALLOW_COPY_AND_ASSIGN(VideoFrameDeliverer);
78 } // namespace content
80 #endif // CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_