Skip to content

Commit 12c8cb3

Browse files
committed
Fix ExecuteReadBigData to read more data that available memory ref #201
1 parent f41089d commit 12c8cb3

File tree

8 files changed

+43
-37
lines changed

8 files changed

+43
-37
lines changed

CameraControl.Devices/BaseMTPCamera.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,33 +267,28 @@ public override void TransferFile(object o, Stream stream)
267267
{
268268
try
269269
{
270-
result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetObject,
271-
(total, current) =>
272-
{
273-
double i = (double)current / total;
274-
TransferProgress =
275-
Convert.ToUInt32(i * 100);
276-
}, Convert.ToUInt32(o));
277-
}
278-
// if not enough memory for transfer catch it and wait and try again
279-
catch (OutOfMemoryException)
280-
{
270+
result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetObject, stream,
271+
(total, current) =>
272+
{
273+
double i = (double) current/total;
274+
TransferProgress =
275+
Convert.ToUInt32(i*100);
276+
}, Convert.ToUInt32(o));
277+
if (result != null && result.Data != null)
278+
{
279+
stream.Write(result.Data, 0, result.Data.Length);
280+
}
281+
break;
281282
}
282283
catch (COMException)
283-
{
284-
}
285-
286-
if (result != null && result.Data != null)
287-
{
288-
stream.Write(result.Data, 0, result.Data.Length);
289-
}
290-
else
291284
{
292285
Log.Error("Transfer error code retrying " + result.ErrorCode.ToString("X"));
293286
Thread.Sleep(200);
294287
retryes--;
295288
}
296-
} while (result.Data == null && retryes > 0);
289+
290+
} while (retryes > 0);
291+
297292
//==================================================================
298293
//=================== direct file write
299294
//StillImageDevice.ExecuteReadBigDataWriteToFile(CONST_CMD_GetObject,

CameraControl.Devices/Nikon/NikonBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,14 +2524,15 @@ public override void TransferFileThumb(object o, string filename)
25242524

25252525
using (var fs = File.Open(filename, FileMode.Create))
25262526
{
2527-
MTPDataResponse result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetLargeThumb,
2527+
MTPDataResponse result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetLargeThumb,fs,
25282528
(total, current) =>
25292529
{
25302530
double i = (double) current/total;
25312531
TransferProgress =
25322532
Convert.ToUInt32(i*100);
25332533
}, Convert.ToUInt32(o));
2534-
fs.Write(result.Data, 0, result.Data.Length);
2534+
if (result.Data != null)
2535+
fs.Write(result.Data, 0, result.Data.Length);
25352536
}
25362537
_timer.Start();
25372538
TransferProgress = 0;

CameraControl.Devices/TransferProtocol/DdServerProtocol.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Text;
34
using CameraControl.Devices.TransferProtocol.DDServer;
45
using PortableDeviceLib;
@@ -21,7 +22,7 @@ public class DdServerProtocol : ITransferProtocol
2122
public string DeviceId { get; private set; }
2223

2324

