1 // Copyright (c) Microsoft. All rights reserved.
2 //
3 // The MIT License (MIT)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files(the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions :
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #pragma once
24 
25 #include "MFIncludes.hpp"
26 
27 namespace Media {
28 
29 class MediaStreamSink WrlSealed :
30     public Microsoft::WRL::RuntimeClass<
31     Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
32     IMFStreamSink,
33     IMFMediaEventGenerator,
34     IMFMediaTypeHandler
35     >
36 {
37 public:
38 
39     MediaStreamSink(
40         __in const MW::ComPtr<IMFMediaSink>& sink,
41         __in DWORD id,
42         __in const MW::ComPtr<IMFMediaType>& mt,
43         __in MediaSampleHandler^ sampleHandler
44     );
45 
46     //
47     // IMFStreamSink
48     //
49 
50     IFACEMETHODIMP GetMediaSink(__deref_out IMFMediaSink **sink);
51     IFACEMETHODIMP GetIdentifier(__out DWORD *identifier);
52     IFACEMETHODIMP GetMediaTypeHandler(__deref_out IMFMediaTypeHandler **handler);
53     IFACEMETHODIMP ProcessSample(__in_opt IMFSample *sample);
54     IFACEMETHODIMP PlaceMarker(__in MFSTREAMSINK_MARKER_TYPE markerType, __in const PROPVARIANT * markerValue, __in const PROPVARIANT * contextValue);
55     IFACEMETHODIMP Flush();
56 
57     //
58     // IMFMediaEventGenerator
59     //
60 
61     IFACEMETHODIMP GetEvent(__in DWORD flags, __deref_out IMFMediaEvent **event);
62     IFACEMETHODIMP BeginGetEvent(__in IMFAsyncCallback *callback, __in_opt IUnknown *state);
63     IFACEMETHODIMP EndGetEvent(__in IMFAsyncResult *result, __deref_out IMFMediaEvent **event);
64     IFACEMETHODIMP QueueEvent(__in MediaEventType met, __in REFGUID extendedType, __in HRESULT status, __in_opt const PROPVARIANT *value);
65 
66     //
67     // IMFMediaTypeHandler
68     //
69 
70     IFACEMETHODIMP IsMediaTypeSupported(__in IMFMediaType *mediaType, __deref_out_opt  IMFMediaType **closestMediaType);
71     IFACEMETHODIMP GetMediaTypeCount(__out DWORD *typeCount);
72     IFACEMETHODIMP GetMediaTypeByIndex(__in DWORD index, __deref_out  IMFMediaType **mediaType);
73     IFACEMETHODIMP SetCurrentMediaType(__in IMFMediaType *mediaType);
74     IFACEMETHODIMP GetCurrentMediaType(__deref_out_opt IMFMediaType **mediaType);
75     IFACEMETHODIMP GetMajorType(__out GUID *majorType);
76 
77     //
78     // Misc
79     //
80 
81     void InternalSetCurrentMediaType(__in const MW::ComPtr<IMFMediaType>& mediaType);
82     void RequestSample();
83     void Shutdown();
84 
85 private:
86 
87     bool _IsMediaTypeSupported(__in const MW::ComPtr<IMFMediaType>& mt) const;
88     void _UpdateMediaType(__in const MW::ComPtr<IMFMediaType>& mt);
89 
_VerifyNotShutdown()90     void _VerifyNotShutdown()
91     {
92         if (_shutdown)
93         {
94             CHK(MF_E_SHUTDOWN);
95         }
96     }
97 
98     MW::ComPtr<IMFMediaSink> _sink;
99     MW::ComPtr<IMFMediaEventQueue> _eventQueue;
100     MW::ComPtr<IMFMediaType> _curMT;
101 
102     MediaSampleHandler^ _sampleHandler;
103 
104     GUID _majorType;
105     GUID _subType;
106     unsigned int _width;
107     unsigned int _height;
108     DWORD _id;
109     bool _shutdown;
110 
111     MWW::SRWLock _lock;
112 };
113 
114 }