ios - Can get NSInputStream data and play video -
i'm using nssinputstream & nsoutputstream send , receiver data, in server(i named it): open stream send data client
- (bool)openstreams { //nslog(@"server: open stream"); [self.delegate callbackmessagefromserver:@"server: open stream"]; nsurl *url = [[nsbundle mainbundle]urlforresource:@"video.mp4" withextension:nil]; nslog(@"path %@",url); nsdata *data =[nsdata datawithcontentsofurl:url]; nslog(@"data %@",data); self.inputstream = [nsinputstream inputstreamwithurl:url]; [self.inputstream setdelegate:self]; [self.outputstream setdelegate:self]; [self.inputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.outputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.inputstream open]; [self.outputstream open]; return yes; }
in - (void)stream:(nsstream *)astream handleevent:(nsstreamevent)eventcode
- (void)stream:(nsstream *)astream handleevent:(nsstreamevent)eventcode { switch (eventcode) { case nsstreameventhasspaceavailable: { [self senddata]; }break; } - (void)senddata { if (self.bufferoffset == self.bufferlimit) { nsinteger bytesread = [self.inputstream read:self.buffer maxlength:ksendbuffersize]; if (bytesread == -1) { nslog(@"server : file read error"); // close stream } else if (bytesread ==0) { nslog(@"server: sending data successful"); // close stream } else { self.bufferoffset = 0; self.bufferlimit = bytesread; } } if (self.bufferoffset != self.bufferlimit) { nsinteger byteswritten; byteswritten = [self.outputstream write:&self.buffer[self.bufferoffset] maxlength:self.bufferlimit - self.bufferoffset]; assert(byteswritten != 0); if (byteswritten == -1) { // close stream } else { self.bufferoffset += byteswritten; } } }
in client, how receiver data , playing video ?, you
here's more efficient code (i'm sure can tell goes; i have working sample app, if want it):
case nsstreameventhasbytesavailable: { nslog(@"nsstreameventhasbytesavailable"); uint8_t * mbuf[data_length]; mlen = [(nsinputstream *)stream read:(uint8_t *)mbuf maxlength:data_length]; nslog(@"mlen == %lu", mlen); [mdata appendbytes:(const void *)mbuf length:mlen]; nslog(@"mdata length == %lu", mdata.length); if (mlen < data_length) { nslog(@"displayimage"); uiimage *image = [uiimage imagewithdata:mdata]; [self.peerconnectionviewcontroller.view.subviews[0].layer setcontents:(__bridge id)image.cgimage]; mdata = nil; mlen = data_length; mdata = [[nsmutabledata alloc] init]; } } break;
...
- (void)captureoutput:(avcaptureoutput *)output didoutputsamplebuffer:(cmsamplebufferref)samplebuffer fromconnection:(avcaptureconnection *)connection { cvimagebufferref imagebuffer = cmsamplebuffergetimagebuffer(samplebuffer); cvpixelbufferlockbaseaddress(imagebuffer,0); uint8_t *baseaddress = (uint8_t *)cvpixelbuffergetbaseaddress(imagebuffer); size_t bytesperrow = cvpixelbuffergetbytesperrow(imagebuffer); size_t width = cvpixelbuffergetwidth(imagebuffer); size_t height = cvpixelbuffergetheight(imagebuffer); cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cgcontextref newcontext = cgbitmapcontextcreate(baseaddress, width, height, 8, bytesperrow, colorspace, kcgbitmapbyteorder32little | kcgimagealphapremultipliedfirst); cgimageref newimage = cgbitmapcontextcreateimage(newcontext); cgcontextrelease(newcontext); cgcolorspacerelease(colorspace); uiimage *image = [[uiimage alloc] initwithcgimage:newimage scale:1 orientation:uiimageorientationup]; cgimagerelease(newimage); cvpixelbufferunlockbaseaddress(imagebuffer,0); nsdata *data = [nsdata datawithdata:uiimagejpegrepresentation(image, 0.25)]; __block bool basecasecondition = no; // should data driven, not hardcoded __block nsinteger _len = data_length; __block nsinteger _byteindex = 0; typedef void (^recursiveblock)(void (^)()); recursiveblock arecursiveblock; arecursiveblock = ^(recursiveblock block) { nslog(@"block called..."); basecasecondition = (data.length > 0 && _byteindex < data.length) ? true : false; if ((basecasecondition) && block) { _len = (data.length - _byteindex) == 0 ? 1 : (data.length - _byteindex) < data_length ? (data.length - _byteindex) : data_length; // nslog(@"start | byteindex: %lu/%lu writing len: %lu", _byteindex, data.length, _len); // uint8_t * bytes[_len]; [data getbytes:&bytes range:nsmakerange(_byteindex, _len)]; _byteindex += [self.outputstream write:(const uint8_t *)bytes maxlength:_len]; // nslog(@"end | byteindex: %lu/%lu wrote len: %lu", _byteindex, data.length, _len); // dispatch_barrier_async(dispatch_get_main_queue(), ^{ block(block); }); } }; if (self.outputstream.hasspaceavailable) arecursiveblock(arecursiveblock); }
Comments
Post a Comment