24-
public MTPDataResponse ExecuteReadBigData(uint code, StillImageDevice.TransferCallback callback, params uint[] parameters)
25+
public MTPDataResponse ExecuteReadBigData(uint code,Stream stream ,StillImageDevice.TransferCallback callback, params uint[] parameters)
2526
{
2627
lock (_syncRoot)
2728
{

CameraControl.Devices/TransferProtocol/ITransferProtocol.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525
#endregion
2626

27+
using System.IO;
2728
using PortableDeviceLib;
2829

2930
namespace CameraControl.Devices.TransferProtocol
@@ -35,10 +36,10 @@ public interface ITransferProtocol
3536
string SerialNumber { get; }
3637
bool IsConnected { get; set; }
3738
string DeviceId { get; }
38-
3939

40-
MTPDataResponse ExecuteReadBigData(uint code, StillImageDevice.TransferCallback callback,
41-
params uint[] parameters);
40+
41+
MTPDataResponse ExecuteReadBigData(uint code, Stream stream, StillImageDevice.TransferCallback callback,
42+
params uint[] parameters);
4243

4344
MTPDataResponse ExecuteReadData(uint code, params uint[] parameters);
4445
uint ExecuteWithNoData(uint code, params uint[] parameters);

CameraControl.Devices/TransferProtocol/PtpIpProtocol.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Text;
34
using CameraControl.Devices.TransferProtocol.PtpIp;
45
using PortableDeviceLib;
@@ -90,7 +91,7 @@ private void LoadDeviceInfo()
9091
SerialNumber = Encoding.Unicode.GetString(res.Data, index, strlen4 - 2);
9192
}
9293

93-
public MTPDataResponse ExecuteReadBigData(uint code, StillImageDevice.TransferCallback callback, params uint[] parameters)
94+
public MTPDataResponse ExecuteReadBigData(uint code,Stream stream, StillImageDevice.TransferCallback callback, params uint[] parameters)
9495
{
9596
lock (_locker)
9697
{

CameraControl.Devices/TransferProtocol/SonyTransferProtocol.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -16,7 +17,7 @@ public class SonyTransferProtocol : ITransferProtocol
1617
public string DeviceId { get; private set; }
1718

1819

19-
public MTPDataResponse ExecuteReadBigData(uint code, StillImageDevice.TransferCallback callback, params uint[] parameters)
20+
public MTPDataResponse ExecuteReadBigData(uint code, Stream stream, StillImageDevice.TransferCallback callback, params uint[] parameters)
2021
{
2122
throw new NotImplementedException();
2223
}

CameraControl.Devices/TransferProtocol/YiCameraProtocol.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Net;
56
using System.Net.Sockets;
@@ -213,7 +214,9 @@ public void SetupRecieveCallback(Socket sock)
213214
Log.Error("SetupRecieveCallback", ex);
214215
}
215216
}
216-
public MTPDataResponse ExecuteReadBigData(uint code, StillImageDevice.TransferCallback callback, params uint[] parameters)
217+
218+
219+
public MTPDataResponse ExecuteReadBigData(uint code, Stream stream, StillImageDevice.TransferCallback callback, params uint[] parameters)
217220
{
218221
throw new NotImplementedException();
219222
}

PortableDeviceLib/StillImageDevice.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Runtime.InteropServices;
56
using System.Text;
@@ -96,7 +97,7 @@ public uint ExecuteWithNoData(uint code, params uint[] parameters)
9697
return 0;
9798
}
9899

99-
public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback, params uint[] parameters)
100+
public MTPDataResponse ExecuteReadBigData(uint code,Stream stream, TransferCallback callback, params uint[] parameters)
100101
{
101102
MTPDataResponse res = new MTPDataResponse();
102103
// source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx
@@ -142,7 +143,7 @@ public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback,
142143
if (((uint)pValue) == PortableDeviceErrorCodes.ERROR_BUSY)
143144
{
144145
Thread.Sleep(50);
145-
return ExecuteReadBigData(code, callback, parameters);
146+
return ExecuteReadBigData(code, stream, callback, parameters);
146147
}
147148
}
148149
}
@@ -181,7 +182,7 @@ public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback,
181182
pParameters.Clear();
182183
pResults.Clear();
183184
uint offset = 0;
184-
res.Data = new byte[(int)tmpBufferSize];
185+
res.Data = null;
185186
bool cont = true;
186187

187188
do
@@ -237,12 +238,14 @@ public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback,
237238

238239
IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr));
239240

240-
//Marshal.Copy(tmpPtr, res.Data, (int)offset, (int)cbBytesRead);
241+
byte[] buffer = new byte[(int)cbBytesRead];
242+
Marshal.Copy(tmpPtr, buffer, 0, (int)cbBytesRead);
243+
stream.Write(buffer, 0, (int)cbBytesRead);
241244

242-
for (int i = 0; i < cbBytesRead; i++)
243-
{
244-
res.Data[offset + i] = Marshal.ReadByte(tmpPtr, i);
245-
}
245+
//for (int i = 0; i < cbBytesRead; i++)
246+
//{
247+
// res.Data[offset + i] = Marshal.ReadByte(tmpPtr, i);
248+
//}
246249

247250
Marshal.FreeHGlobal(tmpPtr);
248251
pinnedArray.Free();

0 commit comments

Comments
 (0)