1 // Copyright 2014 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.
5 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "content/renderer/media/media_stream_video_track.h"
13 #include "media/base/video_frame.h"
17 // VideoTrackAdapter is a helper class used by MediaStreamVideoSource used for
18 // adapting the video resolution from a source implementation to the resolution
19 // a track requires. Different tracks can have different resolution constraints.
20 // The constraints can be set as max width and height as well as max and min
22 // Video frames are delivered to a track using a VideoCaptureDeliverFrameCB on
24 // Adaptations is done by wrapping the original media::VideoFrame in a new
25 // media::VideoFrame with a new visible_rect and natural_size.
26 class VideoTrackAdapter
27 : public base::RefCountedThreadSafe
<VideoTrackAdapter
> {
29 explicit VideoTrackAdapter(
30 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
);
32 // Register |track| to receive video frames in |frame_callback| with
33 // a resolution within the boundaries of the arguments.
34 // Must be called on the main render thread. |frame_callback| is guaranteed to
35 // be released on the main render thread.
36 void AddTrack(const MediaStreamVideoTrack
* track
,
37 VideoCaptureDeliverFrameCB frame_callback
,
38 int max_width
, int max_height
,
39 double min_aspect_ratio
,
40 double max_aspect_ratio
);
41 void RemoveTrack(const MediaStreamVideoTrack
* track
);
43 // Delivers |frame| to all tracks that have registered a callback.
44 // Must be called on the IO-thread.
45 void DeliverFrameOnIO(
46 const scoped_refptr
<media::VideoFrame
>& frame
,
47 const media::VideoCaptureFormat
& format
);
49 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop() {
50 DCHECK(thread_checker_
.CalledOnValidThread());
51 return io_message_loop_
;
55 virtual ~VideoTrackAdapter();
56 friend class base::RefCountedThreadSafe
<VideoTrackAdapter
>;
59 const MediaStreamVideoTrack
* track
,
60 VideoCaptureDeliverFrameCB frame_callback
,
61 int max_width
, int max_height
,
62 double min_aspect_ratio
,
63 double max_aspect_ratio
);
64 void RemoveTrackOnIO(const MediaStreamVideoTrack
* track
);
66 // |thread_checker_| is bound to the main render thread.
67 base::ThreadChecker thread_checker_
;
69 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
71 // |renderer_task_runner_| is used to ensure that
72 // VideoCaptureDeliverFrameCB is released on the main render thread.
73 scoped_refptr
<base::SingleThreadTaskRunner
> renderer_task_runner_
;
75 // VideoFrameResolutionAdapter is an inner class that is created on the main
76 // render thread but operates on the IO-thread. It does the resolution
77 // adaptation and delivers frames to all registered tracks on the IO-thread.
78 class VideoFrameResolutionAdapter
;
79 typedef std::vector
<scoped_refptr
<VideoFrameResolutionAdapter
> >
81 FrameAdapters adapters_
;
83 DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter
);
86 } // namespace content
88 #endif // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_