| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/midi/midi_manager_win.h" | 5 #include "media/midi/midi_manager_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <ks.h> | 8 #include <ks.h> |
| 9 #include <ksmedia.h> | 9 #include <ksmedia.h> |
| 10 #include <mmreg.h> | 10 #include <mmreg.h> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "device/usb/usb_ids.h" | 45 #include "device/usb/usb_ids.h" |
| 46 #include "media/midi/message_util.h" | 46 #include "media/midi/message_util.h" |
| 47 #include "media/midi/midi_manager_winrt.h" | 47 #include "media/midi/midi_manager_winrt.h" |
| 48 #include "media/midi/midi_message_queue.h" | 48 #include "media/midi/midi_message_queue.h" |
| 49 #include "media/midi/midi_port_info.h" | 49 #include "media/midi/midi_port_info.h" |
| 50 #include "media/midi/midi_switches.h" | 50 #include "media/midi/midi_switches.h" |
| 51 | 51 |
| 52 namespace midi { | 52 namespace midi { |
| 53 namespace { | 53 namespace { |
| 54 | 54 |
| 55 using mojom::PortState; |
| 55 using mojom::Result; | 56 using mojom::Result; |
| 56 | 57 |
| 57 static const size_t kBufferLength = 32 * 1024; | 58 static const size_t kBufferLength = 32 * 1024; |
| 58 | 59 |
| 59 // We assume that nullpter represents an invalid MIDI handle. | 60 // We assume that nullpter represents an invalid MIDI handle. |
| 60 const HMIDIIN kInvalidMidiInHandle = nullptr; | 61 const HMIDIIN kInvalidMidiInHandle = nullptr; |
| 61 const HMIDIOUT kInvalidMidiOutHandle = nullptr; | 62 const HMIDIOUT kInvalidMidiOutHandle = nullptr; |
| 62 | 63 |
| 63 std::string GetInErrorMessage(MMRESULT result) { | 64 std::string GetInErrorMessage(MMRESULT result) { |
| 64 wchar_t text[MAXERRORLENGTH]; | 65 wchar_t text[MAXERRORLENGTH]; |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 task_thread_.task_runner()->PostTask( | 675 task_thread_.task_runner()->PostTask( |
| 675 FROM_HERE, base::Bind(&MidiServiceWinImpl::StartInputDeviceOnTaskThread, | 676 FROM_HERE, base::Bind(&MidiServiceWinImpl::StartInputDeviceOnTaskThread, |
| 676 base::Unretained(this), midi_in_handle)); | 677 base::Unretained(this), midi_in_handle)); |
| 677 if (add_new_port) { | 678 if (add_new_port) { |
| 678 const MidiPortInfo port_info( | 679 const MidiPortInfo port_info( |
| 679 // TODO(toyoshim): Use a hash ID insted crbug.com/467448 | 680 // TODO(toyoshim): Use a hash ID insted crbug.com/467448 |
| 680 base::IntToString(static_cast<int>(port_number)), | 681 base::IntToString(static_cast<int>(port_number)), |
| 681 GetManufacturerName(state_device_info), | 682 GetManufacturerName(state_device_info), |
| 682 base::WideToUTF8(state_device_info.product_name), | 683 base::WideToUTF8(state_device_info.product_name), |
| 683 MmversionToString(state_device_info.driver_version), | 684 MmversionToString(state_device_info.driver_version), |
| 684 MIDI_PORT_OPENED); | 685 PortState::OPENED); |
| 685 task_thread_.task_runner()->PostTask( | 686 task_thread_.task_runner()->PostTask( |
| 686 FROM_HERE, base::Bind(&MidiServiceWinImpl::AddInputPortOnTaskThread, | 687 FROM_HERE, base::Bind(&MidiServiceWinImpl::AddInputPortOnTaskThread, |
| 687 base::Unretained(this), port_info)); | 688 base::Unretained(this), port_info)); |
| 688 } else { | 689 } else { |
| 689 task_thread_.task_runner()->PostTask( | 690 task_thread_.task_runner()->PostTask( |
| 690 FROM_HERE, | 691 FROM_HERE, |
| 691 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread, | 692 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread, |
| 692 base::Unretained(this), port_number, | 693 base::Unretained(this), port_number, |
| 693 MidiPortState::MIDI_PORT_CONNECTED)); | 694 PortState::CONNECTED)); |
| 694 } | 695 } |
| 695 } | 696 } |
| 696 | 697 |
| 697 void OnMidiInDataOnMultimediaThread(HMIDIIN midi_in_handle, | 698 void OnMidiInDataOnMultimediaThread(HMIDIIN midi_in_handle, |
| 698 DWORD_PTR param1, | 699 DWORD_PTR param1, |
| 699 DWORD_PTR param2) { | 700 DWORD_PTR param2) { |
| 700 auto state = GetInputDeviceFromHandle(midi_in_handle); | 701 auto state = GetInputDeviceFromHandle(midi_in_handle); |
| 701 if (!state) | 702 if (!state) |
| 702 return; | 703 return; |
| 703 const uint8_t status_byte = static_cast<uint8_t>(param1 & 0xff); | 704 const uint8_t status_byte = static_cast<uint8_t>(param1 & 0xff); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 base::AutoLock auto_lock(input_ports_lock_); | 776 base::AutoLock auto_lock(input_ports_lock_); |
| 776 input_device_map_.erase(state->midi_handle); | 777 input_device_map_.erase(state->midi_handle); |
| 777 input_ports_[port_number] = nullptr; | 778 input_ports_[port_number] = nullptr; |
| 778 input_ports_ages_[port_number] += 1; | 779 input_ports_ages_[port_number] += 1; |
| 779 unused_input_ports_[device_info].push(port_number); | 780 unused_input_ports_[device_info].push(port_number); |
| 780 } | 781 } |
| 781 task_thread_.task_runner()->PostTask( | 782 task_thread_.task_runner()->PostTask( |
| 782 FROM_HERE, | 783 FROM_HERE, |
| 783 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread, | 784 base::Bind(&MidiServiceWinImpl::SetInputPortStateOnTaskThread, |
| 784 base::Unretained(this), port_number, | 785 base::Unretained(this), port_number, |
| 785 MIDI_PORT_DISCONNECTED)); | 786 PortState::DISCONNECTED)); |
| 786 } | 787 } |
| 787 | 788 |
| 788 static void CALLBACK | 789 static void CALLBACK |
| 789 OnMidiOutEventOnMainlyMultimediaThread(HMIDIOUT midi_out_handle, | 790 OnMidiOutEventOnMainlyMultimediaThread(HMIDIOUT midi_out_handle, |
| 790 UINT message, | 791 UINT message, |
| 791 DWORD_PTR instance, | 792 DWORD_PTR instance, |
| 792 DWORD_PTR param1, | 793 DWORD_PTR param1, |
| 793 DWORD_PTR param2) { | 794 DWORD_PTR param2) { |
| 794 MidiServiceWinImpl* self = reinterpret_cast<MidiServiceWinImpl*>(instance); | 795 MidiServiceWinImpl* self = reinterpret_cast<MidiServiceWinImpl*>(instance); |
| 795 if (!self) | 796 if (!self) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 output_ports_[port_number]->port_index = port_number; | 853 output_ports_[port_number]->port_index = port_number; |
| 853 output_ports_[port_number]->port_age = output_ports_ages_[port_number]; | 854 output_ports_[port_number]->port_age = output_ports_ages_[port_number]; |
| 854 } | 855 } |
| 855 if (add_new_port) { | 856 if (add_new_port) { |
| 856 const MidiPortInfo port_info( | 857 const MidiPortInfo port_info( |
| 857 // TODO(toyoshim): Use a hash ID insted. crbug.com/467448 | 858 // TODO(toyoshim): Use a hash ID insted. crbug.com/467448 |
| 858 base::IntToString(static_cast<int>(port_number)), | 859 base::IntToString(static_cast<int>(port_number)), |
| 859 GetManufacturerName(state_device_info), | 860 GetManufacturerName(state_device_info), |
| 860 base::WideToUTF8(state_device_info.product_name), | 861 base::WideToUTF8(state_device_info.product_name), |
| 861 MmversionToString(state_device_info.driver_version), | 862 MmversionToString(state_device_info.driver_version), |
| 862 MIDI_PORT_OPENED); | 863 PortState::OPENED); |
| 863 task_thread_.task_runner()->PostTask( | 864 task_thread_.task_runner()->PostTask( |
| 864 FROM_HERE, base::Bind(&MidiServiceWinImpl::AddOutputPortOnTaskThread, | 865 FROM_HERE, base::Bind(&MidiServiceWinImpl::AddOutputPortOnTaskThread, |
| 865 base::Unretained(this), port_info)); | 866 base::Unretained(this), port_info)); |
| 866 } else { | 867 } else { |
| 867 task_thread_.task_runner()->PostTask( | 868 task_thread_.task_runner()->PostTask( |
| 868 FROM_HERE, | 869 FROM_HERE, |
| 869 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread, | 870 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread, |
| 870 base::Unretained(this), port_number, MIDI_PORT_CONNECTED)); | 871 base::Unretained(this), port_number, |
| 872 PortState::CONNECTED)); |
| 871 } | 873 } |
| 872 } | 874 } |
| 873 | 875 |
| 874 void OnMidiOutDoneOnMultimediaThread(HMIDIOUT midi_out_handle, | 876 void OnMidiOutDoneOnMultimediaThread(HMIDIOUT midi_out_handle, |
| 875 DWORD_PTR param1) { | 877 DWORD_PTR param1) { |
| 876 auto state = GetOutputDeviceFromHandle(midi_out_handle); | 878 auto state = GetOutputDeviceFromHandle(midi_out_handle); |
| 877 if (!state) | 879 if (!state) |
| 878 return; | 880 return; |
| 879 // Take ownership of the MIDIHDR object. | 881 // Take ownership of the MIDIHDR object. |
| 880 ScopedMIDIHDR header(reinterpret_cast<MIDIHDR*>(param1)); | 882 ScopedMIDIHDR header(reinterpret_cast<MIDIHDR*>(param1)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 898 output_device_map_.erase(state->midi_handle); | 900 output_device_map_.erase(state->midi_handle); |
| 899 output_ports_[port_number] = nullptr; | 901 output_ports_[port_number] = nullptr; |
| 900 output_ports_ages_[port_number] += 1; | 902 output_ports_ages_[port_number] += 1; |
| 901 unused_output_ports_[device_info].push(port_number); | 903 unused_output_ports_[device_info].push(port_number); |
| 902 state->closed = true; | 904 state->closed = true; |
| 903 } | 905 } |
| 904 task_thread_.task_runner()->PostTask( | 906 task_thread_.task_runner()->PostTask( |
| 905 FROM_HERE, | 907 FROM_HERE, |
| 906 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread, | 908 base::Bind(&MidiServiceWinImpl::SetOutputPortStateOnTaskThread, |
| 907 base::Unretained(this), port_number, | 909 base::Unretained(this), port_number, |
| 908 MIDI_PORT_DISCONNECTED)); | 910 PortState::DISCONNECTED)); |
| 909 } | 911 } |
| 910 | 912 |
| 911 ///////////////////////////////////////////////////////////////////////////// | 913 ///////////////////////////////////////////////////////////////////////////// |
| 912 // Callbacks on the sender thread. | 914 // Callbacks on the sender thread. |
| 913 ///////////////////////////////////////////////////////////////////////////// | 915 ///////////////////////////////////////////////////////////////////////////// |
| 914 | 916 |
| 915 void AssertOnSenderThread() { | 917 void AssertOnSenderThread() { |
| 916 DCHECK_EQ(sender_thread_.GetThreadId(), base::PlatformThread::CurrentId()); | 918 DCHECK_EQ(sender_thread_.GetThreadId(), base::PlatformThread::CurrentId()); |
| 917 } | 919 } |
| 918 | 920 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1070 void AddInputPortOnTaskThread(MidiPortInfo info) { | 1072 void AddInputPortOnTaskThread(MidiPortInfo info) { |
| 1071 AssertOnTaskThread(); | 1073 AssertOnTaskThread(); |
| 1072 delegate_->OnAddInputPort(info); | 1074 delegate_->OnAddInputPort(info); |
| 1073 } | 1075 } |
| 1074 | 1076 |
| 1075 void AddOutputPortOnTaskThread(MidiPortInfo info) { | 1077 void AddOutputPortOnTaskThread(MidiPortInfo info) { |
| 1076 AssertOnTaskThread(); | 1078 AssertOnTaskThread(); |
| 1077 delegate_->OnAddOutputPort(info); | 1079 delegate_->OnAddOutputPort(info); |
| 1078 } | 1080 } |
| 1079 | 1081 |
| 1080 void SetInputPortStateOnTaskThread(uint32_t port_index, MidiPortState state) { | 1082 void SetInputPortStateOnTaskThread(uint32_t port_index, PortState state) { |
| 1081 AssertOnTaskThread(); | 1083 AssertOnTaskThread(); |
| 1082 delegate_->OnSetInputPortState(port_index, state); | 1084 delegate_->OnSetInputPortState(port_index, state); |
| 1083 } | 1085 } |
| 1084 | 1086 |
| 1085 void SetOutputPortStateOnTaskThread(uint32_t port_index, | 1087 void SetOutputPortStateOnTaskThread(uint32_t port_index, PortState state) { |
| 1086 MidiPortState state) { | |
| 1087 AssertOnTaskThread(); | 1088 AssertOnTaskThread(); |
| 1088 delegate_->OnSetOutputPortState(port_index, state); | 1089 delegate_->OnSetOutputPortState(port_index, state); |
| 1089 } | 1090 } |
| 1090 | 1091 |
| 1091 ///////////////////////////////////////////////////////////////////////////// | 1092 ///////////////////////////////////////////////////////////////////////////// |
| 1092 // Fields: | 1093 // Fields: |
| 1093 ///////////////////////////////////////////////////////////////////////////// | 1094 ///////////////////////////////////////////////////////////////////////////// |
| 1094 | 1095 |
| 1095 // Does not take ownership. | 1096 // Does not take ownership. |
| 1096 MidiServiceWinDelegate* delegate_; | 1097 MidiServiceWinDelegate* delegate_; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1170 } | 1171 } |
| 1171 | 1172 |
| 1172 void MidiManagerWin::OnAddInputPort(MidiPortInfo info) { | 1173 void MidiManagerWin::OnAddInputPort(MidiPortInfo info) { |
| 1173 AddInputPort(info); | 1174 AddInputPort(info); |
| 1174 } | 1175 } |
| 1175 | 1176 |
| 1176 void MidiManagerWin::OnAddOutputPort(MidiPortInfo info) { | 1177 void MidiManagerWin::OnAddOutputPort(MidiPortInfo info) { |
| 1177 AddOutputPort(info); | 1178 AddOutputPort(info); |
| 1178 } | 1179 } |
| 1179 | 1180 |
| 1180 void MidiManagerWin::OnSetInputPortState(uint32_t port_index, | 1181 void MidiManagerWin::OnSetInputPortState(uint32_t port_index, PortState state) { |
| 1181 MidiPortState state) { | |
| 1182 SetInputPortState(port_index, state); | 1182 SetInputPortState(port_index, state); |
| 1183 } | 1183 } |
| 1184 | 1184 |
| 1185 void MidiManagerWin::OnSetOutputPortState(uint32_t port_index, | 1185 void MidiManagerWin::OnSetOutputPortState(uint32_t port_index, |
| 1186 MidiPortState state) { | 1186 PortState state) { |
| 1187 SetOutputPortState(port_index, state); | 1187 SetOutputPortState(port_index, state); |
| 1188 } | 1188 } |
| 1189 | 1189 |
| 1190 void MidiManagerWin::OnReceiveMidiData(uint32_t port_index, | 1190 void MidiManagerWin::OnReceiveMidiData(uint32_t port_index, |
| 1191 const std::vector<uint8_t>& data, | 1191 const std::vector<uint8_t>& data, |
| 1192 base::TimeTicks time) { | 1192 base::TimeTicks time) { |
| 1193 ReceiveMidiData(port_index, &data[0], data.size(), time); | 1193 ReceiveMidiData(port_index, &data[0], data.size(), time); |
| 1194 } | 1194 } |
| 1195 | 1195 |
| 1196 MidiManager* MidiManager::Create() { | 1196 MidiManager* MidiManager::Create() { |
| 1197 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 1197 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1198 switches::kDisableWinrtMidiApi) && | 1198 switches::kDisableWinrtMidiApi) && |
| 1199 base::win::GetVersion() >= base::win::VERSION_WIN10) | 1199 base::win::GetVersion() >= base::win::VERSION_WIN10) |
| 1200 return new MidiManagerWinrt(); | 1200 return new MidiManagerWinrt(); |
| 1201 return new MidiManagerWin(); | 1201 return new MidiManagerWin(); |
| 1202 } | 1202 } |
| 1203 | 1203 |
| 1204 } // namespace midi | 1204 } // namespace midi |
| OLD | NEW |