objective c - Playing Encrypted Videos using AVPlayer in iOS -
so, i'm trying play encrypted video using avplayer, , nothing shows in player.
so progress towards project follows:
1.implemented avplayer have uiview going play av content (this working fine playing non encrypted files.)
avurlasset *asset = [[avurlasset alloc] initwithurl:fileurl options:nil]; //local file url custom scheme avassetresourceloader *loader = [asset resourceloader]; [loader setdelegate:self queue:dispatch_get_main_queue()]; self.playeritem = [avplayeritem playeritemwithasset:asset]; self.player = [avplayer playerwithplayeritem:self.playeritem]; [self.playerview setplayer:self.player];
from above code, know following resource loader method being called:
2.implemented avassetresourceloaderdelegate protocol, , i've implemented resource loader method follows.
- (bool) resourceloader:(avassetresourceloader *)resourceloader shouldwaitforloadingofrequestedresource:(avassetresourceloadingrequest *)loadingrequest { nsurlrequest* request = loadingrequest.request; avassetresourceloadingdatarequest* datarequest = loadingrequest.datarequest; avassetresourceloadingcontentinformationrequest* contentrequest = loadingrequest.contentinformationrequest; nsmutabledata *data; //handle content request if (contentrequest) { contentrequest.contenttype = @"mov"; contentrequest.contentlength = moviefilelengthinbytes contentrequest.byterangeaccesssupported = yes; } if (datarequest) { decryptedstream* readstream = [fs getreadstream:filename error:nil]; if (readstream) { while ([readstream hasbytesavailable]) { nsinteger nread; uint8_t buffer[kbufferreadsize]; nread = [readstream read:buffer maxlength:kbufferreadsize]; nsmutabledata *data = [nsmutabledata data]; [data appendbytes:buffer length:nread]; [datarequest respondwithdata:data]; } } [loadingrequest finishloading]; } return yes; }
based on above code, , other further reading in apple documents resourceloader: "during loading, resource loader object may asked assist in loading of resource. example, resource requires decryption might result in resource loader being asked provide appropriate decryption keys. can assign delegate object resource loader object , use delegate intercept these requests , provide appropriate response" i'm doing. however, can't video play. made sure data i've decrypted correct (i.e, can write tmp file , can play mov).
i stuck on task several day. not difficult, there isn't enough information (or information) of how use avassetresourceloader
, avassetresourceloaderdelegate
. here when doing task:
if loading local video file, must provide custom scheme
fileurl
in order able useavassetresourceloaderdelegate
.let urlcomponents = nsurlcomponents(url: fileurl, resolvingagainstbaseurl: false) urlcomponents?.scheme = "enc" let asset = avurlasset(url: urlcomponents!.url!, options: nil)
simple use
let asset = avurlasset(url: fileurl, options: nil)
won't trigger ofavassetresourceloaderdelegate
- in use of
resourceloader:(avassetresourceloader *)resourceloader shouldwaitforloadingofrequestedresource:(avassetresourceloadingrequest *)loadingrequest
must providecontentinformationrequest
loadingrequest
. can find list of video format av foundation constants reference. , thingcontentlength
. must set size of decrypted file, not file using decrypt. - the
loadingrequest
can request overlap data or request same data many times. can't decode , paste datadatarequest
. need have buffer decoding data. , inresourceloader:(avassetresourceloader *)resourceloader shouldwaitforloadingofrequestedresource:(avassetresourceloadingrequest *)loadingrequest
, check if have enough data return or need decode more. when decode more data, can clean previous buffer keep memory low remember copy unused data in previous buffer.
and, suggest try play non-encrypted file first. make sure method loading data loadingrequest
works before jumping encrypted file. @ end find fun , interesting in doing task this. happy coding :)
Comments
Post a Comment