Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_capture_message_filter.h
1 // Copyright (c) 2012 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 // MessageFilter that handles video capture messages and delegates them to
6 // video captures. VideoCaptureMessageFilter is operated on IO thread of
7 // render process. It intercepts video capture messages and process them on
8 // IO thread since these messages are time critical.
9
10 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_
11 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_
12
13 #include <map>
14
15 #include "base/memory/shared_memory.h"
16 #include "content/common/content_export.h"
17 #include "content/common/media/video_capture.h"
18 #include "ipc/message_filter.h"
19 #include "media/video/capture/video_capture_types.h"
20
21 namespace gpu {
22 struct MailboxHolder;
23 }  // namespace gpu
24
25 namespace content {
26
27 class CONTENT_EXPORT VideoCaptureMessageFilter : public IPC::MessageFilter {
28  public:
29   class CONTENT_EXPORT Delegate {
30    public:
31     // Called when a video frame buffer is created in the browser process.
32     virtual void OnBufferCreated(base::SharedMemoryHandle handle,
33                                  int length,
34                                  int buffer_id) = 0;
35
36     virtual void OnBufferDestroyed(int buffer_id) = 0;
37
38     // Called when a video frame buffer is received from the browser process.
39     virtual void OnBufferReceived(int buffer_id,
40                                   const media::VideoCaptureFormat& format,
41                                   const gfx::Rect& visible_rect,
42                                   base::TimeTicks timestamp) = 0;
43
44     // Called when a video mailbox buffer is received from the browser process.
45     virtual void OnMailboxBufferReceived(
46         int buffer_id,
47         const gpu::MailboxHolder& mailbox_holder,
48         const media::VideoCaptureFormat& format,
49         base::TimeTicks timestamp) = 0;
50
51     // Called when state of a video capture device has changed in the browser
52     // process.
53     virtual void OnStateChanged(VideoCaptureState state) = 0;
54
55     // Called upon reception of device's supported formats back from browser.
56     virtual void OnDeviceSupportedFormatsEnumerated(
57         const media::VideoCaptureFormats& supported_formats) = 0;
58
59     // Called upon reception of format(s) in use by a device back from browser.
60     virtual void OnDeviceFormatsInUseReceived(
61         const media::VideoCaptureFormats& formats_in_use) = 0;
62
63     // Called when the delegate has been added to filter's delegate list.
64     // |device_id| is the device id for the delegate.
65     virtual void OnDelegateAdded(int32 device_id) = 0;
66
67    protected:
68     virtual ~Delegate() {}
69   };
70
71   VideoCaptureMessageFilter();
72
73   // Add a delegate to the map.
74   void AddDelegate(Delegate* delegate);
75
76   // Remove a delegate from the map.
77   void RemoveDelegate(Delegate* delegate);
78
79   // Send a message asynchronously.
80   virtual bool Send(IPC::Message* message);
81
82   // IPC::MessageFilter override. Called on IO thread.
83   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
84   virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
85   virtual void OnFilterRemoved() OVERRIDE;
86   virtual void OnChannelClosing() OVERRIDE;
87
88  protected:
89   virtual ~VideoCaptureMessageFilter();
90
91  private:
92   typedef std::map<int32, Delegate*> Delegates;
93
94   // Receive a newly created buffer from browser process.
95   void OnBufferCreated(int device_id,
96                        base::SharedMemoryHandle handle,
97                        int length,
98                        int buffer_id);
99
100   // Release a buffer received by OnBufferCreated.
101   void OnBufferDestroyed(int device_id,
102                          int buffer_id);
103
104   // Receive a filled buffer from browser process.
105   void OnBufferReceived(int device_id,
106                         int buffer_id,
107                         const media::VideoCaptureFormat& format,
108                         const gfx::Rect& visible_rect,
109                         base::TimeTicks timestamp);
110
111   // Receive a filled texture mailbox buffer from browser process.
112   void OnMailboxBufferReceived(int device_id,
113                                int buffer_id,
114                                const gpu::MailboxHolder& mailbox_holder,
115                                const media::VideoCaptureFormat& format,
116                                base::TimeTicks timestamp);
117
118   // State of browser process' video capture device has changed.
119   void OnDeviceStateChanged(int device_id, VideoCaptureState state);
120
121   // Receive a device's supported formats back from browser process.
122   void OnDeviceSupportedFormatsEnumerated(
123       int device_id,
124       const media::VideoCaptureFormats& supported_formats);
125
126   // Receive the formats in-use by a device back from browser process.
127   void OnDeviceFormatsInUseReceived(
128       int device_id,
129       const media::VideoCaptureFormats& formats_in_use);
130
131   // Finds the delegate associated with |device_id|, NULL if not found.
132   Delegate* find_delegate(int device_id) const;
133
134   // A map of device ids to delegates.
135   Delegates delegates_;
136   Delegates pending_delegates_;
137   int32 last_device_id_;
138
139   IPC::Sender* sender_;
140
141   DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilter);
142 };
143
144 }  // namespace content
145
146 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_