@@ -773,23 +773,55 @@ namespace librealsense
773773
774774 void wmf_uvc_device::set_power_state(power_state state)
775775 {
776- if (state == _power_state)
777- return;
776+ std::lock_guard< std::recursive_mutex > lock( _source_lock );
778777
779778 switch (state)
780779 {
781- case D0: set_d0(); break;
782- case D3: set_d3(); break;
780+ case D0:
781+ {
782+ if( _power_counter.fetch_add( 1 ) == 0 )
783+ {
784+ try
785+ {
786+ set_d0();
787+ }
788+ //In case of failure need to decrease use counter
789+ catch( std::exception const & e )
790+ {
791+ _power_counter.fetch_add( -1 );
792+ throw e;
793+ }
794+ catch( ... )
795+ {
796+ _power_counter.fetch_add( -1 );
797+ throw;
798+ }
799+ }
800+ break;
801+ }
802+ case D3:
803+ {
804+ if( _power_counter.fetch_add( -1 ) == 1 )
805+ {
806+ set_d3();
807+ }
808+ break;
809+ }
783810 default:
784811 throw std::runtime_error(" illegal power state request" );
785812 }
786813 }
787814
788- wmf_uvc_device::wmf_uvc_device(const uvc_device_info& info,
789- std::shared_ptr<const wmf_backend> backend)
790- : _streamIndex(MAX_PINS), _info(info), _is_flushed(), _has_started(), _backend(std::move(backend)),
791- _systemwide_lock(info.unique_id.c_str(), WAIT_FOR_MUTEX_TIME_OUT),
792- _location(" " ), _device_usb_spec(usb3_type)
815+ wmf_uvc_device::wmf_uvc_device( const uvc_device_info & info, std::shared_ptr< const wmf_backend > backend )
816+ : _streamIndex( MAX_PINS )
817+ , _info( info )
818+ , _is_flushed()
819+ , _has_started()
820+ , _backend( std::move( backend ) )
821+ , _systemwide_lock( info.unique_id.c_str(), WAIT_FOR_MUTEX_TIME_OUT )
822+ , _location( " " )
823+ , _device_usb_spec( usb3_type )
824+ , _power_counter( 0 )
793825 {
794826 if (!is_connected(info))
795827 {
@@ -884,7 +916,6 @@ namespace librealsense
884916 //enable reader
885917 CHECK_HR(MFCreateSourceReaderFromMediaSource(_source, _reader_attrs, &_reader));
886918 CHECK_HR(_reader->SetStreamSelection(static_cast<DWORD>(MF_SOURCE_READER_ALL_STREAMS), TRUE));
887- _power_state = D0;
888919
889920 for( auto && xu : _xus )
890921 init_xu( xu );
@@ -899,7 +930,6 @@ namespace librealsense
899930 safe_release(_source);
900931 for (auto& elem : _streams)
901932 elem.callback = nullptr;
902- _power_state = D3;
903933 }
904934
905935 void wmf_uvc_device::foreach_profile(std::function<void(const mf_profile& profile, CComPtr<IMFMediaType> media_type, bool& quit)> action) const
@@ -1210,5 +1240,14 @@ namespace librealsense
12101240 _profiles.clear ();
12111241 _frame_callbacks.clear ();
12121242 }
1213- }
1214- }
1243+
1244+ power_state wmf_uvc_device::get_power_state () const
1245+ {
1246+ LOG_ERROR ( " wmf_uvc_device::get_power_state start. this = " << this );
1247+ std::lock_guard< std::recursive_mutex > lock ( _source_lock );
1248+ std::string tmp = _source ? " D0" : " D3" ;
1249+ LOG_ERROR ( " wmf_uvc_device::get_power_state got lock. power state is " << tmp );
1250+ return _source ? D0 : D3;
1251+ }
1252+ } // namespace platform
1253+ } // namespace librealsense
0 commit comments