summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_video_destination_host.cc
blob: aa3567d20c6e1e82166cf6268648ed7e48cf03e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/pepper/pepper_video_destination_host.h"

#include "base/time/time.h"
#include "content/public/renderer/renderer_ppapi_host.h"
#include "content/renderer/pepper/ppb_image_data_impl.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/host/host_message_context.h"
#include "ppapi/host/ppapi_host.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_image_data_api.h"

using ppapi::host::HostMessageContext;
using ppapi::host::ReplyMessageContext;

namespace content {

PepperVideoDestinationHost::PepperVideoDestinationHost(RendererPpapiHost* host,
                                                       PP_Instance instance,
                                                       PP_Resource resource)
    : ResourceHost(host->GetPpapiHost(), instance, resource),
#if DCHECK_IS_ON()
      has_received_frame_(false),
#endif
      weak_factory_(this) {}

PepperVideoDestinationHost::~PepperVideoDestinationHost() {}

int32_t PepperVideoDestinationHost::OnResourceMessageReceived(
    const IPC::Message& msg,
    HostMessageContext* context) {
  PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDestinationHost, msg)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_Open,
                                      OnHostMsgOpen)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame,
                                      OnHostMsgPutFrame)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close,
                                        OnHostMsgClose)
  PPAPI_END_MESSAGE_MAP()
  return PP_ERROR_FAILED;
}

int32_t PepperVideoDestinationHost::OnHostMsgOpen(
    HostMessageContext* context,
    const std::string& stream_url) {
  GURL gurl(stream_url);
  if (!gurl.is_valid())
    return PP_ERROR_BADARGUMENT;

  FrameWriterInterface* frame_writer = nullptr;
  if (!PepperToVideoTrackAdapter::Open(nullptr /* registry */, gurl.spec(),
                                       &frame_writer))
    return PP_ERROR_FAILED;
  frame_writer_.reset(frame_writer);

  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
  reply_context.params.set_result(PP_OK);
  host()->SendReply(reply_context, PpapiPluginMsg_VideoDestination_OpenReply());
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperVideoDestinationHost::OnHostMsgPutFrame(
    HostMessageContext* context,
    const ppapi::HostResource& image_data_resource,
    PP_TimeTicks timestamp) {
  ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> enter(
      image_data_resource.host_resource(), true);
  if (enter.failed())
    return PP_ERROR_BADRESOURCE;
  PPB_ImageData_Impl* image_data_impl =
      static_cast<PPB_ImageData_Impl*>(enter.object());

  if (!PPB_ImageData_Impl::IsImageDataFormatSupported(
          image_data_impl->format()))
    return PP_ERROR_BADARGUMENT;

  if (!frame_writer_.get())
    return PP_ERROR_FAILED;

  // Convert PP_TimeTicks (a double, in seconds) to a video timestamp (int64_t,
  // nanoseconds).
  const int64_t timestamp_ns =
      static_cast<int64_t>(timestamp * base::Time::kNanosecondsPerSecond);
  // Check that timestamps are strictly increasing.
#if DCHECK_IS_ON()
  if (has_received_frame_)
    DCHECK_GT(timestamp_ns, previous_timestamp_ns_);
  has_received_frame_ = true;
  previous_timestamp_ns_ = timestamp_ns;
#endif

  frame_writer_->PutFrame(image_data_impl, timestamp_ns);

  return PP_OK;
}

int32_t PepperVideoDestinationHost::OnHostMsgClose(
    HostMessageContext* context) {
  frame_writer_.reset(nullptr);
  return PP_OK;
}

}  // namespace content