Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_media_stream_video_track_host.h
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.
4
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_VIDEO_TRACK_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_VIDEO_TRACK_HOST_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/public/renderer/media_stream_video_sink.h"
11 #include "content/renderer/media/media_stream_video_source.h"
12 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
13 #include "media/base/video_frame.h"
14 #include "ppapi/c/ppb_video_frame.h"
15 #include "ppapi/shared_impl/media_stream_video_track_shared.h"
16 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
17 #include "ui/gfx/size.h"
18
19 namespace content {
20
21 class PepperMediaStreamVideoTrackHost : public PepperMediaStreamTrackHostBase,
22                                         public MediaStreamVideoSink,
23                                         public MediaStreamVideoSource {
24  public:
25   // Input mode constructor.
26   // In input mode, this class passes video frames from |track| to the
27   // associated pepper plugin.
28   PepperMediaStreamVideoTrackHost(RendererPpapiHost* host,
29                                   PP_Instance instance,
30                                   PP_Resource resource,
31                                   const blink::WebMediaStreamTrack& track);
32
33   // Output mode constructor.
34   // In output mode, this class passes video frames from the associated
35   // pepper plugin to a newly created blink::WebMediaStreamTrack.
36   PepperMediaStreamVideoTrackHost(RendererPpapiHost* host,
37                                   PP_Instance instance,
38                                   PP_Resource resource);
39
40   virtual bool IsMediaStreamVideoTrackHost() OVERRIDE;
41
42   blink::WebMediaStreamTrack track() { return track_; }
43
44  private:
45
46   virtual ~PepperMediaStreamVideoTrackHost();
47
48   void InitBuffers();
49
50   // PepperMediaStreamTrackHostBase overrides:
51   virtual void OnClose() OVERRIDE;
52   virtual int32_t OnHostMsgEnqueueBuffer(
53       ppapi::host::HostMessageContext* context, int32_t index) OVERRIDE;
54
55   // Sends frame with |index| to |track_|.
56   int32_t SendFrameToTrack(int32_t index);
57
58   void OnVideoFrame(const scoped_refptr<media::VideoFrame>& frame,
59                     const media::VideoCaptureFormat& format);
60
61   // MediaStreamVideoSource overrides:
62   virtual void GetCurrentSupportedFormats(
63       int max_requested_width,
64       int max_requested_height,
65       const VideoCaptureDeviceFormatsCB& callback) OVERRIDE;
66
67   virtual void StartSourceImpl(
68       const media::VideoCaptureParams& params,
69       const VideoCaptureDeliverFrameCB& frame_callback) OVERRIDE;
70
71   virtual void StopSourceImpl() OVERRIDE;
72
73   // ResourceHost overrides:
74   virtual void DidConnectPendingHostToResource() OVERRIDE;
75
76   // ResourceMessageHandler overrides:
77   virtual int32_t OnResourceMessageReceived(
78       const IPC::Message& msg,
79       ppapi::host::HostMessageContext* context) OVERRIDE;
80
81   // Message handlers:
82   int32_t OnHostMsgConfigure(
83       ppapi::host::HostMessageContext* context,
84       const ppapi::MediaStreamVideoTrackShared::Attributes& attributes);
85
86   void InitBlinkTrack();
87   void OnTrackStarted(MediaStreamSource* source, bool success);
88
89   blink::WebMediaStreamTrack track_;
90
91   // True if it has been added to |blink::WebMediaStreamTrack| as a sink.
92   bool connected_;
93
94   // Number of buffers.
95   int32_t number_of_buffers_;
96
97   // Size of frames which are received from MediaStreamVideoSink.
98   gfx::Size source_frame_size_;
99
100   // Plugin specified frame size.
101   gfx::Size plugin_frame_size_;
102
103   // Format of frames which are received from MediaStreamVideoSink.
104   PP_VideoFrame_Format source_frame_format_;
105
106   // Plugin specified frame format.
107   PP_VideoFrame_Format plugin_frame_format_;
108
109   // The size of frame pixels in bytes.
110   uint32_t frame_data_size_;
111
112   // TODO(ronghuawu): Remove |type_| and split PepperMediaStreamVideoTrackHost
113   // into 2 classes for read and write.
114   TrackType type_;
115   bool output_started_;
116
117   // Internal class used for delivering video frames on the IO-thread to
118   // the MediaStreamVideoSource implementation.
119   class FrameDeliverer;
120   scoped_refptr<FrameDeliverer> frame_deliverer_;
121
122   base::WeakPtrFactory<PepperMediaStreamVideoTrackHost> weak_factory_;
123
124   DISALLOW_COPY_AND_ASSIGN(PepperMediaStreamVideoTrackHost);
125 };
126
127 }  // namespace content
128
129 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_VIDEO_TRACK_HOST_H_