Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_frame_deliverer.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 CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_
7
8 #include <utility>
9 #include <vector>
10
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"
17
18 namespace content {
19
20 // VideoFrameDeliverer is a helper class used for registering
21 // VideoCaptureDeliverFrameCB on the main render thread to receive video frames
22 // on the IO-thread.
23 // Its used by MediaStreamVideoTrack and MediaStreamVideoSource.
24 class VideoFrameDeliverer
25     : public base::RefCountedThreadSafe<VideoFrameDeliverer> {
26  public:
27   explicit VideoFrameDeliverer(
28       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
29
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);
33
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);
39
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);
45
46   const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const {
47     return io_message_loop_;
48   }
49
50  protected:
51   void AddCallbackOnIO(void* id, const VideoCaptureDeliverFrameCB& callback);
52
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);
57
58  protected:
59   virtual ~VideoFrameDeliverer();
60   const base::ThreadChecker& thread_checker() const {
61     return thread_checker_;
62   }
63
64  private:
65   friend class base::RefCountedThreadSafe<VideoFrameDeliverer>;
66
67   // Used to DCHECK that AddCallback and RemoveCallback are called on the main
68   // render thread.
69   base::ThreadChecker thread_checker_;
70   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
71
72   typedef std::pair<void*, VideoCaptureDeliverFrameCB> VideoIdCallbackPair;
73   std::vector<VideoIdCallbackPair> callbacks_;
74
75   DISALLOW_COPY_AND_ASSIGN(VideoFrameDeliverer);
76 };
77
78 }  // namespace content
79
80 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_