Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / pepper_media_stream_track_host_base.cc
blob7846bfdb530b9af4f990aacb6642ad5a04c15276
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;
22 namespace content {
24 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
25 RendererPpapiHost* host,
26 PP_Instance instance,
27 PP_Resource resource)
28 : ResourceHost(host->GetPpapiHost(), instance, resource),
29 host_(host),
30 buffer_manager_(this) {}
32 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
34 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
35 int32_t buffer_size,
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
47 // avoid it.
48 base::CheckedNumeric<int32_t> size = number_of_buffers * buffer_size_aligned;
49 if (!size.IsValid())
50 return false;
52 content::RenderThread* render_thread = content::RenderThread::Get();
53 scoped_ptr<base::SharedMemory> shm(
54 render_thread->HostAllocateSharedMemoryBuffer(size.ValueOrDie()).Pass());
55 if (!shm)
56 return false;
58 base::SharedMemoryHandle shm_handle = shm->handle();
59 if (!buffer_manager_.SetBuffers(number_of_buffers,
60 buffer_size_aligned.ValueOrDie(),
61 shm.Pass(),
62 true)) {
63 return false;
66 SerializedHandle handle(host_->ShareSharedMemoryHandleWithRemote(shm_handle),
67 size.ValueOrDie());
68 bool readonly = (track_type == kRead);
69 host()->SendUnsolicitedReplyWithHandles(
70 pp_resource(),
71 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
72 number_of_buffers,
73 buffer_size_aligned.ValueOrDie(),
74 readonly),
75 std::vector<SerializedHandle>(1, handle));
76 return true;
79 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
80 int32_t index) {
81 DCHECK_GE(index, 0);
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,
101 OnHostMsgClose)
102 PPAPI_END_MESSAGE_MAP()
103 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
106 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
107 HostMessageContext* context,
108 int32_t index) {
109 buffer_manager_.EnqueueBuffer(index);
110 return PP_OK;
113 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
114 HostMessageContext* context) {
115 OnClose();
116 return PP_OK;
119 } // namespace content