Upstream version 6.35.121.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/ipc_channel_proxy.h"
19 #include "media/video/capture/video_capture.h"
20
21 namespace gpu {
22 struct MailboxHolder;
23 }  // namespace gpu
24
25 namespace content {
26
27 class CONTENT_EXPORT VideoCaptureMessageFilter
28     : public IPC::ChannelProxy::MessageFilter {
29  public:
30   class CONTENT_EXPORT Delegate {
31    public:
32     // Called when a video frame buffer is created in the browser process.
33     virtual void OnBufferCreated(base::SharedMemoryHandle handle,
34                                  int length,
35                                  int buffer_id) = 0;
36
37     virtual void OnBufferDestroyed(int buffer_id) = 0;
38
39     // Called when a video frame buffer is received from the browser process.
40     virtual void OnBufferReceived(int buffer_id,
41                                   const media::VideoCaptureFormat& format,
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::ChannelProxy::MessageFilter override. Called on IO thread.
83   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
84   virtual void OnFilterAdded(IPC::Channel* channel) 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                         base::TimeTicks timestamp);
109
110   // Receive a filled texture mailbox buffer from browser process.
111   void OnMailboxBufferReceived(int device_id,
112                                int buffer_id,
113                                const gpu::MailboxHolder& mailbox_holder,
114                                const media::VideoCaptureFormat& format,
115                                base::TimeTicks timestamp);
116
117   // State of browser process' video capture device has changed.
118   void OnDeviceStateChanged(int device_id, VideoCaptureState state);
119
120   // Receive a device's supported formats back from browser process.
121   void OnDeviceSupportedFormatsEnumerated(
122       int device_id,
123       const media::VideoCaptureFormats& supported_formats);
124
125   // Receive the formats in-use by a device back from browser process.
126   void OnDeviceFormatsInUseReceived(
127       int device_id,
128       const media::VideoCaptureFormats& formats_in_use);
129
130   // Finds the delegate associated with |device_id|, NULL if not found.
131   Delegate* find_delegate(int device_id) const;
132
133   // A map of device ids to delegates.
134   Delegates delegates_;
135   Delegates pending_delegates_;
136   int32 last_device_id_;
137
138   IPC::Channel* channel_;
139
140   DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilter);
141 };
142
143 }  // namespace content
144
145 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_