OLD | NEW |
---|---|
(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<mojom::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(mojom::StreamClientPtr client) { | |
36 DCHECK(task_runner_->BelongsToCurrentThread()); | |
37 DCHECK(!client_) << "Start() should not be called several times."; | |
38 client_ = std::move(client); | |
39 client_.set_connection_error_handler(error_callback_); | |
40 start_callback_.Run(); | |
41 } | |
42 | |
43 void StreamImpl::Stop() { | |
44 DCHECK(task_runner_->BelongsToCurrentThread()); | |
45 DCHECK(client_); | |
46 close_callback_.Run(); | |
47 } | |
48 | |
49 void StreamImpl::OnFrame(const scoped_refptr<MojoVideoFrame>& frame, | |
perkj_chrome
2016/03/03 13:12:41
This is an implementation of VideoCaptureDeviceCli
mcasas
2016/03/03 18:41:15
The sequence replicates the current ownership grap
| |
50 const base::TimeTicks& timestamp) { | |
51 task_runner_->PostTask( | |
52 FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable, | |
53 weak_factory_.GetWeakPtr(), frame, timestamp)); | |
perkj_chrome
2016/03/03 13:12:41
nit: you can cash a weak ptr in the ctor if you wa
mcasas
2016/03/03 18:41:15
The created WeakPtr is passed to the bound
Callba
| |
54 } | |
55 | |
56 void StreamImpl::OnError(const std::string& reason) { | |
57 DLOG(ERROR) << __FUNCTION__ << " " << reason; | |
58 task_runner_->PostTask( | |
59 FROM_HERE, | |
60 base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason)); | |
61 } | |
62 | |
63 void StreamImpl::NotifyFrameAvailable( | |
64 const scoped_refptr<MojoVideoFrame>& frame, | |
65 const base::TimeTicks& timestamp) { | |
66 DCHECK(task_runner_->BelongsToCurrentThread()); | |
67 DCHECK(client_); | |
68 | |
69 const int32_t buffer_id = running_buffer_id_++; | |
70 | |
71 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New(); | |
72 | |
73 // We duplicate the handle immediately to avoid passing around the primordial | |
74 // handle. While it's OK for someone to continue using the buffer after the | |
75 // pool drops ownership, it's not OK to dupe the primordial handle once it's | |
76 // been invalidated. | |
77 mojo::ScopedSharedBufferHandle handle; | |
78 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle); | |
79 DLOG_IF(ERROR, rv != MOJO_RESULT_OK) << "Error duplicating handle"; | |
80 if (rv != MOJO_RESULT_OK) | |
81 return; | |
82 frame_info->storage_handle = std::move(handle); | |
83 frame_info->storage_size = frame->mapped_size(); | |
84 | |
85 frame_info->pixel_format = | |
86 static_cast<media::interfaces::VideoFormat>(frame->format()); | |
87 frame_info->coded_size = mojo::Size::From(frame->coded_size()); | |
88 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect()); | |
89 frame_info->timestamp = timestamp.ToInternalValue(); | |
90 | |
91 in_use_video_frames_.insert(std::make_pair(buffer_id, frame)); | |
92 | |
93 client_->FrameAvailable(std::move(frame_info), | |
94 base::Bind(&StreamImpl::OnFrameConsumed, | |
95 weak_factory_.GetWeakPtr(), buffer_id)); | |
perkj_chrome
2016/03/03 13:12:41
dito- you can use a cached weakptr here if you wan
mcasas
2016/03/03 18:41:15
Acknowledged.
| |
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_->Error(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 if (video_frame == in_use_video_frames_.end()) { | |
perkj_chrome
2016/03/03 13:12:41
DCHECK this?
mcasas
2016/03/03 18:41:15
Done.
| |
110 DLOG(ERROR) << "Buffer id " << buffer_id << " not found!"; | |
111 return; | |
112 } | |
113 in_use_video_frames_.erase(video_frame); | |
114 } | |
115 | |
116 } // namespace media | |
OLD | NEW |