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.
5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "content/common/pepper_file_util.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "content/public/renderer/renderer_ppapi_host.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/media_stream_buffer.h"
19 using ppapi::host::HostMessageContext
;
20 using ppapi::proxy::SerializedHandle
;
24 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
25 RendererPpapiHost
* host
,
28 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
30 buffer_manager_(this) {}
32 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
34 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers
,
36 TrackType track_type
) {
37 DCHECK_GT(number_of_buffers
, 0);
38 DCHECK_GT(buffer_size
,
39 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header
)));
40 // Make each buffer 4 byte aligned.
41 base::CheckedNumeric
<int32_t> buffer_size_aligned
= buffer_size
;
42 // TODO(amistry): "buffer size" might not == "buffer stride", in the same way
43 // that width != stride in an image buffer.
44 buffer_size_aligned
+= (4 - buffer_size
% 4);
46 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
48 base::CheckedNumeric
<int32_t> size
= number_of_buffers
* buffer_size_aligned
;
52 content::RenderThread
* render_thread
= content::RenderThread::Get();
53 scoped_ptr
<base::SharedMemory
> shm(
54 render_thread
->HostAllocateSharedMemoryBuffer(size
.ValueOrDie()).Pass());
58 base::SharedMemoryHandle shm_handle
= shm
->handle();
59 if (!buffer_manager_
.SetBuffers(number_of_buffers
,
60 buffer_size_aligned
.ValueOrDie(),
66 SerializedHandle
handle(host_
->ShareSharedMemoryHandleWithRemote(shm_handle
),
68 bool readonly
= (track_type
== kRead
);
69 host()->SendUnsolicitedReplyWithHandles(
71 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
73 buffer_size_aligned
.ValueOrDie(),
75 std::vector
<SerializedHandle
>(1, handle
));
79 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
82 DCHECK_LT(index
, buffer_manager_
.number_of_buffers());
83 host()->SendUnsolicitedReply(
84 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index
));
87 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
88 const std::vector
<int32_t>& indices
) {
89 DCHECK_GE(indices
.size(), 0U);
90 host()->SendUnsolicitedReply(pp_resource(),
91 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices
));
94 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
95 const IPC::Message
& msg
,
96 HostMessageContext
* context
) {
97 PPAPI_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase
, msg
)
98 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
99 PpapiHostMsg_MediaStreamTrack_EnqueueBuffer
, OnHostMsgEnqueueBuffer
)
100 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close
,
102 PPAPI_END_MESSAGE_MAP()
103 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg
, context
);
106 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
107 HostMessageContext
* context
,
109 buffer_manager_
.EnqueueBuffer(index
);
113 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
114 HostMessageContext
* context
) {
119 } // namespace content