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.
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.
10 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_
11 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_
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"
27 class CONTENT_EXPORT VideoCaptureMessageFilter
28 : public IPC::ChannelProxy::MessageFilter {
30 class CONTENT_EXPORT Delegate {
32 // Called when a video frame buffer is created in the browser process.
33 virtual void OnBufferCreated(base::SharedMemoryHandle handle,
37 virtual void OnBufferDestroyed(int buffer_id) = 0;
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;
44 // Called when a video mailbox buffer is received from the browser process.
45 virtual void OnMailboxBufferReceived(
47 const gpu::MailboxHolder& mailbox_holder,
48 const media::VideoCaptureFormat& format,
49 base::TimeTicks timestamp) = 0;
51 // Called when state of a video capture device has changed in the browser
53 virtual void OnStateChanged(VideoCaptureState state) = 0;
55 // Called upon reception of device's supported formats back from browser.
56 virtual void OnDeviceSupportedFormatsEnumerated(
57 const media::VideoCaptureFormats& supported_formats) = 0;
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;
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;
68 virtual ~Delegate() {}
71 VideoCaptureMessageFilter();
73 // Add a delegate to the map.
74 void AddDelegate(Delegate* delegate);
76 // Remove a delegate from the map.
77 void RemoveDelegate(Delegate* delegate);
79 // Send a message asynchronously.
80 virtual bool Send(IPC::Message* message);
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;
89 virtual ~VideoCaptureMessageFilter();
92 typedef std::map<int32, Delegate*> Delegates;
94 // Receive a newly created buffer from browser process.
95 void OnBufferCreated(int device_id,
96 base::SharedMemoryHandle handle,
100 // Release a buffer received by OnBufferCreated.
101 void OnBufferDestroyed(int device_id,
104 // Receive a filled buffer from browser process.
105 void OnBufferReceived(int device_id,
107 const media::VideoCaptureFormat& format,
108 base::TimeTicks timestamp);
110 // Receive a filled texture mailbox buffer from browser process.
111 void OnMailboxBufferReceived(int device_id,
113 const gpu::MailboxHolder& mailbox_holder,
114 const media::VideoCaptureFormat& format,
115 base::TimeTicks timestamp);
117 // State of browser process' video capture device has changed.
118 void OnDeviceStateChanged(int device_id, VideoCaptureState state);
120 // Receive a device's supported formats back from browser process.
121 void OnDeviceSupportedFormatsEnumerated(
123 const media::VideoCaptureFormats& supported_formats);
125 // Receive the formats in-use by a device back from browser process.
126 void OnDeviceFormatsInUseReceived(
128 const media::VideoCaptureFormats& formats_in_use);
130 // Finds the delegate associated with |device_id|, NULL if not found.
131 Delegate* find_delegate(int device_id) const;
133 // A map of device ids to delegates.
134 Delegates delegates_;
135 Delegates pending_delegates_;
136 int32 last_device_id_;
138 IPC::Channel* channel_;
140 DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilter);
143 } // namespace content
145 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_