Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(867)

Side by Side Diff: media/capture/service/stream_impl.cc

Issue 1699553002: Mojo Video Capture service in media/capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased to using MojoSharedBufferVideoFrame Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "media/capture/service/stream_impl.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/time/time.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/capture/interfaces/video_capture.mojom.h"
13 #include "media/mojo/common/mojo_shared_buffer_video_frame.h"
14 #include "mojo/converters/geometry/geometry_type_converters.h"
15
16 namespace media {
17
18 StreamImpl::StreamImpl(
19 mojo::InterfaceRequest<mojom::VideoCaptureStream> request,
20 const base::Closure& start_callback,
21 const base::Closure& close_callback,
22 const base::Closure& error_callback)
23 : binding_(this, std::move(request)),
24 start_callback_(start_callback),
25 close_callback_(close_callback),
26 error_callback_(error_callback),
27 task_runner_(base::ThreadTaskRunnerHandle::Get()),
28 weak_factory_(this) {
29 binding_.set_connection_error_handler(error_callback_);
30 }
31
32 StreamImpl::~StreamImpl() {
33 DCHECK(task_runner_->BelongsToCurrentThread());
34 }
35
36 void StreamImpl::Start(mojom::VideoCaptureStreamClientPtr client) {
37 DCHECK(task_runner_->BelongsToCurrentThread());
38 DCHECK(!client_) << "Start() should not be called several times.";
39 client_ = std::move(client);
40 client_.set_connection_error_handler(error_callback_);
41 start_callback_.Run();
perkj_chrome 2016/03/07 23:58:27 This callback seems pretty useless. The implementa
mcasas 2016/03/08 23:57:28 Removed.
42 }
43
44 void StreamImpl::Stop() {
45 DCHECK(task_runner_->BelongsToCurrentThread());
46 DCHECK(client_);
47 close_callback_.Run();
48 }
49
50 void StreamImpl::OnFrame(const scoped_refptr<MojoSharedBufferVideoFrame>& frame,
51 const base::TimeTicks& timestamp) {
52 task_runner_->PostTask(
53 FROM_HERE, base::Bind(&StreamImpl::NotifyOnFrameAvailable,
54 weak_factory_.GetWeakPtr(), frame, timestamp));
55 }
56
57 void StreamImpl::OnError(const std::string& reason) {
58 DLOG(ERROR) << __FUNCTION__ << " " << reason;
59 task_runner_->PostTask(
60 FROM_HERE,
61 base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason));
62 }
63
64 void StreamImpl::NotifyOnFrameAvailable(
65 const scoped_refptr<MojoSharedBufferVideoFrame>& frame,
66 const base::TimeTicks& timestamp) {
67 DCHECK(task_runner_->BelongsToCurrentThread());
68 DCHECK(client_);
69
70 const int32_t buffer_id = running_buffer_id_++;
71
72 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New();
73
74 // We duplicate the handle immediately to avoid passing around the primordial
75 // handle. While it's OK for someone to continue using the buffer after the
76 // pool drops ownership, it's not OK to dupe the primordial handle once it's
77 // been invalidated.
78 mojo::ScopedSharedBufferHandle handle;
79 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle);
80 DLOG_IF(ERROR, rv != MOJO_RESULT_OK) << "Error duplicating handle";
81 if (rv != MOJO_RESULT_OK)
82 return;
83 frame_info->storage_handle = std::move(handle);
84 frame_info->storage_size = frame->mapped_size();
85
86 frame_info->pixel_format =
87 static_cast<media::interfaces::VideoFormat>(frame->format());
88 frame_info->coded_size = mojo::Size::From(frame->coded_size());
89 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect());
90 frame_info->timestamp = timestamp.ToInternalValue();
91
92 in_use_video_frames_.insert(std::make_pair(buffer_id, frame));
93 client_->OnFrameAvailable(std::move(frame_info),
94 base::Bind(&StreamImpl::OnFrameConsumed,
95 weak_factory_.GetWeakPtr(), buffer_id));
96 }
97
98 void StreamImpl::NotifyError(const mojo::String& error) {
99 DCHECK(task_runner_->BelongsToCurrentThread());
100 // A device may notify of error(s) before Start() registers a |client_|.
101 if (client_)
102 client_->OnError(error);
103 error_callback_.Run();
104 }
105
106 void StreamImpl::OnFrameConsumed(int32_t buffer_id) {
107 DCHECK(task_runner_->BelongsToCurrentThread());
108 const auto video_frame = in_use_video_frames_.find(buffer_id);
109 DCHECK(video_frame != in_use_video_frames_.end()) << "Buffer id " << buffer_id
110 << " not found!";
111 in_use_video_frames_.erase(video_frame);
112 }
113
114 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698