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

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: added missing BUILD deps 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(mojo::InterfaceRequest<video_capture::Stream> request,
19 const base::Closure& start_callback,
20 const base::Closure& close_callback,
21 const base::Closure& error_callback)
22 : binding_(this, std::move(request)),
23 start_callback_(start_callback),
24 close_callback_(close_callback),
25 error_callback_(error_callback),
26 task_runner_(base::ThreadTaskRunnerHandle::Get()),
27 weak_factory_(this) {
28 binding_.set_connection_error_handler(error_callback_);
29 }
30
31 StreamImpl::~StreamImpl() {
32 DCHECK(task_runner_->BelongsToCurrentThread());
33 }
34
35 void StreamImpl::Start(video_capture::StreamClientPtr client) {
36 DCHECK(task_runner_->BelongsToCurrentThread());
37 DCHECK(!client_) << "Start() should not be called several times.";
38 DCHECK(!paused_);
39 client_ = std::move(client);
40 client_.set_connection_error_handler(error_callback_);
41 start_callback_.Run();
42 }
43
44 void StreamImpl::Pause() {
45 DCHECK(task_runner_->BelongsToCurrentThread());
46 DCHECK(client_);
47 paused_ = true;
48 }
49
50 void StreamImpl::Resume() {
51 DCHECK(task_runner_->BelongsToCurrentThread());
52 DCHECK(client_);
53 paused_ = false;
54 }
55
56 void StreamImpl::Stop() {
57 DCHECK(task_runner_->BelongsToCurrentThread());
58 DCHECK(client_);
59 close_callback_.Run();
60 }
61
62 void StreamImpl::OnFrame(const scoped_refptr<MojoVideoFrame>& frame,
63 const base::TimeTicks& timestamp) {
64 task_runner_->PostTask(
65 FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable,
66 weak_factory_.GetWeakPtr(), frame, timestamp));
67 }
68
69 void StreamImpl::OnError(const std::string& reason) {
70 DLOG(ERROR) << __FUNCTION__ << " " << reason;
71 task_runner_->PostTask(
72 FROM_HERE,
73 base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason));
74 }
75
76 void StreamImpl::NotifyFrameAvailable(
77 const scoped_refptr<MojoVideoFrame>& frame,
78 const base::TimeTicks& timestamp) {
79 DCHECK(task_runner_->BelongsToCurrentThread());
80 DCHECK(client_);
81
82 if (paused_) // Drop the frame immediately if we aren't playing.
83 return;
84
85 const int32_t buffer_id = running_buffer_id_++;
86
87 video_capture::FrameInfoPtr frame_info = video_capture::FrameInfo::New();
88
89 // We duplicate the handle immediately to avoid passing around the primordial
90 // handle. While it's OK for someone to continue using the buffer after the
91 // pool drops ownership, it's not OK to dupe the primordial handle once it's
92 // been invalidated.
93 mojo::ScopedSharedBufferHandle handle;
94 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle);
95 DLOG_IF(ERROR, rv != MOJO_RESULT_OK) << "Error duplicating handle";
96 if (rv != MOJO_RESULT_OK)
97 return;
98 frame_info->storage_handle = std::move(handle);
99 frame_info->storage_size = frame->mapped_size();
100
101 frame_info->pixel_format =
102 static_cast<media::interfaces::VideoFormat>(frame->format());
103 frame_info->coded_size = mojo::Size::From(frame->coded_size());
104 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect());
105 frame_info->timestamp = timestamp.ToInternalValue();
106
107 in_use_video_frames_.insert(std::make_pair(buffer_id, frame));
108
109 client_->FrameAvailable(std::move(frame_info),
110 base::Bind(&StreamImpl::OnFrameConsumed,
111 weak_factory_.GetWeakPtr(), buffer_id));
112 }
113
114 void StreamImpl::NotifyError(const mojo::String& error) {
115 DCHECK(task_runner_->BelongsToCurrentThread());
116 if (client_) // A devices can notify of errors before there is a |client_|.
Ken Rockot(use gerrit already) 2016/03/02 02:52:08 nit: "A device cannot notify" perhaps?
mcasas 2016/03/02 19:53:51 Rewritten.
117 client_->Error(error);
118 error_callback_.Run();
119 }
120
121 void StreamImpl::OnFrameConsumed(int32_t buffer_id) {
122 DCHECK(task_runner_->BelongsToCurrentThread());
123 const auto video_frame = in_use_video_frames_.find(buffer_id);
124 if (video_frame == in_use_video_frames_.end()) {
125 DLOG(ERROR) << "Buffer id " << buffer_id << " not found!";
126 return;
127 }
128 in_use_video_frames_.erase(video_frame);
129 }
130
131 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698