- add sources.
[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 content {
22
23 class CONTENT_EXPORT VideoCaptureMessageFilter
24     : public IPC::ChannelProxy::MessageFilter {
25  public:
26   class CONTENT_EXPORT Delegate {
27    public:
28     // Called when a video frame buffer is created in the browser process.
29     virtual void OnBufferCreated(base::SharedMemoryHandle handle,
30                                  int length,
31                                  int buffer_id) = 0;
32
33     virtual void OnBufferDestroyed(int buffer_id) = 0;
34
35     // Called when a video frame buffer is received from the browser process.
36     virtual void OnBufferReceived(int buffer_id,
37                                   base::Time timestamp,
38                                   const media::VideoCaptureFormat& format) = 0;
39
40     // Called when state of a video capture device has changed in the browser
41     // process.
42     virtual void OnStateChanged(VideoCaptureState state) = 0;
43
44     // Called when the delegate has been added to filter's delegate list.
45     // |device_id| is the device id for the delegate.
46     virtual void OnDelegateAdded(int32 device_id) = 0;
47
48    protected:
49     virtual ~Delegate() {}
50   };
51
52   VideoCaptureMessageFilter();
53
54   // Add a delegate to the map.
55   void AddDelegate(Delegate* delegate);
56
57   // Remove a delegate from the map.
58   void RemoveDelegate(Delegate* delegate);
59
60   // Send a message asynchronously.
61   virtual bool Send(IPC::Message* message);
62
63   // IPC::ChannelProxy::MessageFilter override. Called on IO thread.
64   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
65   virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
66   virtual void OnFilterRemoved() OVERRIDE;
67   virtual void OnChannelClosing() OVERRIDE;
68
69  protected:
70   virtual ~VideoCaptureMessageFilter();
71
72  private:
73   typedef std::map<int32, Delegate*> Delegates;
74
75   // Receive a newly created buffer from browser process.
76   void OnBufferCreated(int device_id,
77                        base::SharedMemoryHandle handle,
78                        int length,
79                        int buffer_id);
80
81   // Release a buffer received by OnBufferCreated.
82   void OnBufferDestroyed(int device_id,
83                          int buffer_id);
84
85   // Receive a filled buffer from browser process.
86   void OnBufferReceived(int device_id,
87                         int buffer_id,
88                         base::Time timestamp,
89                         const media::VideoCaptureFormat& format);
90
91   // State of browser process' video capture device has changed.
92   void OnDeviceStateChanged(int device_id, VideoCaptureState state);
93
94   // Finds the delegate associated with |device_id|, NULL if not found.
95   Delegate* find_delegate(int device_id) const;
96
97   // A map of device ids to delegates.
98   Delegates delegates_;
99   Delegates pending_delegates_;
100   int32 last_device_id_;
101
102   IPC::Channel* channel_;
103
104   DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilter);
105 };
106
107 }  // namespace content
108
109 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_