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

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: perkj@ comments - mostly simplifications of the mojom. and renaming 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/capture/service/mojo_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();
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<MojoVideoFrame>& frame,
51 const base::TimeTicks& timestamp) {
52 task_runner_->PostTask(
53 FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable,
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::NotifyFrameAvailable(
65 const scoped_refptr<MojoVideoFrame>& 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));
magjed_chromium 2016/03/04 15:41:42 Use this instead: in_use_video_frames_.emplace(buf
mcasas 2016/03/04 19:59:45 Uuuw yeah, now we have emplace() ! When this CL s
93
94 client_->FrameAvailable(std::move(frame_info),
95 base::Bind(&StreamImpl::OnFrameConsumed,
96 weak_factory_.GetWeakPtr(), buffer_id));
97 }
98
99 void StreamImpl::NotifyError(const mojo::String& error) {
100 DCHECK(task_runner_->BelongsToCurrentThread());
101 // A device may notify of error(s) before Start() registers a |client_|.
102 if (client_)
103 client_->Error(error);
104 error_callback_.Run();
105 }
106
107 void StreamImpl::OnFrameConsumed(int32_t buffer_id) {
108 DCHECK(task_runner_->BelongsToCurrentThread());
109 const auto video_frame = in_use_video_frames_.find(buffer_id);
110 DCHECK(video_frame == in_use_video_frames_.end()) << "Buffer id " << buffer_id
magjed_chromium 2016/03/04 15:41:42 Shouldn't this be DCHECK(video_frame != in_use_vid
mcasas 2016/03/04 19:59:45 Yes thanks.
magjed_chromium 2016/03/07 15:36:24 Then why didn't the tests fail?
mcasas 2016/03/07 20:57:49 The capture tests are only compiled in gn builds a
magjed_chromium 2016/03/09 14:11:18 Ok. It's a bit worrying that a DCHECK with 100% fa
111 << " not found!";
112 in_use_video_frames_.erase(video_frame);
113 }
114
115 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698