a1f4b9d4f6a80afff2db7d656000812c7031224e
[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 "content/public/renderer/media_stream_video_sink.h"
17 #include "media/base/video_frame.h"
18
19 namespace content {
20
21 // VideoFrameDeliverer is a helper class used for registering
22 // VideoCaptureDeliverFrameCB on the main render thread to receive video frames
23 // on the IO-thread.
24 // Its used by MediaStreamVideoTrack.
25 class VideoFrameDeliverer
26     : public base::RefCountedThreadSafe<VideoFrameDeliverer> {
27  public:
28   explicit VideoFrameDeliverer(
29       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
30
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);
34
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);
40
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);
47
48   const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const {
49     return io_message_loop_;
50   }
51
52  protected:
53   void AddCallbackOnIO(void* id, const VideoCaptureDeliverFrameCB& callback);
54
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);
59
60  protected:
61   virtual ~VideoFrameDeliverer();
62   const base::ThreadChecker& thread_checker() const {
63     return thread_checker_;
64   }
65
66  private:
67   friend class base::RefCountedThreadSafe<VideoFrameDeliverer>;
68
69   // Used to DCHECK that AddCallback and RemoveCallback are called on the main
70   // render thread.
71   base::ThreadChecker thread_checker_;
72   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
73
74   typedef std::pair<void*, VideoCaptureDeliverFrameCB> VideoIdCallbackPair;
75   std::vector<VideoIdCallbackPair> callbacks_;
76
77   DISALLOW_COPY_AND_ASSIGN(VideoFrameDeliverer);
78 };
79
80 }  // namespace content
81
82 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_FRAME_DELIVERER_H_