Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 15 | #include "base/memory/shared_memory.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 16 | #include "base/message_loop/message_loop_proxy.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | #include "content/common/content_export.h" |
| 18 | #include "content/common/media/video_capture.h" |
| 19 | #include "ipc/ipc_channel_proxy.h" |
| 20 | #include "media/video/capture/video_capture.h" |
| 21 | |
| 22 | namespace content { |
| 23 | |
| 24 | class CONTENT_EXPORT VideoCaptureMessageFilter |
| 25 | : public IPC::ChannelProxy::MessageFilter { |
| 26 | public: |
| 27 | class CONTENT_EXPORT Delegate { |
| 28 | public: |
| 29 | // Called when a video frame buffer is created in the browser process. |
| 30 | virtual void OnBufferCreated(base::SharedMemoryHandle handle, |
| 31 | int length, int buffer_id) = 0; |
| 32 | |
| 33 | // Called when a video frame buffer is received from the browser process. |
| 34 | virtual void OnBufferReceived(int buffer_id, base::Time timestamp) = 0; |
| 35 | |
| 36 | // Called when state of a video capture device has changed in the browser |
| 37 | // process. |
| 38 | virtual void OnStateChanged(VideoCaptureState state) = 0; |
| 39 | |
| 40 | // Called when device info is received from video capture device in the |
| 41 | // browser process. |
| 42 | virtual void OnDeviceInfoReceived( |
| 43 | const media::VideoCaptureParams& device_info) = 0; |
| 44 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 45 | // Called when newly changed device info is received from video capture |
| 46 | // device in the browser process. |
| 47 | virtual void OnDeviceInfoChanged( |
| 48 | const media::VideoCaptureParams& device_info) {}; |
| 49 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | // Called when the delegate has been added to filter's delegate list. |
| 51 | // |device_id| is the device id for the delegate. |
| 52 | virtual void OnDelegateAdded(int32 device_id) = 0; |
| 53 | |
| 54 | protected: |
| 55 | virtual ~Delegate() {} |
| 56 | }; |
| 57 | |
| 58 | VideoCaptureMessageFilter(); |
| 59 | |
| 60 | // Add a delegate to the map. |
| 61 | void AddDelegate(Delegate* delegate); |
| 62 | |
| 63 | // Remove a delegate from the map. |
| 64 | void RemoveDelegate(Delegate* delegate); |
| 65 | |
| 66 | // Send a message asynchronously. |
| 67 | virtual bool Send(IPC::Message* message); |
| 68 | |
| 69 | // IPC::ChannelProxy::MessageFilter override. Called on IO thread. |
| 70 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 71 | virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE; |
| 72 | virtual void OnFilterRemoved() OVERRIDE; |
| 73 | virtual void OnChannelClosing() OVERRIDE; |
| 74 | |
| 75 | protected: |
| 76 | virtual ~VideoCaptureMessageFilter(); |
| 77 | |
| 78 | private: |
| 79 | FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Basic); |
| 80 | FRIEND_TEST_ALL_PREFIXES(VideoCaptureMessageFilterTest, Delegates); |
| 81 | |
| 82 | typedef std::map<int32, Delegate*> Delegates; |
| 83 | |
| 84 | // Receive a newly created buffer from browser process. |
| 85 | void OnBufferCreated(int device_id, |
| 86 | base::SharedMemoryHandle handle, |
| 87 | int length, int buffer_id); |
| 88 | |
| 89 | // Receive a buffer from browser process. |
| 90 | void OnBufferReceived(int device_id, |
| 91 | int buffer_id, base::Time timestamp); |
| 92 | |
| 93 | // State of browser process' video capture device has changed. |
| 94 | void OnDeviceStateChanged(int device_id, VideoCaptureState state); |
| 95 | |
| 96 | // Receive device info from browser process. |
| 97 | void OnDeviceInfoReceived(int device_id, |
| 98 | const media::VideoCaptureParams& params); |
| 99 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 100 | // Finds the delegate associated with |device_id|, NULL if not found. |
| 101 | Delegate* find_delegate(int device_id) const; |
| 102 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | // A map of device ids to delegates. |
| 104 | Delegates delegates_; |
| 105 | Delegates pending_delegates_; |
| 106 | int32 last_device_id_; |
| 107 | |
| 108 | IPC::Channel* channel_; |
| 109 | |
| 110 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(VideoCaptureMessageFilter); |
| 113 | }; |
| 114 | |
| 115 | } // namespace content |
| 116 | |
| 117 | #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_MESSAGE_FILTER_H_ |