Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_media_stream_track_host_base.cc
1 // Copyright 2014 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/pepper/pepper_media_stream_track_host_base.h"
6
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "content/public/renderer/renderer_ppapi_host.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/host/dispatch_host_message.h"
13 #include "ppapi/host/host_message_context.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/shared_impl/media_stream_buffer.h"
17
18 using ppapi::host::HostMessageContext;
19 using ppapi::proxy::SerializedHandle;
20
21 namespace content {
22
23 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
24     RendererPpapiHost* host,
25     PP_Instance instance,
26     PP_Resource resource)
27     : ResourceHost(host->GetPpapiHost(), instance, resource),
28       host_(host),
29       buffer_manager_(this) {}
30
31 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
32
33 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
34                                                  int32_t buffer_size,
35                                                  TrackType track_type) {
36   DCHECK_GT(number_of_buffers, 0);
37   DCHECK_GT(buffer_size,
38             static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header)));
39   // Make each buffer 4 byte aligned.
40   base::CheckedNumeric<int32_t> buffer_size_aligned = buffer_size;
41   // TODO(amistry): "buffer size" might not == "buffer stride", in the same way
42   // that width != stride in an image buffer.
43   buffer_size_aligned += (4 - buffer_size % 4);
44
45   // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
46   // avoid it.
47   base::CheckedNumeric<int32_t> size = number_of_buffers * buffer_size_aligned;
48   if (!size.IsValid())
49     return false;
50
51   content::RenderThread* render_thread = content::RenderThread::Get();
52   scoped_ptr<base::SharedMemory> shm(
53       render_thread->HostAllocateSharedMemoryBuffer(size.ValueOrDie()).Pass());
54   if (!shm)
55     return false;
56
57   base::SharedMemoryHandle shm_handle = shm->handle();
58   if (!buffer_manager_.SetBuffers(number_of_buffers,
59                                   buffer_size_aligned.ValueOrDie(),
60                                   shm.Pass(),
61                                   true)) {
62     return false;
63   }
64
65   base::PlatformFile platform_file =
66 #if defined(OS_WIN)
67       shm_handle;
68 #elif defined(OS_POSIX)
69       shm_handle.fd;
70 #else
71 #error Not implemented.
72 #endif
73   SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
74                           size.ValueOrDie());
75   bool readonly = (track_type == kRead);
76   host()->SendUnsolicitedReplyWithHandles(
77       pp_resource(),
78       PpapiPluginMsg_MediaStreamTrack_InitBuffers(
79           number_of_buffers,
80           buffer_size_aligned.ValueOrDie(),
81           readonly),
82       std::vector<SerializedHandle>(1, handle));
83   return true;
84 }
85
86 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
87     int32_t index) {
88   DCHECK_GE(index, 0);
89   DCHECK_LT(index, buffer_manager_.number_of_buffers());
90   host()->SendUnsolicitedReply(
91       pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index));
92 }
93
94 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
95     const std::vector<int32_t>& indices) {
96   DCHECK_GE(indices.size(), 0U);
97   host()->SendUnsolicitedReply(pp_resource(),
98       PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices));
99 }
100
101 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
102     const IPC::Message& msg,
103     HostMessageContext* context) {
104   PPAPI_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg)
105     PPAPI_DISPATCH_HOST_RESOURCE_CALL(
106         PpapiHostMsg_MediaStreamTrack_EnqueueBuffer, OnHostMsgEnqueueBuffer)
107     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close,
108                                         OnHostMsgClose)
109   PPAPI_END_MESSAGE_MAP()
110   return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
111 }
112
113 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
114     HostMessageContext* context,
115     int32_t index) {
116   buffer_manager_.EnqueueBuffer(index);
117   return PP_OK;
118 }
119
120 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
121     HostMessageContext* context) {
122   OnClose();
123   return PP_OK;
124 }
125
126 }  // namespace content