Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_capture_message_filter.cc
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 #include "content/renderer/media/video_capture_message_filter.h"
6
7 #include "content/common/media/video_capture_messages.h"
8 #include "content/common/view_messages.h"
9
10 namespace content {
11
12 VideoCaptureMessageFilter::VideoCaptureMessageFilter()
13     : last_device_id_(0),
14       channel_(NULL) {
15 }
16
17 void VideoCaptureMessageFilter::AddDelegate(Delegate* delegate) {
18   if (++last_device_id_ <= 0)
19     last_device_id_ = 1;
20   while (delegates_.find(last_device_id_) != delegates_.end())
21     last_device_id_++;
22
23   if (channel_) {
24     delegates_[last_device_id_] = delegate;
25     delegate->OnDelegateAdded(last_device_id_);
26   } else {
27     pending_delegates_[last_device_id_] = delegate;
28   }
29 }
30
31 void VideoCaptureMessageFilter::RemoveDelegate(Delegate* delegate) {
32   for (Delegates::iterator it = delegates_.begin();
33        it != delegates_.end(); it++) {
34     if (it->second == delegate) {
35       delegates_.erase(it);
36       break;
37     }
38   }
39   for (Delegates::iterator it = pending_delegates_.begin();
40        it != pending_delegates_.end(); it++) {
41     if (it->second == delegate) {
42       pending_delegates_.erase(it);
43       break;
44     }
45   }
46 }
47
48 bool VideoCaptureMessageFilter::Send(IPC::Message* message) {
49   if (!channel_) {
50     delete message;
51     return false;
52   }
53
54   return channel_->Send(message);
55 }
56
57 bool VideoCaptureMessageFilter::OnMessageReceived(const IPC::Message& message) {
58   bool handled = true;
59   IPC_BEGIN_MESSAGE_MAP(VideoCaptureMessageFilter, message)
60     IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferReceived)
61     IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnDeviceStateChanged)
62     IPC_MESSAGE_HANDLER(VideoCaptureMsg_NewBuffer, OnBufferCreated)
63     IPC_MESSAGE_HANDLER(VideoCaptureMsg_FreeBuffer, OnBufferDestroyed)
64     IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceSupportedFormatsEnumerated,
65                         OnDeviceSupportedFormatsEnumerated)
66     IPC_MESSAGE_UNHANDLED(handled = false)
67   IPC_END_MESSAGE_MAP()
68   return handled;
69 }
70
71 void VideoCaptureMessageFilter::OnFilterAdded(IPC::Channel* channel) {
72   DVLOG(1) << "VideoCaptureMessageFilter::OnFilterAdded()";
73   channel_ = channel;
74
75   for (Delegates::iterator it = pending_delegates_.begin();
76        it != pending_delegates_.end(); it++) {
77     it->second->OnDelegateAdded(it->first);
78     delegates_[it->first] = it->second;
79   }
80   pending_delegates_.clear();
81 }
82
83 void VideoCaptureMessageFilter::OnFilterRemoved() {
84   channel_ = NULL;
85 }
86
87 void VideoCaptureMessageFilter::OnChannelClosing() {
88   channel_ = NULL;
89 }
90
91 VideoCaptureMessageFilter::~VideoCaptureMessageFilter() {}
92
93 VideoCaptureMessageFilter::Delegate* VideoCaptureMessageFilter::find_delegate(
94     int device_id) const {
95   Delegates::const_iterator i = delegates_.find(device_id);
96   return i != delegates_.end() ? i->second : NULL;
97 }
98
99 void VideoCaptureMessageFilter::OnBufferCreated(
100     int device_id,
101     base::SharedMemoryHandle handle,
102     int length,
103     int buffer_id) {
104   Delegate* delegate = find_delegate(device_id);
105   if (!delegate) {
106     DLOG(WARNING) << "OnBufferCreated: Got video frame buffer for a "
107         "non-existent or removed video capture.";
108
109     // Send the buffer back to Host in case it's waiting for all buffers
110     // to be returned.
111     base::SharedMemory::CloseHandle(handle);
112     Send(new VideoCaptureHostMsg_BufferReady(device_id, buffer_id));
113     return;
114   }
115
116   delegate->OnBufferCreated(handle, length, buffer_id);
117 }
118
119 void VideoCaptureMessageFilter::OnBufferReceived(
120     int device_id,
121     int buffer_id,
122     base::TimeTicks timestamp,
123     const media::VideoCaptureFormat& format) {
124   Delegate* delegate = find_delegate(device_id);
125   if (!delegate) {
126     DLOG(WARNING) << "OnBufferReceived: Got video frame buffer for a "
127         "non-existent or removed video capture.";
128
129     // Send the buffer back to Host in case it's waiting for all buffers
130     // to be returned.
131     Send(new VideoCaptureHostMsg_BufferReady(device_id, buffer_id));
132     return;
133   }
134
135   delegate->OnBufferReceived(buffer_id, timestamp, format);
136 }
137
138 void VideoCaptureMessageFilter::OnBufferDestroyed(
139     int device_id,
140     int buffer_id) {
141   Delegate* delegate = find_delegate(device_id);
142   if (!delegate) {
143     DLOG(WARNING) << "OnBufferDestroyed: Instructed to free buffer for a "
144         "non-existent or removed video capture.";
145     return;
146   }
147
148   delegate->OnBufferDestroyed(buffer_id);
149 }
150
151 void VideoCaptureMessageFilter::OnDeviceStateChanged(
152     int device_id,
153     VideoCaptureState state) {
154   Delegate* delegate = find_delegate(device_id);
155   if (!delegate) {
156     DLOG(WARNING) << "OnDeviceStateChanged: Got video capture event for a "
157         "non-existent or removed video capture.";
158     return;
159   }
160   delegate->OnStateChanged(state);
161 }
162
163 void VideoCaptureMessageFilter::OnDeviceSupportedFormatsEnumerated(
164     int device_id,
165     const media::VideoCaptureFormats& supported_formats) {
166   Delegate* delegate = find_delegate(device_id);
167   if (!delegate) {
168     DLOG(WARNING) << "OnDeviceFormatsEnumerated: unknown device";
169     return;
170   }
171   delegate->OnDeviceSupportedFormatsEnumerated(supported_formats);
172 }
173
174 }  // namespace content