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 #ifndef MEDIA_CAPTURE_SERVICE_STREAM_IMPL_H_ |
| 6 #define MEDIA_CAPTURE_SERVICE_STREAM_IMPL_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "media/capture/interfaces/video_capture.mojom.h" |
| 15 #include "media/capture/service/video_capture_device_client_impl.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "mojo/public/cpp/bindings/interface_request.h" |
| 18 #include "mojo/public/cpp/system/buffer.h" |
| 19 |
| 20 namespace base { |
| 21 class SingleThreadTaskRunner; |
| 22 class TimeTicks; |
| 23 } |
| 24 |
| 25 namespace media { |
| 26 class VideoFrame; |
| 27 } |
| 28 |
| 29 namespace media { |
| 30 |
| 31 class MojoVideoFrame; |
| 32 |
| 33 // This class bridges a VideoCaptureDeviceClientImpl with the mojo pipe where we |
| 34 // push captured VideoFrames. |
| 35 class StreamImpl : public mojom::VideoCaptureStream, |
| 36 public VideoCaptureDeviceClientImpl::Delegate { |
| 37 public: |
| 38 // |start_callback| is called when this object is started by its client. |
| 39 // |close_callback| is called when this object is stopped by its client. |
| 40 // |error_callback| is called upon internal error or NotifyError(). |
| 41 StreamImpl(mojo::InterfaceRequest<mojom::VideoCaptureStream> request, |
| 42 const base::Closure& start_callback, |
| 43 const base::Closure& close_callback, |
| 44 const base::Closure& error_callback); |
| 45 |
| 46 ~StreamImpl() override; |
| 47 |
| 48 // mojom::VideoCaptureStream |
| 49 void Start(mojom::VideoCaptureStreamClientPtr client) override; |
| 50 void Stop() override; |
| 51 |
| 52 // VideoCaptureDeviceClientImpl::Delegate |
| 53 void OnFrame(const scoped_refptr<MojoVideoFrame>& frame, |
| 54 const base::TimeTicks& timestamp) override; |
| 55 void OnError(const std::string& reason) override; |
| 56 |
| 57 private: |
| 58 friend class StreamImplTest; |
| 59 |
| 60 // The following Notify* methods mimic those in StreamClient and act as a |
| 61 // bridge to Mojo. They all must not be called until the stream has been |
| 62 // Start()ed. |
| 63 |
| 64 // Pushes a |frame| to the client. |
| 65 void NotifyOnFrameAvailable(const scoped_refptr<MojoVideoFrame>& frame, |
| 66 const base::TimeTicks& timestamp); |
| 67 void NotifyError(const mojo::String& error); |
| 68 |
| 69 // Bound to the client's OnFrameAvailable message to let it notify us that |
| 70 // it's |
| 71 // finished with the VideoFrame identified by |buffer_id|. |
| 72 void OnFrameConsumed(int32_t buffer_id); |
| 73 |
| 74 mojo::Binding<mojom::VideoCaptureStream> binding_; |
| 75 const base::Closure start_callback_; |
| 76 const base::Closure close_callback_; |
| 77 const base::Closure error_callback_; |
| 78 |
| 79 mojom::VideoCaptureStreamClientPtr client_; |
| 80 |
| 81 // Map of VideoFrames in use by a client. Each VF gets an id assigned by which |
| 82 // the client knows it. The buffer ID is just a simple running counter. |
| 83 int running_buffer_id_ = 0; |
| 84 std::map<int32_t, scoped_refptr<MojoVideoFrame>> in_use_video_frames_; |
| 85 |
| 86 // The TaskRunner where this class lives, needed for OnFrame() and OnError() |
| 87 // to PostTask() to. |
| 88 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 89 |
| 90 base::WeakPtrFactory<StreamImpl> weak_factory_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(StreamImpl); |
| 93 }; |
| 94 |
| 95 } // namespace media |
| 96 |
| 97 #endif // MEDIA_CAPTURE_SERVICE_STREAM_IMPL_H_ |
OLD | NEW |