summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/audiocd/k3baudiomaxspeedjob.cpp
blob: cde09c940ad5988dac8ca56c07ab5ebdc0d0698f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* 
 *
 * $Id: k3baudiomaxspeedjob.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2005 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3baudiomaxspeedjob.h"
#include "k3baudiotrack.h"
#include "k3baudiodatasource.h"
#include "k3baudiodoc.h"
#include "k3baudiocdtracksource.h"
#include "k3baudiodatasourceiterator.h"

#include <k3bdevice.h>

#include <k3bthread.h>

#include <kdebug.h>
#include <klocale.h>

#include <tqdatetime.h>


class K3bAudioMaxSpeedJob::WorkThread : public K3bThread
{
public:
  WorkThread( K3bAudioDoc* doc );
  ~WorkThread();

  void run();

  int speedTest( K3bAudioDataSource* source );
  void cancel();
  int maxSpeedByMedia() const;

  int maxSpeed;

private:
  K3bAudioDoc* m_doc;
  bool m_canceled;
  char* m_buffer;
};


K3bAudioMaxSpeedJob::WorkThread::WorkThread( K3bAudioDoc* doc )
  : K3bThread(),
    m_doc(doc),
    m_canceled(false)
{
  m_buffer = new char[2352*10];
}


K3bAudioMaxSpeedJob::WorkThread::~WorkThread()
{
  delete [] m_buffer;
}


void K3bAudioMaxSpeedJob::WorkThread::run()
{
  kdDebug() << k_funcinfo << endl;
  m_canceled = false;

  emitStarted();

  K3bAudioDataSourceIterator it( m_doc );

  // count sources for minimal progress info
  int numSources = 0;
  int sourcesDone = 0;
  while( it.current() ) {
    ++numSources;
    it.next();
  }

  bool success = true;
  maxSpeed = 175*1000;
  it.first();

  while( it.current() && !m_canceled ) {
    if( !it.current()->seek(0) ) {
      kdDebug() << "(K3bAudioMaxSpeedJob) seek failed." << endl;
      success = false;
      break;
    }
      
    // read some data
    int speed = speedTest( it.current() );

    ++sourcesDone;
    emitPercent( 100*numSources/sourcesDone );

    if( speed < 0 ) {
      success = false;
      break;
    }
    else if( speed > 0 ) {
      // update the max speed
      maxSpeed = TQMIN( maxSpeed, speed );
    }
      
    it.next();
  }

  if( m_canceled ) {
    emitCanceled();
    success = false;
  }

  if( success )
    kdDebug() << "(K3bAudioMaxSpeedJob) max speed: " << maxSpeed << endl;

  emitFinished( success );
}

// returns the amount of data read from this source
int K3bAudioMaxSpeedJob::WorkThread::speedTest( K3bAudioDataSource* source )
{
  //
  // in case of an audio track source we only test when the cd is inserted since asking the user would
  // confuse him a lot.
  //
  // FIXME: there is still the problem of the spin up time.
  //
  if( K3bAudioCdTrackSource* cdts = dynamic_cast<K3bAudioCdTrackSource*>( source ) ) {
    if( K3bDevice::Device* dev = cdts->searchForAudioCD() ) {
      cdts->setDevice( dev );
    }
    else {
      kdDebug() << "(K3bAudioMaxSpeedJob) ignoring audio cd track source." << endl;
      return 0;
    }
  }

  TQTime t;
  int dataRead = 0;
  int r = 0;

  // start the timer
  t.start();

  // read ten seconds of audio data. This is some value which seemed about right. :)
  while( dataRead < 2352*75*10 && (r = source->read( m_buffer, 2352*10 )) > 0 ) {
    dataRead += r;
  }

  // elapsed millisec
  int usedT = t.elapsed();

  if( r < 0 ) {
    kdDebug() << "(K3bAudioMaxSpeedJob) read failure." << endl;
    return -1;
  }

  // KB/sec (add 1 millisecond to avoid division by 0)
  int throughput = (dataRead*1000+usedT)/(usedT+1)/1024;
  kdDebug() << "(K3bAudioMaxSpeedJob) throughput: " << throughput 
	    << " (" << dataRead << "/" << usedT << ")" << endl;


  return throughput;
}


void K3bAudioMaxSpeedJob::WorkThread::cancel()
{
  kdDebug() << k_funcinfo << endl;
  m_canceled = true;
}


int K3bAudioMaxSpeedJob::WorkThread::maxSpeedByMedia() const
{
  int s = 0;
    
  TQValueList<int> speeds = m_doc->burner()->determineSupportedWriteSpeeds();
  // simply use what we have and let the writer decide if the speeds are empty
  if( !speeds.isEmpty() ) {
    // start with the highest speed and go down the list until we are below our max
    TQValueListIterator<int> it = speeds.end();
    --it;
    while( *it > maxSpeed && it != speeds.begin() )
      --it;
      
    // this is the first valid speed or the lowest supported one
    s = *it;
    kdDebug() << "(K3bAudioMaxSpeedJob) using speed factor: " << (s/175) << endl;
  }

  return s;
}




K3bAudioMaxSpeedJob::K3bAudioMaxSpeedJob( K3bAudioDoc* doc, K3bJobHandler* jh, TQObject* tqparent, const char* name )
  : K3bThreadJob( jh, tqparent, name )
{
  m_thread = new WorkThread( doc );
  setThread( m_thread );
}


K3bAudioMaxSpeedJob::~K3bAudioMaxSpeedJob()
{
  delete m_thread;
}


int K3bAudioMaxSpeedJob::maxSpeed() const
{
  return m_thread->maxSpeedByMedia();
}
#include "k3baudiomaxspeedjob.moc"