FilmStripModel.js 1.9 KB

12345678910111213141516
  1. export default class FilmStripModel{constructor(tracingModel,zeroTime){this.reset(tracingModel,zeroTime);}
  2. reset(tracingModel,zeroTime){this._zeroTime=zeroTime||tracingModel.minimumRecordTime();this._spanTime=tracingModel.maximumRecordTime()-this._zeroTime;this._frames=[];const browserMain=SDK.TracingModel.browserMainThread(tracingModel);if(!browserMain){return;}
  3. const events=browserMain.events();for(let i=0;i<events.length;++i){const event=events[i];if(event.startTime<this._zeroTime){continue;}
  4. if(!event.hasCategory(_category)){continue;}
  5. if(event.name===TraceEvents.CaptureFrame){const data=event.args['data'];if(data){this._frames.push(Frame._fromEvent(this,event,this._frames.length));}}else if(event.name===TraceEvents.Screenshot){this._frames.push(Frame._fromSnapshot(this,(event),this._frames.length));}}}
  6. frames(){return this._frames;}
  7. zeroTime(){return this._zeroTime;}
  8. spanTime(){return this._spanTime;}
  9. frameByTimestamp(timestamp){const index=this._frames.upperBound(timestamp,(timestamp,frame)=>timestamp-frame.timestamp)-1;return index>=0?this._frames[index]:null;}}
  10. const _category='disabled-by-default-devtools.screenshot';const TraceEvents={CaptureFrame:'CaptureFrame',Screenshot:'Screenshot'};export class Frame{constructor(model,timestamp,index){this._model=model;this.timestamp=timestamp;this.index=index;this._imageData=null;this._snapshot=null;}
  11. static _fromEvent(model,event,index){const frame=new Frame(model,event.startTime,index);frame._imageData=event.args['data'];return frame;}
  12. static _fromSnapshot(model,snapshot,index){const frame=new Frame(model,snapshot.startTime,index);frame._snapshot=snapshot;return frame;}
  13. model(){return this._model;}
  14. imageDataPromise(){if(this._imageData||!this._snapshot){return Promise.resolve(this._imageData);}
  15. return(this._snapshot.objectPromise());}}
  16. self.SDK=self.SDK||{};SDK=SDK||{};SDK.FilmStripModel=FilmStripModel;SDK.FilmStripModel.Frame=Frame;