1 // Copyright (c) 2012 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 // Implement a simple base class for a DirectShow input pin. It may only be
6 // used in a single threaded apartment.
7 
8 #ifndef MEDIA_CAPTURE_VIDEO_WIN_PIN_BASE_WIN_H_
9 #define MEDIA_CAPTURE_VIDEO_WIN_PIN_BASE_WIN_H_
10 
11 // Avoid including strsafe.h via dshow as it will cause build warnings.
12 #define NO_DSHOW_STRSAFE
13 #include <dshow.h>
14 #include <wrl/client.h>
15 
16 #include "base/memory/ref_counted.h"
17 
18 namespace media {
19 
20 class PinBase : public IPin,
21                 public IMemInputPin,
22                 public base::RefCounted<PinBase> {
23  public:
24   explicit PinBase(IBaseFilter* owner);
25 
26   // Function used for changing the owner.
27   // If the owner is deleted the owner should first call this function
28   // with owner = NULL.
29   void SetOwner(IBaseFilter* owner);
30 
31   // Checks if a media type is acceptable. This is called when this pin is
32   // connected to an output pin. Must return true if the media type is
33   // acceptable, false otherwise.
34   virtual bool IsMediaTypeValid(const AM_MEDIA_TYPE* media_type) = 0;
35 
36   // Enumerates valid media types.
37   virtual bool GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) = 0;
38 
39   // Called when new media is received. Note that this is not on the same
40   // thread as where the pin is created.
41   IFACEMETHODIMP Receive(IMediaSample* sample) override = 0;
42 
43   IFACEMETHODIMP Connect(IPin* receive_pin,
44                          const AM_MEDIA_TYPE* media_type) override;
45 
46   IFACEMETHODIMP ReceiveConnection(IPin* connector,
47                                    const AM_MEDIA_TYPE* media_type) override;
48 
49   IFACEMETHODIMP Disconnect() override;
50 
51   IFACEMETHODIMP ConnectedTo(IPin** pin) override;
52 
53   IFACEMETHODIMP ConnectionMediaType(AM_MEDIA_TYPE* media_type) override;
54 
55   IFACEMETHODIMP QueryPinInfo(PIN_INFO* info) override;
56 
57   IFACEMETHODIMP QueryDirection(PIN_DIRECTION* pin_dir) override;
58 
59   IFACEMETHODIMP QueryId(LPWSTR* id) override;
60 
61   IFACEMETHODIMP QueryAccept(const AM_MEDIA_TYPE* media_type) override;
62 
63   IFACEMETHODIMP EnumMediaTypes(IEnumMediaTypes** types) override;
64 
65   IFACEMETHODIMP QueryInternalConnections(IPin** pins, ULONG* no_pins) override;
66 
67   IFACEMETHODIMP EndOfStream() override;
68 
69   IFACEMETHODIMP BeginFlush() override;
70 
71   IFACEMETHODIMP EndFlush() override;
72 
73   IFACEMETHODIMP NewSegment(REFERENCE_TIME start,
74                             REFERENCE_TIME stop,
75                             double dRate) override;
76 
77   // Inherited from IMemInputPin.
78   IFACEMETHODIMP GetAllocator(IMemAllocator** allocator) override;
79 
80   IFACEMETHODIMP NotifyAllocator(IMemAllocator* allocator,
81                                  BOOL read_only) override;
82 
83   IFACEMETHODIMP GetAllocatorRequirements(
84       ALLOCATOR_PROPERTIES* properties) override;
85 
86   IFACEMETHODIMP ReceiveMultiple(IMediaSample** samples,
87                                  long sample_count,
88                                  long* processed) override;
89   IFACEMETHODIMP ReceiveCanBlock() override;
90 
91   // Inherited from IUnknown.
92   IFACEMETHODIMP QueryInterface(REFIID id, void** object_ptr) override;
93 
94   IFACEMETHODIMP_(ULONG) AddRef() override;
95 
96   IFACEMETHODIMP_(ULONG) Release() override;
97 
98  protected:
99   friend class base::RefCounted<PinBase>;
100   virtual ~PinBase();
101 
102  private:
103   AM_MEDIA_TYPE current_media_type_;
104   Microsoft::WRL::ComPtr<IPin> connected_pin_;
105   // owner_ is the filter owning this pin. We don't reference count it since
106   // that would create a circular reference count.
107   IBaseFilter* owner_;
108 };
109 
110 }  // namespace media
111 
112 #endif  // MEDIA_CAPTURE_VIDEO_WIN_PIN_BASE_WIN_H_
113